From 0aa048a5bd3a9b37d4c1f1546dd0f8f3b1e7d591 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Sat, 2 Jun 2018 20:20:57 +0200 Subject: [PATCH] mesh example --- platformio.ini | 16 ++++- src/MeshNet.h | 10 +++ src/Network.h | 10 +++ src/Sprocket.cpp | 8 +++ src/Sprocket.h | 10 ++- src/WiFiNet.h | 14 ++-- src/examples/{ => basic}/ExampleApp.h | 0 .../basic_example.cpp} | 14 ++-- src/examples/mesh/mesh_example.cpp | 66 +++++++++++++++++++ 9 files changed, 131 insertions(+), 17 deletions(-) create mode 100644 src/MeshNet.h create mode 100644 src/Network.h rename src/examples/{ => basic}/ExampleApp.h (100%) rename src/examples/{basic_sprocket.cpp => basic/basic_example.cpp} (82%) create mode 100644 src/examples/mesh/mesh_example.cpp diff --git a/platformio.ini b/platformio.ini index c2f08bf..abd3b5a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ ; http://docs.platformio.org/page/projectconf.html [platformio] -env_default = build +env_default = mesh [common] framework = arduino @@ -18,9 +18,9 @@ lib_deps = ESP Async WebServer ESPAsyncTCP TaskScheduler -; painlessMesh [env:build] +src_filter = +<*> - #platform = https://github.com/platformio/platform-espressif8266.git#feature/stage #platform = https://github.com/platformio/platform-espressif8266.git #platform = espressif8266@~1.6.0 @@ -32,4 +32,14 @@ framework = ${common.framework} lib_deps = ${common.lib_deps} #build_flags = -DLED_PIN=2 -g ;upload_port = /dev/ttyUSB0 -;upload_port = 192.168.1.168 \ No newline at end of file +;upload_port = 192.168.1.168 + +[env:mesh] +src_filter = +<*> + - +platform = espressif8266 +board = esp12e +upload_speed = 921600 +monitor_baud = 115200 +framework = ${common.framework} +lib_deps = ${common.lib_deps} + painlessMesh \ No newline at end of file diff --git a/src/MeshNet.h b/src/MeshNet.h new file mode 100644 index 0000000..21ace1b --- /dev/null +++ b/src/MeshNet.h @@ -0,0 +1,10 @@ +#ifndef __MESHNET_H__ +#define __MESHNET_H__ + +#include "Network.h" + +class MeshNet : public Network { + +}; + +#endif \ No newline at end of file diff --git a/src/Network.h b/src/Network.h new file mode 100644 index 0000000..3e8a43d --- /dev/null +++ b/src/Network.h @@ -0,0 +1,10 @@ +#ifndef __NETWORK_H__ +#define __NETWORK_H__ + +class Network { + public: + Network(){} + virtual Network* connect(); +}; + +#endif \ No newline at end of file diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp index 86580d1..f64e991 100644 --- a/src/Sprocket.cpp +++ b/src/Sprocket.cpp @@ -4,6 +4,12 @@ Sprocket::Sprocket(){ Serial.println("init sprocket"); } +Sprocket* Sprocket::init(SprocketConfig cfg){ + Serial.begin(cfg.serialBaudRate); + SPIFFS.begin(); + return this; +} + Sprocket* Sprocket::use(AppStack* stk){ stack = stk; return this; @@ -12,10 +18,12 @@ Sprocket* Sprocket::use(AppStack* stk){ Sprocket* Sprocket::addTask(Task& tsk){ scheduler.addTask(tsk); tsk.enable(); + return this; } Sprocket* Sprocket::registerApp(App& app){ app.activate(&scheduler); + return this; } void Sprocket::loop(){ diff --git a/src/Sprocket.h b/src/Sprocket.h index 389f456..4e2e34e 100644 --- a/src/Sprocket.h +++ b/src/Sprocket.h @@ -9,6 +9,9 @@ #include "AppStack.h" #include "App.h" +struct SprocketConfig { + int serialBaudRate; +}; class Sprocket { private: @@ -16,9 +19,10 @@ class Sprocket { Scheduler scheduler; public: Sprocket(); - Sprocket* use(AppStack* stack); - Sprocket* addTask(Task& tsk); - Sprocket* registerApp(App& app); + Sprocket* init(SprocketConfig); + Sprocket* use(AppStack*); + Sprocket* addTask(Task&); + Sprocket* registerApp(App&); void loop(); }; diff --git a/src/WiFiNet.h b/src/WiFiNet.h index 90fbcbc..f2127ee 100644 --- a/src/WiFiNet.h +++ b/src/WiFiNet.h @@ -7,33 +7,37 @@ #else #include #endif - #include #include #include #include //https://github.com/tzapu/WiFiManager -class WiFiNet { +#include "Network.h" + +class WiFiNet : public Network { private: AsyncWiFiManager* wifiManager; AsyncWebServer* server; DNSServer* dns; const char* hostName = "foo"; public: - WiFiNet(){ - //server = new AsyncWebServer(80); + WiFiNet() : Network() { + server = new AsyncWebServer(80); dns = new DNSServer(); } WiFiNet* use(WebStack* stack) { server = stack->server; return this; } - void connect(/* char const *apName, char const *apPassword */){ + WiFiNet* connect(){ + server = new AsyncWebServer(80); + dns = new DNSServer(); wifiManager = new AsyncWiFiManager(server, dns); wifiManager->autoConnect(/* apName, apPassword */); Serial.println("Hostname: " + String(hostName)); WiFi.hostname(hostName); startMDNS(); + return this; } void startMDNS(){ if (!MDNS.begin(hostName)) { diff --git a/src/examples/ExampleApp.h b/src/examples/basic/ExampleApp.h similarity index 100% rename from src/examples/ExampleApp.h rename to src/examples/basic/ExampleApp.h diff --git a/src/examples/basic_sprocket.cpp b/src/examples/basic/basic_example.cpp similarity index 82% rename from src/examples/basic_sprocket.cpp rename to src/examples/basic/basic_example.cpp index 2164537..651f727 100644 --- a/src/examples/basic_sprocket.cpp +++ b/src/examples/basic/basic_example.cpp @@ -1,4 +1,3 @@ - #define _TASK_SLEEP_ON_IDLE_RUN #define _TASK_STD_FUNCTION @@ -12,23 +11,26 @@ #define SERIAL_BAUD_RATE 115200 #define STARTUP_DELAY 3000 +SprocketConfig config = { SERIAL_BAUD_RATE }; + Sprocket sprocket; AppStack stack; WiFiNet net; - ExampleApp app; void setup() { - Serial.begin(SERIAL_BAUD_RATE); - SPIFFS.begin(); + delay(STARTUP_DELAY); Serial.println("setup"); - //net.use(&stack)->connect(); - + //sprocket.use(&stack); + sprocket.init(config); sprocket.registerApp(app); + + net.connect(); + //stack.begin(); diff --git a/src/examples/mesh/mesh_example.cpp b/src/examples/mesh/mesh_example.cpp new file mode 100644 index 0000000..3d9c648 --- /dev/null +++ b/src/examples/mesh/mesh_example.cpp @@ -0,0 +1,66 @@ +//************************************************************ +// this is a simple example that uses the painlessMesh library +// +// 1. sends a silly message to every node on the mesh at a random time between 1 and 5 seconds +// 2. prints anything it receives to Serial.print +// +// +//************************************************************ +#include "painlessMesh.h" + +#define MESH_PREFIX "whateverYouLike" +#define MESH_PASSWORD "somethingSneaky" +#define MESH_PORT 5555 + +Scheduler userScheduler; // to control your personal task +painlessMesh mesh; + +// User stub +void sendMessage() ; // Prototype so PlatformIO doesn't complain + +Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage ); + +void sendMessage() { + String msg = "Hello from node "; + msg += mesh.getNodeId(); + mesh.sendBroadcast( msg ); + taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 )); +} + +// Needed for painless library +void receivedCallback( uint32_t from, String &msg ) { + Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str()); +} + +void newConnectionCallback(uint32_t nodeId) { + Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); +} + +void changedConnectionCallback() { + Serial.printf("Changed connections %s\n",mesh.subConnectionJson().c_str()); +} + +void nodeTimeAdjustedCallback(int32_t offset) { + Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset); +} + +void setup() { + Serial.begin(115200); + +//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on + mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages + + mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT ); + mesh.onReceive(&receivedCallback); + mesh.onNewConnection(&newConnectionCallback); + mesh.onChangedConnections(&changedConnectionCallback); + mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback); + + userScheduler.addTask( taskSendMessage ); + taskSendMessage.enable(); +} + +void loop() { + userScheduler.execute(); // it will run mesh scheduler as well + mesh.update(); +}