mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-16 21:34:08 +01:00
get OTA enabling working
This commit is contained in:
@@ -55,6 +55,7 @@ lib_deps = ${common.lib_deps}
|
|||||||
painlessMesh
|
painlessMesh
|
||||||
ESP8266mDNS
|
ESP8266mDNS
|
||||||
ArduinoOTA
|
ArduinoOTA
|
||||||
|
upload_port = 192.168.1.247
|
||||||
|
|
||||||
[env:meshMqttBridge]
|
[env:meshMqttBridge]
|
||||||
src_filter = +<*> -<examples/> +<examples/meshMqttBridge/>
|
src_filter = +<*> -<examples/> +<examples/meshMqttBridge/>
|
||||||
|
|||||||
@@ -15,13 +15,14 @@ Network* MeshNet::init(){
|
|||||||
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
|
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
|
||||||
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
|
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
|
||||||
|
|
||||||
connectStation();
|
connectStation(config.stationMode);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
Network* MeshNet::connectStation() {
|
Network* MeshNet::connectStation(int doConnect) {
|
||||||
if(config.stationMode){
|
Serial.println("connect station?");
|
||||||
Serial.println("connect station");
|
if(doConnect){
|
||||||
|
Serial.println("connect station!");
|
||||||
mesh.stationManual(config.stationSSID, config.stationPassword);
|
mesh.stationManual(config.stationSSID, config.stationPassword);
|
||||||
mesh.setHostname(config.hostname);
|
mesh.setHostname(config.hostname);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class MeshNet : public Network {
|
|||||||
|
|
||||||
MeshNet(MeshConfig cfg);
|
MeshNet(MeshConfig cfg);
|
||||||
Network* init();
|
Network* init();
|
||||||
Network* connectStation();
|
Network* connectStation(int);
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
void newConnectionCallback(uint32_t nodeId);
|
void newConnectionCallback(uint32_t nodeId);
|
||||||
|
|||||||
@@ -8,24 +8,26 @@
|
|||||||
#define JSON_FROM "from"
|
#define JSON_FROM "from"
|
||||||
#define JSON_TO "target"
|
#define JSON_TO "target"
|
||||||
#define JSON_MSG "msg"
|
#define JSON_MSG "msg"
|
||||||
|
#define JSON_TYPE "type"
|
||||||
|
|
||||||
struct MeshMessage {
|
struct MeshMessage {
|
||||||
String domain;
|
String domain;
|
||||||
String to;
|
String to;
|
||||||
String from;
|
String from;
|
||||||
String msg;
|
String msg;
|
||||||
enum MeshMessageType { NONE, SYSTEM, OTA } type;
|
enum MeshMessageType { NONE, SYSTEM, APP, OTA } type;
|
||||||
int valid = 0;
|
int valid = 0;
|
||||||
// ------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------
|
||||||
void init() {
|
//void init() {
|
||||||
//from = reinterpret_cast<char*>(ESP.getChipId());
|
// from = reinterpret_cast<char*>(ESP.getChipId());
|
||||||
}
|
//}
|
||||||
int verifyJsonObject(JsonObject& json){
|
int verifyJsonObject(JsonObject& json){
|
||||||
return json.success()
|
return json.success();
|
||||||
//&& json.containsKey(JSON_DOMAIN)
|
//&& json.containsKey(JSON_DOMAIN)
|
||||||
//&& json.containsKey(JSON_TO)
|
//&& json.containsKey(JSON_TO)
|
||||||
//&& json.containsKey(JSON_FROM)
|
//&& json.containsKey(JSON_FROM)
|
||||||
&& json.containsKey(JSON_MSG);
|
//&& json.containsKey(JSON_TYPE)
|
||||||
|
// && json.containsKey(JSON_MSG); // msg is only tx'ed from mqtt
|
||||||
};
|
};
|
||||||
String toJsonString(){
|
String toJsonString(){
|
||||||
//StaticJsonBuffer<200> jsonBuffer;
|
//StaticJsonBuffer<200> jsonBuffer;
|
||||||
@@ -35,6 +37,7 @@ struct MeshMessage {
|
|||||||
root[JSON_TO] = to;
|
root[JSON_TO] = to;
|
||||||
root[JSON_FROM] = from;
|
root[JSON_FROM] = from;
|
||||||
root[JSON_MSG] = msg;
|
root[JSON_MSG] = msg;
|
||||||
|
root[JSON_TYPE] = type;
|
||||||
String jsonString;
|
String jsonString;
|
||||||
root.printTo(jsonString);
|
root.printTo(jsonString);
|
||||||
return jsonString;
|
return jsonString;
|
||||||
@@ -52,25 +55,26 @@ struct MeshMessage {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// Map a json object to this struct.
|
// Map a json object to this struct.
|
||||||
void fromJsonObject(JsonObject& json){
|
int fromJsonObject(JsonObject& json){
|
||||||
if(!verifyJsonObject(json)){
|
if(!verifyJsonObject(json)){
|
||||||
Serial.println("ERROR: cannot parse MeshMessage JSON object");
|
Serial.println("ERROR: cannot parse MeshMessage JSON object");
|
||||||
valid = 0;
|
valid = 0;
|
||||||
//return;
|
return valid;
|
||||||
}
|
}
|
||||||
domain = getAttrFromJson(json, JSON_DOMAIN);
|
domain = getAttrFromJson(json, JSON_DOMAIN);
|
||||||
to = getAttrFromJson(json, JSON_TO);
|
to = getAttrFromJson(json, JSON_TO);
|
||||||
from = getAttrFromJson(json, JSON_FROM);
|
from = getAttrFromJson(json, JSON_FROM);
|
||||||
msg = getAttrFromJson(json, JSON_MSG);
|
msg = getAttrFromJson(json, JSON_MSG);
|
||||||
type = (MeshMessageType) getIntAttrFromJson(json, "type");
|
type = (MeshMessageType) getIntAttrFromJson(json, JSON_TYPE);
|
||||||
valid = 1;
|
valid = 1;
|
||||||
|
return valid;
|
||||||
};
|
};
|
||||||
// Parse a json string and map parsed object
|
// Parse a json string and map parsed object
|
||||||
void fromJsonString(String& str){
|
int fromJsonString(String& str){
|
||||||
//StaticJsonBuffer<200> jsonBuffer;
|
//StaticJsonBuffer<200> jsonBuffer;
|
||||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||||
JsonObject& json = jsonBuffer.parseObject(str);
|
JsonObject& json = jsonBuffer.parseObject(str);
|
||||||
fromJsonObject(json);
|
return fromJsonObject(json);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ class MeshSprocket : public Sprocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void dispatchMessageToPlugins(MeshMessage msg){
|
void dispatchMessageToPlugins(MeshMessage msg){
|
||||||
for(Plugin* p : plugins){
|
if(msg.type != MeshMessage::NONE){ // baaaa
|
||||||
if(msg.type != MeshMessage::NONE){
|
for(Plugin* p : plugins){
|
||||||
Serial.println("dispatch to plugins");
|
Serial.println("dispatch to plugins");
|
||||||
p->onMessage(msg);
|
p->onMessage(msg);
|
||||||
}
|
}
|
||||||
@@ -55,10 +55,8 @@ class MeshSprocket : public Sprocket {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void dispatch( uint32_t from, String &msg ) {
|
void dispatch( uint32_t from, String &msg ) {
|
||||||
// TODO handle OTA before passing to onMessage
|
|
||||||
MeshMessage mMsg;
|
MeshMessage mMsg;
|
||||||
mMsg.fromJsonString(msg);
|
if(mMsg.fromJsonString(msg)){
|
||||||
if(mMsg.valid){
|
|
||||||
dispatchMessageToPlugins(mMsg);
|
dispatchMessageToPlugins(mMsg);
|
||||||
}
|
}
|
||||||
onMessage(from, msg);
|
onMessage(from, msg);
|
||||||
|
|||||||
@@ -32,9 +32,9 @@ class MeshApp : public MeshSprocket {
|
|||||||
msg.domain = "wirelos";
|
msg.domain = "wirelos";
|
||||||
msg.to = "broadcast";
|
msg.to = "broadcast";
|
||||||
msg.msg = "alive";
|
msg.msg = "alive";
|
||||||
|
msg.type = MeshMessage::APP;
|
||||||
String msgStr = msg.toJsonString();
|
String msgStr = msg.toJsonString();
|
||||||
//String msg = "{ \"domain\": \"wirelos\",\"from\": \"0\",\"target\": \"broadcast\", \"msg \": \"alive\" }";
|
network->mesh.sendBroadcast(msgStr/* , true */);
|
||||||
network->mesh.sendBroadcast(msgStr, true);
|
|
||||||
}
|
}
|
||||||
void onMessage( uint32_t from, String &msg ) {
|
void onMessage( uint32_t from, String &msg ) {
|
||||||
Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());
|
Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());
|
||||||
|
|||||||
7
src/examples/mesh/README.md
Normal file
7
src/examples/mesh/README.md
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Mesh Sprocket Example
|
||||||
|
|
||||||
|
## OTA
|
||||||
|
mosquitto_sub -h citadel.lan -p 1883 -v -t '#'
|
||||||
|
|
||||||
|
Enable OTA:
|
||||||
|
mosquitto_pub -h citadel.lan -p 1883 -t '/down/wirelos/gateway' -m '{"target":"broadcast", "domain": "wirelos", "msg": {"target":"broadcast", "type": 3, msg: "OTA"} }'
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
#define STARTUP_DELAY 3000
|
#define STARTUP_DELAY 3000
|
||||||
|
|
||||||
// Mesh config
|
// Mesh config
|
||||||
#define STATION_MODE 1
|
#define SPROCKET_MODE 0
|
||||||
#define WIFI_CHANNEL 11
|
#define WIFI_CHANNEL 11
|
||||||
#define MESH_PORT 5555
|
#define MESH_PORT 5555
|
||||||
#define MESH_PREFIX "whateverYouLike"
|
#define MESH_PREFIX "whateverYouLike"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include "MeshApp.cpp"
|
#include "MeshApp.cpp"
|
||||||
|
|
||||||
MeshNet net({
|
MeshNet net({
|
||||||
STATION_MODE, WIFI_CHANNEL,
|
SPROCKET_MODE, WIFI_CHANNEL,
|
||||||
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
||||||
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
||||||
MESH_DEBUG_TYPES
|
MESH_DEBUG_TYPES
|
||||||
|
|||||||
@@ -18,29 +18,33 @@ class OtaTcpPlugin : public Plugin {
|
|||||||
private:
|
private:
|
||||||
OtaConfig config;
|
OtaConfig config;
|
||||||
Task otaTask;
|
Task otaTask;
|
||||||
MeshNet* network;
|
MeshNet* net;
|
||||||
public:
|
public:
|
||||||
OtaTcpPlugin(OtaConfig cfg){
|
OtaTcpPlugin(OtaConfig cfg){
|
||||||
config = cfg;
|
config = cfg;
|
||||||
|
|
||||||
}
|
}
|
||||||
void connectUpdateNetwork(Network* network) {
|
void connectUpdateNetwork() {
|
||||||
// if(!network->isConnected()){
|
Serial.println("OTA connect to update-network");
|
||||||
// static_cast<MeshNet*>(network)->config.stationMode = 1;
|
net->connectStation(1);
|
||||||
// }
|
|
||||||
network->connectStation();
|
|
||||||
}
|
}
|
||||||
void enable() {
|
void enable() {
|
||||||
|
Serial.println("OTA enable");
|
||||||
ArduinoOTA.begin();
|
ArduinoOTA.begin();
|
||||||
otaTask.enable();
|
otaTask.enable();
|
||||||
}
|
}
|
||||||
void onMessage(MeshMessage msg) {
|
void onMessage(MeshMessage msg) {
|
||||||
//enable();
|
if(msg.type == MeshMessage::OTA){
|
||||||
Serial.println("OTA msg received");
|
Serial.println("OTA msg received");
|
||||||
|
connectUpdateNetwork();
|
||||||
|
enable();
|
||||||
|
//net->mesh.sendBroadcast("my ip:" + WiFi.localIP().toString(), true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void setup(Scheduler* userScheduler, Network* network){
|
void setup(Scheduler* userScheduler, Network* network){
|
||||||
// connect done in network class
|
// connect done in network class?
|
||||||
//connectUpdateNetwork(network);
|
//connectUpdateNetwork(network);
|
||||||
|
net = static_cast<MeshNet*>(network);
|
||||||
// setup task
|
// setup task
|
||||||
scheduler = userScheduler;
|
scheduler = userScheduler;
|
||||||
otaTask.set(TASK_MILLISECOND * 100, TASK_FOREVER, [](){
|
otaTask.set(TASK_MILLISECOND * 100, TASK_FOREVER, [](){
|
||||||
@@ -72,7 +76,6 @@ class OtaTcpPlugin : public Plugin {
|
|||||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
|
else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
|
||||||
else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
|
else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
|
||||||
});
|
});
|
||||||
enable();
|
|
||||||
}
|
}
|
||||||
void disable(){
|
void disable(){
|
||||||
otaTask.disable();
|
otaTask.disable();
|
||||||
|
|||||||
Reference in New Issue
Block a user