https://toitlang.org/ logo
ESPNow crashes on ESP32-S3
# help
i
When I try to add a peer to a ESPNow device to connect to a ESPNow Station I am receiving a crash (will post in a new message) I am starting the station with:
Copy code
service := espnow.Service.station --key=null
  //3030F9791AE4
  service.add-peer (Address #[0x30, 0x30, 0xf9, 0x79, 0x1a, 0xe4]) --channel=1
and the client side using:
Copy code
service := espnow.Service.station --key=null
  print "Add peer"
  service.add-peer ADDRESS
      --channel=1
I also flashed the device using the proxy option: Flashing:
jag flash -p COM19 --chip=esp32s3 --uart-endpoint-rx=44
Monitor:
jag monitor -p COM19 --proxy
f
I will see if I can reproduce
i
It might has to do with the fact that I am using ESPNow which somehow crashes the WiFi when it tries to turn it off for espnow and reboots the device which then of course stops my program. But I am also getting in a loop there if I try to run it as a container
Client:
Copy code
import esp32.espnow
import esp32.espnow show Address
import log
import gpio
import .utils

logger ::= log.Logger log.DEBUG_LEVEL log.DefaultTarget --name="bouy"
// Station MAC: 3030F9791AE4
ADDRESS ::= Address #[0x30, 0x30, 0xF9, 0x79, 0x1A, 0xE4]

main args:
  logger.debug "MACA: $get-mac-address-str"
  logger.debug "Start ESP-NOW"
  service := espnow.Service.station --key=null
  service.add-peer ADDRESS
      --channel=11

  paket := "test-data".to_byte_array
  logger.debug "Send datagram: \"$paket\""
  service.send paket --address=ADDRESS
Station:
Copy code
import gpio
import esp32.espnow
import esp32.espnow show Address
import log
import .utils

logger ::= log.Logger log.DEBUG_LEVEL log.DefaultTarget --name="station"

main:
  logger.debug "MACA: $get-mac-address-str"
  logger.debug "Start Station"
  service := espnow.Service.station --key=null
  //3030F97919C8
  service.add-peer (Address #[0x30, 0x30, 0xF9, 0x79, 0x1A, 0xE4]) 
    --channel=11

  while true:
    print "Waiting for data"
    datagram := service.receive
    logger.debug "Received data $datagram.stringify"
I am also confused again which MACA I have to add as a peer on each side
I refactored the code to look absolut identical: Station:
Copy code
import esp32.espnow
import esp32.espnow show Address
import log
import gpio
import .utils

ADDRESS ::= Address #[0x30, 0x30, 0xF9, 0x79, 0x1A, 0xE4]
CHANNEL ::= 0

logger ::= log.Logger log.DEBUG_LEVEL log.DefaultTarget --name="station"

main args:
  logger.debug "MACA: $get-mac-address-str"
  logger.debug "Start Station"
  service := espnow.Service.station --key=null
  service.add-peer ADDRESS
    --channel=CHANNEL

  while true:
    print "Waiting for data"
    datagram := service.receive
    logger.debug "Received data $datagram.stringify"
Client:
Copy code
import esp32.espnow
import esp32.espnow show Address
import log
import gpio
import .utils

ADDRESS ::= Address #[0x30, 0x30, 0xF9, 0x79, 0x1A, 0xE4]
CHANNEL ::= 0

logger ::= log.Logger log.DEBUG_LEVEL log.DefaultTarget --name="bouy"

main args:
  logger.debug "MACA: $get-mac-address-str"
  logger.debug "Start ESP-NOW"
  service := espnow.Service.station --key=null
  service.add-peer ADDRESS
      --channel=CHANNEL

  paket := "test-data".to_byte_array
  logger.debug "Send datagram: \"$paket\""
  service.send paket --address=ADDRESS
with that I am receiving the data
I changed the channel to be 0 on the client side. It seems that I can not change the channel and it is always 0. I tried to set it to 1 on the station side and kept it 0 on the client side and still was able to send data. Setting it to 1 on the client side was giving me the channel error again. I think this came with an update of the IDF
something similar is explained here: https://esp32.com/viewtopic.php?t=18291
f
WiFi and ESPNow don't work together, and Toit doesn't protect itself well against it. If you ever try to use both at the same time you will get a crash.
i
I thought that is was the proxy is "fixing" for me
f
No. The proxy allows you to still have access to your device even without WiFi.
It gives you a different way of interacting with the ESP32 since you must turn off WiFi.
It would work with ethernet, though.
But Jaguar doesn't use ethernet yet (iirc).
i
no ethernet in this project. How can I disable wifi?
f
With Jaguar you have to add a flag when running the program or installing the container.
-D jaguar.disabled
i
what happens currently is that whenever I call
jag run ...
it will crash and I have to call it a second time
f
I would forget the proxy for now since the committed version doesn't work nicely yet. I have a PR outstanding which should improve things. Until then my recommendation is: -
jag run -D jag.disabled -D jag.timeout=45s my-app.toit
. This gives your app 45s to run before Jaguar kills it, so it can accept new requests. If your app stops earlier Jaguar will start up earlier too. -
jag container install -D jag.disabled -D jag.timeout=1000000m my-container my-app.toit
. But in this scenario, I would add a small test to the beginning of your code:
Copy code
main:
  pin := gpio.Pin 32 --input --pull-up
  if pin.get == 0: return
  // Normal code.
This gives you the option of going into Jaguar by just pulling pin 32 (or whichever you want) low (connecting it to ground) when resetting your device.
i
okay I see - the channel part seems to be still valid anyway. If I change it from 0 to 1 they can not connect anymore
f
Fwiw, ESPNow + WiFi is always limited. It is possible (not in Toit) to have both, but the ESPNow would then need to use the same frequency as the WiFi. And it can't force a specific frequency.
i
for this project I would not need WiFi tho
f
I will have a look at the channel.
You would need to turn off WiFi while using ESPNow.
i
if I fail with this disable option I can anyway always simply re-flash the device, right?
f
Yes.
i
is this related to the channel issue?
f
If WiFi is running at the same time all bets are off.
Yes. this is a good hint. I will see if I can fix it.
The new SDK now disallows WiFi and EspNow at the same time. It also changed the API slightly for the channel, which is now given at construction time of the station.
k
Jaguar v1.31.0 is out with the new SDK.
i
@kasperl it seems that I can not send data anymore with this version?!
Copy code
[bouy] DEBUG: MACA: 3030F9791AE4
[bouy] DEBUG: Start ESP-NOW
[bouy] DEBUG: Add peer: 30:30:f9:79:1a:e4 on channel 0
[bouy] DEBUG: Send datagram: "#[0x74, 0x65, 0x73, 0x74, 0x2d, 0x64, 0x61, 0x74, 0x61]"

******************************************************************************
Decoding by `jag`, device has version <2.0.0-alpha.134>
******************************************************************************
EXCEPTION error.
ESP-Now send failed
  0: Service.send.<block>      <sdk>\esp32\espnow.toit:149:9
  1: Mutex.do.<monitor-block>  <sdk>\monitor.toit:28:27
  2: __Monitor__.locked_.<block> <sdk>\core\monitor_impl_.toit:123:12
  3: __Monitor__.locked_       <sdk>\core\monitor_impl_.toit:95:3
  4: Mutex.do                  <sdk>\monitor.toit:28:3
  5: Service.send              <sdk>\esp32\espnow.toit:143:17
  6: main                      espnow\test-bouy-minimal.toit:26:11
******************************************************************************
the station is also on channel 0
CLIENT Code:
Copy code
import esp32.espnow
import esp32.espnow show Address
import log
import gpio
import .utils
import .rgb-led show RGBLED

ADDRESS ::= Address #[0x30, 0x30, 0xF9, 0x79, 0x1A, 0xE4]
CHANNEL ::= 0

logger ::= log.Logger log.DEBUG_LEVEL log.DefaultTarget --name="bouy"
led := RGBLED

main args:
  led.blue

  logger.debug "MACA: $get-mac-address-str"
  logger.debug "Start ESP-NOW"
  service := espnow.Service.station --key=null
  logger.debug "Add peer: $ADDRESS on channel $CHANNEL"
  service.add-peer ADDRESS
      --channel=CHANNEL

  paket := "test-data".to_byte_array
  logger.debug "Send datagram: \"$paket\""
  service.send paket --address=ADDRESS
f
Can you try a different channel?
i
Station:
Copy code
import esp32.espnow
import esp32.espnow show Address
import log
import gpio
import .utils
import .rgb-led show RGBLED

ADDRESS ::= Address #[0x30, 0x30, 0xF9, 0x79, 0x1A, 0xE4]
CHANNEL ::= 0

logger ::= log.Logger log.DEBUG_LEVEL log.DefaultTarget --name="station"
led := RGBLED

main args:
  led.green

  logger.debug "MACA: $get-mac-address-str"
  logger.debug "Start Station"
  service := espnow.Service.station --key=null
  logger.debug "Add peer: $ADDRESS on channel $CHANNEL"
  service.add-peer ADDRESS
    --channel=CHANNEL

  while true:
    print "Waiting for data"
    datagram := service.receive
    logger.debug "Received data $datagram.stringify"
f
Maybe it must be greater than 0.
I think I only tested 1+.
i
let me check if I am actually running the latest version
f
The new version takes the channel as argument to the station construction
It ignores it for
add-peer
and should give you a deprecation warning if you do use it.
i
it says add-peer is deprecated?
espnow\test-station-minimal.toit:21:11: warning: Deprecated 'Service.add-peer' service.add-peer ADDRESS ^~~~~~~~
f
See my last message
i
yes yes that why I am saying it. I add the channel via the service
just like in the example but I still get the deprecation warning
ah no sorry
f
You shouldn't use
--channel
for
add-peer
anymore since you gave it to the constructor earlier
i
yes yes I was changing the wrong file
f
Classic 🙂
i
it says now it is already in use
f
Could be the WiFi is running?
i
I have not disabled it yet, so I guess so
f
We would have crashed with an earlier release ..
i
okay I disabled it
that seems to work
so sorry to bring that up again: currently I am in development and would like to just disable wifi forever. I am connected via USB and I am also enabled the proxy. so it should be fine to do so, right?
at some point I would just like to keep it running for a while and don't want it to stop the container
f
The proxy doesn't fully work yet but is close. Hopefully next Jaguar release.
i
but it seems that the client can still not send its data
f
I will probably also add an option never to use WiFi.
i
I just added a 5m timeout for now
f
At the moment you just need to use a big timeout
i
Copy code
[bouy] DEBUG: Add peer: 30:30:f9:79:1a:e4 on channel 2
[bouy] DEBUG: Send datagram: "#[0x74, 0x65, 0x73, 0x74, 0x2d, 0x64, 0x61, 0x74, 0x61]"

******************************************************************************
Decoding by `jag`, device has version <2.0.0-alpha.143>
******************************************************************************
EXCEPTION error.
ESP-Now send failed
  0: Service.send.<block>      <sdk>\esp32\espnow.toit:157:9
  1: Mutex.do.<monitor-block>  <sdk>\monitor.toit:28:27
  2: __Monitor__.locked_.<block> <sdk>\core\monitor_impl_.toit:123:12
  3: __Monitor__.locked_       <sdk>\core\monitor_impl_.toit:95:3
  4: Mutex.do                  <sdk>\monitor.toit:28:3
  5: Service.send              <sdk>\esp32\espnow.toit:151:17
  6: main                      espnow\test-bouy-minimal.toit:25:11
******************************************************************************
they are both on channel 2 now
f
But if your program stops/crashes Jaguar still starts up
Ok. I will have another look on Monday
Can you show me the espnow code for both?
i
could give me a hint on the peer again? I am adding the MACA of the station on both sides! the client and the station, that is correct, right? (thts how I did it beefore)
station:
Copy code
import esp32.espnow
import esp32.espnow show Address
import log
import gpio
import .utils
import .rgb-led show RGBLED

ADDRESS ::= Address #[0x30, 0x30, 0xF9, 0x79, 0x1A, 0xE4]
CHANNEL ::= 2

logger ::= log.Logger log.DEBUG_LEVEL log.DefaultTarget --name="station"
led := RGBLED

main args:
  led.green

  logger.debug "MACA: $get-mac-address-str"
  logger.debug "Start Station"
  service := espnow.Service.station --key=null --channel=CHANNEL
  logger.debug "Add peer: $ADDRESS on channel $CHANNEL"
  service.add-peer ADDRESS

  while true:
    print "Waiting for data"
    datagram := service.receive
    logger.debug "Received data $datagram.stringify"
f
Don't remember, but if that's what you did before then it should still work. Didn't change anything there
i
Client:
Copy code
import esp32.espnow
import esp32.espnow show Address
import log
import gpio
import .utils
import .rgb-led show RGBLED

ADDRESS ::= Address #[0x30, 0x30, 0xF9, 0x79, 0x1A, 0xE4]
CHANNEL ::= 2

logger ::= log.Logger log.DEBUG_LEVEL log.DefaultTarget --name="bouy"
led := RGBLED

main args:
  led.blue

  logger.debug "MACA: $get-mac-address-str"
  logger.debug "Start Bouy"
  service := espnow.Service.station --key=null --channel=CHANNEL
  logger.debug "Add peer: $ADDRESS on channel $CHANNEL"
  service.add-peer ADDRESS

  paket := "test-data".to_byte_array
  logger.debug "Send datagram: \"$paket\""
  service.send paket --address=ADDRESS
f
Shouldn't the address be different for the two?
i
yea thats the part I never get in this ESPNow thing
AAHHH
man
OF COURSE I changed the station now, its the other device
🙄
f
As long as it eventually works 🙂
i
it does! of course... thats the pain when you work with 2 identical hardware and start swapping there behavior
f
🙂
i
I always used the other one as station
pretty bad that you have to hardcode the MACA for that. I might use broadcast later or so. I wanted to try this because it can handle more devices (which is not really needed tbh)
okay - man sorry for wasting your time here
f
Np.
2 Views