1 | /**
|
2 | * Temperature monitor for DS18B20 (Dallas) or similar one (like DS18S20)
|
3 | * supported 1 until 4 sonsors (more is possible by changing code-lines)
|
4 | * target: ESP8266, especially Kit-8 type
|
5 | * written by Peter Rebesky, February 2023
|
6 | * Copyright (©): 2023 by Peter Rebesky
|
7 | * 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)
|
8 | * Every private using can exchange some parts of code or modify some code-lines. This code is allowed change for private use only.
|
9 | * This software is basicly owned by Peter Rebesky and any comercial using is forbidden without approval of us (me).
|
10 | * Using for connect WIFI by WiFiManager (tzapu) https://github.com/tzapu/WiFiManager
|
11 | * and OLED driver written by http://stefanfrings.de/
|
12 | */
|
13 | #include <SPI.h>
|
14 | #include <Wire.h>
|
15 | #include <ESP8266WiFi.h> //ESP8266 Core WiFi Library
|
16 | #include <ESP8266WiFiMulti.h> //ESP8266 Core WiFi Library
|
17 | #include <DNSServer.h>
|
18 | #include <ESP8266WebServer.h> //Local WebServer
|
19 | #include <NTPClient.h>
|
20 | #include <WiFiUdp.h>
|
21 |
|
22 | #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
23 | #include <ESP8266HTTPClient.h>
|
24 | #include <ESP8266httpUpdate.h>
|
25 | #include <WiFiClient.h>
|
26 | #include <OneWire.h>
|
27 | #include <DallasTemperature.h>
|
28 |
|
29 | #include "global.h"
|
30 | #include "configFS.h"
|
31 | #include "WebClient.h"
|
32 |
|
33 | #ifdef OLED_CONNECT
|
34 | #include <oled.h> // OLED-Display driver, http://stefanfrings.de
|
35 | OLED display(Wire, 0x3C, 16, 128, 32);
|
36 | #endif
|
37 |
|
38 | WiFiManager wm; // global wm instance
|
39 | WiFiManagerParameter custom_field; // global param ( for non blocking w params )
|
40 | configFS Config;
|
41 | OneWire oneWire(_oneWireBus);
|
42 | DallasTemperature sensors(&oneWire);
|
43 | WiFiServer server(80);
|
44 | ESP8266WiFiMulti WiFiMulti;
|
45 | // Define NTP Client to get time
|
46 | WiFiUDP ntpUDP;
|
47 | NTPClient timeClient(ntpUDP, "pool.ntp.org", _UTCOFFSET);
|
48 |
|
49 | //***********************************************************
|
50 | bool setupWIFI() {
|
51 | WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
|
52 | delay(1000);
|
53 | Serial.println("\n Starting");
|
54 | pinMode(_BoardTaster, INPUT);
|
55 | // add a custom input field
|
56 | int customFieldLength = 40;
|
57 | const char* custom_radio_str = "<br/><label for='customfieldid'>Custom Field Label</label><input type='radio' name='customfieldid' value='1' checked> One<br><input type='radio' name='customfieldid' value='2'> Two<br><input type='radio' name='customfieldid' value='3'> Three";
|
58 | new (&custom_field) WiFiManagerParameter(custom_radio_str); // custom html input
|
59 |
|
60 | wm.addParameter(&custom_field);
|
61 | wm.setSaveParamsCallback(saveParamCallback);
|
62 |
|
63 | // custom menu via array or vector
|
64 | std::vector<const char *> menu = {"wifi","info","param","sep","restart","exit"};
|
65 | wm.setMenu(menu);
|
66 |
|
67 | // set dark theme
|
68 | wm.setClass("invert");
|
69 | wm.setConfigPortalTimeout(180); // auto close configportal after n seconds
|
70 | checkButton(); // if pressed then reset WIFI config to start new change ssid
|
71 | bool res;
|
72 | res = wm.autoConnect("Temp_WIFI_Setup"); // anonymous ap, first parameter is name of access point
|
73 | if(!res) {
|
74 | Serial.println("Failed to connect or hit timeout");
|
75 | return false;
|
76 | }
|
77 | else {
|
78 | //if you get here you have connected to the WiFi
|
79 | Serial.println("Connected to saved ssid successfully");
|
80 | return true;
|
81 | }
|
82 | }
|
83 | //***********************************************************
|
84 | void checkButton(){
|
85 | // check for button press
|
86 | if ( digitalRead(_BoardTaster) == LOW ) {
|
87 | // poor mans debounce/press-hold
|
88 | delay(50);
|
89 | if( digitalRead(_BoardTaster) == LOW ){
|
90 | #ifdef OLED_CONNECT
|
91 | buttonHoldOLED();
|
92 | #endif
|
93 | Serial.println("Button Pressed");
|
94 | // still holding button for 3000 ms, reset settings, code is not ideal
|
95 | delay(5000); // reset delay hold
|
96 | if( digitalRead(_BoardTaster) == LOW ){
|
97 | // Serial.println("Button Held");
|
98 | // Serial.println("Erasing config now");
|
99 | // Serial.println("Restarting now!");
|
100 | wm.resetSettings();
|
101 | delay(4000);
|
102 | ESP.restart();
|
103 | }
|
104 | }
|
105 | }
|
106 | }
|
107 | //***********************************************************
|
108 | String getParam(String name){
|
109 | //read parameter from server, for customhmtl input
|
110 | String value;
|
111 | if(wm.server->hasArg(name)) {
|
112 | value = wm.server->arg(name);
|
113 | }
|
114 | return value;
|
115 | }
|
116 | //***********************************************************
|
117 | void saveParamCallback(){
|
118 | Serial.println("[CALLBACK] saveParamCallback fired");
|
119 | Serial.println("PARAM customfieldid = " + getParam("customfieldid"));
|
120 | }
|
121 | //***********************************************************
|
122 |
|
123 | //*************************** function for get time & date as timestamp ********************************
|
124 | bool syncron_NTP_Time(bool correctOn) {
|
125 | uint32_t timestamp=0;
|
126 | timeClient.begin();
|
127 | if(timeClient.update()) {
|
128 | timestamp = timeClient.getEpochTime(); // get as GMT
|
129 | if(timestamp !=0){
|
130 | timestamp += (_MESZ * _UTCOFFSET); // means add 1 hour
|
131 | if(correctOn == true) {
|
132 | _secAdjust +=clockAdjust(timestamp-_timestamp); // negative difference clock is running too fast / positive difference clock is running too slow
|
133 | }
|
134 | _timestamp = timestamp; // syncron timestamp
|
135 | return true;
|
136 | }
|
137 | }
|
138 | return false;
|
139 | }
|
140 | //********* adjust intern clock if time is runnung false ****************//
|
141 | int clockAdjust(int timeDifference){
|
142 | int adjust=0;
|
143 | if(timeDifference > 60 && _secAdjust > 940){ // positive difference clock is running too slow
|
144 | // if(timeDifference <= 60) adjust--;
|
145 | adjust = timeDifference / -60;
|
146 | }
|
147 | else if(timeDifference < -60 && _secAdjust < 999){ // negative difference clock is running too fast
|
148 | // if(timeDifference >= -60) adjust++;
|
149 | adjust = timeDifference / -60;
|
150 | }
|
151 | if(_secAdjust > 1000){ // if time-correctur get wrong then check if time higher than 1000ms (1s)
|
152 | _secAdjust = 1000;
|
153 | adjust = 0;
|
154 | }
|
155 | return adjust;
|
156 | }
|
157 | //*************************** create a string from values for data and time ********************************
|
158 | String getTime2String(int hour,int minute,int second){
|
159 | String act_time;
|
160 | if(hour < 10) act_time += "0"; // if value < 10 then show a zero before
|
161 | act_time += String(hour); //combine string for time
|
162 | act_time += ":";
|
163 | if(minute < 10) act_time += "0";
|
164 | act_time += String(minute);
|
165 | act_time += ":";
|
166 | if(second < 10) act_time += "0";
|
167 | act_time += String(second);
|
168 | return act_time;
|
169 | }
|
170 | String getDate2String(int year,int month,int day){
|
171 | String act_date;
|
172 | if (day < 10) act_date += "0"; // if value < 10 then show a zero before
|
173 | act_date += String(day); //combine string for time
|
174 | act_date += ".";
|
175 | if(month < 10) act_date += "0";
|
176 | act_date += String(month);
|
177 | act_date += ".";
|
178 | act_date += String(year);
|
179 | return act_date;
|
180 | }
|
181 | //**************************** intern clock *******************************
|
182 | bool internClock(uint32_t currentMillis) {
|
183 | if (currentMillis < _secondMillis) _secondMillis = 0; // handle if currentMillis are overflows, max 32 bits
|
184 | if (currentMillis - _secondMillis >=_secAdjust){
|
185 | _secondMillis = currentMillis;
|
186 | _act_date_time[_SECOND]++;
|
187 | _timestamp++;
|
188 | if (_act_date_time[_SECOND] >=60){ _act_date_time[_SECOND]=0, _act_date_time[_MINUTE]++;}
|
189 | if (_act_date_time[_MINUTE] >=60){ _act_date_time[_MINUTE]=0, _act_date_time[_HOUR]++;}
|
190 | if (_act_date_time[_HOUR] >=24){ _act_date_time[_HOUR]=0; _act_date_time[_DAY]++;}
|
191 | return true;
|
192 | }
|
193 | else return false;
|
194 | }
|
195 | //***********************************************************
|
196 | void check4Client() {
|
197 | // Check if a client has connected
|
198 | // String line;
|
199 | if (WiFi.status() == WL_CONNECTED) {
|
200 | WiFiClient client = server.available();
|
201 | if (!client) {
|
202 | return;
|
203 | }
|
204 | // Wait until the client sends some data
|
205 | // Serial.println("new client");
|
206 | unsigned long timeout = millis() + 3000;
|
207 | while (!client.available() && millis() < timeout) {
|
208 | delay(1);
|
209 | }
|
210 | if (millis() > timeout) {
|
211 | // Serial.println("timeout");
|
212 | client.flush();
|
213 | client.stop();
|
214 | return;
|
215 | }
|
216 | // Read the first line of the reply from server and print it to Serial and copy it into variable "_argument"
|
217 | if(client.available()){
|
218 | // String line;
|
219 | String line = client.readStringUntil('\n');
|
220 | int argBegin = line.indexOf("/");
|
221 | int argEnd = line.indexOf("HTTP");
|
222 | int argEQ = line.indexOf("?");
|
223 | if (argEQ > 0 && argEQ < argEnd) {
|
224 | _argument = line.substring(argBegin+1, argEQ);
|
225 | }
|
226 | else _argument = line.substring(argBegin+1, argEnd-1);
|
227 | // Serial.println(_argument);
|
228 | if (argEQ > 0 && argEQ < argEnd) {
|
229 | _argumentValue = line.substring(argEQ+1, argEnd-1);
|
230 | }
|
231 | else _argumentValue = "none";
|
232 | // Serial.println(_argumentValue);
|
233 | }
|
234 | // Match the request
|
235 | client.flush();
|
236 | // Prepare the response
|
237 | String s;
|
238 | if (_argument =="refresh"){
|
239 | s += getRefreshJSON();
|
240 | }
|
241 | else if(_argument =="firmware"){
|
242 | s = HTTP_ANSWER;
|
243 | s += "Your versions number: "+Version+"<br>";
|
244 | s += "Check for new firmware now.<br>";
|
245 | s += getFilesFromServer(WiFi.localIP());
|
246 | s += HTTP_TEXT_END;
|
247 | }
|
248 | else if(_argument == "upDate"){
|
249 | s = HTTP_ANSWER;
|
250 | s += "Update will be start now.<br>";
|
251 | s += "Update-file: "+_argumentValue+"<br>";
|
252 | s += "Don't turn off power during update!<br><br>";
|
253 | s += "After update, the reconnection only via IP, without arguments behind it!<br><br>";
|
254 | s += "<a href=\"http://"+WiFi.localIP().toString()+"\">Click here for reconnect after update</a><br>";
|
255 | s += HTTP_TEXT_END;
|
256 | }
|
257 | else if (_argument == "showsetup") {
|
258 | s+=webShowSetup();
|
259 | }
|
260 | else if(_argument == "setup"){
|
261 | s += webSetup();
|
262 | }
|
263 | else if(_argument == "save"){
|
264 | String postValues = client.readString();
|
265 | int postBegin = postValues.indexOf("d_data"); // search of the first value for eleminate the header! "d_data" must be the first value of post-data!
|
266 | s+=webSave(postValues.substring(postBegin)); // deliver the short string of post values to save
|
267 | }
|
268 | else if(_argument == "setTime"){
|
269 | s+=webSetTime();
|
270 | }
|
271 | else if(_argument =="getJSON"){
|
272 | s+=valuesAsJSON();
|
273 | }
|
274 | else {
|
275 | s+=webMain(_transmitResult);
|
276 | }
|
277 | // Send the response to the client
|
278 | client.print(s);
|
279 | delay(1);
|
280 | if(_argument == "upDate"){
|
281 | delay(1000); //wait one second
|
282 | updateStromLog(_argumentValue); // start update now
|
283 | }
|
284 | // The client will actually be disconnected
|
285 | // when the function returns and 'client' object is detroyed
|
286 | }
|
287 | }
|
288 | //*********** update new software for ESP ***************************/
|
289 | //*********** get available files ****************************************/
|
290 | String getFilesFromServer(IPAddress ip){
|
291 | String files = "";
|
292 | if (WiFiMulti.run() == WL_CONNECTED) {
|
293 | WiFiClient client;
|
294 | HTTPClient http;
|
295 | String target ="http://";
|
296 | target += Config.getTargetServer();
|
297 | target += "/firmware/getFiles.php";
|
298 | if (http.begin(client,target)){
|
299 | http.addHeader("Content-Type","application/json;charset=utf-8");
|
300 | int httpCode = http.POST("{\"userIP\":\""+ip.toString()+"\",\"build\":\""+Version+"\"}");
|
301 | if(httpCode >= 100){
|
302 | files = http.getString();
|
303 | }
|
304 | else files = "Error: no connection.";
|
305 | }
|
306 | http.end();
|
307 | }
|
308 | return files;
|
309 | }
|
310 | //********** update, input file-name ************************************/
|
311 | String updateStromLog(String file){
|
312 | String result = "ERROR: Update failed!";
|
313 | #ifdef OLED_CONNECT
|
314 | showUpdateOLED();
|
315 | #endif
|
316 | ESPhttpUpdate.rebootOnUpdate(false); // remove automatic update
|
317 | // digitalWrite(LED,ON);
|
318 | String target = Config.getTargetServer();
|
319 | WiFiClient client;
|
320 | t_httpUpdate_return ret = ESPhttpUpdate.update(client, target, 80, "/firmware/"+file);
|
321 | switch (ret) {
|
322 | case HTTP_UPDATE_FAILED:
|
323 | // Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
|
324 | break;
|
325 | case HTTP_UPDATE_NO_UPDATES:
|
326 | break;
|
327 | case HTTP_UPDATE_OK:
|
328 | String result = "Update successful.";
|
329 | delay(1000); // Wait a second and restart
|
330 | ESP.restart();
|
331 | break;
|
332 | }
|
333 | return result;
|
334 | }
|
335 | //*********** update end *********************************//
|
336 | //**** get temperature from sensor DS18B20 ***************//
|
337 | void getValuesTemperature() {
|
338 | sensors.requestTemperatures();
|
339 | _temperature1 = sensors.getTempCByIndex(0);
|
340 | if (_numberOfSensors > 1)_temperature2 = sensors.getTempCByIndex(1);
|
341 | if (_numberOfSensors > 2)_temperature3 = sensors.getTempCByIndex(2);
|
342 | if (_numberOfSensors > 3)_temperature4 = sensors.getTempCByIndex(3);
|
343 | }
|
344 | /***********************************************************
|
345 | * convert a unix-timestamp into single bytes as value
|
346 | * function works up to 1972 until 19.01.2038
|
347 | ***********************************************************/
|
348 | void Timestamp2DateTime(uint32_t unixTime){
|
349 | int dummy = 0; int years=0;
|
350 | uint32_t SecOfYear; uint32_t timeDummy;
|
351 | bool leapYear = false; // default: don't be a leap-year
|
352 | timeDummy = unixTime - 31536000 *2; // sub two years (1970 and 1971) add the years later
|
353 | years= (timeDummy / 126230400) *4; // get the periodes of four years cycle; 3Y+leapY = 126230400 seconds
|
354 | timeDummy = timeDummy % 126230400; // get the rest of timestamp without the cycles like above this line
|
355 | if (timeDummy >= 31622400) {
|
356 | timeDummy -= 31622400; // sub the first leap-year if it's over
|
357 | years++; // add the leapyear
|
358 | years += timeDummy / 31536000; // get the rest of years between 0 until 2 years
|
359 | }
|
360 | years = years + 1972; // add start-year +2: now, we know the actual year and can figure out if is a leap-year
|
361 | _date_time[_YEAR]=years;
|
362 | // if (year % 4 == 0) leapYear = true; // if year divison by four without some rest, then is a leap-year...yeeeh -> it works until 2036 only
|
363 | if ( bitRead(years,0)==0 && bitRead(years,1)==0) { // it's very faster than %-arithmetic
|
364 | leapYear = true;
|
365 | SecOfYear = 31622400; // set the counts of seconds leap-year
|
366 | // Serial.print("LeapYear ");
|
367 | } else SecOfYear = 31536000; // or don't be a leap-year -> regulary seconds
|
368 | _date_time[_DAY] = (timeDummy % SecOfYear) / 86400; // get the days of actual year
|
369 | _date_time[_MONTH] = 0; // set zero
|
370 | for (int i=0; _date_time[_DAY]>=0; i++ ) { // do it until the day(s) are negativ, this loop has be exicute one time -> to set month at ones
|
371 | dummy = _date_time[_DAY]; // store the value befor result is negativ for break, because the last one was the day of actual month
|
372 | _date_time[_DAY] = _date_time[_DAY] - DaysPerMonth[i]; // sub the days of month until it's negativ, according byte-array
|
373 | if (i == 1 && leapYear == true) {_date_time[_DAY] -= 1;} // sub the 29. of february on leapyears ;-)
|
374 | _date_time[_MONTH]++; // add one month on every loop
|
375 | if (i > 11) return; // if this function wild ;-) then break (max. months are 0 - 11)
|
376 | }
|
377 | _date_time[_DAY] = dummy+1; // add one, because the start was on 1. january (start of timstamp = 01.01.1970 and we have to add it)
|
378 | /****** get time ******/
|
379 | _date_time[_HOUR] = (timeDummy % 86400) / 3600; // the value of _timeDummy contents the rest of hours, minutes and seconds only
|
380 | _date_time[_MINUTE] = (timeDummy % 3600) / 60; // it's shorter to take reduced _timeDummy than the whole timestamp
|
381 | _date_time[_SECOND] = timeDummy % 60;
|
382 | // return values are in array "unsigned int _date_time[6]; //[_YEAR][_MONTH][_DAY][_HOUR][_MINUTE][_SECOND]"
|
383 | }
|
384 | void writeTime(){
|
385 | _act_date_time[_YEAR] = _date_time[_YEAR];
|
386 | _act_date_time[_MONTH] = _date_time[_MONTH];
|
387 | _act_date_time[_DAY] = _date_time[_DAY];
|
388 | _act_date_time[_HOUR] = _date_time[_HOUR];
|
389 | _act_date_time[_MINUTE] = _date_time[_MINUTE];
|
390 | _act_date_time[_SECOND] = _date_time[_SECOND];
|
391 | }
|
392 | void getTime2Value(){
|
393 | Timestamp2DateTime(_timestamp);
|
394 | writeTime();
|
395 | }
|
396 | //***********************************************************
|
397 | bool send2dbase(String content){
|
398 | // Serial.println(t1); Serial.println(hum); Serial.println(hPa); Serial.println(heaterOnOff);
|
399 | bool r=false;
|
400 | if (WiFiMulti.run() == WL_CONNECTED) {
|
401 | WiFiClientSecure clientSSL;
|
402 | WiFiClient client;
|
403 | HTTPClient http;
|
404 | bool connect;
|
405 | String getTarget = Config.getTargetURLclean();
|
406 | String target;
|
407 |
|
408 | if(Config.SendSSL>0){
|
409 | target = "https://"; target += getTarget;
|
410 | int httpsPort=443;
|
411 | clientSSL.setInsecure(); // magic line without check certificate because memory insufficient
|
412 | clientSSL.connect(target,httpsPort); // set connection by https
|
413 | connect = http.begin(clientSSL,target);
|
414 | }else{
|
415 | target= "http://"; target += getTarget;
|
416 | connect = http.begin(client,target);
|
417 | }
|
418 | if(connect){
|
419 | // http.setTimeout(10000); // 10 seconds?
|
420 | if(Config.sendJSON) http.addHeader("Content-Type","application/json;charset=utf-8");
|
421 | else http.addHeader("Content-Type","application/x-www-form-urlencoded");
|
422 | int httpCode = 0; _transmitResult = "";
|
423 | httpCode = http.POST(content);
|
424 | _httpMarker = httpCode; //debug
|
425 | if (httpCode >= 100) {
|
426 | if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_NO_CONTENT){
|
427 | _transmitResult = http.getString();
|
428 | if (_transmitResult == "OK<br>" || httpCode == HTTP_CODE_NO_CONTENT){
|
429 | _transmitResult = "Werte wurden gesendet: "+getTime2String(_act_date_time[_HOUR],_act_date_time[_MINUTE],_act_date_time[_SECOND]);
|
430 | r = true; // if data saved successful than result=true else false
|
431 | } else {
|
432 | r = false;
|
433 | }
|
434 | } else {_transmitResult ="[HTTP] Unable to connect! "; _transmitResult +=httpCode;}
|
435 | } else {_transmitResult ="[HTTP] missing return-code!"; r = false;}
|
436 | } else {_transmitResult ="[HTTP] Service not connect!"; r = false;}
|
437 | http.end();
|
438 | }
|
439 | return r;
|
440 | }
|
441 | //**** prepaire send string *******************//
|
442 | String prepaire2Send(bool type){
|
443 | String ret="";
|
444 | if(type){
|
445 | ret=valuesAsJSON();
|
446 | } else {
|
447 | ret ="&time="; ret += _timestamp;
|
448 | ret+="&name="; ret += Config.getName();
|
449 | ret+="&usip="; ret += WiFi.localIP().toString();
|
450 | ret+="&name1="; ret += Config.getSensorName(1); ret+="&temp1="; ret += int(_temperature1*1000);
|
451 | if(_numberOfSensors > 1){ ret+="&name2="; ret += Config.getSensorName(2); ret+="&temp2="; ret += int(_temperature2*1000);}
|
452 | if(_numberOfSensors > 2){ ret+="&name3="; ret += Config.getSensorName(3); ret+="&temp3="; ret += int(_temperature3*1000);}
|
453 | if(_numberOfSensors > 3){ ret+="&name4="; ret += Config.getSensorName(4); ret+="&temp4="; ret += int(_temperature4*1000);}
|
454 | }
|
455 | return ret;
|
456 | }
|
457 | //**** collect values as JSON *****************//
|
458 | String valuesAsJSON(){
|
459 | String ret="";
|
460 | ret ="{\"time\":"; ret += _timestamp; ret += ",";
|
461 | ret+="\"name\":\""; ret += Config.getName(); ret+="\",";
|
462 | ret+="\"usip\":\""; ret += WiFi.localIP().toString(); ret+="\",";
|
463 | ret+="\"name1\":\""; ret += Config.getSensorName(1); ret+="\","; ret+="\"temp1\":"; ret += int(_temperature1*1000); ret+=",";
|
464 | if(_numberOfSensors > 1){ ret+="\"name2\":\""; ret += Config.getSensorName(2); ret+="\","; ret+="\"temp2\":"; ret += int(_temperature2*1000); ret+=",";}
|
465 | if(_numberOfSensors > 2){ ret+="\"name3\":\""; ret += Config.getSensorName(3); ret+="\","; ret+="\"temp3\":"; ret += int(_temperature3*1000); ret+=",";}
|
466 | if(_numberOfSensors > 3){ ret+="\"name4\":\""; ret += Config.getSensorName(4); ret+="\","; ret+="\"temp4\":"; ret += int(_temperature4*1000); ret+=",";}
|
467 | ret+="\"end\":0}";
|
468 | return ret;
|
469 | }
|
470 | #ifdef OLED_CONNECT
|
471 | //**** if OLED connected, shoe values *****//
|
472 | void ShowValuesOnOLED(){
|
473 | char temp[20];
|
474 | String s;
|
475 | switch (_showTemperature){
|
476 | case 0: s= getTime2String(_act_date_time[_HOUR],_act_date_time[_MINUTE],_act_date_time[_SECOND]); break;
|
477 | case 1: s= "1: "; s += String(_temperature1); s += "°C"; break;
|
478 | case 2: s= "2: "; s += String(_temperature2); s += "°C"; break;
|
479 | case 3: s= "3: "; s += String(_temperature3); s += "°C"; break;
|
480 | case 4: s= "4: "; s += String(_temperature4); s += "°C"; break;
|
481 | }
|
482 | if(_showTemperature >= _numberOfSensors)_showTemperature=0;
|
483 | else _showTemperature++;
|
484 | int oledX = random(0,64);
|
485 | int oledY = random(0,24);
|
486 | s.toCharArray(temp,s.length()+1);
|
487 | display.draw_string(oledX,oledY,temp);
|
488 | display.display();
|
489 | }
|
490 | void buttonHoldOLED(){
|
491 | display.clear();
|
492 | display.draw_string_P(12,16,PSTR("Button is pressed!"));
|
493 | display.display();
|
494 | }
|
495 | void showUpdateOLED(){
|
496 | display.clear();
|
497 | display.draw_string_P(0,16,PSTR("Update is in progress!"));
|
498 | display.display();
|
499 | }
|
500 | //**** intial OLED display *****//
|
501 | void initOLED(){
|
502 | Wire.begin(); //init I2C
|
503 | display.init(); // initialize OLED-Display
|
504 | display.set_contrast(64);
|
505 | impressumOLED();
|
506 | display.draw_string_P(10,24,PSTR("Try to connect WLAN"));
|
507 | display.display();
|
508 | // Serial.println("Init OLED successful");
|
509 | }
|
510 | void impressumOLED(){
|
511 | display.draw_string_P(30,2,_impressum);
|
512 | display.draw_string_P(30,12,_typeVersion);
|
513 | }
|
514 | void connectWlanOLED(){
|
515 | display.clear();
|
516 | impressumOLED();
|
517 | display.draw_string_P(10,24,PSTR("WLAN is connected"));
|
518 | display.display();
|
519 | }
|
520 | //**** show valueson OLED *****/
|
521 | void displayValuesOnOLED(){
|
522 | if(_oledDisplayWait > _OLED_TIME_WAIT){
|
523 | display.clear();
|
524 | ShowValuesOnOLED();
|
525 | } else {
|
526 | if(_oledDisplayWait ==0 ){display.clear(); impressumOLED();}
|
527 | _oledDisplayWait++;
|
528 | display.draw_string(10,24,"Start:");
|
529 | display.draw_string(40+(5*_oledDisplayWait),22,".");
|
530 | display.display();
|
531 | }
|
532 | // Serial.println(_oledDisplayWait);
|
533 | }
|
534 | #endif
|
535 | //***********************************************************
|
536 | void setup() {
|
537 | pinMode(_BoardTaster, INPUT_PULLUP); // set IO0 as input
|
538 |
|
539 | #ifdef OLED_CONNECT
|
540 | initOLED();
|
541 | #endif
|
542 | Serial.begin(115200);
|
543 | // Serial.setDebugOutput(true);
|
544 | Serial.println("Try to connect WIFI");
|
545 | wifi_station_set_hostname(wiFiHostname);
|
546 | bool result = setupWIFI();
|
547 | if (result) {
|
548 | Serial.print("connected to ssid: ");
|
549 | Serial.print(WiFi.SSID());
|
550 | Serial.print(" with IP:");
|
551 | Serial.println(WiFi.localIP());
|
552 | }
|
553 | else {
|
554 | Serial.println("Failed to connect!");
|
555 | Serial.println("Please take a reset,");
|
556 | Serial.println("or hold the button for 6s!");
|
557 | delay(3000);
|
558 | checkButton();
|
559 | ESP.restart();
|
560 | }
|
561 | #ifdef OLED_CONNECT
|
562 | connectWlanOLED();
|
563 | #endif
|
564 | sensors.begin();
|
565 | Serial.println("Start Server now");
|
566 | server.begin();
|
567 | Config.begin();
|
568 | syncron_NTP_Time(false);
|
569 | getTime2Value();
|
570 | _numberOfSensors = sensors.getDeviceCount(); // get numbers of connected sensors
|
571 | delay(500);
|
572 | getValuesTemperature(); // get the first values from DS18B20
|
573 | }
|
574 | //***********************************************************
|
575 | void loop() {
|
576 | yield();
|
577 | uint32_t _currentMillis = millis();
|
578 | checkButton(); // set new SSID (WLAN Setup)
|
579 | if(internClock(_currentMillis)== true) { // every second will run this component
|
580 | if (_act_date_time[_HOUR]==23 && _act_date_time[_MINUTE]==5 && _act_date_time[_SECOND]==10) { // update timestamp every new day at the 30th second
|
581 | if (syncron_NTP_Time(true) == true) getTime2Value();
|
582 | else if(!WiFi.isConnected()) ESP.restart(); // check if connected yet
|
583 | }
|
584 | if (_timestamp < _TimeOf2021 && _act_date_time[_SECOND] == 50)
|
585 | if (syncron_NTP_Time(false) == true) getTime2Value(); // get timestamp again if sync was failure, perhaps after power down
|
586 | if (_act_date_time[_SECOND] == 1) { // is called every minute at the first second
|
587 | if (_act_date_time[_MINUTE] == 0 || _act_date_time[_MINUTE] == 15 || _act_date_time[_MINUTE] == 30 || _act_date_time[_MINUTE] == 45) {
|
588 | if (Config.sendPUSH){
|
589 | send2dbase(prepaire2Send(Config.sendJSON));
|
590 | } else {_transmitResult="Automatisches Senden ist aus.<br>Pull-Mode";}
|
591 | }
|
592 | }
|
593 | #ifdef OLED_CONNECT
|
594 | displayValuesOnOLED(); // show values on OLED
|
595 | #endif
|
596 | if(_act_date_time[_SECOND] % 5 ==0)getValuesTemperature(); // get data from sensor every 5 seconds
|
597 | }
|
598 | check4Client(); // thats is if with the browser show data on port 80 by html
|
599 | }
|