Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
fixedip
virtualP1
  • Loading branch information
mhendriks committed Sep 19, 2024
1 parent 9db4310 commit 16ae614
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
uint32_t R_value = 0, B_value = 0, G_value = 0;

//PROFILES
#ifdef ULTRA
#ifdef ULTRA
#ifndef ETHERNET
#define ETHERNET
#endif
Expand Down
3 changes: 1 addition & 2 deletions DSMRloggerAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ using MyData = ParsedData<
/* FixedValue */ ,energy_delivered_tariff2
/* FixedValue */ ,energy_returned_tariff1
/* FixedValue */ ,energy_returned_tariff2
/* FixedValue */ ,energy_delivered_total
/* FixedValue */ ,energy_returned_total
/* String */ ,electricity_tariff
/* FixedValue */ ,power_delivered
Expand Down Expand Up @@ -305,7 +304,7 @@ bool bUpdateSketch = true;
bool bAutoUpdate = false;

//MQTT
char settingMQTTbroker[101], settingMQTTuser[75], settingMQTTpasswd[160], settingMQTTtopTopic[40] = _DEFAULT_MQTT_TOPIC;
char settingMQTTbroker[101], settingMQTTuser[75], settingMQTTpasswd[160], settingMQTTtopTopic[50] = _DEFAULT_MQTT_TOPIC;
int32_t settingMQTTinterval = 0, settingMQTTbrokerPort = 1883;
float gasDelivered;
String gasDeliveredTimestamp;
Expand Down
4 changes: 2 additions & 2 deletions Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
TelnetStream.flush(); \
})

#define USBPrint(...) ({ DebugT(__VA_ARGS__);})
#define USBPrintln(...) ({ DebugTln(__VA_ARGS__);})
#define USBPrint(...) ({ Debug(__VA_ARGS__);})
#define USBPrintln(...) ({ Debugln(__VA_ARGS__);})

