Skip to content

Commit

Permalink
Merge pull request #48 from ohAnd/29-ipport-to-mqtt-platform-httpswww…
Browse files Browse the repository at this point in the history
…hivemqcom

implements IP/port to MQTT Platform (https://www.hivemq.com) #29 - fo…
  • Loading branch information
ohAnd authored Sep 3, 2024
2 parents fbf1445 + 70ead87 commit 576f95f
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 68 deletions.
11 changes: 11 additions & 0 deletions include/mqttHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
#include <PubSubClient.h>
#include <WiFiClientSecure.h>

// MQTT_CONNECTION_TIMEOUT (-4): The server didn't respond within the keep-alive time.
// MQTT_CONNECTION_LOST (-3): The network connection was broken.
// MQTT_CONNECT_FAILED (-2): The network connection failed.
// MQTT_DISCONNECTED (-1): The client is disconnected.
// MQTT_CONNECTED (0): The client is connected.
// MQTT_CONNECT_BAD_PROTOCOL (1): The server doesn't support the requested version of MQTT.
// MQTT_CONNECT_BAD_CLIENT_ID (2): The server rejected the client identifier.
// MQTT_CONNECT_UNAVAILABLE (3): The server was unable to accept the connection.
// MQTT_CONNECT_BAD_CREDENTIALS (4): The username/password were rejected.
// MQTT_CONNECT_UNAUTHORIZED (5): The client was not authorized to connect.

struct PowerLimitSet {
int8_t setValue = 0;
boolean update = false;
Expand Down
6 changes: 3 additions & 3 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define VERSION "1.9.457_localDev"
#define BUILDTIME "01.09.2024 - 07:43:09"
#define BUILDTIMESTAMP "1725169389"
#define VERSION "1.9.622_localDev"
#define BUILDTIME "03.09.2024 - 23:07:43"
#define BUILDTIMESTAMP "1725397663"
8 changes: 4 additions & 4 deletions include/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.9.457_localDev",
"versiondate": "01.09.2024 - 07:43:09",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.9.457_localDev.bin",
"link": "https://github.com/ohAnd/dtuGateway/releases/latest/download/dtuGateway_release_1.9.457_localDev.bin"
"version": "1.9.622_localDev",
"versiondate": "03.09.2024 - 23:07:43",
"linksnapshot": "https://github.com/ohAnd/dtuGateway/releases/download/snapshot/dtuGateway_snapshot_1.9.622_localDev.bin",
"link": "https://github.com/ohAnd/dtuGateway/releases/latest/download/dtuGateway_release_1.9.622_localDev.bin"
}
6 changes: 3 additions & 3 deletions include/web/index_html.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ const char INDEX_HTML[] PROGMEM = R"=====(
<div>
<input type="text" id="mqttIP" class="ipv4Input" name="ipv4" placeholder="xxx.xxx.xxx.xxx">
</div>
<!-- <div> -->
<!-- <input type="checkbox" id="mqttUseTLS"> TLS connection (e.g. 123456789.s1.eu.hivemq.cloud:8883) -->
<!-- </div> -->
<div>
<input type="checkbox" id="mqttUseTLS"> <small>TLS connection (e.g. 123456789.s1.eu.hivemq.cloud:8883) - works only with ESP32</small>
</div>
<div>
<br>specify user on your mqtt broker instance:
</div>
Expand Down
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ framework = arduino
monitor_speed = 115200
monitor_port = COM3
upload_port = COM3
upload_speed = 921600
lib_deps =
arduino-libraries/NTPClient @ ^3.2.1
robtillaart/CRC @ ^1.0.2
Expand Down Expand Up @@ -65,6 +66,7 @@ framework = arduino
monitor_speed = 115200
monitor_port = COM7
upload_port = COM7
upload_speed = 921600
lib_deps =
arduino-libraries/NTPClient @ ^3.2.1
robtillaart/CRC @ ^1.0.2
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ So I decided to put this abstraction in an **ESP8266** to have a stable abstract
- binding: updating to a MQTT broker with readed data incl. set PowerLimit over MQTT
- 2 ways to configure - simple mqtt publishing with base topic or together with HA MQTT AutoDiscovery based
- for all publishing retain flag is set (keeping last seen data in broker)
- TLS connection to mqtt broker e.g. for hivemq.cloud - ! only possible for ESP32 setup
- can act as a remote display for another dtuGateway
- data will be received by MQTT
- webUI shows the same data as the host
Expand Down
64 changes: 10 additions & 54 deletions src/dtuGateway.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,21 @@
#include <ESP8266_ISR_Timer.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// #include <ESP8266WebServer.h>
// #include <ESP8266HTTPUpdateServer.h>
// #include <ESP8266httpUpdate.h>
#include <ESP8266mDNS.h>
// #include <WiFiClientSecureBearSSL.h>
#elif defined(ESP32)
// #define HARDWARE "ESP32"
#include <ESP32TimerInterrupt.h>
#include <ESP32_ISR_Timer.h>
#include <WiFi.h>
#include <HTTPClient.h>
// #include <WebServer.h>
// #include <HTTPUpdateServer.h>
// #include <ESP32httpUpdate.h>
#include <ESPmDNS.h>
// #include <WiFiClientSecure.h>
#include <map>
#endif

#include <WiFiUdp.h>
#include <NTPClient.h>

#include <ArduinoJson.h>
// #include <ArduinoJson.h>

#include <base/webserver.h>
#include <base/platformData.h>
Expand Down Expand Up @@ -54,7 +46,6 @@ unsigned long previousMillis50ms = 0;
unsigned long previousMillis100ms = 0;
unsigned long previousMillisShort = 1704063600; // in seconds
unsigned long previousMillis5000ms = 1704063600; // in seconds
// dtuNextUpdateCounterSeconds = 1704063600; -> with platformData
unsigned long previousMillisLong = 1704063600;

#define WIFI_RETRY_TIME_SECONDS 30
Expand Down Expand Up @@ -126,12 +117,6 @@ DTUInterface dtuInterface("192.168.0.254"); // initialize with default IP

MQTTHandler mqttHandler(userConfig.mqttBrokerIpDomain, userConfig.mqttBrokerPort, userConfig.mqttBrokerUser, userConfig.mqttBrokerPassword, userConfig.mqttUseTLS);

// #if defined(ESP8266)
// ESP8266WebServer server(80);
// #elif defined(ESP32)
// WebServer server(80);
// #endif

// Init ESP8266 only and only Timer 1
#if defined(ESP8266)
ESP8266Timer ITimer;
Expand Down Expand Up @@ -165,7 +150,6 @@ boolean checkWifiTask()
wifi_connecting = true;
blinkCode = BLINK_TRY_CONNECT_DTU;

// startServices();
return false;
}
else if (WiFi.status() != WL_CONNECTED && wifi_connecting && wifiTimeoutShort > 0) // check during connecting wifi and decrease for short timeout
Expand Down Expand Up @@ -248,6 +232,7 @@ boolean scanNetworksResult()
}

