-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path0_SETUP.ino
151 lines (137 loc) · 5.69 KB
/
0_SETUP.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
void setup()
{
Serial.begin(115200);
// Serial.begin(9600); // Debug Nextion SoftwareSerial
#ifdef ESP32
snprintf(mqtt_clientid, maxHostSign, "ESP32-%llX", ESP.getEfuseMac());
Serial.printf("\n*** SYSINFO: MQTTDevice32 ID: %X\n", mqtt_clientid);
// WLAN Events
WiFi.onEvent(WiFiEvent);
WiFiEventId_t eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info)
{ log_e("WiFiStationDisconnected reason: %d", info.wifi_sta_disconnected.reason); },
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
#elif ESP8266
snprintf(mqtt_clientid, maxHostSign, "ESP8266-%08X", ESP.getChipId());
Serial.printf("\n*** SYSINFO: start up MQTTDevice - device ID: %s\n", mqtt_clientid);
// WLAN Events
wifiConnectHandler = WiFi.onStationModeGotIP(EM_WIFICONNECT);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(EM_WIFIDISCONNECT);
#endif
wifiManager.setDebugOutput(false);
wifiManager.setMinimumSignalQuality(10);
wifiManager.setConfigPortalTimeout(300);
wifiManager.setAPCallback(configModeCallback);
wifiManager.setSaveConfigCallback(saveConfigCallback);
WiFiManagerParameter p_hint("<small>*Connect your MQTTDevice to WLAN. When connected open http://mqttdevice.local in your brower</small>");
wifiManager.addParameter(&p_hint);
wifiManager.autoConnect(nameMDNS);
wifiManager.setWiFiAutoReconnect(true);
WiFi.mode(WIFI_STA);
#ifdef ESP32
WiFi.setSleep(false);
#elif ESP8266
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#endif
WiFi.persistent(true);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
// Lade Dateisystem
if (LittleFS.begin())
{
Serial.printf("*** SYSINFO: setup LittleFS free heap: %d\n", ESP.getFreeHeap());
// Starte NTP
timeClient.begin();
timeClient.forceUpdate();
if (!timeClient.isTimeSet())
timeClient.setPoolServerName(NTP_ADDRESS); // Fallback default ntp.org
checkSummerTime();
// Prüfe WebUpdate
if (LittleFS.exists(UPDATESYS))
updateSys();
if (LittleFS.exists(UPDATETOOLS))
updateTools();
// webUpdate log
if (LittleFS.exists(LOGUPDATESYS) || LittleFS.exists(LOGUPDATETOOLS))
EM_LOG();
// Erstelle Ticker Objekte
setTicker();
// ISR
#ifdef ESP32
gpio_install_isr_service(0);
#endif
// Starte Sensoren
DS18B20.begin();
pins_used[ONE_WIRE_BUS] = true;
if (LittleFS.exists(CONFIG)) // Lade Konfiguration
loadConfig();
else
Serial.println("*** SYSINFO: config file config.txt missing. Load defaults ...");
// Starte MQTT
EM_MQTTCONNECT();
EM_MQTTSUBSCRIBE();
TickerPUBSUB.start(); // Ticker PubSubClient
}
else
Serial.println("*** SYSINFO: error - cannot mount LittleFS!");
// Starte Webserver
setupServer();
// Starte mDNS
if (startMDNS)
EM_MDNSET();
else
Serial.printf("*** SYSINFO: ESP8266 IP address: %s Time: %s RSSI: %d\n", WiFi.localIP().toString().c_str(), timeClient.getFormattedTime().c_str(), WiFi.RSSI());
}
void setupServer()
{
server.on("/", handleRoot);
server.on("/index.htm", handleRoot);
server.on("/index", handleRoot);
server.on("/index.html", handleRoot);
server.on("/setupActor", handleSetActor); // Einstellen der Aktoren
server.on("/setupSensor", handleSetSensor); // Einstellen der Sensoren
server.on("/reqSensors", handleRequestSensors); // Liste der Sensoren ausgeben
server.on("/reqActors", handleRequestActors); // Liste der Aktoren ausgeben
server.on("/reqInduction", handleRequestInduction);
server.on("/reqSearchSensorAdresses", handleRequestSensorAddresses);
server.on("/reqPins", handlereqPins); // GPIO Pins actors
server.on("/reqIndu", handleRequestIndu); // Induction für WebConfig
server.on("/setSensor", handleSetSensor); // Sensor ändern
server.on("/setActor", handleSetActor); // Aktor ändern
server.on("/setIndu", handleSetIndu); // Indu ändern
server.on("/delSensor", handleDelSensor); // Sensor löschen
server.on("/delActor", handleDelActor); // Aktor löschen
server.on("/reboot", EM_REBOOT); // reboots the whole Device
server.on("/reqMisc", handleRequestMisc); // Misc Infos für WebConfig
server.on("/reqMisc2", handleRequestMisc2); // Misc Infos für WebConfig
server.on("/reqMiscAlert", handleRequestMiscAlert); // Misc Infos für WebConfig
server.on("/reqFirm", handleRequestFirm); // Firmware version
server.on("/setMisc", handleSetMisc); // Misc ändern
server.on("/startHTTPUpdate", startHTTPUpdate); // Firmware WebUpdate
server.on("/channel", handleChannel); // Server Sent Events will be handled from this URI
server.on("/startSSE", startSSE); // Server Sent Events will be handled from this URI
server.on("/checkAliveSSE", checkAliveSSE); // Server Sent Events check IP on channel
server.on("/language", handleGetLanguage);
server.on("/title", handleGetTitle);
server.on("/reqSys", handleReqSys);
// FSBrowser initialisieren
server.on("/edit", HTTP_GET, handleGetEdit);
server.on("/status", HTTP_GET, handleStatus);
server.on("/list", HTTP_GET, handleFileList);
server.on("/edit", HTTP_PUT, handleFileCreate);
server.on("/edit", HTTP_DELETE, handleFileDelete);
server.on(
"/edit", HTTP_POST, []()
{ server.send(200, "text/plain", ""); },
handleFileUpload);
server.on(
"/restore", HTTP_POST, []()
{ server.send(200, FPSTR("text/plain"), "ok"); },
handleRestore);
server.onNotFound(handleAll);
#ifdef ESP32
httpUpdateServer.setup(&server); // DateiUpdate
#elif ESP8266
httpUpdate.setup(&server);
#endif
server.begin();
}