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