// OTA

// // ---> /updateRequest
// void handleUpdateRequest()
// {
Expand All @@ -256,30 +241,23 @@ boolean scanNetworksResult()
// urlToBin = updateInfo.updateURLRelease;
// else
// urlToBin = updateInfo.updateURL;

// #if defined(ESP8266)
// BearSSL::WiFiClientSecure updateclient;
// #elif defined(ESP32)
// WiFiClientSecure updateclient;
// #endif
// updateclient.setInsecure();

// if (urlToBin == "" || updateInfo.updateAvailable != true)
// {
// Serial.println(F("[update] no url given or no update available"));
// return;
// }

// server.sendHeader("Connection", "close");
// server.send(200, "application/json", "{\"update\": \"in_progress\"}");

// Serial.println(F("[update] Update requested"));
// Serial.println("[update] try download from " + urlToBin);

// // ESPhttpUpdate.rebootOnUpdate(false); // remove automatic update

// Serial.println(F("[update] starting update"));

// #if defined(ESP8266)
// ESPhttpUpdate.onStart(update_started);
// ESPhttpUpdate.onEnd(update_finished);
Expand All @@ -290,9 +268,7 @@ boolean scanNetworksResult()
// #elif defined(ESP32)
// // ...
// #endif

// updateInfo.updateRunning = true;

// // stopping all services to prevent OOM/ stackoverflow
// timeClient.end();
// #if defined(ESP8266)
Expand All @@ -305,13 +281,11 @@ boolean scanNetworksResult()
// #endif
// server.stop();
// server.close();

