diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..3e82ae6 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,35 @@ +platformio: + before_script: + - apt-get update -qq && apt-get install -y -qq python-pip + - pip install platformio + - platformio lib --global install ArduinoJson TaskScheduler PubSubClient ESPAsyncTCP AsyncTCP + script: + - platformio ci --lib="." --board=nodemcuv2 examples/basic/basic.ino -O "build_flags = -Werror" + - platformio ci --lib="." --board=nodemcuv2 examples/mesh/mesh.ino -O "build_flags = -Wall -Wextra -Wno-unused-parameter -Werror" + +arduino: + before_script: + - wget https://downloads.arduino.cc/arduino-1.8.5-linux64.tar.xz + - tar xvfJ arduino-1.8.5-linux64.tar.xz + - cd arduino-1.8.5 + - ./arduino --pref "boardsmanager.additional.urls=https://adafruit.github.io/arduino-board-index/package_adafruit_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json" --save-prefs + - ./arduino --install-boards esp8266:esp8266 + - ./arduino --install-library TaskScheduler + - ./arduino --install-library ArduinoJson + - git clone https://github.com/me-no-dev/ESPAsyncTCP; cp -r ESPAsyncTCP/src ~/Arduino/libraries/ESPAsyncTCP + - git clone https://github.com/me-no-dev/AsyncTCP; cp -r AsyncTCP/src ~/Arduino/libraries/AsyncTCP + - cp -r ../src ~/Arduino/libraries/painlessMesh + script: + - ./arduino -v --board esp8266:esp8266:d1_mini:CpuFrequency=80,FlashSize=4M1M --verify ../examples/startHere/basic.ino + - ./arduino -v --board esp8266:esp8266:d1_mini:CpuFrequency=80,FlashSize=4M1M --verify ../examples/startHere/startHere.ino + +pages: + script: + - apt-get update && apt-get install -y doxygen + - doxygen doxygen/Doxyfile + - mv doxygen/documentation/html/ public/ + artifacts: + paths: + - public + only: + - master diff --git a/platformio.ini b/platformio.ini index abd3b5a..59f00d7 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,12 +9,11 @@ ; http://docs.platformio.org/page/projectconf.html [platformio] -env_default = mesh +env_default = basic [common] framework = arduino lib_deps = - ESPAsyncWifiManager ESP Async WebServer ESPAsyncTCP TaskScheduler @@ -34,6 +33,17 @@ lib_deps = ${common.lib_deps} ;upload_port = /dev/ttyUSB0 ;upload_port = 192.168.1.168 +[env:basic] +src_filter = +<*> + - +platform = espressif8266 +board = esp12e +upload_speed = 921600 +monitor_baud = 115200 +framework = ${common.framework} +lib_deps = ${common.lib_deps} + ESPAsyncWifiManager + + [env:mesh] src_filter = +<*> + - platform = espressif8266 diff --git a/src/App.h b/src/App.h index ea9bc28..be4753a 100644 --- a/src/App.h +++ b/src/App.h @@ -2,27 +2,15 @@ #define _APP_H_ #include -//#include "Sprocket.h" +#include "Sprocket.h" #include "AppStack.h" - -/* template -class App { - private: - T* stack; - public: - App(); - App(T* stack); -}; - */ +#include "Network.h" class App { - protected: - //Sprocket* sprocket; public: - /* App(Sprocket* sprkt){ - sprocket = sprkt; - } */ - virtual void activate(Scheduler* scheduler); + virtual void join(Network& network) {}; + virtual void activate(Scheduler* scheduler, Network* network) {}; + virtual void activate(Scheduler* scheduler) {}; }; #endif \ No newline at end of file diff --git a/src/MeshNet.h b/src/MeshNet.h index 21ace1b..cde167c 100644 --- a/src/MeshNet.h +++ b/src/MeshNet.h @@ -1,10 +1,56 @@ #ifndef __MESHNET_H__ #define __MESHNET_H__ +#include + #include "Network.h" -class MeshNet : public Network { +using namespace std; +using namespace std::placeholders; + +#define MESH_PREFIX "whateverYouLike" +#define MESH_PASSWORD "somethingSneaky" +#define MESH_PORT 5555 + + + +class MeshNet : public Network { + public: + painlessMesh mesh; + Network* init(){ + Serial.println("init mesh"); + mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION ); // set before init() so that you can see startup messages + mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, 11 ); + mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2)); + mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1)); + mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this)); + mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1)); + + return this; + } + Network* connect(){ + return this; + } + void update(){ + Serial.println("update mesh"); + mesh.update(); + } + 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); + } }; #endif \ No newline at end of file diff --git a/src/Network.h b/src/Network.h index 3e8a43d..6504d1c 100644 --- a/src/Network.h +++ b/src/Network.h @@ -1,10 +1,20 @@ #ifndef __NETWORK_H__ #define __NETWORK_H__ +#include + class Network { + protected: + Scheduler* scheduler; public: Network(){} - virtual Network* connect(); + virtual Network* init() { return this; }; + virtual Network* connect() { return this; }; + virtual void update() {}; + Network* setScheduler(Scheduler* s) { + scheduler = s; + return this; + } }; #endif \ No newline at end of file diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp index f64e991..5d80298 100644 --- a/src/Sprocket.cpp +++ b/src/Sprocket.cpp @@ -9,7 +9,15 @@ Sprocket* Sprocket::init(SprocketConfig cfg){ SPIFFS.begin(); return this; } - +Sprocket* Sprocket::join(Network& net){ + //network = net; + Serial.println("join network"); + net.setScheduler(&scheduler); + net.init(); + net.connect(); + Serial.println("connected"); + return this; +} Sprocket* Sprocket::use(AppStack* stk){ stack = stk; return this; @@ -21,12 +29,15 @@ Sprocket* Sprocket::addTask(Task& tsk){ return this; } -Sprocket* Sprocket::registerApp(App& app){ +Sprocket* Sprocket::app(App& app){ + //app.join(&network); + //app.activate(&scheduler, network); app.activate(&scheduler); return this; } void Sprocket::loop(){ + //network->update(); scheduler.execute(); //stack->loop(); } \ No newline at end of file diff --git a/src/Sprocket.h b/src/Sprocket.h index 4e2e34e..51cc223 100644 --- a/src/Sprocket.h +++ b/src/Sprocket.h @@ -5,9 +5,9 @@ #include #include -#include "WiFiNet.h" #include "AppStack.h" #include "App.h" +#include "Network.h" struct SprocketConfig { int serialBaudRate; @@ -17,12 +17,14 @@ class Sprocket { private: AppStack* stack; Scheduler scheduler; + Network* network; public: Sprocket(); Sprocket* init(SprocketConfig); + Sprocket* join(Network&); Sprocket* use(AppStack*); Sprocket* addTask(Task&); - Sprocket* registerApp(App&); + Sprocket* app(App&); void loop(); }; diff --git a/src/WiFiNet.h b/src/WiFiNet.h index f2127ee..79a22b2 100644 --- a/src/WiFiNet.h +++ b/src/WiFiNet.h @@ -29,7 +29,8 @@ class WiFiNet : public Network { server = stack->server; return this; } - WiFiNet* connect(){ + WiFiNet* init(){} + WiFiNet* connect(){ server = new AsyncWebServer(80); dns = new DNSServer(); wifiManager = new AsyncWiFiManager(server, dns); diff --git a/src/examples/basic/basic_example.cpp b/src/examples/basic/basic.cpp similarity index 79% rename from src/examples/basic/basic_example.cpp rename to src/examples/basic/basic.cpp index 651f727..cccd622 100644 --- a/src/examples/basic/basic_example.cpp +++ b/src/examples/basic/basic.cpp @@ -4,7 +4,7 @@ #include #include "Sprocket.h" #include "AppStack.h" -//#include "WiFiNet.h" +#include "WiFiNet.h" #include "ExampleApp.h" @@ -13,9 +13,9 @@ SprocketConfig config = { SERIAL_BAUD_RATE }; -Sprocket sprocket; -AppStack stack; WiFiNet net; +Sprocket sprocket; +//AppStack stack; ExampleApp app; @@ -23,20 +23,17 @@ void setup() { delay(STARTUP_DELAY); - Serial.println("setup"); - //sprocket.use(&stack); sprocket.init(config); - sprocket.registerApp(app); + sprocket.app(app); + sprocket.join(net); - net.connect(); - - + //net.connect(); //stack.begin(); - } void loop() { sprocket.loop(); + net.update(); yield(); } \ No newline at end of file diff --git a/src/examples/mesh/MeshApp.h b/src/examples/mesh/MeshApp.h new file mode 100644 index 0000000..8859881 --- /dev/null +++ b/src/examples/mesh/MeshApp.h @@ -0,0 +1,59 @@ +#ifndef __MESH_APP__ +#define __MESH_APP__ + +//#include "Sprocket.h" +#include + +#include "App.h" + +#define MESH_PREFIX "whateverYouLike" +#define MESH_PASSWORD "somethingSneaky" +#define MESH_PORT 5555 + +class MeshApp : public App { + public: + Task someTask; + MeshNet* net; + MeshApp() /* App(sprkt) */ { + Serial.println("joo"); + } + + void activate(Scheduler* scheduler, MeshNet* network) { + Serial.println("activate"); + net = network; + Serial.println("join mesh"); + /* net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2)); + net->mesh.onNewConnection(bind(&MeshApp::newConnectionCallback, this, _1)); + net->mesh.onChangedConnections(bind(&MeshApp::changedConnectionCallback, this)); + net->mesh.onNodeTimeAdjusted(bind(&MeshApp::nodeTimeAdjustedCallback, this, _1)); */ + join(); + /* someTask.set(TASK_SECOND * 5, TASK_FOREVER, [this, network](){ + Serial.println("task triggered"); + String msg = "Hello from node "; + msg += network.mesh.getNodeId(); + network.mesh.sendBroadcast( msg ); + }); + scheduler->addTask(someTask); + someTask.enable(); */ + } + void join(){ + + } + 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",net->mesh.subConnectionJson().c_str()); + } + + void nodeTimeAdjustedCallback(int32_t offset) { + Serial.printf("Adjusted time %u. Offset = %d\n", net->mesh.getNodeTime(),offset); + } +}; + +#endif \ No newline at end of file diff --git a/src/examples/mesh/mesh.cpp b/src/examples/mesh/mesh.cpp new file mode 100644 index 0000000..2b01a2f --- /dev/null +++ b/src/examples/mesh/mesh.cpp @@ -0,0 +1,35 @@ +#define _TASK_SLEEP_ON_IDLE_RUN +#define _TASK_STD_FUNCTION + +#include "Network.h" +#include "MeshNet.h" +#include "Sprocket.h" +#include "AppStack.h" +#include "MeshApp.h" + +#define SERIAL_BAUD_RATE 115200 +#define STARTUP_DELAY 3000 + +SprocketConfig config = { SERIAL_BAUD_RATE }; + +Sprocket sprocket; +AppStack stack; +MeshNet net; +MeshApp app; + +void setup() { + + delay(STARTUP_DELAY); + + sprocket.init(config); + sprocket.join(&net); + //sprocket.app(app); + //sprocket.use(&stack); + //stack.begin(); + +} + +void loop() { + sprocket.loop(); + yield(); +} \ No newline at end of file diff --git a/src/examples/mesh/mesh_example.cpp b/src/examples/mesh/mesh_example_non_sprocket.cpp_ similarity index 98% rename from src/examples/mesh/mesh_example.cpp rename to src/examples/mesh/mesh_example_non_sprocket.cpp_ index 3d9c648..828f94a 100644 --- a/src/examples/mesh/mesh_example.cpp +++ b/src/examples/mesh/mesh_example_non_sprocket.cpp_ @@ -50,7 +50,7 @@ void setup() { //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.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 6 ); mesh.onReceive(&receivedCallback); mesh.onNewConnection(&newConnectionCallback); mesh.onChangedConnections(&changedConnectionCallback); @@ -63,4 +63,4 @@ void setup() { void loop() { userScheduler.execute(); // it will run mesh scheduler as well mesh.update(); -} +} \ No newline at end of file