Skip to content

Commit

Permalink
Fix MQTT Json subscribe path
Browse files Browse the repository at this point in the history
Fix MQTT Json Control Load
  • Loading branch information
softwarecrash committed Sep 25, 2023
1 parent aa692f6 commit 2e57e96
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ framework = arduino
monitor_speed = 115200
monitor_filters = esp8266_exception_decoder, default, time, printable, colorize

custom_prog_version = 2.0.1
custom_prog_version = 2.0.2

build_flags =
-DVERSION=${this.custom_prog_version}
Expand Down
133 changes: 68 additions & 65 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void setup()

bool res = wm.autoConnect("EPEver2MQTT-AP");

// wm.setConnectTimeout(30); // how long to try to connect for before continuing
// wm.setConnectTimeout(30); // how long to try to connect for before continuing
wm.setConfigPortalTimeout(120); // auto close configportal after n seconds

// save settings if wifi setup is fire up
Expand Down Expand Up @@ -348,7 +348,9 @@ void setup()
}
request->send(200, "text/plain", resultMsg.c_str()); });

server.on("/update", HTTP_POST, [](AsyncWebServerRequest *request){
server.on(
"/update", HTTP_POST, [](AsyncWebServerRequest *request)
{
//https://gist.github.com/JMishou/60cb762047b735685e8a09cd2eb42a60
// the request handler is triggered after the upload has finished...
// create the response, add header, and send response
Expand All @@ -357,36 +359,44 @@ void setup()
response->addHeader("Access-Control-Allow-Origin", "*");
//restartNow = true; // Tell the main loop to restart the ESP
//RestartTimer = millis(); // Tell the main loop to restart the ESP
request->send(response);
},[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
//Upload handler chunks in data

if(!index){ // if index == 0 then this is the first frame of data
Serial.printf("UploadStart: %s\n", filename.c_str());
Serial.setDebugOutput(true);

// calculate sketch space required for the update
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if(!Update.begin(maxSketchSpace)){//start with max available size
Update.printError(Serial);
}
Update.runAsync(true); // tell the updaterClass to run in async mode
}
request->send(response); },
[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
// Upload handler chunks in data

if (!index)
{ // if index == 0 then this is the first frame of data
Serial.printf("UploadStart: %s\n", filename.c_str());
Serial.setDebugOutput(true);

// calculate sketch space required for the update
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace))
{ // start with max available size
Update.printError(Serial);
}
Update.runAsync(true); // tell the updaterClass to run in async mode
}

//Write chunked data to the free sketch space
if(Update.write(data, len) != len){
Update.printError(Serial);
}

if(final){ // if the final flag is set then this is the last frame of data
if(Update.end(true)){ //true to set the size to the current progress
Serial.printf("Update Success: %u B\nRebooting...\n", index+len);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
});
// Write chunked data to the free sketch space
if (Update.write(data, len) != len)
{
Update.printError(Serial);
}

if (final)
{ // if the final flag is set then this is the last frame of data
if (Update.end(true))
{ // true to set the size to the current progress
Serial.printf("Update Success: %u B\nRebooting...\n", index + len);
}
else
{
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
});

server.onNotFound([](AsyncWebServerRequest *request)
{ request->send(418, "text/plain", "418 I'm a teapot"); });
Expand All @@ -407,25 +417,25 @@ void setup()

void loop()
{
if (Update.isRunning())
if (Update.isRunning())
{
workerCanRun = false;
}
// Make sure wifi is in the right mode
if (WiFi.status() == WL_CONNECTED && workerCanRun)
if (WiFi.status() == WL_CONNECTED && workerCanRun)
{ // No use going to next step unless WIFI is up and running.
ws.cleanupClients(); // clean unused client connections
MDNS.update();
mqttclient.loop(); // Check if we have something to read from MQTT
epWorker(); // the loop worker
epWorker(); // the loop worker
}

if (restartNow && millis() >= (RestartTimer + 500))
{
DEBUG_WEBLN("Restart");
ESP.reset();
}
if (workerCanRun)
if (workerCanRun)
{
notificationLED(); // notification LED routine
}
Expand All @@ -440,13 +450,15 @@ bool epWorker()
}
else
{
if(errorcode != 0 && millis() > (notifyTimer +1000)){
if (errorcode != 0 && millis() > (notifyTimer + 1000))
{
notifyClients(); // anyway, call the client something
notifyTimer = millis();
} else if(errorcode == 0) {
}
else if (errorcode == 0)
{
notifyClients(); // anyway, call the client something
}

}

// mqtt part, when time is come, fire up the mqtt function to send all data to the broker
Expand Down Expand Up @@ -711,7 +723,7 @@ bool getJsonData(int invNum)
liveJson["DEVICE_QUANTITY"] = _settings.data.deviceQuantity;
liveJson["DEVICE_FREE_HEAP"] = ESP.getFreeHeap();
liveJson["DEVICE_FREE_JSON"] = (JSON_BUFFER - liveJson.memoryUsage());
liveJson["ESP_VCC"] = (ESP.getVcc() / 1000.0)+0.3;
liveJson["ESP_VCC"] = (ESP.getVcc() / 1000.0) + 0.3;
liveJson["Wifi_RSSI"] = WiFi.RSSI();
liveJson["sw_version"] = SOFTWARE_VERSION;
return true;
Expand All @@ -732,13 +744,15 @@ bool connectMQTT()
mqttclient.subscribe(_settings.data.mqttTriggerPath);
}

for (size_t i = 1; i < ((size_t)_settings.data.deviceQuantity + 1); i++)
{
if (!_settings.data.mqttJson) // classic mqtt DP
if (!_settings.data.mqttJson) // classic mqtt DP

for (size_t i = 1; i < ((size_t)_settings.data.deviceQuantity + 1); i++)
{
mqttclient.subscribe((topic + "/" + devicePrefix + i + "/DeviceControl/LOAD_STATE").c_str());
else // subscribe json
mqttclient.subscribe((topic + "/" + devicePrefix + i + "/DATA").c_str());
}
}
else // subscribe json
mqttclient.subscribe((topic + "/DATA").c_str());

return true;
}
else
Expand Down Expand Up @@ -792,7 +806,7 @@ bool sendtoMQTT()

void callback(char *top, byte *payload, unsigned int length)
{
// updateProgress = true; // stop servicing data
// updateProgress = true; // stop servicing data
if (!_settings.data.mqttJson)
{
String messageTemp;
Expand All @@ -818,34 +832,23 @@ void callback(char *top, byte *payload, unsigned int length)
StaticJsonDocument<1024> mqttJsonAnswer;
deserializeJson(mqttJsonAnswer, (const byte *)payload, length);

if ((size_t)_settings.data.deviceQuantity > 1)
for (size_t k = 1; k < ((size_t)_settings.data.deviceQuantity + 1); k++)
{
for (size_t k = 1; k < ((size_t)_settings.data.deviceQuantity + 1); k++)
if (mqttJsonAnswer.containsKey(devicePrefix + k))
{
if (mqttJsonAnswer["DEVICE_NAME_" + k] == (_settings.deviceNameStr + "_" + k))
{
epnode.setSlaveId(k);
if (mqttJsonAnswer["LOAD_STATE"] == true)
epnode.writeSingleCoil(0x0002, 1);
if (mqttJsonAnswer["LOAD_STATE"] == false)
epnode.writeSingleCoil(0x0002, 0);
}
epnode.setSlaveId(k);
if (mqttJsonAnswer[devicePrefix + k]["LiveData"]["LOAD_STATE"] == true)
epnode.writeSingleCoil(0x0002, 1);
if (mqttJsonAnswer[devicePrefix + k]["LiveData"]["LOAD_STATE"] == false)
epnode.writeSingleCoil(0x0002, 0);
}
}
else
{
epnode.setSlaveId(1);
if (mqttJsonAnswer["LOAD_STATE"] == true)
epnode.writeSingleCoil(0x0002, 1);
if (mqttJsonAnswer["LOAD_STATE"] == false)
epnode.writeSingleCoil(0x0002, 0);
}
}

if (strlen(_settings.data.mqttTriggerPath) > 0 && strcmp(top, _settings.data.mqttTriggerPath) == 0)
{
DEBUG_WEBLN("MQTT Data Trigger Firered Up");
mqtttimer = 0;
}
// updateProgress = false; // start data servicing again
// updateProgress = false; // start data servicing again
}

0 comments on commit 2e57e96

Please sign in to comment.