-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfarmer.ino
297 lines (260 loc) · 9.59 KB
/
fabfarmer.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
========================================================================================
FabFarmer Version 1.5 by JTL / thirsch
FabLab-Bayreuth e.V.
fablab-bayreuth.de
Configurator Engine based on
ESP_WebConfig Latest version: 1.1.3 - 2015-07-20
Special thanks to John Lassen
========================================================================================
Hardware:
* Wemos D1 mini (ESP-8266EX),
* LM393 based soil moisture sensor
Pin map:
D5 GND of sensor
D6 VCC of sensor
D0 digital out of sensor
A0 analog out of sensor
========================================================================================
Board: Wemos D1 Mini
(Add http://arduino.esp8266.com/stable/package_esp8266com_index.json to your moard manager path)
========================================================================================
Libraries:
ESPAsyncTCP (https://github.com/me-no-dev/ESPAsyncTCP)
ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
Thinger.io
========================================================================================
Notes for programming the ESP8266:
There is a cooperative task scheduler running on this device. The user's loop() function
is only one among other task. In order to give execution time to other tasks, return from
the loop() function, or call delay(), eg. delay(0). If the user task blocks for too long,
network connections may break off.
========================================================================================
*/
#include <Arduino.h>
#include <Esp.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESPAsyncWebServer.h>
#include <Ticker.h>
#include <EEPROM.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <ThingerWifi.h>
#include "helpers.h"
#include "global.h"
#define PGNV "1.5"
#define ACCESS_POINT_NAME "FabFarmer"
#define ACCESS_POINT_PASSWORD "12345678"
#define AdminTimeOut 600 // Defines the Time in Seconds, when the Admin-Mode will be diabled
/*
Include the HTML, STYLE and Script "Pages"
*/
#include "Page_Root.h"
#include "Page_Admin.h"
#include "Page_Script.js.h"
#include "Page_Chart.h"
#include "Page_Style.css.h"
#include "Page_NTPSettings.h"
#include "Page_Information.h"
#include "Page_General.h"
#include "PAGE_NetworkConfiguration.h"
#include "PAGE_FabFarmer.h"
#include "DNSServer.h"
#include "CaptiveRequestHandler.h"
#include "SoilMoistureSensor.h"
const byte DNS_PORT = 53;
DNSServer dnsServer;
const char *myHostname = "fabfarmer";
SoilMoistureSensor s1(A0, D1, D6, D5);
void setup ( void ) {
EEPROM.begin(512);
Serial.begin(115200);
delay(500);
pinMode(LED_BUILTIN, OUTPUT);
Serial.printf("Starting FabFarmer %s\n", PGNV);
IPAddress apIp(192, 168, 4, 1);
/* Setup the DNS server redirecting all the domains to the apIP */
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(DNS_PORT, "*", apIp);
if (!ReadConfig())
{
// DEFAULT CONFIG
config.ssid = "myssid";
config.password = "mypassword";
config.dhcp = true;
config.IP[0] = 192; config.IP[1] = 168; config.IP[2] = 1; config.IP[3] = 100;
config.Netmask[0] = 255; config.Netmask[1] = 255; config.Netmask[2] = 255; config.Netmask[3] = 0;
config.Gateway[0] = 192; config.Gateway[1] = 168; config.Gateway[2] = 1; config.Gateway[3] = 1;
config.ntpServerName = "0.de.pool.ntp.org";
config.Update_Time_Via_NTP_Every = 1;
config.timezone = 10;
config.daylight = true;
config.DeviceName = "Not Named FabFarmer";
config.IoTOn = false;
config.IoTUserName = "Not Named IoT User";
config.IoTDeviceID = "Not Named IoT ID";
config.IoTCredential = "Not Named IoT Credential";
config.AutoTurnOff = false; // currently not used
config.AutoTurnOn = false;
config.TurnOffHour = 0;
config.TurnOffMinute = 0;
config.TurnOnHour = 0;
config.TurnOnMinute = 0;
config.SensCalMin = 60;
config.SensCalMax = 900;
config.SensCalc = 100;
config.InfoOn = false; // currently not used -> LED
config.LEDOn = true; // enable switch LED on
config.SensRefreshTime = 1;
config.MinWarn = 0; // currently not used
WriteConfig();
Serial.println("General config applied");
}
s1.begin(config.SensCalMin, config.SensCalMax, config.SensCalc);
char buffer[33];
sprintf(buffer, "%s_%06x", ACCESS_POINT_NAME, ESP.getChipId());
if (AdminEnabled)
{
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(buffer, ACCESS_POINT_PASSWORD);
}
else
{
WiFi.mode(WIFI_STA);
}
ConfigureWifi();
server.addHandler(new CaptiveRequestHandler(myHostname)).setFilter(ON_AP_FILTER);
server.on ( "/", processFabFarmer );
server.on ( "/admin/filldynamicdata", filldynamicdata );
server.on ( "/favicon.ico", [](AsyncWebServerRequest *request) {
Serial.println("favicon.ico");
AsyncWebServerResponse *response = request->beginResponse(200, "text/html", "");
sendCacheHeader(response);
request->send(response);
} );
server.on ( "/admin.html", [](AsyncWebServerRequest *request) {
Serial.println("admin.html");
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_AdminMainPage);
sendCacheHeader(response);
request->send(response);
} );
server.on ( "/config.html", send_network_configuration_html );
server.on ( "/info.html", [](AsyncWebServerRequest *request) {
Serial.println("info.html");
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_Information);
sendCacheHeader(response);
request->send(response);
} );
server.on ( "/ntp.html", send_NTP_configuration_html );
server.on ( "/general.html", send_general_html );
server.on ( "/fabfarmer.html", [](AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_FabFarmer);
sendCacheHeader(response);
request->send(response);
} );
server.on ( "/style.css", [](AsyncWebServerRequest *request) {
Serial.println("style.css");
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", style_css_gz, sizeof(style_css_gz));
response->addHeader("Content-Encoding", "gzip");
sendCacheHeader(response);
request->send(response);
} );
server.on ( "/microajax.js", [](AsyncWebServerRequest *request) {
Serial.println("microajax.js");
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", microajax_js_gz, sizeof(microajax_js_gz));
response->addHeader("Content-Encoding", "gzip");
sendCacheHeader(response);
request->send(response);
} );
server.on ( "/chart.min.js", [](AsyncWebServerRequest *request) {
Serial.println("chart.min.js");
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", chart_js_gz, sizeof(chart_js_gz));
response->addHeader("Content-Encoding", "gzip");
sendCacheHeader(response);
request->send(response);
} );
server.on ( "/admin/values", send_network_configuration_values_html );
server.on ( "/admin/connectionstate", send_connection_state_values_html );
server.on ( "/admin/sensrefreshtimes", send_information_values_html );
server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
server.on ( "/admin/generalvalues", send_general_configuration_values_html);
server.on ( "/admin/devicename", send_devicename_value_html);
server.onNotFound(handleNotFound);
server.begin();
Serial.println( "HTTP server started" );
tkSecond.attach(1, Second_Tick);
UDPNTPClient.begin(2390); // Port for NTP receive
ConfigureIoT();
WiFi.scanNetworks(true);
}
void loop ( void ) {
// Als erstes wieder eine evtl. neue Seite "bereitstellen", danach werden ggf. Admin abgeschaltet, NTP aktualisiert etc.
dnsServer.processNextRequest();
if (AdminEnabled)
{
if (AdminTimeOutCounter > AdminTimeOut)
{
AdminEnabled = false;
Serial.println("Admin Mode disabled!");
WiFi.mode(WIFI_STA);
}
}
if (config.Update_Time_Via_NTP_Every > 0 )
{
if (syncNtp) {
if (NTPRefresh() > 0) {
syncNtp = false;
}
}
if ((cNTP_Update > 5 && firstStart) || cNTP_Update > (config.Update_Time_Via_NTP_Every * 60))
{
syncNtp = true;
NTPRefresh();
cNTP_Update = 0;
firstStart = false;
}
}
if (DateTime.minute != Minute_Old)
{
Minute_Old = DateTime.minute;
if (config.AutoTurnOn)
{
if (DateTime.hour == config.TurnOnHour && DateTime.minute == config.TurnOnMinute)
{
Time_On = true;
Serial.println("Enabled FabFarmer IoT Communication");
}
if (DateTime.hour == config.TurnOffHour && DateTime.minute == config.TurnOffMinute)
{
Time_On = false;
Serial.println("Disabled FabFarmer IoT Communication");
}
}
}
if (config.SensRefreshTime > 0 && (cSens_Update > config.SensRefreshTime * 60 || Sens_Value == -1))
{
Sens_Value = s1.readProbe();
db.push(Sens_Value);
Serial.println("Read sensor value - start");
Serial.println(Sens_Value);
cSens_Update = 0;
}
if (Time_On && config.IoTOn && thing && WiFi.status() == WL_CONNECTED)
{
thing->handle();
}
if (Refresh)
{
Refresh = false;
///Serial.println("Refreshing...");
//Serial.printf("FreeMem:%d %d:%d:%d %d.%d.%d \n",ESP.getFreeHeap() , DateTime.hour,DateTime.minute, DateTime.second, DateTime.year, DateTime.month, DateTime.day);
}
if(WiFi.status() == WL_CONNECTED) {
cWiFi_Connect = 0;
} else if(cWiFi_Connect > 10 && WiFi.status() == WL_NO_SSID_AVAIL) {
Serial.println("WiFi-Data wrong? Stop trying to connect!");
WiFi.disconnect();
}
blinkLED();
}