/**
* DTZ Logger
* Version 3.8
* 18.02.2022 P. Rebesky
* author Creator P.Rebesky
* Copyright (©): 2020-2022 by Peter Rebesky
* This code can use in private cases only. Every business or companies using of this codes or codes parts is required an approval of us (me)
* Every private using can exchange some parts of code or modify some code-lines. This code is allowed change for private use only.
* This software is basicly owned by Peter Rebesky and any comercial using is forbidden without approval of us (me).
**/
#include //ESP8266 Core WiFi Library
#include //ESP8266 Core WiFi Library
#include
#include //Local WebServer
#include "configDTZ.h"
#include "strings.h"
#include // LittleFS is declared
#include
#include
#include // https://github.com/tzapu/WiFiManager
#include
#include
ADC_MODE(ADC_VCC);
#define BoardTaster 0
#define LED 14
#define Receive485 12
#define ON 1
#define OFF 0
#define FLASHTIME 300
#define MESZ 0 // summer-time = 1 else winter-time = 0
#define MaxBuffer 700 // maximum buffer for received bytes
#define polynom 0x1021 // x16 + x12 + x5 + 1 0x1021
#define TimeOf2021 1609459260
#define Watt 0x1b
#define Watt/h 0x1e
#define var 0x1d
#define var/h 0x20
#define Ampere 0x21
#define Voltage 0x23
#define Herz 0x2c
unsigned int year = 0; byte month = 0; int day = 0; int years=0;
byte hour = 0; byte minute =0; byte second =0;
// int showProtocol = 1;
int ShowProtocolTime = 0;
uint32_t timestamp = 0;
uint32_t timeDummy = 0;
uint32_t SecOfYear =0;
uint32_t ProtocolCounter =0;
byte oldsecond = 0;
uint32_t previousMillis = 0;
uint32_t secondMillis =0;
String act_time;
String act_date;
String TransmitResult;
String MeterType = NOT_DETECT;
const long utcOffsetInSeconds = 3600;
uint32_t FromGridS = 0; uint32_t FromGridA = 0; uint32_t FromGridB = 0;
uint32_t IntoGrid = 0;
int U_L1 =0; int U_L2 =0; int U_L3 =0;
int I_L1 =0; int I_L2 =0; int I_L3 =0;
int Frequency = 0; int Consumtion = 0; float DispValue; int dtznumber = 0;
int ValueScale;
uint32_t meterID0=0; uint32_t meterID1=0; uint32_t meterID2=0;
uint16_t crc_ccitt = 0xffff;
uint16_t crc = 0xffff;
int x=0;
int errorCounter = 0;
int pointer = 0;
int singleValue = 0;
uint32_t value0 = 0;
uint32_t value1 = 0;
uint32_t value2 = 0;
uint32_t valueD = 0;
char dummy0 = 0;
char dummy1 = 0;
int flashCounter = 0;
int zaehler =0;
int pointerEOT = MaxBuffer-2;
int sendProtocols = 0;
bool sendingOK = false;
char receivedByte[2];
char extractValue[14];
char dataDTZ[MaxBuffer];
WiFiManager wm; // global wm instance
//WiFiManagerParameter custom_field; // global param ( for non blocking w params )
WiFiServer server(80);
ESP8266WiFiMulti WiFiMulti;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
configDTZ cDTZ;
//*********************** use WIFI manager by Tzapu https://github.com/tzapu/WiFiManager ************************************
bool setupWIFI() {
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
pinMode(LED, OUTPUT); // set as output
digitalWrite(LED,ON); // show setup-mode on LED
delay(1000);
std::vector menu = {"wifi","info","sep","restart","exit"};
wm.setMenu(menu);
// set dark theme
wm.setClass("invert");
wm.setConfigPortalTimeout(500); // auto close configportal after n seconds
checkButton(); // if pressed then reset WIFI config to start new change ssid
bool res;
res = wm.autoConnect("StromLog_Setup"); // anonymous ap, first parameter is name of access point
if(!res) {
return false;
}
else {
return true;
}
}
//************************* press button for set a new SSID **********************************
void checkButton(){
// check for button press
if ( digitalRead(BoardTaster) == LOW ) {
// poor mans debounce/press-hold
delay(50);
if( digitalRead(BoardTaster) == LOW ){
digitalWrite(LED,OFF); // LED OFF
delay(9000); // reset delay hold
if( digitalRead(BoardTaster) == LOW ){
digitalWrite(LED,ON); // show button pressed on LED
wm.resetSettings();
delay(1000);
ESP.restart();
}
}
}
}
//*************************** function by WIFI manager ********************************
String getParam(String name){
//read parameter from server, for customhmtl input
String value;
if(wm.server->hasArg(name)) {
value = wm.server->arg(name);
}
return value;
}
//*************************** function by WIFI manager ********************************
void saveParamCallback(){
Serial.println("[CALLBACK] saveParamCallback fired");
Serial.println("PARAM customfieldid = " + getParam("customfieldid"));
}
//*************************** function for get time & date as timestamp ********************************
bool syncron_NTP_Time() {
timeClient.begin();
if(timeClient.update()) {
timeDummy = timeClient.getEpochTime();
if(timeDummy !=0){
timestamp = timeDummy + (MESZ * 3600); // 3600 means add 1 hour
return true;
}
}
return false;
}
//*************************** create a string from values for data and time ********************************
void getTimeDate2String(){
act_time = "";
act_date = "";
if(hour < 10) act_time += "0"; // if value < 10 then show a zero before
act_time += String(hour); //combine string for time
act_time += ":";
if(minute < 10) act_time += "0";
act_time += String(minute);
act_time += ":";
if(second < 10) act_time += "0";
act_time += String(second);
if (day < 10) act_date += "0"; // if value < 10 then show a zero before
act_date += String(day); //combine string for time
act_date += ".";
if(month < 10) act_date += "0";
act_date += String(month);
act_date += ".";
if(year < 10) act_date += "0";
act_date += String(year);
}
//**************************** intern clock *******************************
bool internClock(uint32_t currentMillis) {
if (currentMillis < secondMillis) secondMillis = 0; // handle if currentMillis are overflows, max 32 bits
if (currentMillis - secondMillis >=1000){
secondMillis = currentMillis;
second++;
timestamp++;
if (second >=60){ second=0, minute++;}
if (minute >=60){ minute=0, hour++;}
if (hour >=24){ hour=0; day++;}
return true;
}
else return false;
}
//***************************** show DTZ-Data on WEB-Browser ******************************
void check4Client() {
// Check if a client has connected
String argument;
String PostValues;
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
unsigned long timeout = millis() + 3000;
while (!client.available() && millis() < timeout) {
delay(1);
}
if (millis() > timeout) {
client.flush();
client.stop();
return;
}
// Read the first line of the reply from server and print it to Serial and copy it into variable "argument"
if(client.available()){
String methode;
String line;
methode = client.readStringUntil(' ');
if (methode == "GET") line = client.readStringUntil('\n');
else {
line = client.readString();
int postBegin = line.indexOf("\r\n\r\n");
PostValues = line.substring(postBegin+4, line.length());
}
int argEnd = line.indexOf("HTTP");
argument = line.substring(0, argEnd-1);
// Match the request
client.flush();
String s;
if (argument == "/save") {
cDTZ.savePostValues(PostValues);
s = HTTP_ANSWER;
s += HTTP_HEAD_DTZ;
s += ""; s += cDTZ.getNameDTZ(); s += " ";
s += HTTP_BODYSTYLE_DTZ; s += "";
s += HTTP_DIV_FIELD;
s += "Ihre Eingaben wurden gespeichert.
";
s += "
";
s += "\n";
}
else if (argument == "/setup?") {
s = HTTP_ANSWER;
s += HTTP_HEAD_DTZ;
s += ""; s += cDTZ.getNameDTZ(); s += " ";
s += HTTP_BODYSTYLE_DTZ; s += "";
s += HTTP_DIV_FIELD;
s += "Grundeinstellungen
";
s += "";
s += " StromLog-Version: "; s += LoggerVersion;
s += "Hilfe & Impressum
";
s += "\n";
}
else if (argument == "/showsetup") {
s = HTTP_ANSWER;
s += "none";
}
else if (argument == "/favicon.ico") {
s = HTTP_ANSWER;
s += "none";
}
else if (argument.substring(0,13) == "/searchLogger") {
s = HTTP_ANSWER;
int argBegin = argument.indexOf("?");
if (argBegin != -1){
int idEx = argument.substring(argBegin+1,argument.length()).toInt();
if(idEx == cDTZ.getUserID() || cDTZ.getUserID() == 0){
s += "{\"DTZLOGGER\":\"" + LoggerVersion + "\"}";
}
}
}
else if (argument == "/" || argument == "/back" || argument == "/back?") {
// Prepare the response
s = HTTP_ANSWER;
s += HTTP_HEAD_DTZ_REFRESH;
s += ""; s += cDTZ.getNameDTZ(); s += " ";
s += HTTP_BODYSTYLE_DTZ; s += "";
s += HTTP_DIV_FIELD;
s += "StromLog-Status
";
s += "Zeit: ";
s += "Datum: ";
s += "Stromlog-IP: ";
s += "
";
s += TransmitResult;
s += " ";
s += ""; s += cDTZ.getNameDTZ(); s += "
";
s += "Hersteller-ID: ";
s += "Zähler-ID: ";
s += "Bezug in kWh: ";
s += "Einspeisung in kWh: ";
s += "Summen-Leistung: ";
if(U_L1>0 || U_L2>0 || U_L3>0){
s += "Spannung L1: ";
s += "Spannung L2: ";
s += "Spannung L3: ";
s += "Strom L1: ";
s += "Strom L2: ";
s += "Strom L3: ";
s += "Netzfrequenz: ";
}
s += "Received protocols: ";
if (ProtocolCounter == 0) ProtocolCounter++; // if zero then exeption!!! easy way solution add 1
s += "Protocol errors: ";
if(sendingOK == false)s += "🔴"; // show a red bubble when StromLog not connect on server at the first time
s += "Saved protocols:"; s += " ";
s += "
";
s += HTTP_SETUP_BUTTON;
s += "Hilfe & Impressum
";
s += "\n";
}
else if (argument == "/memory") {
s = HTTP_ANSWER;
s += "Free space: "; s += cDTZ.FREESPACE; s += " ";
if(cDTZ.saveCounter > 0){
for(int i=1;i<=cDTZ.saveCounter;i++){
s += cDTZ.readSavedValues(i);
s += " ";
}
}
else s += "No saved data. ";
s += "