// #if defined(ESP8266)
// t_httpUpdate_return ret = ESPhttpUpdate.update(updateclient, urlToBin);
// #elif defined(ESP32)
// t_httpUpdate_return ret = ESPhttpUpdate.update(urlToBin);
// #endif

// switch (ret)
// {
// case HTTP_UPDATE_FAILED:
Expand All @@ -332,8 +306,6 @@ boolean scanNetworksResult()
// Serial.println("[update] Update routine done - ReturnCode: " + String(ret));
// }

//

// get the info about update from remote
// boolean getUpdateInfo()
// {
Expand All @@ -346,7 +318,6 @@ boolean scanNetworksResult()
// WiFiClientSecure secClient;
// secClient.setInsecure();
// #endif

// if (userConfig.selectedUpdateChannel == 0)
// {
// versionUrl = updateInfo.updateInfoWebPathRelease;
Expand All @@ -355,12 +326,9 @@ boolean scanNetworksResult()
// {
// versionUrl = updateInfo.updateInfoWebPath;
// }

// Serial.print("\n---> getUpdateInfo - check for: " + versionUrl + "\n");

// // create an HTTPClient instance
// HTTPClient https;

// // Initializing an HTTPS communication using the secure client
// #if defined(ESP8266)
// if (https.begin(*secClient, versionUrl))
Expand All @@ -374,7 +342,6 @@ boolean scanNetworksResult()
// https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); // Enable automatic following of redirects
// int httpCode = https.GET();
// Serial.println("\n---> getUpdateInfo - got http ret code:" + String(httpCode));

// // httpCode will be negative on error
// if (httpCode > 0)
// {
Expand All @@ -383,11 +350,9 @@ boolean scanNetworksResult()
// if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
// {
// String payload = https.getString();

// // Parse JSON using ArduinoJson library
// JsonDocument doc;
// DeserializationError error = deserializeJson(doc, payload);

// // Test if parsing succeeds.
// if (error)
// {
Expand All @@ -411,7 +376,6 @@ boolean scanNetworksResult()
// {
// localVersion = localVersion.substring(0, localVersion.indexOf("_"));
// }

// if (userConfig.selectedUpdateChannel == 0)
// {
// strcpy(updateInfo.versionServerRelease, (const char *)(doc["version"]));
Expand All @@ -426,7 +390,6 @@ boolean scanNetworksResult()
// {
// versionSnapshot = versionSnapshot.substring(0, versionSnapshot.indexOf("_"));
// }

// strcpy(updateInfo.versiondateServer, (const char *)(doc["versiondate"]));
// strcpy(updateInfo.updateURL, (const char *)(doc["linksnapshot"]));
// updateInfo.updateAvailable = checkVersion(localVersion, versionSnapshot);
Expand All @@ -435,7 +398,6 @@ boolean scanNetworksResult()
// strcpy(updateInfo.updateURL, (const char *)(doc["linksnapshot"]));
// updateInfo.updateAvailable = checkVersion(localVersion, versionSnapshot);
// }

// server.sendHeader("Connection", "close");
// server.send(200, "application/json", "{\"updateRequest\": \"done\"}");
// }
Expand Down Expand Up @@ -470,7 +432,6 @@ boolean scanNetworksResult()
// // if v1 is smaller, 0 if equal
// // int result = 0;
// int vnum1 = 0, vnum2 = 0;

// // loop until both string are
// // processed
// for (unsigned int i = 0, j = 0; (i < v1.length() || j < v2.length());)
Expand All @@ -482,29 +443,25 @@ boolean scanNetworksResult()
// vnum1 = vnum1 * 10 + (v1[i] - '0');
// i++;
// }

// // storing numeric part of
// // version 2 in vnum2
// while (j < v2.length() && v2[j] != '.')
// {
// vnum2 = vnum2 * 10 + (v2[j] - '0');
// j++;
// }

// if (vnum1 > vnum2)
// {
// // result = 1; // v2 is smaller
// // Serial.println("vgl (i=" + String(i) + ") v2 smaller - vnum1 " + String(vnum1) + " - " + String(vnum2));
// return false;
// }

// if (vnum2 > vnum1)
// {
// // result = -1; // v1 is smaller
// // Serial.println("vgl (i=" + String(i) + ") v1 smaller - vnum1 " + String(vnum1) + " - " + String(vnum2));
// return true;
// }

