forked from jp112sdl/WemosD1_HomeMatic_StatusDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWemosD1_HomeMatic_StatusDisplay.ino
235 lines (200 loc) · 6.06 KB
/
WemosD1_HomeMatic_StatusDisplay.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <ArduinoJson.h>
#include <ArduinoOTA.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FastLED.h>
#include <FS.h>
#include <WiFiUdp.h>
#include "WM_Custom.h"
#include "global_css.h"
#include "webConfigHTML.h"
#define SERIALDEBUG
//#define WM_DEBUG_OUTPUT
//#define UDPDEBUG
#define CONFIG_PIN D1
#define PIR_PIN D5
#define WEBSERVER_PORT 80
#define UDPPORT 6690
#define HTTPGETTIMEOUT 2000 // 2 Sekunden Timeout für Anfragen an die CCU
#define KEYPRESSLONGMILLIS 1500 // ms für langen Tastendruck
#define KEYBOUNCEMILLIS 200 // ms Mindestzeit zwischen 2 Tastendrücken
#define KEYTOLERANCE 7 // +/-Toleranz für 16er Widerstands-/Tastermatrix
#ifdef UDPDEBUG
const char * SYSLOGIP = "192.168.1.251";
#define SYSLOGUDPPORT 514
#endif
enum _SyslogSeverity {
_slEmergency,
_slAlert,
_slCritical,
_slError,
_slWarning,
_slNotice,
_slInformational,
_slDebug
};
// FastLED
#define LED_PIN D7
#define LED_TYPE WS2812
#define MAX_LEDS 255
struct led_t {
CRGB leds[MAX_LEDS];
CRGB ledsBackup[MAX_LEDS];
int Colors[MAX_LEDS];
bool Blink[MAX_LEDS];
int Keys[16] {1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023, 1023};
} LEDConfig;
enum _EOrder {
_RGB = 0012,
_RBG = 0021,
_GRB = 0102,
_GBR = 0120,
_BRG = 0201,
_BGR = 0210
};
#define IP_SIZE 16
#define VARIABLE_SIZE 100
struct globalconfig_t {
byte LedBrightness = 255;
int SelectedEOrder = _RGB;
int NumLeds = 16;
char CcuIp[IP_SIZE];
char DeviceName[VARIABLE_SIZE];
bool RestoreStateFromCCU = false;
bool PIRtoCCU = false;
byte DimBlink = 11;
} GlobalConfig;
#define HEXARRAY_SIZE 24
#define COLOR_COUNT 10
String Dimmer2ColorDefinition[COLOR_COUNT] {
"00FF00", "F39C12", "FF0000", "FFFFFF", "0000FF", "00FF00", "F39C12", "FF0000", "FFFFFF", "0000FF"
};
enum DisplayStates {
Sleep,
Wake
};
ESP8266WebServer WebServer(WEBSERVER_PORT);
ESP8266HTTPUpdateServer httpUpdater;
//UDP
struct udp_t {
WiFiUDP UDP;
char incomingPacket[255];
} UDPClient;
//WifiManager - don't touch
byte configPortalTimeout = 180;
bool shouldSaveConfig = false;
String configJsonFile = "config.json";
char ip[IP_SIZE] = "0.0.0.0";
char netmask[IP_SIZE] = "0.0.0.0";
char gw[IP_SIZE] = "0.0.0.0";
boolean startWifiManager = false;
int DisplayTimeoutSeconds = 0;
unsigned long LastDisplayTimeOutMillis = 0;
unsigned long LastBlinkMillis = 0;
bool OTAStart = false;
bool DisplayState = Wake;
bool blinkState = false;
bool UDPReady = false;
volatile byte PIRInterruptDetected = 0;
bool ConfigKeyPress = false;
bool ConfigKeyPressLONG = false;
unsigned long ConfigKeyPressDownMillis = 0;
unsigned long LastMillisConfigKeyPress = 0;
void setup() {
pinMode(A0, INPUT);
pinMode(CONFIG_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
initPIR();
digitalWrite(LED_BUILTIN, HIGH);
#ifdef SERIALDEBUG
Serial.begin(115200);
#endif
DEBUG("Programmstart...");
if (digitalRead(CONFIG_PIN) == LOW) {
DEBUG("Taster gedrückt - Config Mode wird gestartet!");
startWifiManager = true;
bool state = LOW;
for (int i = 0; i < 7; i++) {
state = !state;
digitalWrite(LED_BUILTIN, state);
delay(100);
}
}
loadSystemConfig();
if (doWifiConnect()) {
DEBUG("WLAN erfolgreich verbunden!");
printWifiStatus();
} else ESP.restart();
startWebServer();
initUDPClient();
initLEDs();
showLedBootSequence();
startOTAhandling();
if (GlobalConfig.RestoreStateFromCCU)
getValuesFromCCU();
DEBUG(String(GlobalConfig.DeviceName) + " - Boot abgeschlossen, SSID = " + WiFi.SSID() + ", IP = " + String(IpAddress2String(WiFi.localIP())) + ", RSSI = " + WiFi.RSSI() + ", MAC = " + WiFi.macAddress(), "Setup", _slInformational);
}
void loop() {
if (LastDisplayTimeOutMillis > millis())
LastDisplayTimeOutMillis = millis();
if (LastBlinkMillis > millis())
LastBlinkMillis = millis();
ArduinoOTA.handle();
if (!OTAStart) {
WebServer.handleClient();
handleUDP();
handlePIR();
handleKEY();
if (DisplayTimeoutSeconds > 0 && DisplayState == Wake && millis() - LastDisplayTimeOutMillis > DisplayTimeoutSeconds * 1000) {
LastDisplayTimeOutMillis = millis();
DEBUG("Display Timeout", "loop()", _slInformational);
setLedMode(Sleep);
}
if (millis() - LastBlinkMillis > 1000 && DisplayState == Wake) {
LastBlinkMillis = millis();
blinkState = !blinkState;
for (int i = 0; i < GlobalConfig.NumLeds; i++) {
LEDConfig.ledsBackup[i] = LEDConfig.leds[i];
if (blinkState && LEDConfig.Blink[i]) {
LEDConfig.leds[i] = CRGB::Black;
}
}
FastLED.show();
for (int i = 0; i < GlobalConfig.NumLeds; i++) {
LEDConfig.leds[i] = LEDConfig.ledsBackup[i];
}
}
//Tasterbedienung CONFIG_PIN
if (digitalRead(CONFIG_PIN) == LOW) {
if (!ConfigKeyPress) {
ConfigKeyPressDownMillis = millis();
if (millis() - LastMillisConfigKeyPress > KEYBOUNCEMILLIS) {
LastMillisConfigKeyPress = millis();
ConfigKeyPress = true;
}
}
if ((millis() - ConfigKeyPressDownMillis) > KEYPRESSLONGMILLIS && !ConfigKeyPressLONG) {
//PRESS_LONG
DEBUG("ConfigKeyPress LONG", "loop()", _slInformational);
ResistorConfigKeys();
ConfigKeyPressLONG = true;
}
} else {
if (ConfigKeyPress) {
if ((millis() - ConfigKeyPressDownMillis) < KEYPRESSLONGMILLIS) {
int brightness = GlobalConfig.LedBrightness;
brightness = brightness + 25;
if (brightness > 255) brightness = 10;
DEBUG("ConfigKeyPress SHORT, setting brightness to " + String(brightness), "loop()", _slInformational);
setLedBrightness(brightness);
delay(10); //Entprellen
}
}
ConfigKeyPress = false;
ConfigKeyPressLONG = false;
}
delay(10); //wg. UDP
}
}