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