ESP8266 painlessMesh Topology Visualizer in Python

A few months ago I have been experimenting with the ESP8266 painlessMesh network. The library provides a JSON string of the formed mesh topology, for example it looks like this:

[
    {
        "nodeId" : 2147321731,
        "subs" : [
            {
                "nodeId" : 2147319552,
                "subs" : [
                ]
            }
        ]
    },
    {
        "nodeId" : 2142436483,
        "subs" : [
            {
                "nodeId" : 2787708644,
                "subs" : [
                ]
            }
        ]
    },
    {
        "nodeId" : 2142436495,
        "subs" : [
            {
                "nodeId" : 2787708676,
                "subs" : [
                    {
                        "nodeId" : 2142436497,
                        "subs" : [
                        ]
                    },
                    {
                        "nodeId" : 2787708620,
                        "subs" : [
                        ]
                    }
                ]
            }
        ]
    }
]

At some points I think it would be very nice if I can immediately see how the mesh topology looks like. So I decide to write a simple GUI application that can visualize the mesh topology. For a rapid development I go for Python. I have not much prior experience in Python programming, and would like to use a nice and free IDE. I found one and really enjoyed using it. It is called PyCharm (v. 2017.2.3). It has editor, project viewer, debugger, runner, version control, etc. So it is quite a comprehensive IDE.

These videos demonstrate how the visualizer works:

Basically, the visualizer receives MeshTopology strings sent by a ESP8266 on serial port. The visualizer then parse the strings, build NetworkX graph, and draw the graph.  Graph drawing is based on the NetworkX (v.2.0) library. However, to make it work in this application, a little modification is made on the file networkx/drawing/nx_pylab.py. nx_pylab.py has a function called draw(). By default, draw() returns nothing. But to make it work in this application, I modify the draw() function to return cf (figure reference), node_collection.

return cf, node_collection

Higher version of NetworkX most likely will not work because the drawing function is discarded. NetworkX drawing functions depend on Qt5 library. So you need to install PyQt library as well.

Before running the codes, ensure the correct serial port number is provided matching to your setup. The line is in file top.py, function setupUi(self, MainWindow):

# init and open serial port
# SET COMxx to the corresponding serial port number used by the ESP
comPortNum ='COM31'<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0;">&#65279;</span>

As you can see from the videos above, some of the application features are:

  • Visualize mesh topology. The figure is updated whenever the topology has changed. The draw() function distributes the node position based on some sort of random statistical formula, which results in most likely different node position every time the graph is updated
  • A click on node ‘Me’ will pop up ‘broadcast’ dialog box. Through this dialog, we can set parameters all of nodes.
  • A click on nodes other than ‘Me’ will pop up ‘single’ dialog box. We can set and read parameters on that particular node

On the ESP8266 side, the library painlessMesh v.1.0.1 is used. The ESP8266 regularly sends the MeshTopology to serial port. It can interpret commands from the visualizer application, such as:

  • Reading the node available heap memory
  • Forwarding messages to a single node
  • Forwarding messages to all nodes
  • Set and read parameters (i.e. ‘timer’ and ‘brightness’)

The Python mesh visualizer source codes are available at:

https://github.com/yoppy-tjhin/meshTopologyVisualizer

The app is started from main_window.py. It then loads a window from top.py. The top.py orchestrates all other functions, such as Serial thread, parsing MeshTopology, drawing graphs, etc.

And the Arduino ESP8266 painlessMesh codes can be found at:

https://github.com/yoppy-tjhin/espMesh

The following video shows a demo  of 10-node ESP8266 painlessMesh network:

Advertisements

Programming ESP8266 using Visual Studio Code with Arduino Extension

So now I have used the official Arduino IDE for some time to program the ESP8266. It is a convenient and quick-to-get-started method (check here). But recently I plan to write an application that I feel it would be uncomfortable to write codes using the limited features of Arduino IDE. I find particularly two feature-rich IDEs out there, which are free and highly recommended by communities, namely Atom and Visual Studio Code. With Atom, we need to add the PlatformIO extension for programming embedded systems, including the ESP8266. While with VS Code, we need to install either PlatformIO or Arduino extension.

