Skip to content

Commit

Permalink
Merge pull request #14 from Roos-AID/add-mq-commands
Browse files Browse the repository at this point in the history
Add mq commands
  • Loading branch information
Roos-AID authored Jul 31, 2023
2 parents e421d4b + 416f15d commit 357c5db
Show file tree
Hide file tree
Showing 30 changed files with 697 additions and 450 deletions.
Binary file modified .DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules/*
package-lock.json
*.log
*.bin
.vscode/*
.DS_Store
**/.DS_Store
Arduino/**
libraries/**
staging/**
arduino-cli.yaml
6 changes: 3 additions & 3 deletions .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"board": "esp8266:esp8266:nodemcuv2",
"configuration": "xtal=160,vt=flash,exception=disabled,stacksmash=disabled,ssl=all,mmu=3232,non32xfer=fast,eesz=4M2M,led=2,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=115200",
"board": "esp8266:esp8266:d1_mini_pro",
"configuration": "xtal=160,vt=flash,exception=disabled,stacksmash=disabled,ssl=all,mmu=3232,non32xfer=fast,eesz=16M14M,ip=lm2f,dbg=Serial,lvl=None____,wipe=none,baud=460800",
"sketch": "ModbusRTU-ESP8266-webui.ino",
"output": "../buildmodbus",
"port": "/dev/cu.usbserial-0001",
"port": "/dev/tty.usbserial-210",
"prebuild": "/Users/robroosm1/Documents/Arduino/autoinc-semver-master/semver-incr-build.sh ./version.h"
}
56 changes: 46 additions & 10 deletions Debug.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
***************************************************************************
** Program : Debug.h, part of ModbusRTU-webui
** Version 1.8.0
** Version 1.11.0
**
** Copyright (c) 2021 Rob Roos
** Copyright (c) 2023 Rob Roos
** based on Framework ESP8266 from Willem Aandewiel and modifications
** from Robert van Breemen
**
Expand Down Expand Up @@ -63,17 +63,53 @@

// needs #include <TelnetStream.h> // Version 0.0.1 - https://github.com/jandrassy/TelnetStream

char _bol[128];
//#include <sys/time.h>
// #include <time.h>
// extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);

void _debugBOL(const char *fn, int line)
{
char _bol[128];
// This commented out code is using mix of system time and acetime to print, but it will not work on microsecond level correctly
// // //calculate fractional seconds to millis fraction
// double fractional_seconds;
// int microseconds;
// struct timespec tp; //to enable clock_gettime()
// clock_gettime(CLOCK_REALTIME, &tp);
// fractional_seconds = (double) tp.tv_nsec;
// fractional_seconds /= 1e3;
// fractional_seconds = round(fractional_seconds);
// microseconds = (int) fractional_seconds;

/* snprintf(_bol, sizeof(_bol), "%02d:%02d:%02d.%06d (%7u|%6u) %-12.12s(%4d): ", \
hour(), minute(), second(), microseconds, \
ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(),\
fn, line);
*/

//Alternative based on localtime function
timeval now;
//struct tm *tod;
gettimeofday(&now, nullptr);
//tod = localtime(&now.tv_sec);

snprintf(_bol, sizeof(_bol), "[%02d:%02d:%02d][%7u|%6u] %-12.12s(%4d): ", \
hour(), minute(), second(), \
ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(),\
fn, line);
/*
snprintf(_bol, sizeof(_bol), "%02d:%02d:%02d.%06d (%7u|%6u) %-12.12s(%4d): ", \
tod->tm_hour, tod->tm_min, tod->tm_sec, (int)now.tv_usec, \
ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(),\
fn, line);
*/

DEBUG_PORT.print (_bol);
TelnetStream.print(_bol);
}
TimeZone myTz = timezoneManager.createForZoneName(CSTR(settingNTPtimezone));
ZonedDateTime myTime = ZonedDateTime::forUnixSeconds64(time(nullptr), myTz);

//DebugTf(PSTR("%02d:%02d:%02d %02d-%02d-%04d\r\n"), myTime.hour(), myTime.minute(), myTime.second(), myTime.day(), myTime.month(), myTime.year());

snprintf(_bol, sizeof(_bol), "%02d:%02d:%02d.%06d (%7u|%6u) %-12.12s(%4d): ", \
myTime.hour(), myTime.minute(), myTime.second(), (int)now.tv_usec, \
ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(),\
fn, line);