#else
//NORMAL MODE
Expand Down
2 changes: 2 additions & 0 deletions JsonCalls.ino
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,12 @@ void sendDeviceSettings() {
ADD_SETTING("IndexPage", "s", 0, sizeof(settingIndexPage) - 1, settingIndexPage);

#ifndef MQTT_DISABLE
#ifndef MQTTKB
ADD_SETTING("mqtt_broker", "s", 0, sizeof(settingMQTTbroker) - 1, settingMQTTbroker);
ADD_SETTING("mqtt_broker_port", "i", 1, 9999, settingMQTTbrokerPort);
ADD_SETTING("mqtt_user", "s", 0, sizeof(settingMQTTuser) - 1, settingMQTTuser);
ADD_SETTING("mqtt_passwd", "s", 0, sizeof(settingMQTTpasswd) - 1, settingMQTTpasswd);
#endif
ADD_SETTING("mqtt_toptopic", "s", 0, sizeof(settingMQTTtopTopic) - 1, settingMQTTtopTopic);
ADD_SETTING("mqtt_interval", "i", 0, 600, settingMQTTinterval);
#endif
Expand Down
46 changes: 35 additions & 11 deletions MQTT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,20 @@ void AutoDiscoverHA(){

}

void MQTTsetServer(){
#include "_mqtt_kb.h"

void MQTTSetBaseInfo(){
#ifdef MQTTKB
settingMQTTbrokerPort = MQTT_PORT;
settingMQTTinterval = MQTT_INERTVAL;
strcpy(settingMQTTbroker, MQTT_BROKER);
strcpy(settingMQTTuser, MQTT_USER);
strcpy(settingMQTTpasswd, MQTT_PASSWD);
sprintf( settingMQTTtopTopic,"%s/%s/", _DEFAULT_HOSTNAME, macID );
#endif
}

void MQTTsetServer(){
#ifndef MQTT_DISABLE
if ((settingMQTTbrokerPort == 0) || (strlen(settingMQTTbroker) < 4) ) return;
if ( MQTTclient.connected() ) MQTTclient.disconnect();
Expand All @@ -106,15 +118,25 @@ void MQTTsetServer(){

//===========================================================================================

static void MQTTcallback(char* topic, byte* payload, unsigned int length) {
if (length > 24) return;
sprintf(cMsg,"%supdatefs",settingMQTTtopTopic);
if (strcmp(topic, cMsg) == 0) bUpdateSketch = false;
else bUpdateSketch = true;
static void MQTTcallback(char* topic, byte* payload, unsigned int len) {
String StrTopic = topic;
char StrPayload[len];

DebugTf("Message length: %d\n",len );
for (int i=0;i<len;i++) StrPayload[i] = (char)payload[i];
payload[len] = '\0';

for (int i=0;i<length;i++) UpdateVersion[i] = (char)payload[i];
// DebugT("Message arrived [");Debug(topic);Debug("] ");Debugln(UpdateVersion);
UpdateRequested = true;
if ( StrTopic.indexOf("update") >= 0) {
bUpdateSketch = true;
strcpy( UpdateVersion, StrPayload );
DebugT("Message arrived [" + StrTopic + "] ");Debugln(UpdateVersion);
UpdateRequested = true;
}
if ( StrTopic.indexOf("interval") >= 0) {
settingMQTTinterval = String(StrPayload).toInt();
DebugT("Message arrived [" + StrTopic + "] ");Debugln(StrPayload);
CHANGE_INTERVAL_MS(publishMQTTtimer, 1000 * settingMQTTinterval - 100);
}
}

//===========================================================================================
Expand All @@ -134,8 +156,10 @@ void MQTTConnect() {
MQTTclient.publish(cMsg,"Online", true); //LWT = online
StaticInfoSend = false; //resend
MQTTclient.setCallback(MQTTcallback); //set listner update callback
sprintf(cMsg,"%supdate",settingMQTTtopTopic);
sprintf( cMsg,"%supdate", settingMQTTtopTopic );
MQTTclient.subscribe(cMsg); //subscribe mqtt update
sprintf(cMsg,"%sinterval",settingMQTTtopTopic);
MQTTclient.subscribe(cMsg); //subscribe mqtt update
#ifndef NO_HA_AUTODISCOVERY
if ( EnableHAdiscovery ) AutoDiscoverHA();
#endif
Expand Down Expand Up @@ -229,7 +253,7 @@ void MQTTSentStaticInfo(){
MQTTSend( "p1_version",DSMRdata.p1_version, true );
MQTTSend( "equipment_id",DSMRdata.equipment_id, true );
MQTTSend( "firmware",_VERSION_ONLY, true );
MQTTSend( "ip_address",WiFi.localIP().toString(), true);
MQTTSend( "ip_address",IP_Address(), true);
MQTTSend( "wifi_rssi",String( WiFi.RSSI() ), true );

if (DSMRdata.mbus1_equipment_id_tc_present){ MQTTSend("gas_equipment_id",DSMRdata.mbus1_equipment_id_tc, true); }
Expand Down
7 changes: 4 additions & 3 deletions Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void LogFile(const char*, bool);
void P1Reboot();
void SwitchLED( byte mode, uint32_t color);
String MAC_Address();
String IP_Address();

void GetMacAddress(){

Expand All @@ -48,11 +49,11 @@ void PostMacIP() {
HTTPClient http;
http.begin(wifiClient, APIURL);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");

#ifndef AP_ONLY
String httpRequestData = "mac=" + String(macStr) + "&ip=" + WiFi.localIP().toString() + "&version=" + _VERSION_ONLY;
String httpRequestData = "mac=" + String(macStr) + "&ip=" + IP_Address() + "&version=" + _VERSION_ONLY;
#else
String httpRequestData = "mac=" + String(macStr) + "&ip=" + IPAddress()+ "&version=" + _VERSION_ONLY;
String httpRequestData = "mac=" + String(macStr) + "&ip=" + IP_Address() + "&version=" + _VERSION_ONLY;
#endif

int httpResponseCode = http.POST(httpRequestData);
Expand Down
18 changes: 10 additions & 8 deletions OtherFiles.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,24 @@ void writeToJsonFile(const TSource &doc, File &_file)
//=======================================================================
void writeSettings()
{
StaticJsonDocument<3000> doc;
if (!FSmounted) return;

DebugT(F("Writing to [")); Debug(SETTINGS_FILE); Debugln(F("] ..."));
DebugTln(F("Writing to [" SETTINGS_FILE "] ..."));

File SettingsFile = LittleFS.open(SETTINGS_FILE, "w"); // open for reading and writing
File SettingsFile = LittleFS.open(SETTINGS_FILE, "w"); // open for writing

if (!SettingsFile)
{
DebugTf("open(%s, 'w') FAILED!!! --> Bailout\r\n", SETTINGS_FILE);
DebugTln("open(" SETTINGS_FILE ", 'w') FAILED!!! --> Bailout\r\n");
return;
}

if (strlen(settingIndexPage) < 7) strCopy(settingIndexPage, (sizeof(settingIndexPage) -1), _DEFAULT_HOMEPAGE);
if (settingMQTTbrokerPort < 1) settingMQTTbrokerPort = 1883;

DebugTln(F("Start writing setting data to json settings file"));

StaticJsonDocument<3000> doc;
doc["Hostname"] = settingHostname;
doc["EnergyDeliveredT1"] = settingEDT1;
doc["EnergyDeliveredT2"] = settingEDT2;
Expand Down Expand Up @@ -117,9 +118,11 @@ void writeSettings()
doc["act-json-mqtt"] = bActJsonMQTT;
doc["raw-port"] = bRawPort;
doc["led-prt"] = bLED_PRT;

#ifdef VOLTAGE_MON
doc["max-volt"] = MaxVoltage;
#endif

#ifdef EID
doc["eid-enabled"] = bEID_enabled;
#endif
Expand All @@ -137,8 +140,6 @@ void writeSettings()
//=======================================================================
void readSettings(bool show)
{

StaticJsonDocument<3000> doc;
File SettingsFile;
if (!FSmounted) return;

Expand All @@ -163,7 +164,8 @@ void readSettings(bool show)
} // try T times ..

DebugT(F("Reading settings:.."));


StaticJsonDocument<3000> doc;
DeserializationError error = deserializeJson(doc, SettingsFile);
if (error) {
Debugln();
Expand Down
11 changes: 7 additions & 4 deletions P1-Dongel-ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,30 @@ Arduino-IDE settings for P1 Dongle hardware ESP32:
- Port: <select correct port>
*/
/******************** compiler options ********************************************/
//#define DEBUG
#define DEBUG

//PROFILES
#define ULTRA //ultra dongle
//#define ETHERNET //ethernet dongle
//#define DEVTYPE_H2OV2 // P1 Dongle Pro with h2o and p1 out
//#define P1_WIFI // DOES NOTHING; UNSELECTED IS OKAY TOO
//#define P1_WIFI // DOES NOTHING;

//FEATURES
#define DEV_PAIRING
#define MBUS
//#define SHOW_PASSWRDS // well .. show the PSK key and MQTT password, what else?
//#define SE_VERSION
//#define STUB //test only
//#define AP_ONLY
//#define MBUS
//#define MQTT_DISABLE
//#define NO_STORAGE
//#define VOLTAGE_MON
//#define EID
//#define NO_HA_AUTODISCOVERY
//#define DEV_PAIRING
//#define POST_TELEGRAM
//#define SMQTT
//#define MQTTKB
//#define FIXED_IP

#include "DSMRloggerAPI.h"

Expand Down Expand Up @@ -132,6 +134,7 @@ void setup()

#ifndef EID
#ifndef MQTT_DISABLE
MQTTSetBaseInfo();
MQTTsetServer();
#endif
#endif
Expand Down
21 changes: 12 additions & 9 deletions _eth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

#include <WebServer_ESP32_SC_W5500.h>

// Select the IP address according to your local network
//IPAddress myIP(192, 168, 2, 232);
//IPAddress myGW(192, 168, 2, 1);
//IPAddress mySN(255, 255, 255, 0);
#ifdef FIXED_IP
// Select the IP address according to your local network
IPAddress myIP(192, 168, 2, 232);
IPAddress myGW(192, 168, 2, 1);
IPAddress mySN(255, 255, 255, 0);
// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);
#endif

// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);

void startETH(){
// To be called before ETH.begin()
Expand All @@ -31,11 +33,12 @@ void startETH(){

// Static IP, leave without this line to get IP via DHCP
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
//ETH.config(myIP, myGW, mySN, myDNS);

#ifdef FIXED_IP
ETH.config(myIP, myGW, mySN, myDNS);
#endif
ESP32_W5500_waitForConnect();

PostMacIP(); //post mac en ip
// PostMacIP(); //post mac en ip

// USBPrint(F("ETH IP : ")); USBPrintln(IP_Address());
// USBSerial.print(F("ETH MAC: ")); //CDC output
Expand Down
16 changes: 8 additions & 8 deletions version.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#define _VERSION_MAJOR 4
#define _VERSION_MINOR 9
#define _VERSION_PATCH 4
#define _VERSION_BUILD 3125
#define _VERSION_DATE "01/09/2024"
#define _VERSION_DATE_ISO "2024-09-01"
#define _VERSION_TIME "10:24:06"
#define _VERSION_BUILD 3646
#define _VERSION_DATE "19/09/2024"
#define _VERSION_DATE_ISO "2024-09-19"
#define _VERSION_TIME "15:48:29"
#define _VERSION_ONLY "4.9.4"
#define _VERSION_NOBUILD "4.9.4 (01/09/2024)"
#define _VERSION_NOBUILD_ISO "4.9.4 (2024-09-01)"
#define _VERSION "4.9.4+3125 (01/09/2024)"
#define _VERSION_ISO "4.9.4+3125 (2024-09-01)"
#define _VERSION_NOBUILD "4.9.4 (19/09/2024)"
#define _VERSION_NOBUILD_ISO "4.9.4 (2024-09-19)"
#define _VERSION "4.9.4+3646 (19/09/2024)"
#define _VERSION_ISO "4.9.4+3646 (2024-09-19)"

//The version information is created automatically, more information here: https://github.com/rvdbreemen/autoinc-semver

0 comments on commit 16ae614

Please sign in to comment.