I had difficulties to get both Atom-PlatformIO and VS Code-PlatformIO working. Somehow the PlatformIO installation was not able to complete. I suspected it was caused by my office internet proxy. I spent quite some time to solve it but did not succeed, so I abandoned it and tried the Arduino extension.

Getting the VS Code with Arduino extension working was not that straight forward either. If you search for the Arduino extension, you will find two choices, either developed by Microsoft or Steve Yin. Initially I chose the one developed by Microsoft. Well, Arduino extension is just an extension that allows users to tell the VS Code where the tools for compiling and uploading code reside. So we must have the official Arduino IDE installed because its tools for compiling and uploading will be used by the VS Code. Then we need to specify the Arduino IDE path in the VS Code ‘User Settings’. Btw, my OS was Windows 8, and I installed the official Arduino IDE version 1.8.2. For unknown reasons compiling sketch using VS Code was always fail with a message saying ‘[Error] Exit with code=undefined’. Actually, I also had an Ubuntu machine, and I managed to get the Arduino extension working. I could compile and upload codes to the ESP8266. But for some reasons, I still preferred to work on my Windows 8 machine.

So, then I gave the Steve Yin’s Arduino extension a try on my Win 8. The compile and upload options in User Settings were more complicated than those of Microsoft one.  But happily I succeeded to compile and write bits of codes to the ESP8266 flash memory. The easiest way to specify the compile and upload options is by adapting them from the command list spat out from the Arduino IDE console output. Again, you can follow this instruction to program ESP8266 using Arduino IDE.

arduino ide compile - for vs code arduino ext

Here is my VS Code ‘User Settings’:

"arduino.uploader" : "C:\\Users\\User-pc\\AppData\\Local\\Arduino15\\packages\\esp8266\\tools\\esptool\\0.4.9\\esptool.exe",
"arduino.uploadOptions": "-vv -cd nodemcu -cb 921600 -cp COM12 -ca 0x00000 -cf $TARGET.bin",
"arduino.compileOptions": "-compile -logger=machine -hardware D:/Master/arduino-1.8.2/hardware -hardware C:/Users/User-pc/AppData/Local/Arduino15/packages -tools D:/Master/arduino-1.8.2/tools-builder -tools D:/Master/arduino-1.8.2/hardware/tools/avr -tools C:/Users/User-pc/AppData/Local/Arduino15/packages -built-in-libraries D:/Master/arduino-1.8.2/libraries -libraries C:/Users/User-pc/Documents/Arduino/libraries -fqbn=esp8266:esp8266:nodemcuv2:CpuFrequency=80,UploadSpeed=115200,FlashSize=4M3M -vid-pid=0X2341_0X0043 -ide-version=10802 -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.esptool.path=C:/Users/User-pc/AppData/Local/Arduino15/packages/esp8266/tools/esptool/0.4.9 -prefs=runtime.tools.xtensa-lx106-elf-gcc.path=C:/Users/User-pc/AppData/Local/Arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2 -prefs=runtime.tools.mkspiffs.path=C:/Users/User-pc/AppData/Local/Arduino15/packages/esp8266/tools/mkspiffs/0.1.2 -verbose",
In the “arduino.compileOptions” I omitted the built-path and built-cache options since they caused compiling to fail. The Arduino extension had its own built-cache which worked fine. I had not looked further for the meaning of the other options. They might be fine to be deleted. However they seems didn’t do any harm, by now I just leave it there.
In the “arduino.uploadOptions” we could set the COM port and upload speed. By default, the upload speed is 115200, which was quite slow. I found it somewhere that says we can increase the speed by a factor of 8, so it becomes 921600. It was indeed much faster. But the reference I read also says that this high speed can sometimes be unstable. So, bear this in mind. I did try other higher baud rates: 1500000, 1843200, 2000000. However the uploading time took longer than that of 921600.
vs code arduino ext compile
Initially I thought installing these tools for programming ESP8266 would be just some clicks away. But in fact I had spent a lot of time to get it done properly. I hope you have a nice and smooth installation. In case you face the same problem, hope my experience would help.
Happy coding!