Skip to content

Instantly share code, notes, and snippets.

@krokosik
Created January 2, 2025 19:41
Show Gist options
  • Select an option

  • Save krokosik/9fd9b1c5af02d8ecf043b1d83229bd5d to your computer and use it in GitHub Desktop.

Select an option

Save krokosik/9fd9b1c5af02d8ecf043b1d83229bd5d to your computer and use it in GitHub Desktop.
ESP32-S3 Rust esp-idf-svc Continuous ADC over UDP
Chip type: esp32s3 (revision v0.1)
Crystal frequency: 40 MHz
Flash size: 4MB
Features: WiFi, BLE
MAC address: 64:e8:33:74:75:14
Bootloader: /home/esp/esp_pulser/target/xtensa-esp32s3-espidf/debug/build/esp-idf-sys-cb5009e5bca1cb05/out/build/bootloader/bootloader.bin
Partition table: partitions.csv
App/part. size: 1,133,808/2,097,152 bytes, 54.06%
[2025-01-02T19:39:29Z INFO ] Segment at address '0x0' has not changed, skipping write
[2025-01-02T19:39:29Z INFO ] Segment at address '0x8000' has not changed, skipping write
[00:00:09] [========================================] 661/661 0x10000 [2025-01-02T19:39:40Z INFO ] Flashing has completed!
Commands:
CTRL+R Reset chip
CTRL+C Exit
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x40378bb3
0x40378bb3 - pm_get_null_max_tx_time
at ??:??
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fce3810,len:0x178c
load:0x403c9700,len:0x4
load:0x403c9704,len:0xcbc
load:0x403cc700,len:0x2d9c
entry 0x403c9914
I (27) boot: ESP-IDF v5.2.2 2nd stage bootloader
I (27) boot: compile time Jan 2 2025 15:45:46
I (27) boot: Multicore bootloader
I (30) boot: chip revision: v0.1
I (34) boot.esp32s3: Boot SPI Speed : 40MHz
I (39) boot.esp32s3: SPI Mode : DIO
I (44) boot.esp32s3: SPI Flash Size : 4MB
I (48) boot: Enabling RNG early entropy source...
I (54) boot: Partition Table:
I (57) boot: ## Label Usage Type ST Offset Length
I (65) boot: 0 nvs WiFi data 01 02 00009000 00005000
I (72) boot: 1 otadata OTA data 01 00 0000e000 00002000
I (80) boot: 2 app0 OTA app 00 10 00010000 00200000
I (87) boot: 3 spiffs Unknown data 01 82 00210000 001e0000
I (95) boot: 4 coredump Unknown data 01 03 003f0000 00010000
I (102) boot: End of partition table
I (106) esp_image: segment 0: paddr=00010020 vaddr=3c0c0020 size=3c8ech (248044) map
I (176) esp_image: segment 1: paddr=0004c914 vaddr=3fc97d00 size=03704h ( 14084) load
I (181) esp_image: segment 2: paddr=00050020 vaddr=42000020 size=bffb4h (786356) map
I (378) esp_image: segment 3: paddr=0010ffdc vaddr=3fc9b404 size=0104ch ( 4172) load
I (379) esp_image: segment 4: paddr=00111030 vaddr=40374000 size=13c94h ( 81044) load
I (415) boot: Loaded app from partition at offset 0x10000
I (415) boot: Disabling RNG early entropy source...
I (426) cpu_start: Multicore app
I (437) cpu_start: Pro cpu start user code
I (437) cpu_start: cpu freq: 160000000 Hz
I (437) cpu_start: Application information:
I (440) cpu_start: Project name: libespidf
I (445) cpu_start: App version: 44e4273-dirty
I (450) cpu_start: Compile time: Jan 2 2025 15:45:40
I (456) cpu_start: ELF file SHA256: 000000000...
I (462) cpu_start: ESP-IDF: v5.2.2
I (467) cpu_start: Min chip rev: v0.0
I (471) cpu_start: Max chip rev: v0.99
I (476) cpu_start: Chip rev: v0.1
I (481) heap_init: Initializing. RAM available for dynamic allocation:
I (488) heap_init: At 3FCA0490 len 00049280 (292 KiB): RAM
I (494) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
I (500) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (506) heap_init: At 600FE010 len 00001FD8 (7 KiB): RTCRAM
I (514) spi_flash: detected chip: generic
I (517) spi_flash: flash io: dio
W (522) timer_group: legacy driver is deprecated, please migrate to `driver/gptimer.h`
I (530) sleep: Configure to isolate all GPIO pins in sleep state
I (537) sleep: Enable automatic switching of GPIO sleep configuration
I (544) main_task: Started on CPU0
I (554) main_task: Calling app_main()
I (584) pp: pp rom version: e7ae62f
I (584) net80211: net80211 rom version: e7ae62f
I (594) wifi:wifi driver task: 3fcaa518, prio:23, stack:6656, core=0
I (604) wifi:wifi firmware version: 3e0076f
I (604) wifi:wifi certification version: v7.0
I (604) wifi:config NVS flash: enabled
I (604) wifi:config nano formating: disabled
I (604) wifi:Init data frame dynamic rx buffer num: 32
I (614) wifi:Init static rx mgmt buffer num: 5
I (614) wifi:Init management short buffer num: 32
I (624) wifi:Init dynamic tx buffer num: 32
I (624) wifi:Init static tx FG buffer num: 2
I (634) wifi:Init static rx buffer size: 1600
I (634) wifi:Init static rx buffer num: 10
I (634) wifi:Init dynamic rx buffer num: 32
I (644) wifi_init: rx ba win: 6
I (644) wifi_init: tcpip mbox: 32
I (654) wifi_init: udp mbox: 6
I (654) wifi_init: tcp mbox: 6
I (654) wifi_init: tcp tx win: 5760
I (664) wifi_init: tcp rx win: 5760
I (664) wifi_init: tcp mss: 1440
I (674) wifi_init: WiFi IRAM OP enabled
I (674) wifi_init: WiFi RX IRAM OP enabled
I (684) phy_init: phy_version 670,b7bc9b9,Apr 30 2024,10:54:13
I (734) wifi:mode : sta (64:e8:33:74:75:14)
I (734) wifi:enable tsf
I (734) esp_pulser: Wifi started
I (3154) wifi:new:<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:1
I (3154) wifi:state: init -> auth (b0)
I (3154) wifi:state: auth -> assoc (0)
I (3164) wifi:state: assoc -> run (10)
I (3194) wifi:<ba-add>idx:0 (ifx:0, 8c:fd:de:af:76:dc), tid:5, ssn:0, winSize:64
I (3204) wifi:connected with Orange_Swiatlowod_76DC, aid = 3, channel 11, BW20, bssid = 8c:fd:de:af:76:dc
I (3214) wifi:security: WPA2-PSK, phy: bgn, rssi: -66
I (3214) wifi:pm start, type: 1
I (3214) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
I (3224) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 25000, mt_pti: 0, mt_time: 10000
I (3234) esp_pulser: Wifi connected
I (3244) wifi:<ba-add>idx:1 (ifx:0, 8c:fd:de:af:76:dc), tid:0, ssn:0, winSize:64
I (3274) wifi:AP's beacon interval = 102400 us, DTIM period = 3
I (4234) esp_netif_handlers: sta ip: 192.168.1.124, mask: 255.255.255.0, gw: 192.168.1.1
I (4234) esp_pulser: Wifi netif up
I (4234) esp_pulser: Wifi DHCP info: IpInfo { ip: 192.168.1.124, subnet: Subnet { gateway: 192.168.1.1, mask: Mask(24) }, dns: Some(192.168.1.1), secondary_dns: Some(0.0.0.0) }
I (4254) esp_pulser: Starting ADC
I (4254) gpio: GPIO[8]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (4264) esp_pulser: ADC started
I (4264) esp_pulser: Socket bound to 0.0.0.0:3333
I (4274) esp_pulser: Read 100 bytes
I (4284) esp_pulser: Sent 100 bytes
I (4284) esp_pulser: Read 100 bytes
I (4284) esp_pulser: Sent 100 bytes
I (4294) esp_pulser: Read 100 bytes
I (4294) esp_pulser: Sent 100 bytes
I (4294) esp_pulser: Read 100 bytes
I (4304) wifi:state: run -> init (0)
I (4304) wifi:pm stop, total sleep time: 650928 us / 1090954 us
I (4314) wifi:<ba-del>idx:1, tid:0
I (4314) wifi:<ba-del>idx:0, tid:5
I (4314) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (4354) wifi:flush txq
I (4354) wifi:stop sw txq
I (4354) wifi:lmac stop hw txq
I (4354) esp_idf_svc::wifi: EspWifi dropped
I (4354) esp_idf_svc::netif: Dropped
I (4354) esp_idf_svc::netif: Dropped
I (4364) wifi:Deinit lldesc rx mblock:10
I (4374) esp_idf_svc::nvs: NvsDefault dropped
I (4374) esp_idf_svc::eventloop: System event loop dropped
Error: Not enough space (os error 12)
I (4374) main_task: Returned from app_main()
use std::env;
use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket};
use esp_idf_svc::hal::modem::WifiModemPeripheral;
use esp_idf_svc::timer::EspTaskTimerService;
use esp_idf_svc::wifi::{AsyncWifi, AuthMethod, ClientConfiguration, Configuration, EspWifi};
use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition};
use esp_idf_svc::hal::{adc, prelude::*, task::*};
use log::info;
const SSID: &str = env!("WIFI_SSID");
const PASSWORD: &str = env!("WIFI_PASS");
// mod pulse_sensor;
fn main() -> Result<(), anyhow::Error> {
// 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_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
let peripherals = Peripherals::take()?;
let sys_loop = EspSystemEventLoop::take()?;
let timer_service = EspTaskTimerService::new()?;
let nvs = EspDefaultNvsPartition::take()?;
let wifi = block_on_send(connect_wifi(
peripherals.modem,
sys_loop.clone(),
nvs,
timer_service,
))?;
let ip_info = wifi.wifi().sta_netif().get_ip_info()?;
info!("Wifi DHCP info: {:?}", ip_info);
let pins = peripherals.pins;
info!("Starting ADC");
let adc_config = adc::AdcContConfig::default();
let adc_channel = adc::Attenuated::db11(pins.gpio8);
let mut adc = adc::AdcContDriver::new(peripherals.adc1, &adc_config, adc_channel)?;
adc.start()?;
info!("ADC started");
let mut samples = [0u8; 100];
let socket = UdpSocket::bind(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 3333))?;
info!("Socket bound to {:?}", socket.local_addr()?);
block_on(async {
loop {
if let Ok(num_read) = adc.read_bytes(&mut samples, 10) {
info!("Read {} bytes", num_read);
let res = socket.send_to(&samples.clone(), "192.168.1.45:34254")?;
info!("Sent {} bytes", res);
}
}
})
}
fn block_on_send<F>(f: F) -> F::Output
where
F: core::future::Future + Send + 'static, // These constraints are why this additional example exists in the first place
{
block_on(f)
}
async fn connect_wifi<M>(
modem: M,
sys_loop: EspSystemEventLoop,
nvs: EspDefaultNvsPartition,
timer_service: EspTaskTimerService,
) -> anyhow::Result<AsyncWifi<EspWifi<'static>>>
where
M: WifiModemPeripheral + 'static,
{
let mut wifi = AsyncWifi::wrap(
EspWifi::new(modem, sys_loop.clone(), Some(nvs))?,
sys_loop,
timer_service,
)?;
let wifi_configuration: Configuration = Configuration::Client(ClientConfiguration {
ssid: SSID.try_into().unwrap(),
bssid: None,
auth_method: AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
..Default::default()
});
wifi.set_configuration(&wifi_configuration)?;
wifi.start().await?;
info!("Wifi started");
wifi.connect().await?;
info!("Wifi connected");
wifi.wait_netif_up().await?;
info!("Wifi netif up");
Ok(wifi)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment