ESP-Now Support

Issue #453 resolved
Eric Vanderhoof created an issue

I am a high school robotics teacher using microblocks with ESP32-based control systems for robots. Upon trying the new Wifi Radio library, I found that it works in the evening when the students are gone, but not during class. My hypothesis is that additional network traffic and radio interference causes the weak wifi radios of the ESP32s to be unable to connect. I am considering ESP-Now as a replacement, since it is intended to simplify communications in network-crowded environments. I tried downloading the experimental ESP-Now library and example from Bitbucket, but have not so far been able to get them to function. I know that ESP-Now has recently been implemented in the Ioty project (https://quirkycort.github.io/IoTy/public/editor.html) based on micropython, and that is the next connection method I plan on trying. The EZ ESP-Now library in Ioty is set up as analogous to the microbit radio. Perhaps that could shed some light on its implementation in Microblocks? I am attaching links to the controller-side and robot-side code I have tried so far with the experimental ESP-Now library. Perhaps I am missing another component that just copying out the library does not provide? Any insight would be appreciated. Thank you!


Here are links in case attaching the code does not work. Robot Side Code: https://drive.google.com/file/d/1Ocic4o70huMCElImurVCjJa_uKLCDBP0/view?usp=sharing Controller Side Code: https://drive.google.com/file/d/1F9WWztCyCA6WIzuC9G4bacIfX3NL9uAf/view?usp=sharing

Comments (19)

  1. John Maloney repo owner

    I am a high school robotics teacher using microblocks with ESP32-based control systems for robots. Upon trying the new Wifi Radio library, I found that it works in the evening when the students are gone, but not during class. My hypothesis is that additional network traffic and radio interference causes the weak wifi radios of the ESP32s to be unable to connect.

    That's very interesting.

    What are the symptoms you are seeing? Are some or all of the ESP32's unable to connect to the WiFi network?

    One issue I've seen is that many WiFi routers support only a limited number of simultaneous client connection. It can be as little as 8 or even 4 (for a Raspberry Pi acting as a hotspot). Even commercial WiFi routers are often limited to just 8 connections. Of course, that would be a big problem in a classroom with 25-30 students. It's possible to find WiFi routers that support up to 32 connections, but they are not too common and it is often difficult to even find a spec for maximum number of connections a given router can support.

    The WiFi Radio library uses the UDP protocol to communicate with other devices on the same network, but for that to work you need to be able to connect all the devices to the same network. MQTT has a similar problem -- you need to connect to a WiFi network.

    The MicroBlocks ESP-Now library is an experimental feature contributed by a community member; it is not supported by the MicroBlocks team and, in fact, I have never used it myself. However, it may help with your problem.

    If you'd like to try it, you'll need to install a custom version of the MicroBlocks firmware that includes the necessary support. I just built one for you. You can install it using the "install ESP firmware from URL" command in the gear menu (only visible after enabling "show advanced blocks") to download from this link:

    https://microblocks.fun/mbtest/tmp3/vm_esp32_espNOW.bin
    

    The ESP-Now may be limited of 20 clients:

    https://docs.espressif.com/projects/esp-faq/en/latest/application-solution/esp-now.html#:~:text=If%20unicast%20packets%20are%20used,devices%20that%20can%20be%20controlled

    although it sounds like there is a "broadcast mode" that does not have that limit. I hope the MicroBlocks feature uses broadcast mode.

    We've recently been exploring using various ways to use Bluetooth Low Energy (BLE) in MicroBlocks. One of our users has created an experimental feature that allows MicroBlocks to communicate with mobile devices running the new OctoStudio programming system. (OctoStudio is from the same MIT team that created Scratch.) We may be able use the same technique to implement a version of the Radio library that uses BLE as the transport mechanism. If that idea works, it would be a good solution to your problem since BLE works without connecting to a WiFi network.

    If you try ESP-Now, please let me know how well it works. I'm especially interested in how well it works in your classroom setting, if it supports more than 20 ESP32's, and if you need to do anything special to "pair" the ESP32's with each other.

  2. Eric Vanderhoof reporter

    Thanks John! I would say about 3/4 of the ESP32s cannot connect to wifi. I might have the students turn off their other devices' wifi to see if it is based on the number of clients. The ESP32s that can connect do so only sporadically. They just keep flashing the user LED and after a minute or so say that they could not connect to wifi. I had our IT department expressly allow inter-device UDP communication on our specific wifi router. In general, it is disabled to limit exposure to hacking attempts.


    The experimental ESP-Now library seems to only use broadcast mode. Thanks for making a custom image. I flashed the firmware with the provided link and tried to run a simple program. It gave the error: “Error: Primitive not implemented in this virtual machine.” I had not gotten this error when I tried to run the library with the normal firmware, but the library also didn’t do anything at all previously.

    BLE could be a good option for our needs as long as the range is about ten meters. Our robots have to operate via a video link across the room. Have you looked at the MQTT control panel app builder that Cort put together for Ioty (https://quirkycort.github.io/IoTy-MQTT-Client/public/)? A web interface like that could be a good way to control BLE devices without requiring the installation of any apps. App installation can be a difficult roadblock in schools. Our ideal use case would be a web page that can be opened on a touchscreen chrome book and send joystick control signals directly to ESP32s running Microblocks without an external wifi router. I don’t care too much what type of signals they are.

    Thanks again for all your help.

  3. John Maloney repo owner

    Ahh, you need a new version of the library. You can get that here.

    Just drag-and-drop the library file onto MicroBlocks to load it.

    The updated library will be in the next pilot release of MicroBlocks, coming soon.

  4. John Maloney repo owner

    BLE could be a good option for our needs as long as the range is about ten meters.

    In theory, it should be good for 10 (up to 50 meters with no obstructions) but I would expect the range to be less when there are a lot of devices using BLE heavily in the same area. I'm hoping to do some tests of that soon.

    Our robots have to operate via a video link across the room. Have you looked at the MQTT control panel app builder that Cort put together for Ioty (https://quirkycort.github.io/IoTy-MQTT-Client/public/)? A web interface like that could be a good way to control BLE devices without requiring the installation of any apps.

    I haven't look at Cort's MQTT UI builder but I've played with Adafruit's version. MQTT is certainly useful but it depends on WiFi. Thus, it would be a problem your router can't support simultanous connections all the devices (both laptops and ESP32 boards) in your classroom. Both ESP-Now and BLE avoid the WiFI router issue.

  5. Eric Vanderhoof reporter

    After flashing both ESP-32’s firmware to the version you supplied and updating the ESP-Now library, I was able to consistently get communication with a simple receiving app from one side of my classroom to the other, perhaps 15 meters. This is with no other devices running. I will have my students also install the applications and let you know how it works in a noisy environment with 16 devices running at once. Thanks for all your help!

  6. Eric Vanderhoof reporter

    All the students who tried to use ESP-Now were successful today. That makes about eight devices operating at the same time with no evidence of interference. I’ll have the whole class try it on Monday. Here’s the current code in case it’s helpful. I had the controller send a short comma-separated string that the receiver then parsed into a list. It is intentionally basic, as I want the students to optimize it to eliminate factors like noise in the control values.


    Receiver Side Robot Code: https://drive.google.com/file/d/1FPFd-CAz8QqeEACgKo46uIXQMnxkTmhV/view?usp=sharing


    Sender Side Sender Code: https://drive.google.com/file/d/1Fr_-j5vtHF2GKEXOjDE8nJTlwbMdcgUd/view?usp=sharing


    In case someone is looking at this in the future, we had to install this firmware to be able to use ESP-Now on the ESP-32: https://microblocks.fun/mbtest/tmp3/vm_esp32_espNOW.bin

  7. John Maloney repo owner

    Thanks for the update. Nice to know that ESP Now works for up to eight students.

    As I mentioned, I've been exploring BLE and I am hoping to be able to provide functionality similar the ESP Now library using BLE. That would several advantages:

    • it could co-exist with other BLE applications, such as the OctoStudio library
    • it could inter-operate with phones, laptops, and non-ESP boards like the micro:bit and, eventually, Raspberry Pi Pico-W
    • it could co-exist with the ability to program the board wirelessly over BLE, which is very useful when working on robotic vehicles

    My understanding is that ESP-Now and BLE cannot co-exist because they use the ESP32 radio in non-compatible ways. Thus, in the long term, ESP-Now is unlikely to become a supported feature, although it may continue indefinitely as a community-supported experiment. You can certainly continue to use it for the rest of this school year or until we have a good BLE alternative.

  8. Eric Vanderhoof reporter

    Thanks John! I agree that if only one communication protocol can be used, BLE would make a better long-term option due to compatibility with other platforms . From the ESP-32 documentation I’ve seen, BLE, wifi, and ESP-Now all use the same radio. Here’s the documentation for their coexistence strategy: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/coexist.html . I haven’t taken the time to understand it fully, but perhaps it will be a help in the design process.


    Testing today showed independent operation of the robots with no interference problems. We’ll be playing matball with eight robots and eight transmitters for the midterm, so I’ll let you know how that goes. Thanks again for your help!

  9. John Maloney repo owner

    Thanks for the link to the "coexist" page. It's useful for understanding the issue although, so far, I have not figured out how to make the NimBLE Bluetooth stack actually coexist with WiFi, let alone with ESP-Now.

    However, even if I could make several protocols share the radio, performance and reliability might be degraded. For example, BLE packets might be missed when the radio was in WiFi mode and vice versa.

    My current thinking is to run the radio in either BLE or WiFi mode and provide a way to switch between those based on the application. But it's still early days so we'll see...

  10. Eric Vanderhoof reporter

    I agree it seems quite complicated. This document seems to say that initialization order matters, and that wifi must be initialized before ESP-Now. I really haven’t looked into BLE yet.

  11. John Maloney repo owner

    Interesting. From this page, it certainly sounds like IoTy is using only one mode at a time: serial, BLE, or WiFi (in either station mode or hotspot mode).

  12. Eric Vanderhoof reporter

    I haven’t tried using BLE to connect with Ioty, since the students are used to using wired serial connections. I know that you can use the wifi blocks within the program while using serial debugging, as we’ve done that before. I have two students regularly using Ioty with micropython, thirteen using Microblocks, and one using Arduinoblocks with Arduino’s C++. We default to Microblocks for more complex projects, since I can write block-based libraries for the kids to use. Microblocks also gets most of my attention since the largest number of students use it.


    There is also BIPES, which was developed independently of Ioty but has a lot of the same features. It also has Bluetooth connection capability. I tried using it last year, but wasn’t able to continue doing so as it only supports one servo at a time and that was inadequate for our robots.


    I’ll be in touch after winter break and let you know how everything went. Thanks again!

  13. John Maloney repo owner

    Here's an update about this.

    The latest MicroBlocks pilot release has support for BLE built in on ESP32 boards. We have also verified that BLE and WiFi can be used at the same time -- although I haven't measured the impact of that on performance/throughput.

    MicroBlocks now supports beaming messages using the OctoStudio technique, based on BLE advertisements. Unfortunately, the BLE advertisting mechanism turns out to be very limited which is not so surprising since wasn't meant for this purpose. In particular, the receiver can miss adverisements unless the advertisements are sent continuously for at least 100 msecs -- and even then, the success rate is not 100%. That means you can send at most 10 messages per second. Finally, the messages themselves cannot carry much data.

    UDP, ESP Now, and (on Nordic nRF5x boards) the Nordic radio system provide much higher throughput and reliability combined with the ability to do one-to-many broadcasting.

    However, ESP Now has some quirks. In particular, it needs to use the same radio channel as the WiFi system, which can lead to situations where boards cannot communicate with each other over ESP Now because they are on different channels.I don't think you'll run into that problem is you just use ESP Now and never connect to WiFI.

    Because of its potentially confusing interactions with WiFi, I don't plan to build ESP Now into the standard MicroBlocks firmware that can be installed directly from the IDE. However, it will continue to be available as a "user supported" option. That means that you can either built and install the firmware from source or install it from a server using the "install firmware from URL" mechanism. I'll leave the current .bin file up, at least for a while, but I probably won't update it regularly so you may want to install platformio so you can build an updated version whenever you want.

    In case you want to try it, here is a version with BLE, WiFi, and ESPNow:

  14. Eric Vanderhoof reporter

    Thanks John, I will give the BLE capabilities a try and let you know how it goes. Due to some limitations with our (very cheap) electronic speed controls, I wasn’t able to test more than a few robots working at once with ESP-Now. We didn’t notice any interference issues and had perhaps 15 feet of reliable control. The ESP-32’s were pretty deeply embedded in a metal robot frame, which probably hurt their radio performance.

    Am I right in thinking the “BLE Scanner” library is the BLE advertisements technique you mentioned?

  15. John Maloney repo owner

    The BLE Scanner is just for exploring what BLE devices are around; what you want is the OctoStudio library.

    The OctoStudio library allows you to both send and receive simple messages either between ESP32 board or from an iOS or Android phone running the free OctoStudio blocks programming environment. For example, you could make an OctoStudio project with five button sprites, each of which broadcasts a command. The command is one of five shapes, so it is very limited right now, but I believe the OctoStudio team is considering extending the mechanism in the future.

    There is no way to direct the commands to a specific robot -- every robot within BLE range will respond to the command.

    I had hoped to use the same mechanism with a different protocol to send a number and a short string, and to allow messages to be sent different "groups" (like the Radio library). That may happen eventually, but the low speed and reliablity of BLE makes me less excited about that direction.

    The WiFi Radio library (based on UDP) offers much higher throughput and reliability. The downside is that you need to have a WiFi router that supports enough simultaneous connections. I found that many home WiFi routers have a limit of 8 or 16 connections and a Raspberry Pi or ESP32 acting as a hotspot is even more limited. Of course, school and other public WiFi installations support much larger numbers of connections, so if you are working in such a place you may be all set.

  16. Log in to comment