mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-16 05:24:30 +01:00
Resolve "OTA"
This commit is contained in:
@@ -15,15 +15,18 @@ Network* MeshNet::init(){
|
||||
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
|
||||
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
|
||||
|
||||
connectStation();
|
||||
|
||||
return this;
|
||||
}
|
||||
Network* MeshNet::connectStation() {
|
||||
if(config.stationMode){
|
||||
Serial.println("connect station");
|
||||
mesh.stationManual(config.stationSSID, config.stationPassword);
|
||||
mesh.setHostname(config.hostname);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
void MeshNet::sendTo(uint32_t target, String msg){
|
||||
mesh.sendSingle(target, msg);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
#ifndef __MESHNET_H__
|
||||
#define __MESHNET_H__
|
||||
|
||||
#ifdef ESP32
|
||||
#include <WiFi.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#endif // ESP32
|
||||
|
||||
#include <painlessMesh.h>
|
||||
#include <WiFiClient.h>
|
||||
#include "Network.h"
|
||||
@@ -8,6 +14,7 @@
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
// FIXME non-mesh config should have it's own struct
|
||||
struct MeshConfig {
|
||||
int stationMode;
|
||||
int channel;
|
||||
@@ -27,14 +34,18 @@ class MeshNet : public Network {
|
||||
|
||||
MeshNet(MeshConfig cfg);
|
||||
Network* init();
|
||||
|
||||
void update(); // only needed when no scheduler was passed to mesh.init
|
||||
Network* connectStation();
|
||||
|
||||
void update();
|
||||
void newConnectionCallback(uint32_t nodeId);
|
||||
void changedConnectionCallback();
|
||||
void nodeTimeAdjustedCallback(int32_t offset);
|
||||
void broadcast(String msg);
|
||||
void sendTo(uint32_t target, String msg);
|
||||
void onReceive(std::function<void(uint32_t from, String &msg)>);
|
||||
int isConnected(){
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -14,6 +14,8 @@ class Network {
|
||||
virtual Network* init() { return this; };
|
||||
virtual Network* init(Scheduler* s) { scheduler = s; return init(); };
|
||||
virtual Network* connect() { return this; };
|
||||
virtual Network* connectStation() { return this; };
|
||||
virtual int isConnected(){ return 0; };
|
||||
virtual void update() {};
|
||||
virtual void broadcast(String msg){};
|
||||
virtual void sendTo(uint32_t target, String msg) {};
|
||||
|
||||
74
src/OtaTcpPlugin.cpp
Normal file
74
src/OtaTcpPlugin.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef __OTA_CLASSIC_H__
|
||||
#define __OTA_CLASSIC_H__
|
||||
|
||||
#include "TaskSchedulerDeclarations.h"
|
||||
#include "ArduinoOTA.h"
|
||||
#include "MeshNet.h"
|
||||
#include "Plugin.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
struct OtaConfig {
|
||||
int port;
|
||||
const char* password;
|
||||
};
|
||||
|
||||
// TODO rename to OtaTcpPlugin
|
||||
class OtaTcpPlugin : public Plugin {
|
||||
private:
|
||||
OtaConfig config;
|
||||
Task otaTask;
|
||||
public:
|
||||
OtaTcpPlugin(OtaConfig cfg){
|
||||
config = cfg;
|
||||
}
|
||||
void enable(Scheduler* userScheduler, Network* network){
|
||||
scheduler = userScheduler;
|
||||
|
||||
// connect to network
|
||||
if(!network->isConnected()){
|
||||
// TODO when config is refactored, cast is not necessary anymore
|
||||
static_cast<MeshNet*>(network)->config.stationMode = 1;
|
||||
network->connectStation();
|
||||
// TODO set service message to mesh to announce station IP
|
||||
}
|
||||
// setup task
|
||||
otaTask.set(TASK_MILLISECOND * 100, TASK_FOREVER, [](){
|
||||
ArduinoOTA.handle();
|
||||
});
|
||||
scheduler->addTask(otaTask);
|
||||
|
||||
// configure OTA
|
||||
ArduinoOTA.setPort(config.port);
|
||||
//ArduinoOTA.setHostname(HOSTNAME);
|
||||
if(strlen(config.password) > 0){
|
||||
ArduinoOTA.setPassword(config.password);
|
||||
}
|
||||
// setup callbacks
|
||||
ArduinoOTA.onStart([]() {
|
||||
Serial.println("OTA: Start");
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Serial.println("OTA: End");
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
Serial.printf("OTA: Progress: %u%%\r", (progress / (total / 100)));
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
Serial.printf("OTA: Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) Serial.println("OTA: Auth Failed");
|
||||
else if (error == OTA_BEGIN_ERROR) Serial.println("OTA: Begin Failed");
|
||||
else if (error == OTA_CONNECT_ERROR) Serial.println("OTA: Connect Failed");
|
||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
|
||||
else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
otaTask.enable();
|
||||
}
|
||||
void disable(){
|
||||
otaTask.disable();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
15
src/Plugin.h
Normal file
15
src/Plugin.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef __SPROCKET_PLUGIN__
|
||||
#define __SPROCKET_PLUGIN__
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include <Network.h>
|
||||
|
||||
class Plugin {
|
||||
protected:
|
||||
Scheduler* scheduler;
|
||||
public:
|
||||
virtual void enable(Scheduler*, Network*);
|
||||
virtual void disable();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,7 +3,10 @@
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include <Arduino.h>
|
||||
#include <FS.h>
|
||||
#include "FS.h"
|
||||
#ifdef ESP32
|
||||
#include "SPIFFS.h"
|
||||
#endif
|
||||
#include "Network.h"
|
||||
|
||||
using namespace std;
|
||||
@@ -31,6 +34,7 @@ class Sprocket {
|
||||
virtual Sprocket* activate();
|
||||
virtual Sprocket* activate(Scheduler*) { return this; }
|
||||
virtual Sprocket* activate(Scheduler*, Network*) { return this; }
|
||||
virtual Sprocket* enable() { return this; };
|
||||
// TODO bind onMessage to network->onReceive
|
||||
//virtual void onMessage(uint32_t from, String &msg) {};
|
||||
};
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#define STATION_SSID "Th1ngs4P"
|
||||
#define STATION_PASSWORD "th3r31sn0sp00n"
|
||||
#define HOSTNAME "mesh-node"
|
||||
#define MESH_DEBUG_TYPES ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE
|
||||
#define MESH_DEBUG_TYPES ERROR | CONNECTION | COMMUNICATION
|
||||
//ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,7 @@
|
||||
#define STATION_MODE 1
|
||||
#define WIFI_CHANNEL 11
|
||||
#define MESH_PORT 5555
|
||||
#define MESH_PREFIX "WirelosContraption"
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define STATION_SSID "Th1ngs4P"
|
||||
#define STATION_PASSWORD "th3r31sn0sp00n"
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
// Bridge config
|
||||
#define MQTT_CLIENT_NAME HOSTNAME
|
||||
#define MQTT_BROKER "iot.eclipse.org"
|
||||
#define MQTT_BROKER "citadel.lan"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_TOPIC_ROOT "mesh"
|
||||
|
||||
|
||||
40
src/examples/ota/SprocketOTA.cpp
Normal file
40
src/examples/ota/SprocketOTA.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef __MESH_APP__
|
||||
#define __MESH_APP__
|
||||
|
||||
#define DEBUG_ESP_OTA
|
||||
#include <painlessMesh.h>
|
||||
#include <Sprocket.h>
|
||||
#include <MeshNet.h>
|
||||
#include <OtaTcpPlugin.cpp>
|
||||
#include "config.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
class SprocketOTA : public Sprocket {
|
||||
public:
|
||||
MeshNet* net;
|
||||
OtaTcpPlugin* ota;
|
||||
|
||||
SprocketOTA(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) {
|
||||
ota = new OtaTcpPlugin(otaCfg);
|
||||
}
|
||||
|
||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
||||
net = static_cast<MeshNet*>(network);
|
||||
net->onReceive(bind(&SprocketOTA::dispatch,this, _1, _2));
|
||||
ota->enable(scheduler, network);
|
||||
return this;
|
||||
} using Sprocket::activate;
|
||||
|
||||
void dispatch( uint32_t from, String &msg ) {
|
||||
Serial.printf("Sprocket: received from %u msg=%s\n", from, msg.c_str());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
net->update();
|
||||
scheduler.execute();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
27
src/examples/ota/config.h
Normal file
27
src/examples/ota/config.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef __OTA_NODE__CONFIG__
|
||||
#define __OTA_NODE__CONFIG__
|
||||
|
||||
// Scheduler config
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
|
||||
// Chip config
|
||||
#define SERIAL_BAUD_RATE 115200
|
||||
#define STARTUP_DELAY 3000
|
||||
|
||||
// Mesh config
|
||||
#define STATION_MODE 1 // must be 1 => connected to a normal network to receive update
|
||||
#define WIFI_CHANNEL 11
|
||||
#define MESH_PORT 5555
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define STATION_SSID "Th1ngs4P"
|
||||
#define STATION_PASSWORD "th3r31sn0sp00n"
|
||||
#define HOSTNAME "ota-node"
|
||||
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
|
||||
|
||||
// OTA config
|
||||
#define OTA_PORT 8266
|
||||
#define OTA_PASSWORD "f4ncy"
|
||||
|
||||
#endif
|
||||
25
src/examples/ota/main.cpp
Normal file
25
src/examples/ota/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <ArduinoOTA.h>
|
||||
#include "config.h"
|
||||
#include "MeshNet.h"
|
||||
#include "SprocketOTA.cpp"
|
||||
|
||||
MeshNet net({
|
||||
STATION_MODE, WIFI_CHANNEL,
|
||||
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
||||
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
||||
MESH_DEBUG_TYPES
|
||||
});
|
||||
|
||||
SprocketOTA sprocket(
|
||||
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
|
||||
{OTA_PORT, OTA_PASSWORD}
|
||||
);
|
||||
|
||||
void setup() {
|
||||
sprocket.join(net);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sprocket.loop();
|
||||
yield();
|
||||
}
|
||||
Reference in New Issue
Block a user