// // if equal, reset variables and
// // go for next numeric part
// // Serial.println("vgl (i=" + String(i) + ") v1 equal 2 - vnum1 " + String(vnum1) + " - " + String(vnum2));
Expand Down Expand Up @@ -692,38 +649,36 @@ void updateValuesToMqtt(boolean haAutoDiscovery = false)
{
Serial.println("MQTT:\t\t publish data (HA autoDiscovery = " + String(haAutoDiscovery) + ")");
std::map<std::string, std::string> keyValueStore;

keyValueStore["time_stamp"] = String(dtuGlobalData.currentTimestamp).c_str();

// grid
keyValueStore["grid_U"] = String(dtuGlobalData.grid.voltage).c_str();
keyValueStore["grid_I"] = String(dtuGlobalData.grid.current).c_str();
keyValueStore["grid_P"] = String(dtuGlobalData.grid.power).c_str();
keyValueStore["grid_dailyEnergy"] = String(dtuGlobalData.grid.dailyEnergy, 3).c_str();
if (dtuGlobalData.grid.totalEnergy != 0)
keyValueStore["grid_totalEnergy"] = String(dtuGlobalData.grid.totalEnergy, 3).c_str();

// pv0
keyValueStore["pv0_U"] = String(dtuGlobalData.pv0.voltage).c_str();
keyValueStore["pv0_I"] = String(dtuGlobalData.pv0.current).c_str();
keyValueStore["pv0_P"] = String(dtuGlobalData.pv0.power).c_str();
keyValueStore["pv0_dailyEnergy"] = String(dtuGlobalData.pv0.dailyEnergy, 3).c_str();
if (dtuGlobalData.pv0.totalEnergy != 0)
keyValueStore["pv0_totalEnergy"] = String(dtuGlobalData.pv0.totalEnergy, 3).c_str();

// pv1
keyValueStore["pv1_U"] = String(dtuGlobalData.pv1.voltage).c_str();
keyValueStore["pv1_I"] = String(dtuGlobalData.pv1.current).c_str();
keyValueStore["pv1_P"] = String(dtuGlobalData.pv1.power).c_str();
keyValueStore["pv1_dailyEnergy"] = String(dtuGlobalData.pv1.dailyEnergy, 3).c_str();
if (dtuGlobalData.pv0.totalEnergy != 0)
keyValueStore["pv1_totalEnergy"] = String(dtuGlobalData.pv1.totalEnergy, 3).c_str();

// inverter
keyValueStore["inverter_Temp"] = String(dtuGlobalData.inverterTemp).c_str();
keyValueStore["inverter_PowerLimit"] = String(dtuGlobalData.powerLimit).c_str();
keyValueStore["inverter_WifiRSSI"] = String(dtuGlobalData.dtuRssi).c_str();

// copy
for (const auto &pair : keyValueStore)
{
String entity = (pair.first).c_str();
// subtopic.replace("_", "/");
mqttHandler.publishStandardData(entity, (pair.second).c_str());
}
}
Expand Down Expand Up @@ -858,7 +813,8 @@ void setup()
displayTFT.drawFactoryMode(String(platformData.fwVersion), platformData.espUniqueName, apIP.toString());
userConfig.displayConnected = 0;
}
// deafult setting for mqtt main topic

// default setting for mqtt main topic
("dtu_" + String(platformData.chipID)).toCharArray(userConfig.mqttBrokerMainTopic, sizeof(userConfig.mqttBrokerMainTopic));
configManager.saveConfig(userConfig);

Expand Down Expand Up @@ -886,6 +842,7 @@ void setup()
}
else
Serial.println(F("Can't set ITimer correctly. Select another freq. or interval"));

// delay for startup background tasks in ESP
delay(2000);
}
Expand Down Expand Up @@ -1387,7 +1344,6 @@ void loop()
Serial.print("local: " + dtuInterface.getTimeStringByTimestamp(dtuGlobalData.currentTimestamp));
Serial.println(" --- NTP: " + timeClient.getFormattedTime());

// Serial.print(" --- currentMillis " + String(currentMillis) + " --- ");
previousMillis5000ms = currentMillis;
// -------->
// -----------------------------------------
Expand Down
Loading

0 comments on commit 576f95f

Please sign in to comment.