Created ESP32 app that scans local APs and reports signal strength.

This commit is contained in:
Luke Else 2023-06-19 21:57:53 +01:00
parent e161dbd881
commit 63b6627867
8 changed files with 127 additions and 31 deletions

17
.cargo/config.toml Normal file
View File

@ -0,0 +1,17 @@
[build]
target = "xtensa-esp32-espidf"
[target.xtensa-esp32-espidf]
linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash com5 --monitor" # Select this runner for espflash v2.x.x
[unstable]
build-std = ["std", "panic_abort"]
[env]
# Note: these variables are not used when using pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "release/v4.4"

34
.gitignore vendored
View File

@ -1,30 +1,4 @@
# ---> Rust /.vscode
# Generated by Cargo /.embuild
# will have compiled files and executables /target
debug/ /Cargo.lock
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# ---> VisualStudioCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix

38
Cargo.toml Normal file
View File

@ -0,0 +1,38 @@
[package]
name = "esp32-wifi"
version = "0.1.0"
authors = ["Luke Else <mail@luke-else.co.uk>"]
edition = "2021"
resolver = "2"
rust-version = "1.66"
[profile.release]
opt-level = "s"
[profile.dev]
debug = true # Symbols are nice and they don't increase the size on Flash
opt-level = "z"
[features]
default = ["std", "hal", "esp-idf-sys/native"]
pio = ["esp-idf-sys/pio"]
all = ["std", "nightly", "experimental", "embassy"]
hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"]
std = ["alloc", "esp-idf-sys/std", "esp-idf-sys/binstart", "embedded-svc?/std", "esp-idf-hal?/std", "esp-idf-svc?/std"]
alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"]
nightly = ["embedded-svc?/nightly", "esp-idf-svc?/nightly"] # Future: "esp-idf-hal?/nightly"
experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"]
embassy = ["esp-idf-hal?/embassy-sync", "esp-idf-hal?/critical-section", "esp-idf-hal?/edge-executor", "esp-idf-svc?/embassy-time-driver", "esp-idf-svc?/embassy-time-isr-queue"]
[dependencies]
log = { version = "0.4.17", default-features = false }
esp-idf-sys = { version = "0.33", default-features = false }
esp-idf-hal = { version = "0.41", optional = true, default-features = false }
esp-idf-svc = { version = "0.46", optional = true, default-features = false }
embedded-svc = { version = "0.25", optional = true, default-features = false }
[build-dependencies]
embuild = "0.31.2"

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) <year> <copyright holders> Copyright (c) <2023> <Luke Else>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

6
build.rs Normal file
View File

@ -0,0 +1,6 @@
// Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641
fn main() -> Result<(), Box<dyn std::error::Error>> {
embuild::build::CfgArgs::output_propagated("ESP_IDF")?;
embuild::build::LinkArgs::output_propagated("ESP_IDF")?;
Ok(())
}

2
rust-toolchain.toml Normal file
View File

@ -0,0 +1,2 @@
[toolchain]
channel = "esp"

10
sdkconfig.defaults Normal file
View File

@ -0,0 +1,10 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granuality for thread sleeps (10 ms by default).
#CONFIG_FREERTOS_HZ=1000
# Workaround for https://github.com/espressif/esp-idf/issues/7631
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n

49
src/main.rs Normal file
View File

@ -0,0 +1,49 @@
use embedded_svc::wifi::{Configuration, ClientConfiguration};
use esp_idf_svc::{wifi::EspWifi, eventloop::EspSystemEventLoop};
use esp_idf_sys::{self as _, EspError}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use esp_idf_hal::prelude::Peripherals;
fn main() -> Result<(), EspError> {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
// Get Peripherals and sysloop ready
let sysloop = EspSystemEventLoop::take();
let peripherals = Peripherals::take().unwrap();
let modem = peripherals.modem;
// Enable WIFI Module
let mut wifi = EspWifi::new(modem, sysloop.clone()?, None)?;
wifi.set_configuration(&Configuration::Client(ClientConfiguration::default()))?;
wifi.start()?;
loop {
//Synchronously perform a scan and return nearby APs
let ap_scan_res = wifi.scan()?;
println!("\n\n=======================================");
for net in ap_scan_res.into_iter().map(|ap| (ap.ssid, ap.signal_strength, rssi_to_percentage(ap.signal_strength))) {
println!("{}: {}dBm, {:.2}%", net.0, net.1, net.2);
}
println!("=======================================");
}
}
fn rssi_to_percentage(signal_strength: i8) -> f32 {
let max_i8 = std::u8::MAX as f32;
let percentage = (((signal_strength as f32) / max_i8) + 0.5) * 100.0;
percentage
}
mod tests {
#[cfg(test)]
#[test]
fn rssi_to_percentage_test() {
use super::rssi_to_percentage;
println!("{}", rssi_to_percentage(0));
assert_eq!(rssi_to_percentage(0), 50.0);
}
}