painlessMesh and Android

Links

Source code (Open Source, MIT license)

App on Google Play

App on F-DROID (pending)

painlessMesh

Intro

Some time ago I got interested in painlessMesh for ESP8266 and ESP32.
painlessMesh is a mesh network for ESP8266 and ESP32 and it is quite easy to implement in your ESP projects. The advantage of a mesh network is that your sensor and actuator nodes are not depending on a connection to your home WiFi network. There are several different possibilities to build up a mesh network. For ESP a possibility is the ESP Mesh framework from Espressif itself, but unfortunately that isn’t integrated into the ESP-Arduino framework yet. ESP Mesh is implemented on a low level of the WiFi communication protocols and specific for ESP chips.
painlessMesh works on a higher level using TCP communication. For the connection each node in the mesh network opens a WiFi access point in AP mode and connects the the strongest AP belonging to the mesh network it can find. Each node offers a TCP server for the communication between the nodes. A full description of the communication can be found in the Wiki pages of painlessMesh. painlessMesh is open source and the sources can be found in the Gitlab repository.

Why painlessMesh for Android?

After building up a test network with a mix of ESP8266 and ESP32 devices I soon realized that I cannot check the status of the network without having at least one of the devices connected over USB to my computer. Of course I could setup one of the devices as a gateway between the mesh network and my home WiFi, but that would mean additional setup work and one node dedicated to work as this gateway. And I would need to write an Android program anyway to connect to the gateway.

So I looked for a different solution. In the Gitlab painlessMesh group is an example for a painlessMesh listener, but that is based on a programming language that I never heard of before (Did you know there is a language called “D”?). The code didn’t look too complicated, but as I have already some experience in writing programs for Android I decided to go the “JAVA” way. In addition to know how to do it, it gives in addition the comfort that I can connect to the painlessMesh network from any point in my house.

Status

  • The app is still in testing status, but I can connect to a painlessMesh network, send and receive the routing information, send and receive broadcast and single messages (if they are addressed to the applications nodeId).
  • Same as on an ESP8266 or ESP32 the nodeID is created from the MAC address of the device.
  • Received messages can be filtered by nodeId to focus on a specific node.
  • What is not implemented is the time sync functionality of the painlessMesh network. But even without that the app works quite well.

Restrictions

  • For now the application is tested only on Android Oreo (Android 8 – SDK 26), but should work on any Android version down to Android Lollipop (Android 5 – SDK 21).
  • Due to new security features since Android Marshmallow (Android 6 – SDK 23), on the first start the application is asking the user for permission to get the devices location. This permission is required to start a WiFi scan for access points. (Don’t ask me what the location has to do with a WiFi scan. Same restriction applies if you write a Bluetooth application and want to scan for advertising Bluetooth devices.)
  • On some devices Android will complain that the network you connected to (the mesh network) has no internet connectivity and asks you to change to a different WiFi network. Of course you have to say NO here to keep connected to the mesh network.
  • Even worse, my Huawei tablet has a function that switches automatically to a known WiFi AP if that AP has a better signal or internet connectivity. Usually a very good thing, but not if you want to monitor your painlessMesh network. Luckily on my device I could disable this function.
  • Still from time to time the Android device gets disconnected from the mesh network. Still investigating why this happens.

Principle function

On first start the application will ask for the permission to access the device location. As explained this is a security feature since Android Marshmallow.

After entering the painlessMesh credentials (Mesh name, Mesh password and Mesh port) a connection can be started by pushing the Connect button.
The app will then add the painlessMesh AP to the list of known WiFi APs and try to connect to it.

If successful connected the received messages will be displayed in a scrollable text window.
If not successful the user has to abort the connection TODO ==> Add a timeout function

The received message list can be cleared by pushing the Delete button.

At any time when connected it is possible to send a message into the painlessMesh network. The message can be either a broadcast message (send to all known nodes) or a single message (send to one node).
During the sending process, first a list of known nodes is displayed, with BROADCAST as first option. Once a receiver is selected a dialog box will open where either a predefined control message (Time sync request or Node sync request) can be send or a user message can be entered and send.

Source code

The complete source code is available on Gitlab. Feel free to use it or clone and change it if you  want to build the app by yourself or use parts of it to adapt it to your own painlessMesh setup. The source codes are freely usable under the MIT license

Some code snippets

Get WiFi MAC address of the Android device

Create painlessMesh node ID

Send a Node Sync Request

Send a user message

Build list of connected nodes, requires recursive calls of subfunction, because the routing information is a nested JSON object

I hope this helps someone to get his painlessMesh setup under control.

Loading...
Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

2 comments

  • Jack Menchin

    You list your background and an interest in opportunities to apply those skills.
    I am looking for a way (via ESP32) to monitor a lot of pumps and log/report their total daily run time and running current. The daily report would be collected – Mesh, and sent in the night to a cloud account. The packet would be Street address (about 10 characters), Run time, and typical Current – once a day.

    Why? I live in a senior community in Tennessee where the wastewater is moved by individual (one per household) grinder pumps to a 2 or 3” common collector line which then goes to a larger pump station from where it is sent on to a treatment plant. They have no individual monitoring system available and go out only when there is a failure. The grinder pump system was necessary as the area is hilly and rock is very shallow.

    I have used a WiFi mesh network previously for a job in a NY rail yard with adjacent track for AmTrack´s North East Corridor. The job was tunneling under the AmTrack and the rail Yard for what was called the East Side Connection. That was about 10 years ago. The WiFi chip had a processor attached to handle the data monitoring and comms. We collected the info with a field computer on a 1 hour cycle and transferred the data to an office computer via a Raven radio. Basically a cell phone where the speaker/microphone was connected to a RS232 chip and FTP took care of the rest.

    The data collection would be triggered by an Amp clamp (a small transformer that snaps over 1 of the 2 wires to the pump) The 20 amp version puts out a 0 to 1 volt ac signal as the current changes form 0 to a max of 20 amps. Priice $8 from China – about twice that price when ordered from US suppliers.

    Logging of all daily events would go to a uSD flash chip on an add on logger board. There was a ESP32 with a flash card slot but it has been discontinued. This data would be retrieveable by WiFi or BlueTooth to a personal computer parked at the curb. Like many water companies read their meters.

    The midnight ESP / Android phone home would be to a public Fi provided by the local cable provider Xfinity and sent to a cloud account for morning review.

    Interested?
    Jack Menchin
    609-456-3525

    Reply
    • beegee1962

      Hi Jack,

      Sorry for the late reply, but I abandoned my website in the last months due to some heavy project work.

      I am interested in the project, is the offer still open?

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory