Sometime I'll learn from my past mistakes and start from the position of writing a library, but this project was not that time.
Now I'm in a position where an Arduino sketch needs just four lines of code to be a functional mesh network node that routes traffic.
- Import the library eg. #include <EspNowMesh.h>
- Declare an object eg. EspNowMesh mesh;
- In setup() start the mesh eg. mesh.initEspNow();
- In loop() keep it ticking over eg. mesh.meshHousekeeping();
As my goal is to make this simple usable code other people can work with I'm happy with this. My last mesh network code was too byzantine to be usable, even once wrapped up as a library.
I still need to refactor things to match common Arduino style conventions.
For example "initEspNow()" should really be "begin()" and so on. Also the functions I have for putting data into packets and retrieving it are very much single purpose kludgey things rather than something I'd want to offer for real use.
Maybe I'll also change the name of the class completely, I'm not sure I like EspNowMesh as a name
I've written Arduino libraries before but this was harder work. The big battle was around callback functions, which are sprinkled through the ESP8266 WiFi and ESP-Now libraries my library relies on.
Using class member functions as callbacks, or even worse, using class member functions as callbacks for C libraries is a real roadblock if your C++ skills are beginner level. When it's all a monolithic sketch this stuff 'just works'. Make it a class in a library and it's suddenly very broken. This is why I'm prone to just writing a flat Arduino sketch as a proof of concept and worrying about making it re-usable later.
I may do a couple of blog topics with the workarounds I did as other people may find them useful. Simple solutions did not jump off the page of a Google search.