Skip to content

Commit

Permalink
JSON MQTT, enabled webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Hermen committed Sep 2, 2022
1 parent 027d1ac commit fd499d7
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
Settings.h
secrets.h
5 changes: 5 additions & 0 deletions .gitignore~
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions .vscode/extensions.json
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
1 change: 1 addition & 0 deletions Debug.h
@@ -1,6 +1,7 @@
#ifndef __DEBUG_H
#define __DEBUG_H

#include "Settings.h"

#ifdef DEBUG
#define DEBUG_OUT Serial
Expand Down
2 changes: 1 addition & 1 deletion ModWebserver.h
Expand Up @@ -81,7 +81,7 @@ void handleRoot() {
out += "<th>" + String(getChannelName(i)) + "</th>";
out += "</tr>";

for (byte pv=0; pv <=3; pv++){
for (byte pv=0; pv <NRofPV; pv++){
out += "<tr>";
for (byte i = 0; i < (ANZAHL_VALUES); i++)
if (i==0) out += "<td>" + String(int(VALUES[pv][i])) + "</td>"; //PVnr must not be displayed as a floatnr
Expand Down
52 changes: 39 additions & 13 deletions NRF24_DTUMIesp.ino
Expand Up @@ -332,7 +332,7 @@ void setup(void) {
DEBUG_OUT.printf("Setup mqtt ......\r\n");
setupMQTT();
DEBUG_OUT.printf("Setup www .......\r\n");
// setupWebServer();
setupWebServer();
// setupUpdateByOTA();
calcSunUpDown (getNow());
istTag = isDayTime(0);
Expand Down Expand Up @@ -740,6 +740,26 @@ void SendMQTTMsg(String topic, String value){

}//---SendMQTTMsg-------------------------------------------------------------------------------------------------------

void SendMQTTJSON(String topic) {
// compose and sent JSON Message
String json = String("{\"TIME\":\"")+(String)getDateStr(getNow())+String("T")+(String)getTimeStr(getNow())+String("\", \"ENERGY\":{")+
String("\"Power\":")+String(PMI)+String(", ")+
String("\"Limit\":")+String(Limit)+String(", ")+
String("\"Power_"+(String)(PV+1)+"\":")+String(P_DC)+String(", ")+
String("\"Voltage_"+(String)(PV+1)+"\":")+String(U_DC)+String(", ")+
String("\"Current_"+(String)(PV+1)+"\":")+String(I_DC)+String(", ")+
String("\"Energy_"+(String)(PV+1)+"\":")+String(Q_DC)+String(", ")+
String("\"Temperature\":")+String(TEMP)+String(", ")+
String("\"Status"+(String)(PV+1)+"\":\"")+String(STAT)+String("\"")+
String("}}");

mqttClient.beginMessage(topic);
mqttClient.print(json);
mqttClient.endMessage();
}



void PrintOutValues(void){
//----------------------------------------------------------------------------------------------------------------------
DEBUG_OUT.printf("CH:%02i MI:%04iW [PV%1i %5sW %4sV %4sA %04iWh][%5sV %4sHz %4sC S:%i] Grd:%04iW Lm:%04iW PVok:%i ",
Expand Down Expand Up @@ -1080,18 +1100,24 @@ void loop(void) {
if (MQTT && DataOK ){
//DEBUG_OUT.printf("Update Mqtt Services\r\n");
mqttClient.poll();
SendMQTTMsg((String)INVTOT_P, (String) PMI);
SendMQTTMsg((String)LIMIT_P, (String) Limit);
SendMQTTMsg((String)INV_P_PVNR+(String)(PV+1), (String) P_DC);
SendMQTTMsg((String)INV_UDC_PVNR+(String)(PV+1), (String) U_DC);
SendMQTTMsg((String)INV_IDC_PVNR+(String)(PV+1), (String) I_DC);
SendMQTTMsg((String)INV_Q_PVNR+(String)(PV+1), (String) Q_DC);
SendMQTTMsg((String)INV_TEMP, (String) TEMP);
SendMQTTMsg((String)INV_STS_PVNR+(String)(PV+1), (String) STAT);
SendMQTTMsg((String)INV_CONSUM_P, (String) (abs(GridPower) + PMI));
SendMQTTMsg((String)DAY, (String)getDateStr(getNow()));
SendMQTTMsg((String)TIME, (String)getTimeStr(getNow()));
SendMQTTMsg((String)INFO, "NRF24");

#ifdef SENDJSON
SendMQTTJSON(JSON_TOPIC);
#else
SendMQTTMsg((String)INVTOT_P, (String) PMI);
SendMQTTMsg((String)LIMIT_P, (String) Limit);
SendMQTTMsg((String)INV_P_PVNR+(String)(PV+1), (String) P_DC);
SendMQTTMsg((String)INV_UDC_PVNR+(String)(PV+1), (String) U_DC);
SendMQTTMsg((String)INV_IDC_PVNR+(String)(PV+1), (String) I_DC);
SendMQTTMsg((String)INV_Q_PVNR+(String)(PV+1), (String) Q_DC);
SendMQTTMsg((String)INV_TEMP, (String) TEMP);
SendMQTTMsg((String)INV_STS_PVNR+(String)(PV+1), (String) STAT);
SendMQTTMsg((String)INV_CONSUM_P, (String) (abs(GridPower) + PMI));
SendMQTTMsg((String)DAY, (String)getDateStr(getNow()));
SendMQTTMsg((String)TIME, (String)getTimeStr(getNow()));
SendMQTTMsg((String)INFO, "NRF24");

#endif
}
else {
//DEBUG_OUT.printf("No MQTT..\r\n");
Expand Down
14 changes: 9 additions & 5 deletions Settings.h
Expand Up @@ -46,9 +46,9 @@ int WR_LIMITTED = 0; //fixed limiting in Watt. if you dont want fixed lim
// WR Config ..................................................................................
#define SerialWR MY_MI_WR // <<<<<<<<<<<<<<<<<<<<<<< define in secrets.h
bool MI300 = 0; //<<<<<<<<<<<< choose which model of Hoymiles MI microinverter
bool MI600 = 0; //choose this for TSUN TSOL-M800 also
bool MI1500 = 1;
#define NRofPV 3 //<<<<<<<<<<<< number of PV panels in use (connected) 1-4. (not the number of WR-ports)
bool MI600 = 1; //choose this for TSUN TSOL-M800 also
bool MI1500 = 0;
#define NRofPV 2 //<<<<<<<<<<<< number of PV panels in use (connected) 1-4. (not the number of WR-ports)
//assume the ports are connected in order from 1 to 4
//we have to know how many PVs are really connected to WR.
//if we know it, we dont have to wait of all ports!
Expand All @@ -62,9 +62,13 @@ IPAddress ROUTER = IPAddress(192,168,1,1); // your routers IP???
//#define TIMESERVER_NAME "fritz.box"

// MQTT ........................................................................................
const char MQTTbroker[] = "192.168.1.11";
const char MQTTbroker[] = "broker";
int MQTTport = 1883;
char MQTTclientid[] = "MYDTU";
char MQTTclientid[] = "TSUN";

// send Tasmota compatible MQTT JSON instead of single topics
#define SENDJSON
static String JSON_TOPIC = String(MQTTclientid)+"/SENSOR";
// END OF YOUR CONFIGURATION ===================================================================


Expand Down
156 changes: 156 additions & 0 deletions Settings.h.orig
@@ -0,0 +1,156 @@
/* die orig. SW ist vom Hubi, wurde von mir(Ziyat T.) für den MI-WR abgeaendert.
Getestet auf ESP8266/ArduinoUNO.
https://www.mikrocontroller.net/topic/525778
https://github.com/hm-soft/Hoymiles-DTU-Simulation
----------------------------------------
Alle Einstellungen sind in Settings.h UND secrets.h !!
----------------------------------------
*/
#ifndef __SETTINGS_H
#define __SETTINGS_H

#include <stdint.h>
#include <RF24.h>
#include <Pinger.h>
#include "secrets.h" //<<<<<<<<<<<<<<<<<< put your secrets here

// Hardware configuration, CHECK THIS OUT with your board !!!!!!!!!!!!!!!!!!!!!!!!!!
// ESP8266 PIN Setting====================================================================================
#ifdef ESP8266
#define RF1_CE_PIN (D2) //(D2)
#define RF1_CS_PIN (D8) //(D8)
#define RF1_IRQ_PIN (D3)
//GPIO0 D3 -> IRQ
//GPIO2 D4 -> CE
//GPIO14 D5 -> SCK
//GPIO12 D6 -> MIso
//GPIO13 D7 -> MOsi
//GPIO15 D8 -> CSN
#else //arduino PIN setting
#define RF1_CE_PIN (9)
#define RF1_CS_PIN (10)
#define RF1_IRQ_PIN (2)
#endif
// ESP8266 PIN Setting====================================================================================


//=== DEFINE YOUR CONFIGURATION HERE =====================================================================
#define SER_BAUDRATE (115200)
#define DEBUG // output all infos on serial monitor, comment it for no infos

bool WITHWIFI = 1; // wifi yes/no
bool WITHMQTT = 1; //do you need mqtt? if yes, may be you want to change the topic names in mqtt.h
bool ZEROEXP = 0; //zero export to the grid
int WR_LIMITTED = 0; //fixed limiting in Watt. if you dont want fixed limiting set it to NULL

// WR Config ..................................................................................
#define SerialWR MY_MI_WR // <<<<<<<<<<<<<<<<<<<<<<< define in secrets.h
bool MI300 = 0; //<<<<<<<<<<<< choose which model of Hoymiles MI microinverter
bool MI600 = 0; //choose this for TSUN TSOL-M800 also
bool MI1500 = 1;
#define NRofPV 3 //<<<<<<<<<<<< number of PV panels in use (connected) 1-4. (not the number of WR-ports)
//assume the ports are connected in order from 1 to 4
//we have to know how many PVs are really connected to WR.
//if we know it, we dont have to wait of all ports!

// Webserver ..................................,,,,,,...........................................
IPAddress ROUTER = IPAddress(192,168,1,1); // your routers IP???
#define WEBSERVER_PORT 80

// Time Server .............................,,,,................................................
#define TIMESERVER_NAME "pool.ntp.org"
//#define TIMESERVER_NAME "fritz.box"

// MQTT ........................................................................................
const char MQTTbroker[] = "192.168.1.11";
int MQTTport = 1883;
char MQTTclientid[] = "MYDTU";

// send Tasmota compatible MQTT JSON instead of single topics
#define SENDJSON
static String JSON_TOPIC = String(MQTTclientid)+"/SENSOR";
// END OF YOUR CONFIGURATION ===================================================================


//=== define your ADVANCED configuration========================================================

bool DEBUG_RCV_DATA = 0; // output all rcv data on serial monitor
bool DEBUG_TX_DATA = 0; // output all tx data on serial monitor
bool CHECK_CRC = 1; //without crc, more data but may be wrong, must be checked
bool INTERRUPT = 0; //with or without interrupt
bool SNIFFER = 0; //as sniffer you just listen everything
bool ONLY_RX = 0; //nothing will be sent to inverter, only receive, if you have a dtu
//#define WITH_OTA // mit OTA Support, also update der Firmware über WLan mittels IP/update

#define DEFAULT_RF_DATARATE (RF24_250KBPS) // Datarate
uint8_t PA_LEVEL = RF24_PA_HIGH; // <<<<<<<<<<<<<<<<<<<<<<< define PA LEVEL on NRF24

#define milisek 1000
#define IPServicesUPDATETICK (15*milisek) //15sek
#define ZEXPUPDATETICK (60*milisek) //60sek update zeroexport
#define FIXLIMITTICK (ZEXPUPDATETICK*5) // 5 min to set the limit again
#define TXTIMER 700 //send request on every TXTIMER
#define TIMEOUTRXACK (10*milisek) //10sek RxAck timeout
#define TIMERPVCHECK (120*milisek) //60sek PV check timeout

// OTA ................................................................................................
#ifdef WITH_OTA
// OTA Einstellungen
#define UPDATESERVER_PORT WEBSERVER_PORT+1
#define UPDATESERVER_DIR "/update" // mittels IP:81/update kommt man dann auf die OTA-Seite
#define UPDATESERVER_USER "username_für_OTA" // <<<<<<<<<<<<<<<<<<<<<<< anpassen
#define UPDATESERVER_PW "passwort_für_OTA" // <<<<<<<<<<<<<<<<<<<<<<< anpassen
#endif

// globals --DO NOT CHANGE ===========================================================================
static String STARTTIME="xx";



union longlongasbytes {
uint64_t ull;
uint8_t bytes[8];
};

uint64_t Serial2RadioID (uint64_t sn) {
//----------------------------------
longlongasbytes llsn;
longlongasbytes res;
llsn.ull = sn;
res.ull = 0;
res.bytes[4] = llsn.bytes[0];
res.bytes[3] = llsn.bytes[1];
res.bytes[2] = llsn.bytes[2];
res.bytes[1] = llsn.bytes[3];
res.bytes[0] = 0x01;
return res.ull;
}

// DTU&WR =====================================================================================
uint64_t WR1_RADIO_ID = Serial2RadioID (SerialWR);
#define DUMMY_RADIO_ID ((uint64_t)0xDEADBEEF01ULL)
#define DTU_RADIO_ID MY_DTU_PRO // <<<<<<<<<<<<<<<<<<<<<<< secrets.h

char MIWHAT[10];

// zeroexport ===================================================================================
#define PVPOWER 350 //each PV
int MAXPOWER = NRofPV * PVPOWER; //for zeroexport
int MINPOWER = int(MAXPOWER / 10); //watt (10%) under this is the WR off
#define TOLERANCE 15 //watt


// WIFI ===============================================================================================
// PREFIXE dienen dazu, die eigenen WLans (wenn mehrere) vonfremden zu unterscheiden
// gehe hier davon aus, dass alle WLans das gleiche Passwort haben. Wenn nicht, dann mehre Passwörter hinterlegen
#define SSID_PREFIX1 MY_SSID // <<<<<<<<<<<<<<<<<<<<<<< secrets.h
//#define SSID_PREFIX2 "wlan2-Prefix" // <<<<<<<<<<<<<<<<<<<<<<< anpassen
#define SSID_PASSWORD MY_WIFIPW // <<<<<<<<<<<<<<<<<<<<<<< secrets.h

// Geo location ========================================================================================
#define geoBreite MY_BREITE // <<<<<<<<<<<<<<<<<<<<<<< secrets.h
#define geoLaenge MY_LAENGE // <<<<<<<<<<<<<<<<<<<<<<< secrets.h

static bool istTag = true;

#endif

0 comments on commit fd499d7

Please sign in to comment.