-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSON_vBus_Nextion_ESP8266.ino
191 lines (176 loc) · 6.27 KB
/
JSON_vBus_Nextion_ESP8266.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <LittleFS.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ModbusIP_ESP8266.h>
#include <Arduino.h>
#define ARDUINOJSON_DECODE_UNICODE 1
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Nextion.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <simpleDSTadjust.h>
#include <time.h>
#include <WiFiClientSecure.h>
#define DEBUG_LED
#define DEBUG_SERIAL_ENABLE
#define DEBUG_NETATMO
//#define DEBUG_vBUS
//#define DEBUG_BME280
//#define DEBUG_NTP
//#define DEBUG_WIFI
//#define DEBUG_OTA
//#define DEBUG_MQTT
//#define DEBUG_SOLAREDGE
// Konfiguration Scheduler
unsigned long ms1000 = 1000; // 1 Sekunden Takt
unsigned long msold1000;
unsigned long ms15000 = 15000; // 15 Sekunden Takt
unsigned long msold15000;
unsigned long ms30000 = 30000; // 30 Sekunden Takt
unsigned long msold30000;
unsigned long ms300000 = 300000; // 5 Minuten Takt
unsigned long msold300000;
unsigned long ms900000 = 900000; // 15 Minuten Takt
unsigned long msold900000;
int aktuelle_cTest = 0;
// Konfiguration WiFi
WiFiClient client;
// Konfiguration JSON
char cNexadresse [40]; // Server Adresse (kommt von Nextion Display)
char resource [] = "/dlx/download/live?sessionAuthUsername=admin&sessionAuthPassword=xxxxxx"; //Server Adresse
const unsigned long HTTP_TIMEOUT = 6000; // Maximale Antwortzeit Server
const size_t MAX_CONTENT_SIZE = 9000; // max size of the HTTP response
//Einstellung Modbus MASTER für SolarEDGE Slave
IPAddress remote(192, 168, 1, 26); // Address of Modbus Slave device
ModbusIP mb; //ModbusIP object
int status = 0;
uint16_t power_inverter = 0;
uint16_t power_inverter_scale = 0;
uint16_t power_meter = 0;
uint16_t power_meter_scale = 0;
int16_t power_house = 0;
// Adafruit MQTT Setup
#define AIO_SERVER ""
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME ""
#define AIO_KEY ""
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Subscribe Zisterne_Liter = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/zisterne-liter");
Adafruit_MQTT_Publish Zisterne_Liter_get = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/zisterne-liter/get");
// Konfiguration Deklariere Nextion
// RealTimeClock
NexRtc rtc;
//Einstellungen
NexVariable NexSSID = NexVariable(0, 3, "Startseite.SSID");
NexVariable Nexpass = NexVariable(0, 3, "Startseite.pass");
NexVariable Nexadresse = NexVariable (0, 3, "Startseite.adresse");
NexVariable NexTemperatur = NexVariable (0, 3, "Startseite.Temperatur");
NexVariable NexZeit = NexVariable (0, 3, "Startseite.Zeit");
NexVariable Seite = NexVariable(0, 57, "Startseite.Seite");
unsigned int N_Seite = 0;
//1 Sekunden Alive LED
const int ledred = LED_BUILTIN;
int ledStatered = LOW;
const int ledblue = 2;
int ledStateblue = HIGH;
void setup() {
initSerial(); // Start Serielle Schnittstelle
pinMode(ledred, OUTPUT); // Keep Alive LED
pinMode(ledblue, OUTPUT); // Keep Alive LED
#ifndef DEBUG_LED
digitalWrite(ledred, HIGH); //Alive LED
digitalWrite(ledblue, HIGH); //Alive LED
#endif
//LittleFS.format();
if (!LittleFS.begin()) {
#ifdef DEBUG_NETATMO
Serial.println("Nutzung ESP8266 Datei System fehlgeschlagen");
#endif
return;
}
initBME280(); // Start Temperatursensor
delay(1000); // Pause damit Nextion hochfährt
nexInit(); // Start Nextion Display
delay(1000); // Pause Nextion
readBME280(); // einmaliges lesen Temperatursensor
initvBUS(); // Start JSON - holen der Adresse von Nextion Display
initWifi(); // Start WiFi
Lese_Einstellungen_Netatmo(); //Access Token für Netatmo laden
mb.client(); // Modbus Init
readSolarEdge(); // SolarEdge
initOTA(); // init OTA Update - Webserver
updateNTP(); // einmaliges synchronisieren der RTC von Nexton Display
readvBUS(); // einmaliges lesen vBUS Daten
mqtt.subscribe(&Zisterne_Liter); //Auswählen der MQTT Daten
MQTT(); // Abfragen der MQTT Daten
int ret = readNetatmo(); //Abfrage Daten Netatmo
readSolarEdge(); // SolarEdge
}
void loop() {
OTA();
unsigned long currentMillis = millis();
#ifdef DEBUG_LED
if (currentMillis - msold1000 >= ms1000) { //1 Sekunden Schleife - aktuell nur für LED
msold1000 = currentMillis;
if (ledStatered == LOW) {
ledStatered = HIGH;
}
else
{
ledStatered = LOW;
}
if (ledStateblue == HIGH) {
ledStateblue = LOW;
}
else
{
ledStateblue = HIGH;
}
digitalWrite(ledred, ledStatered); //Alive LED
digitalWrite(ledblue, ledStateblue); //Alive LED
}
#endif
if (currentMillis - msold15000 >= ms15000) { //15 Sekunden Schleife - aktuell BME280, vBUS, SolarEdge
msold15000 = currentMillis;
Seitenabfrage ();
readBME280(); // lesen Temperatursensor
readvBUS(); // lesen vBUS Daten
readSolarEdge(); // SolarEdge
}
if ((currentMillis - msold30000 >= ms30000)) { //30 Sekunden Schleife - aktuell nur für DEBUG
msold30000 = currentMillis;
#ifdef DEBUG_SERIAL_ENABLE
Serial.print("\nFreeHeap: ");
Serial.print(ESP.getFreeHeap());
Serial.print(" HeapFragmentation: ");
Serial.print(ESP.getHeapFragmentation());
Serial.print(" MaxFreeBlockSize: ");
Serial.println(ESP.getMaxFreeBlockSize());
Serial.println("\n30 Sekunden Schleife");
#endif
}
if ((currentMillis - msold300000 >= ms300000)) { //5 Minuten Schleife
msold300000 = currentMillis;
#ifdef DEBUG_SERIAL_ENABLE
Serial.println("");
Serial.println("5 Minuten Schleife");
#endif
Seitenabfrage ();
int ret = readNetatmo();
}
if ((currentMillis - msold900000 >= ms900000)) { //15 Minuten Schleife
msold900000 = currentMillis;
#ifdef DEBUG_SERIAL_ENABLE
Serial.println("");
Serial.println("15 Minuten Schleife");
#endif
Seitenabfrage ();
MQTT();
}
}