TelnetStream.print (_bol);
}
#endif // DEBUG_H
20 changes: 10 additions & 10 deletions FSexplorer.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
***************************************************************************
** Program : FSexplorer
** Version 1.8.1
** Version 1.11.0
**
**
** Copyright (c) 2021 Rob Roos
** Copyright (c) 2023 Rob Roos
** based on Framework ESP8266 from Willem Aandewiel and modifications
** from Robert van Breemen
**
Expand Down Expand Up @@ -90,7 +90,7 @@ void startWebserver(){
// Set up first message as the IP address
DebugTln("HTTP Server started\r");
sprintf(cMsg, "%03d.%03d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
DebugTf("Assigned IP=%s\r\n", cMsg);
DebugTf(PSTR("Assigned IP=%s\r\n"), cMsg);
}
//=====================================================================================
void setupFSexplorer(){
Expand All @@ -111,10 +111,10 @@ void setupFSexplorer(){

httpServer.onNotFound([]()
{
if (bDebugRestAPI) DebugTf("in 'onNotFound()'!! [%s] => \r\n", String(httpServer.uri()).c_str());
if (bDebugRestAPI) DebugTf(PSTR("in 'onNotFound()'!! [%s] => \r\n"), String(httpServer.uri()).c_str());
if (httpServer.uri().indexOf("/api/") == 0)
{
if (bDebugRestAPI) DebugTf("next: processAPI(%s)\r\n", String(httpServer.uri()).c_str());
if (bDebugRestAPI) DebugTf(PSTR("next: processAPI(%s)\r\n"), String(httpServer.uri()).c_str());
processAPI();
}
// else if (httpServer.uri() == "/")
Expand All @@ -124,7 +124,7 @@ void setupFSexplorer(){
// }
else
{
if (bDebugRestAPI) DebugTf("next: handleFile(%s)\r\n"
if (bDebugRestAPI) DebugTf(PSTR("next: handleFile(%s)\r\n")
, String(httpServer.urlDecode(httpServer.uri())).c_str());
if (!handleFile(httpServer.urlDecode(httpServer.uri())))
{
Expand Down Expand Up @@ -158,13 +158,13 @@ void apilistfiles() // Senden aller Daten an den Client
dirMap[fileNr].Size = dir.fileSize();
fileNr++;
}
//DebugTf("fileNr[%d], Max[%d]\r\n", fileNr, MAX_FILES_IN_LIST);
//DebugTf(PSTR("fileNr[%d], Max[%d]\r\n"), fileNr, MAX_FILES_IN_LIST);

// -- bubble sort dirMap op .Name--
for (int8_t y = 0; y < fileNr; y++) {
yield();
for (int8_t x = y + 1; x < fileNr; x++) {
//DebugTf("y[%d], x[%d] => seq[y][%s] / seq[x][%s] ", y, x, dirMap[y].Name, dirMap[x].Name);
//DebugTf(PSTR("y[%d], x[%d] => seq[y][%s] / seq[x][%s] "), y, x, dirMap[y].Name, dirMap[x].Name);
if (strcasecmp(dirMap[x].Name, dirMap[y].Name) <= 0)
{
//Debug(" !switch!");
Expand All @@ -190,7 +190,7 @@ void apilistfiles() // Senden aller Daten an den Client
String temp = "[";
for (int f=0; f < fileNr; f++)
{
DebugTf("[%3d] >> [%s]\r\n", f, dirMap[f].Name);
DebugTf(PSTR("[%3d] >> [%s]\r\n"), f, dirMap[f].Name);
temp += R"({"name":")" + String(dirMap[f].Name) + R"(","size":")" + formatBytes(dirMap[f].Size) + R"("},)";
}

Expand All @@ -209,7 +209,7 @@ bool handleFile(String&& path)
{
if (httpServer.hasArg("delete"))
{
DebugTf("Delete -> [%s]\n\r", httpServer.arg("delete").c_str());
DebugTf(PSTR("Delete -> [%s]\n\r"), httpServer.arg("delete").c_str());
LittleFS.remove(httpServer.arg("delete")); // Datei löschen
httpServer.sendContent(Header);
return true;
Expand Down
60 changes: 30 additions & 30 deletions MQTTstuff.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
***************************************************************************
** Program : MQTTstuff
** Version 1.8.0
** Version 1.11.0
**
** Copyright (c) 2021 Rob Roos
** Copyright (c) 2023 Rob Roos
** based on Framework ESP8266 from Willem Aandewiel and modifications
** from Robert van Breemen
** TERMS OF USE: MIT License. See bottom of file.
Expand Down Expand Up @@ -69,11 +69,11 @@ void handleMQTTcallback(char *topic, byte *payload, unsigned int length) {

if (bDebugMQTT) {
DebugT("Message arrived on topic ["); Debug(topic); Debug("] = [");
for (int i = 0; i < length; i++)
for (unsigned int i = 0; i < length; i++)
{
Debug((char)payload[i]);
}
Debug("] ("); Debug(length); Debug(")"); Debugln();
Debug("] ("); Debug(length); Debug(")"); Debugln(); DebugFlush();
}
char subscribeTopic[100];
// naming convention <mqtt top>/set/<node id>/<command>
Expand All @@ -82,8 +82,8 @@ if (bDebugMQTT) {
//what is the incoming message?
if (stricmp(topic, subscribeTopic) == 0)
{
//incoming command to be forwarded to MODBUS
sendModbus((char *)payload, length);
//incoming command to be analysed and processed
processMQcommand((char *)payload, length);
}
}

Expand All @@ -108,7 +108,7 @@ void handleMQTT()
sprintf(MQTTbrokerIPchar, "%d.%d.%d.%d", MQTTbrokerIP[0], MQTTbrokerIP[1], MQTTbrokerIP[2], MQTTbrokerIP[3]);
if (isValidIP(MQTTbrokerIP))
{
MQTTDebugTf("[%s] => setServer(%s, %d)\r\n", CSTR(settingMQTTbroker), MQTTbrokerIPchar, settingMQTTbrokerPort);
MQTTDebugTf(PSTR("[%s] => setServer(%s, %d)\r\n"), CSTR(settingMQTTbroker), MQTTbrokerIPchar, settingMQTTbrokerPort);
MQTTclient.disconnect();
MQTTclient.setServer(MQTTbrokerIPchar, settingMQTTbrokerPort);
MQTTclient.setCallback(handleMQTTcallback);
Expand All @@ -120,7 +120,7 @@ void handleMQTT()
}
else
{ // invalid IP, then goto error state
MQTTDebugTf("ERROR: [%s] => is not a valid URL\r\n", CSTR(settingMQTTbroker));
MQTTDebugTf(PSTR("ERROR: [%s] => is not a valid URL\r\n"), CSTR(settingMQTTbroker));
stateMQTT = MQTT_STATE_ERROR;
//DebugTln(F("Next State: MQTT_STATE_ERROR"));
}
Expand All @@ -129,9 +129,9 @@ void handleMQTT()

case MQTT_STATE_TRY_TO_CONNECT:
MQTTDebugTln(F("MQTT State: MQTT try to connect"));
MQTTDebugTf("MQTT server is [%s], IP[%s]\r\n", settingMQTTbroker.c_str(), MQTTbrokerIPchar);
MQTTDebugTf("MQTT clientid [%s] \r\n", MQTTclientId.c_str());
MQTTDebugTf("MQTT PubNamespace [%s], SubNamespace [%s]\r\n", MQTTPubNamespace.c_str(), MQTTSubNamespace.c_str());
MQTTDebugTf(PSTR("MQTT server is [%s], IP[%s]\r\n"), settingMQTTbroker.c_str(), MQTTbrokerIPchar);
MQTTDebugTf(PSTR("MQTT clientid [%s] \r\n"), MQTTclientId.c_str());
MQTTDebugTf(PSTR("MQTT PubNamespace [%s], SubNamespace [%s]\r\n"), MQTTPubNamespace.c_str(), MQTTSubNamespace.c_str());

MQTTDebugT(F("Attempting MQTT connection .. "));
reconnectAttempts++;
Expand Down Expand Up @@ -163,21 +163,21 @@ void handleMQTT()
char topic[100];
strcpy(topic, CSTR(MQTTSubNamespace));
strlcat(topic, "/#", sizeof(topic));
MQTTDebugTf("Subscribe to MQTT: TopicId [%s]\r\n", topic);
MQTTDebugTf(PSTR("Subscribe to MQTT: TopicId [%s]\r\n"), topic);
if (MQTTclient.subscribe(topic))
{
MQTTDebugTf("MQTT: Subscribed successfully to TopicId [%s]\r\n", topic);
MQTTDebugTf(PSTR("MQTT: Subscribed successfully to TopicId [%s]\r\n"), topic);
}
else
{
MQTTDebugTf("MQTT: Subscribe TopicId [%s] FAILED! \r\n", topic);
MQTTDebugTf(PSTR("MQTT: Subscribe TopicId [%s] FAILED! \r\n"), topic);
}
sendMQTTversioninfo();
}
else
{ // no connection, try again, do a non-blocking wait for 3 seconds.
MQTTDebugln(F(" .. \r"));
MQTTDebugTf("failed, retrycount=[%d], rc=[%d] .. try again in 3 seconds\r\n", reconnectAttempts, MQTTclient.state());
MQTTDebugTf(PSTR("failed, retrycount=[%d], rc=[%d] .. try again in 3 seconds\r\n"), reconnectAttempts, MQTTclient.state());
RESTART_TIMER(timerMQTTwaitforretry);
stateMQTT = MQTT_STATE_WAIT_CONNECTION_ATTEMPT; // if the re-connect did not work, then return to wait for reconnect
MQTTDebugTln(F("Next State: MQTT_STATE_WAIT_CONNECTION_ATTEMPT"));
Expand Down Expand Up @@ -282,12 +282,12 @@ void sendMQTTData(const char *topic, const char *json, const bool retain = false
{
if (!settingMQTTenable) return;
if (!MQTTclient.connected() || !isValidIP(MQTTbrokerIP)) return;
MQTTDebugTf("Sending data to MQTT server [%s]:[%d]\r\n", settingMQTTbroker.c_str(), settingMQTTbrokerPort);
MQTTDebugTf(PSTR("Sending data to MQTT server [%s]:[%d]\r\n"), settingMQTTbroker.c_str(), settingMQTTbrokerPort);
char full_topic[100];
snprintf(full_topic, sizeof(full_topic), "%s/", CSTR(MQTTPubNamespace));
strlcat(full_topic, topic, sizeof(full_topic));
MQTTDebugTf("Sending MQTT: TopicId [%s] Message [%s]\r\n", full_topic, json);
if (!MQTTclient.publish(full_topic, json, retain)) DebugTln("MQTT publish failed.");
MQTTDebugTf(PSTR("Sending MQTT: TopicId [%s] Message [%s]\r\n"), full_topic, json);
if (!MQTTclient.publish(full_topic, json, retain)) DebugTln(F("MQTT publish failed."));
// feedWatchDog(); //feed the dog
} // sendMQTTData()

Expand All @@ -306,8 +306,8 @@ void sendMQTT(const char *topic, const char *json, const size_t len)
{
if (!settingMQTTenable) return;
if (!MQTTclient.connected() || !isValidIP(MQTTbrokerIP)) return;
MQTTDebugTf("Sending data to MQTT server [%s]:[%d] ", settingMQTTbroker.c_str(), settingMQTTbrokerPort);
MQTTDebugTf("Sending MQTT: TopicId [%s] Message [%s]\r\n", topic, json);
MQTTDebugTf(PSTR("Sending data to MQTT server [%s]:[%d] "), settingMQTTbroker.c_str(), settingMQTTbrokerPort);
MQTTDebugTf(PSTR("Sending MQTT: TopicId [%s] Message [%s]\r\n"), topic, json);
if (MQTTclient.getBufferSize() < len)
MQTTclient.setBufferSize(len); //resize buffer when needed

Expand All @@ -327,17 +327,17 @@ Publish usefull firmware version information to MQTT broker.
*/
void sendMQTTversioninfo()
{
MQTTDebugln("sendMQTT versioninfo");
MQTTDebugln(F("sendMQTT versioninfo"));
sendMQTTData("ModbusRTU-webui/version", _VERSION);
MQTTDebugln("sendMQTT rebootcount");
MQTTDebugln(F("sendMQTT rebootcount"));

sendMQTTData("ModbusRTU-webui/reboot_count", CSTR(String(rebootCount)));
// sendMQTTData("ModbusRTU-webui/reboot_count", rebootCount.c_str);
// int16_t _value = rebootCount ;
// char _msg[15]{0};
// itoa(_value, _msg, 10);
// sendMQTTData("ModbusRTU-webui/reboot_count", _msg);
MQTTDebugln("end versioninfo ");
MQTTDebugln(F("end versioninfo "));
}
//===========================================================================================
void resetMQTTBufferSize()
Expand Down Expand Up @@ -380,11 +380,11 @@ void doAutoConfigure()
return;
}

MQTTDebugln("Start doAutoConfigureMB");
MQTTDebugln(F("Start doAutoConfigureMB"));

// String sTopic_template = "%homeassistant%/sensor/%node_id%/%label%/config";
// String sMsg_template = "{\"avty_t\":\"%mqtt_pub_topic%\",\"dev\":{\"identifiers\":\"%node_id%\",\"manufacturer\":\"Rob Roos\",\"model\":\"modbusRTUrdr\",\"name\":\"ModbusRTU reader(%hostname%)\",\"sw_version\":\"%version%\"},\"uniq_id\":\"%node_id%-%label%\",\"device_class\":\"%devclass%\",\"state_class\":\"%stateclass%\",\"name\":\"%hostname%_%friendlyname%\", \"stat_t\":\"%mqtt_pub_topic%/%label%\",\"unit_of_measurement\": \"%unit%\", \"value_template\": \"{{ value }}\" }" ;
String sTopic_template = "%homeassistant%/sensor/%label%/config";
String sTopic_template = "%homeassistant%/sensor/%node_id%/%label%/config";
String sMsg_template = "{\"avty_t\": \"%mqtt_pub_topic%\", \"dev\": {\"identifiers\": \"%node_id%\", \"manufacturer\": \"Rob Roos\", \"model\": \"modbusRTUrdr\", \"name\": \"ModbusRTU reader(%hostname%)\", \"sw_version\": \"%version%\"}, \"uniq_id\": \"%node_id%-%label%\", \"device_class\": \"%devclass%\", \"state_class\": \"%stateclass%\", \"name\": \"%hostname%_%friendlyname%\", \"stat_t\": \"%mqtt_pub_topic%/%label%\", \"unit_of_measurement\": \"%unit%\", \"value_template\": \"{{ value }}\" }";
String sTopic = "";
String sMsg = "";
Expand All @@ -393,7 +393,7 @@ void doAutoConfigure()
sTopic_template.replace("%homeassistant%", CSTR(settingMQTThaprefix));
// node
sTopic_template.replace("%node_id%", CSTR(settingMQTTuniqueid));
MQTTDebugTf("sTopic_template[%s]\r\n", CSTR(sTopic_template));
MQTTDebugTf(PSTR("sTopic_template[%s]\r\n"), CSTR(sTopic_template));

/// node
sMsg_template.replace("%node_id%", CSTR(settingMQTTuniqueid));
Expand All @@ -410,15 +410,15 @@ void doAutoConfigure()
// sub topics
sMsg_template.replace("%mqtt_sub_topic%", CSTR(MQTTSubNamespace));

MQTTDebugTf("sMsg_template[%s]\r\n", CSTR(sMsg_template));
MQTTDebugTf(PSTR("sMsg_template[%s]\r\n"), CSTR(sMsg_template));
/// ----------------------
// Now for all records in Modbusmap, convert sTopic and sMsg

for (int i = 1; i <= ModbusdataObject.NumberRegisters; i++)
{
if ((Modbusmap[i].oper == Modbus_READ || Modbusmap[i].oper == Modbus_RW) && Modbusmap[i].mqenable == 1 )
{
MQTTDebugTf("Record: %d, id %d, oper: %d, label:%s \r\n", i, Modbusmap[i].id, Modbusmap[i].oper, Modbusmap[i].label);
MQTTDebugTf(PSTR("Record: %d, id %d, oper: %d, label:%s \r\n"), i, Modbusmap[i].id, Modbusmap[i].oper, Modbusmap[i].label);

sTopic = sTopic_template;
sMsg = sMsg_template;
Expand All @@ -436,8 +436,8 @@ void doAutoConfigure()
// unit
sMsg.replace("%unit%", Modbusmap[i].unit);

MQTTDebugTf("sTopic[%s]\r\n", CSTR(sTopic));
MQTTDebugTf("sMsg[%s]==>\r\n", CSTR(sMsg));
MQTTDebugTf(PSTR("sTopic[%s]\r\n"), CSTR(sTopic));
MQTTDebugTf(PSTR("sMsg[%s]==>\r\n"), CSTR(sMsg));
DebugFlush();

//sendMQTT(CSTR(sTopic), CSTR(sMsg), (sTopic.length() + sMsg.length()+2));
Expand Down
Loading

0 comments on commit 357c5db

Please sign in to comment.