With a lot of help from @phoddie and @ralphwetzel, I managed at last to measure distances from a HC-SR04 sensor by running node-red-mcu on my WT32-ETH01 board. So the least I could do is write down how we did it. And hopefully then more users start experimenting with this nice technology. Because these guys could use some help from this community...
Note that a lot of other boards are supported (e.g. Wemos D1 mini, ...) but the concept is always the same. The WT32-ETH01 board is a bit more difficult, so other boards need a few less steps as described below.
I need this project to measure the water level in some underground tanks in my garden. Since there is no wifi, I have installed a waterproof CAT5 wire. At the tanks I will in install a WT32-ETH01 board, which has an ESP32 microcontroller and an onboard RJ45 connector:
Since the board has no build-in POE, I use a POE splitter as described in another discussion:
While the HC-SR04 is a well known ultrasonic distance sensor, I have used a JSN-SR04T which is a waterproof alternative.
Setup Moddable SDK & node-red-mcu(-plugin)
Before you can get started, you will need to install some tools on the host were Node-RED is running. In my case this is a Raspberry.
- Installing: steps you need to do only the first time that you setup the SDK
- Updating: steps you need to do when you want to install a new version of the SDK. Because the SDK is frequently updated with bug fixes and new features (e.g. new sensor types).
At the end of their Getting Started guide, there is also a section " What's Next: Building and Running Apps on Development Boards and MCUs". For each device, there is a link to an installation procedure to install the development tools for your target device. Since I wanted to program an ESP32, I have clicked the "Using the Moddable SDK with ESP32" link and executed all the steps.
Now we need to install the MCU sidebar for our Node-RED flow editor. Of course you could also run all the required Moddable
mcconfigcommands manually, but the sidebar generates these commands automatically behind the scenes. Go to the "manage palette" menu to install the node-red-mcu-plugin:
This sidebar node will also install node-red-mcu automatically as a dependency. The node-red-mcu is a Moddable SDK project to run a Node-RED flow on a microcontroller.
Install the Sensor node via this command:
cd ~/.node-red npm install /home/pi/.node-red/node_modules/@ralphwetzel/node-red-mcu-plugin/node-red-mcu/nodes/mcu
Now you will get a bunch of MCU related nodes in your palette:
Setup the serial interface
To be able to flash and debug the MCU, you need to connect your host (where Node-RED is running) to your MCU via a serial interface. For most boards (e.g. Wemos D1 mini) this will happen via a simple USB connection. However the WT32-ETH01 board has no USB connector, we need a CH340G Serial Converter USB to TTL converter:
And then the 4 wires need to be connected to these pins of the WT32-ETH01 board:
|TX||RX0 (not RXD)|
|RX||TX0 (not TXD)|
On the USB-TTL converter, set the jumper to S1 and 5V (not S1 and 3.3V).
Program the ESP32
These steps need to be repeated, every time you want to flash a Node-RED flow to your MCU:
Draw a flow that needs to be flashed to the MCU. For example:
- On the node-red-mcu readme page you will see that a series of core nodes have an MCU implementation, which means they are fully supported on MCU's.
- As you can see there, also some existing sensor related nodes have been modified, to make sure they can run on an MCU. But you can also control these same sensors via their Sensor node, and the Sensor node offers an implementation for much more sensor types...
- Other nodes (e.g. function nodes) will also be converted automatically for your MCU, but they 'might' contain a bit too much code for a low-resource MCU. I haven't tried that, but perhaps @colinl can give some extra info.
Select in your MCU sidebar which flow needs to be flashed to the MCU:
Select your target platform and target board in the sidebar. In my case:
Select which serial port on the Raspberry you want to use. To do this, click on the serial port field and press the down array to show a list of all detected serial ports. If your TTL-USB converter is correctly inserted, it should show up in the list automatically:
If you need WIFI, then enter the SSID and password in the sidebar. At the moment of writing this tutorial, I realize that I have only tested my board via wifi. So I need to test it via CAT cable as soon as I have time...
Optionally it might be a good idea to cleanup all temporary files from the previous build. Select build target "clean" in the sidebar, and then press the blue build button:
The cleanup is completed, when the following text appears in the Console Monitor tabsheet:
Note that a build will take much longer after a clean, because the build cache will also be cleared. Which means everything needs to be build from scratch, even the stuff that hasn't changed. Of course you could skip this cleanup step, unless you have the impression that something did not went well (e.g. when you have the impression that the updates in your flow did not get deployed on your MCU....).
Build the binary image of the selected flow for your MCU. Select build target "build" in the sidebar, and then press the blue build button:
You can follow the status of the build process in the Console Monitor tabsheet. When the build is completed, the following text appears:
Now put the WT32-ETH01 board in programming mode, by connecting its IO01 pin to GND. Then power off/on the board to activate the programming mode. This is not required on most other board types, since it will then happen all automatically via the serial USB interface.
Now flash the binary image to the MCU, via the build target "deploy" in the sidebar, and then press the blue build button:
You can follow the status of the deploy process in the Console Monitor tabsheet. When the flashing is completed, the following text appears:
Now put the WT32-ETH01 board in normal mode, by deconnecting the IO01 pin again from GND. Then power off/on the board to activate the normal mode. This is not required on most other board types, since it will happen all automatically via the serial USB interface.
Finally activate the Moddable debugger, called xsbug. Because that debugger communicates with the MCU via the serial interface, to exchange messages for step 11. You can start it (to receive messages from the MCU) by selecting build target "xsbug" and pressing the blue build button.
As you can see in the screenshot, there are two debugger types (i.e. debugger targets):
- console: to start xsbug-log, which is a console logger for headless Raspberry systems like mine. Since there is no graphical layer installed, I needed to use the console log...
When the debugger has started listening to messages arriving from the MCU, the following text appears:
Note that there is a shortcut (to do exactly the same) by clicking on the arrow of the blue button, and then "Reconnect to xsbug":
If everything goes well, the nodes should get their correct status in your flow editor (which is send by the MCU). If you have deployed Debug nodes, the corresponding messages should be displayed in the debug sidebar.
As mentioned in the previous step, all this data arrives from the MCU via the serial interface and xsbug (or xsbug-log) will send this data to Node-RED. Which means that you will see only node status updates and messages in the Debug sidebar when you have a serial connection and xsbug (or xsbug-log) is active!!!
In case it doesn't work, you might have a look at my cheat-sheet. That might give you some pointers of where to start searching.
Have fun with it, and please support the Moddable team by start using their stuff!!!