This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*WORKING! | |
Do I deserve a gnu license? | |
COMPILES AND webPage | |
OTAWebUpdater.ino Example from ArduinoOTA Library | |
Rui Santos | |
Complete Project Details http://randomnerdtutorials.com | |
[tutorial]: | |
The current %STATE% value is simply a marker that will allow the ESP32 to initialize the user interface with the correct class attribute value before sending the index.html file to the client browser. | |
String processor(const String &var) { | |
return String(var == "STATE" && led.on ? "on" : "off"); | |
} | |
Serial.printf("Trying to connect [%s] ", WiFi.macAddress().c_str()); | |
Serial.printf(" %s\n", WiFi.localIP().toString().c_str());COMPILES AND webPage | |
https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/ | |
https://www.dropbox.com/s/bsnzdzh3hsb2usa/page%20forum.png?dl=0 | |
COMPILES AND webPage % % | |
*/ | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <WebServer.h> | |
#include <ESPmDNS.h> | |
#include <Update.h> | |
#include <WiFiUdp.h> | |
#include <NTPClient.h> | |
#include "esp_sleep.h" | |
#include "FS.h" | |
#include "SPIFFS.h" | |
#include <time.h> | |
#include "DHT.h" | |
#line 33 "OTAWebUpdaterWebPage3.ino" | |
const char *host = "esp32"; | |
const char *ssid = "your"; | |
const char *password = "your"; | |
WebServer server(80); | |
IPAddress local_IP(199, 177, 0, 111); | |
IPAddress staticIP(199, 177, 0, 111); | |
IPAddress subnet(255, 255, 255, 0); | |
IPAddress gateway(199, 177, 0, 1); | |
IPAddress primaryDNS(199, 177, 0, 1); | |
IPAddress secondaryDNS(99, 177, 0, 1); | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP); | |
WiFiClient client; | |
#define Threshold 40 | |
#define DHTTYPE DHT22 | |
#define uS_TO_S_FACTOR 1000000ULL | |
#define HOURS 60 | |
#define TIME_TO_SLEEP_MAX 80ULL | |
#define TIME_TO_SLEEP_CASE1 3600ULL//two readings | |
#define TIME_TO_SLEEP_CASE2 79200LL// | |
#define DAY_PERIOD_START 360//540 960 16:00 hour | |
#define DAY_PERIOD_END 1080//840 1080 18:00 hour | |
#define bufflen 300 | |
long timezone = -5; | |
long calcul5 = 0.0; | |
unsigned long currentMillis; | |
const unsigned long period = 10000; | |
uint8_t daysavetime = 1; | |
struct tm tmstruct; | |
unsigned long timeDiff, timeDiff2; | |
int TIME_TO_SLEEP_WiFi = 10; | |
String formattedDate; | |
String timeStamp; | |
RTC_DATA_ATTR uint8_t dryRedLightAlarm = 0; | |
RTC_DATA_ATTR const int relay = 26; | |
const int GROUND_PIN = 34;//to be changed | |
const int probe1 = 35; //to be changed | |
const int DHTPin = 16; | |
DHT dht(DHTPin, DHTTYPE); | |
char groundTemp[7]; | |
char celsiusTemp[7]; | |
char humidityTemp[7]; | |
unsigned long long previouzMillis; | |
char dayBuff[bufflen]; | |
char clientReadBuff[bufflen]; | |
char atoiBuff1[3]; | |
char atoiBuff2[3]; | |
bool eraseFile; | |
int pageOn; | |
long PuissanceWifi = 0; | |
int heureNormale = 3600; | |
double totalDelay = 0, webPageTime = 0; | |
float hic; | |
const char *awaken = "Page disponible pour pageOn heures entre 16 et 18:00 heures.\n" | |
"Peut être consultée de 17:58 jusqu'à 17:58 plus "; | |
String aNewLine; | |
int datCnt, moreLess, morePlus = 250; | |
char concatened[5500];//page + 14 lignes | |
String datArray[250]; | |
String newLinesArray[40]; | |
String newLinesArray2[40]; | |
unsigned long currentTime = millis(); | |
unsigned long previousTime = 0; | |
const long timeoutTime = 2000; | |
/* | |
Login page | |
*/ | |
const char *loginIndex = | |
"<form name='loginForm'>" | |
"<table width='20%' bgcolor='A09F9F' align='center'>" | |
"<tr>" | |
"<td colspan=2>" | |
"<center><font size=4><b>ESP32 Login Page</b></font></center>" | |
"<br>" | |
"</td>" | |
"<br>" | |
"<br>" | |
"</tr>" | |
"<td>Username:</td>" | |
"<td><input type='text' size=25 name='userid'><br></td>" | |
"</tr>" | |
"<br>" | |
"<br>" | |
"<tr>" | |
"<td>Password:</td>" | |
"<td><input type='Password' size=25 name='pwd'><br></td>" | |
"<br>" | |
"<br>" | |
"</tr>" | |
"<tr>" | |
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>" | |
"</tr>" | |
"</table>" | |
"</form>" | |
"<script>" | |
"function check(form)" | |
"{" | |
"if(form.userid.value=='admin' && form.pwd.value=='admin')" | |
"{" | |
"window.open('/serverIndex')" | |
"}" | |
"else" | |
"{" | |
" alert('Error Password or Username')/*displays error message*/" | |
"}" | |
"}" | |
"</script>"; | |
/* | |
Server Index Page | |
*/ | |
const char *serverIndex = | |
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>" | |
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>" | |
"<input type='file' name='update'>" | |
"<input type='submit' value='Update'>" | |
"</form>" | |
"<div id='prg'>progress: 0%</div>" | |
"<div><p><a href='concatened'><button>concatened</button></a></p></div>" | |
"<script>" | |
"$('form').submit(function(e){" | |
"e.preventDefault();" | |
"var form = $('#upload_form')[0];" | |
"var data = new FormData(form);" | |
" $.ajax({" | |
"url: '/update'," | |
"type: 'POST'," | |
"data: data," | |
"contentType: false," | |
"processData:false," | |
"xhr: function() {" | |
"var xhr = new window.XMLHttpRequest();" | |
"xhr.upload.addEventListener('progress', function(evt) {" | |
"if (evt.lengthComputable) {" | |
"var per = evt.loaded / evt.total;" | |
"$('#prg').html('progress: ' + Math.round(per*100) + '%');" | |
"}" | |
"}, false);" | |
"return xhr;" | |
"}," | |
"success:function(d, s) {" | |
"console.log('success!')" | |
"}," | |
"error: function (a, b, c) {" | |
"}" | |
"});" | |
"});" | |
"</script>"; | |
String moreLines() | |
{ | |
//String moreLines = ""; | |
// for (int i = newLinesCnt ; i >= 0 ; i--) | |
// { | |
// moreLines += newLinesArray[i] + "<br>"; | |
// } | |
String moreLines = "<br>-------------------------------------------<br><br>"; | |
for (int spiffDatas = datCnt; spiffDatas >= (datCnt - 14); spiffDatas--) | |
{ | |
if (datArray[spiffDatas] != 0) | |
{ | |
moreLines += datArray[spiffDatas] + "<br>"; | |
} | |
} | |
return moreLines; | |
} | |
void delayFunction(long long secondes) | |
{ | |
unsigned long long previousMillis; | |
volatile long long xx = 0; | |
secondes = secondes * 1000000; | |
previousMillis = micros(); | |
while ((micros() - previousMillis) < secondes) | |
{ | |
analogRead(probe1); | |
xx += 1; | |
} | |
} | |
int capacitiveProbe(uint8_t readings, long dlay, uint8_t foroncer, int inProbe) | |
{ | |
unsigned long long previousMillis; | |
int totVal = 0, lecture = 0; | |
uint8_t oncer = foroncer; | |
uint8_t turns = 0; | |
dlay = dlay * 1000; | |
while (turns < readings) | |
{ | |
if (oncer < 1) | |
{ | |
previousMillis = millis(); | |
while ((millis() - previousMillis) < 180000) | |
{ | |
; | |
} | |
oncer = 1; | |
} | |
lecture = analogRead(inProbe); | |
previousMillis = millis(); | |
while ((millis() - previousMillis) < dlay) | |
{ | |
; | |
} | |
if (isnan(lecture)) | |
{ | |
Serial.println("Failed to read from inProbe."); | |
return 0; | |
} | |
printf("inProbe#%d valeur brute %d \n", inProbe, lecture); | |
lecture = map(lecture, 3200, 0, 100, 0); | |
totVal += lecture; | |
turns++; | |
} | |
printf("dryRedLightAlarm#%d %d <--> %d (mean)\n", inProbe, dryRedLightAlarm, (totVal / readings)); | |
return (totVal / readings); | |
} | |
void tooDryRedLightAlarm(int probesValue) | |
{ | |
if (probesValue < dryRedLightAlarm) | |
{ | |
printf("probesValue %d < %d dryRedLightAlarm\n", probesValue, dryRedLightAlarm); | |
digitalWrite(relay, HIGH); | |
while (probesValue < dryRedLightAlarm) | |
{ | |
probesValue = capacitiveProbe(1, 20, 1, probe1); | |
delayFunction(10); | |
} | |
} | |
digitalWrite(relay, LOW); | |
} | |
void print_wakeup_reason() | |
{ | |
esp_sleep_wakeup_cause_t wakeup_reason; | |
wakeup_reason = esp_sleep_get_wakeup_cause(); | |
switch (wakeup_reason) | |
{ | |
case ESP_SLEEP_WAKEUP_EXT0: | |
Serial.println("Wakeup caused by external signal using RTC_IO"); | |
break; | |
case ESP_SLEEP_WAKEUP_EXT1: | |
Serial.println("Wakeup caused by external signal using RTC_CNTL"); | |
break; | |
case ESP_SLEEP_WAKEUP_TIMER: | |
Serial.println("Wakeup caused by timer"); | |
break; | |
case ESP_SLEEP_WAKEUP_TOUCHPAD: | |
Serial.println("Wakeup caused by touchpad"); | |
break; | |
case ESP_SLEEP_WAKEUP_ULP: | |
Serial.println("Wakeup caused by ULP program"); | |
break; | |
default: | |
printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); | |
break; | |
} | |
} | |
long longMinutStamp() | |
{ | |
String formattedDate; | |
String dayStamp; | |
String timeStamp, timeStamp1, timeStamp2, timeStamp3; | |
long longMinutStamp; | |
while (!timeClient.update()) | |
{ | |
timeClient.forceUpdate(); | |
} | |
timeClient.setTimeOffset(-18000 + heureNormale); | |
formattedDate = timeClient.getFormattedDate(); | |
int splitT = formattedDate.indexOf("T"); | |
dayStamp = formattedDate.substring(0, splitT); | |
timeStamp1 = formattedDate.substring(splitT + 3, formattedDate.length() - 9); | |
timeStamp2 = formattedDate.substring(splitT + 4, formattedDate.length() - 4); | |
timeStamp3 = formattedDate.substring(splitT + 7, formattedDate.length() - 1); | |
longMinutStamp = atoi(timeStamp1.c_str()) * 60; | |
longMinutStamp += atoi(timeStamp2.c_str()); | |
longMinutStamp += atoi(timeStamp3.c_str()) / 60; | |
return longMinutStamp; | |
} | |
long timeDiffData(long minut) | |
{ | |
RTC_DATA_ATTR static long previous; | |
Serial.println("_256 - previous = " + (String)previous); | |
if (previous != 0) | |
{ | |
return (minut - previous); | |
} | |
else | |
{ | |
previous = minut; | |
return (minut - longMinutStamp()); | |
} | |
} | |
String myDate() | |
{ | |
String formattedDate; | |
String dayStamp; | |
String timeStamp; | |
String myDate; | |
while (!timeClient.update()) | |
{ | |
timeClient.forceUpdate(); | |
} | |
timeClient.setTimeOffset(-18000 + heureNormale); | |
formattedDate = timeClient.getFormattedDate(); | |
int splitT = formattedDate.indexOf("T"); | |
dayStamp = formattedDate.substring(0, splitT); | |
timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1); | |
myDate = dayStamp + " | " + timeStamp; | |
return myDate; | |
} | |
void readData() | |
{ | |
int datCnt = 0; | |
File f = SPIFFS.open("/gardenTemp.txt", "r"); | |
if (!f) | |
{ | |
Serial.println("====== Reading from SPIFFS file FAILED ======="); | |
} | |
else | |
{ | |
Serial.println("====== Reading from SPIFFS file ======="); | |
} | |
while (f.available()) | |
{ | |
String line = f.readStringUntil('\n'); | |
Serial.print(datCnt); | |
Serial.print(" "); | |
Serial.println(line); | |
datCnt++; | |
} | |
f.close(); | |
Serial.println("====== End reading from SPIFFS file ======="); | |
} | |
void appendFile(fs::FS &fs, const char *path, const char *message) | |
{ | |
File file = fs.open(path, FILE_APPEND); | |
if (!file) | |
{ | |
Serial.println("Failed to open file for appending"); | |
return; | |
} | |
if (file.print(message)) | |
{ | |
printf("\n%s\n", message); | |
printf("->|__Appended to file: %s\n", path); | |
} | |
else | |
{ | |
printf("\nAppend failed: %s\n", message); | |
} | |
file.close(); | |
} | |
double dewPoint(double celsius, double humidity) | |
{ | |
double RATIO = 373.15 / (273.15 + celsius); | |
double RHS = -7.90298 * (RATIO - 1); | |
RHS += 5.02808 * log10(RATIO); | |
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO))) - 1); | |
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1); | |
RHS += log10(1013.246); | |
double VP = pow(10, RHS - 3) * humidity; | |
double T = log(VP / 0.61078); | |
return (241.88 * T) / (17.558 - T); | |
} | |
void listDir(fs::FS &fs, const char *dirname, uint8_t levels) | |
{ | |
Serial.println("====== Listing DIR SPIFFS files ======="); | |
printf("Listing directory: %s\n", dirname); | |
File root = fs.open(dirname); | |
if (!root) | |
{ | |
Serial.println("Failed to open directory"); | |
return; | |
} | |
if (!root.isDirectory()) | |
{ | |
Serial.println("Not a directory"); | |
return; | |
} | |
File file = root.openNextFile(); | |
while (file) | |
{ | |
if (file.isDirectory()) | |
{ | |
Serial.print(" DIR : "); | |
Serial.print(file.name()); | |
time_t t = file.getLastWrite(); | |
struct tm *tmstruct = localtime(&t); | |
printf(" LAST WRITE: %d-%02d-%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday); | |
if (levels) | |
{ | |
listDir(fs, file.name(), levels - 1); | |
} | |
} | |
else | |
{ | |
Serial.print(" FILE: "); | |
Serial.print(file.name()); | |
Serial.print(" SIZE: "); | |
Serial.print(file.size()); | |
time_t t = file.getLastWrite(); | |
struct tm *tmstruct = localtime(&t); | |
printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); | |
} | |
file = root.openNextFile(); | |
} | |
Serial.println("====== End listing DIR SPIFFS files ======="); | |
} | |
void writeFile(const char *path, const char *message) | |
{ | |
File file = SPIFFS.open(path, "w"); | |
if (!file) | |
{ | |
Serial.println("Failed to open file for writing" + *path); | |
file.close(); | |
return; | |
} | |
if (file.print(message)) | |
{ | |
printf("\nErasing or writing file: %s OK\n", path); | |
} | |
else | |
{ | |
Serial.println("Write or erasing failed\n" + *path); | |
} | |
file.close(); | |
} | |
void callback() | |
{ | |
Serial.println(); | |
Serial.println("Awaken by pin GPIO(4 fourth pin) touch ..."); | |
Serial.println("On the boot button side of the card."); | |
Serial.println(); | |
ESP.restart(); | |
} | |
int minuteSort(String theMinuts) | |
{ | |
char atoiBuff[3]; | |
if (theMinuts.charAt(0) == 48) | |
{ | |
atoiBuff[0] = theMinuts.charAt(1); | |
atoiBuff[1] = '\0'; | |
atoiBuff[2] = '\0'; | |
} | |
else | |
{ | |
atoiBuff[0] = theMinuts.charAt(0); | |
atoiBuff[1] = theMinuts.charAt(1); | |
atoiBuff[2] = '\0'; | |
} | |
int minutCp5 = atoi(atoiBuff); | |
return minutCp5; | |
} | |
void lightTest() | |
{ | |
digitalWrite(relay, HIGH); | |
printf("Ampoule allumée.\n"); | |
delayFunction(300); | |
digitalWrite(relay, LOW); | |
printf("Ampoule éteinte.\n"); | |
} | |
char *unconstchar(const char *s) | |
{ | |
if (!s) | |
return NULL; | |
int i; | |
char *res = NULL; | |
res = (char *)malloc(strlen(s) + 1); | |
if (!res) | |
{ | |
fprintf(stderr, "Memory Allocation Failed! Exiting...\n"); | |
exit(EXIT_FAILURE); | |
} | |
else | |
{ | |
for (i = 0; s[i] != '\0'; i++) | |
{ | |
res[i] = s[i]; | |
} | |
res[i] = '\0'; | |
return res; | |
} | |
} | |
void whyling(const char awaken[]) | |
{ | |
long long _Entre_Deux = longMinutStamp(); | |
Serial.print("Sleep at "); | |
Serial.println(myDate()); | |
delayFunction(1); | |
if ((_Entre_Deux >= DAY_PERIOD_START) && (_Entre_Deux <= DAY_PERIOD_END)) | |
{ | |
Serial.println("Going to light sleep.\n"); | |
delayFunction(1); | |
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_CASE1 * uS_TO_S_FACTOR); | |
esp_light_sleep_start(); | |
ESP.restart(); | |
} | |
else | |
{ | |
//newLinesCnt2 = (newLinesCnt - 1); | |
//newLinesArray[newLinesCnt2] | |
aNewLine.toCharArray(dayBuff, bufflen); | |
const char *result = dayBuff; | |
appendFile(SPIFFS, "/gardenTemp.txt", result); | |
Serial.println("Going to deep sleep."); | |
Serial.println(myDate()); | |
delayFunction(1); | |
//newLinesCnt = 0; | |
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); | |
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP_CASE2 * uS_TO_S_FACTOR); | |
esp_deep_sleep_start(); | |
} | |
} | |
void filldatArray() | |
{ | |
SPIFFS.begin(); | |
if (!SPIFFS.begin()) | |
{ | |
Serial.println(" Not SPIFFS.begin()"); | |
return; | |
} | |
if (eraseFile) { | |
listDir(SPIFFS, "/", 0); | |
writeFile("/gardenTemp.txt", '\0');//EEspiffs.txt | |
} | |
File f = SPIFFS.open("/gardenTemp.txt", "r"); | |
if (!f) | |
{ | |
Serial.println("file open failed"); | |
} | |
Serial.println("====== Setup Start reading from SPIFFS file ======="); | |
while (f.available() && datCnt < morePlus) | |
{ | |
String line = f.readStringUntil('\n'); | |
if (line != 0) | |
{ | |
if (datCnt > 9) | |
{ | |
Serial.print(datCnt); | |
Serial.print(" "); | |
Serial.println(line); | |
datArray[datCnt] = line; | |
} | |
else | |
{ | |
Serial.print(0); | |
Serial.print(datCnt); | |
Serial.print(" "); | |
Serial.println(line); | |
datArray[datCnt] = line; | |
} | |
datCnt++; | |
} | |
} | |
moreLess = datCnt - 1; | |
f.close(); | |
Serial.println("====== Setup End reading from SPIFFS file =======\n"); | |
} | |
void aNewline() | |
{ | |
float humid = dht.readHumidity(); | |
delayFunction(10); | |
float cTemp = dht.readTemperature(); | |
delayFunction(10); //Avoiding all the sensors at once. | |
//The ESP32 has a built-in brownout detector which can detect if | |
//the voltage is lower than a specific value. If this happens, | |
//it will reset the chip in order to prevent unintended behaviour. | |
double dew = dewPoint(cTemp, humid); | |
float hic = dht.computeHeatIndex(cTemp, humid, false); | |
dtostrf(hic, 2, 1, celsiusTemp); | |
dtostrf(humid, 2, 1, humidityTemp); | |
float val1 = capacitiveProbe(1, 60, 1, probe1); | |
delayFunction(10); | |
double ground = analogRead(GROUND_PIN); | |
delayFunction(10); | |
dtostrf(ground, 2, 1, groundTemp); | |
if (isnan(humid) || isnan(cTemp)) | |
{ | |
aNewLine = "\n"; | |
aNewLine += myDate(); | |
aNewLine += " Humidité du sol: " + (String)val1 + "%"; | |
aNewLine += " Température du sol: " + (String)groundTemp + " Température: failed, Humidité: failed"; | |
aNewLine += "% Ressentie: failed, Rosé failed."; | |
} | |
else | |
{ | |
aNewLine = "\n"; | |
aNewLine += myDate(); | |
aNewLine += " Humidité du sol: " + (String)val1 + "%"; | |
aNewLine += " Température du sol: " + (String)groundTemp + " Température: " + (String)cTemp + "°C Humidité: " + (String)humid; | |
aNewLine += "% Ressentie: " + (String)hic + "°C Rosé " + (String)dew + "°C"; | |
} | |
printf("-aNewLine size %d->¬\n", aNewLine.length()); | |
Serial.println(aNewLine); | |
// return aNewLine; | |
} | |
char *processor(const char *var, char *un) | |
{ | |
float humid = 0, ctemp = 0, hic = 0, dew = 0, pw = 0; | |
float val1 = 0, ground = 0; | |
un = ((char *)malloc(sizeof(char) * 20)); | |
char *tmp = ((char *)malloc(sizeof(char) * 6)); | |
if (strcmp(var, "MYDATE") == 0) | |
{ | |
//24 -6 | |
*un += 18; | |
return unconstchar(myDate().c_str()); | |
} | |
else if (strcmp(var, "WIFISTRENGHT") == 0) | |
{ | |
pw = WiFi.RSSI(); | |
return dtostrf(pw, 2, 1, tmp); //isnan(pw) ? unconstchar(" Error ") : unconstchar(pw); | |
} | |
else if (strcmp(var, "CTEMP") == 0) | |
{ | |
//Read temperature as Celsius (the default) | |
ctemp = dht.readTemperature(); | |
delayFunction(10); | |
return dtostrf(ctemp, 2, 1, tmp); //unconstchar("ctemp"); | |
} | |
else if (strcmp(var, "VAL1") == 0) | |
{ | |
val1 = capacitiveProbe(1, 20, 1, probe1); | |
return dtostrf(val1, 2, 1, tmp); | |
} | |
else if (strcmp(var, "GROUND") == 0) | |
{ | |
ground = analogRead(GROUND_PIN); | |
return dtostrf(ground, 2, 1, groundTemp); | |
} | |
else if (strcmp(var, "HUMID_TEMP") == 0) | |
{ | |
humid = dht.readHumidity(); | |
delayFunction(10); | |
return dtostrf(humid, 2, 1, tmp); // ("Decimal to String Float"). | |
} | |
else if (strcmp(var, "HIC") == 0) | |
{ | |
hic = dht.computeHeatIndex(ctemp, humid, false); | |
return dtostrf(hic, 2, 1, tmp); | |
} | |
else if (strcmp(var, "DEW") == 0) | |
{ | |
dew = dewPoint(ctemp, humid); | |
return dtostrf(dew, 2, 1, tmp); | |
} | |
else if (strcmp(var, "ANEWLINE") == 0) | |
{ | |
return unconstchar(aNewLine.c_str()); | |
} | |
else if (strcmp(var, "MORE_LINES") == 0) | |
{ | |
return unconstchar(moreLines().c_str()); | |
} | |
return unconstchar(var); | |
} | |
void fillPlaceHolders() | |
{ | |
const char *webPage = | |
"<!DOCTYPE HTML><html lang=\"fr\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">" | |
"<meta http-equiv=\"refresh\" content=\"30\"></head>" | |
"<body style='color:#f3d6e1;'><div style='font-size:3.5rem;color:green;'><p style=\"text-align:center;\">Météo</p></div>" | |
"<script language='Javascript' type='text/javascript'>if (%CTEMP% >= 25){" | |
"document.write(\"<div style='color:#930000;'><p style='text-align:center;'>\");}else if (%CTEMP% < 25 && %CTEMP% >= 5){" | |
"document.write(\"<div style='color:#006601;'><p style='text-align:center;'>\");}else if (%CTEMP% < 5){" | |
"document.write(\"<div style='color:#009191;'><p style='text-align:center;'>\");}</script>RELOADED%MYDATE%" | |
"</p><p style='text-align:center;font-size:14pt;'>" | |
"PuissanceWifi = %WIFISTRENGHT%</p><p style=\"text-align:center;\"> %CTEMP%°C</p>" | |
"<p style=\"text-align:center;font-size:20pt;\">%ANEWLINE%</p>" | |
"<p style=\"text-align:center;font-size:16pt;\">%MORE_LINES%</p></div></body></html>"; | |
char *holdersPage = unconstchar(webPage); // ~1163 | |
printf("holdersPage:\n%s\n", holdersPage); | |
char *token = strtok(holdersPage, "%"); | |
while (token != NULL) | |
{ | |
strcat(concatened, processor(token, (concatened + strlen(token)))); | |
token = strtok(NULL, "%"); | |
} | |
printf("%s\n", concatened); | |
} | |
/* | |
setup function | |
*/ | |
void setup(void) | |
{ | |
Serial.begin(115200); | |
touchAttachInterrupt(T3, callback, Threshold); | |
esp_sleep_enable_touchpad_wakeup(); | |
dht.begin(); | |
// setCpuFrequencyMhz(10); | |
int cpuSpeed = getCpuFrequencyMhz(); | |
Serial.println("Running at " + String(cpuSpeed) + "MHz"); | |
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) | |
{ | |
Serial.println("STA Failed to configure"); | |
} | |
// Connect to WiFi network | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("MAC address: "); | |
Serial.println(WiFi.macAddress()); | |
/*use mdns for host name resolution*/ | |
if (!MDNS.begin(host)) | |
{ //http://esp32.local | |
Serial.println("Error setting up MDNS responder!"); | |
while (1) | |
{ | |
delay(1000); | |
} | |
} | |
Serial.println("mDNS responder started"); | |
/*return index page which is stored in serverIndex */ | |
server.on("/", HTTP_GET, []() { | |
server.sendHeader("Connection", "close"); | |
server.send(200, "text/html", loginIndex); | |
}); | |
server.on("/serverIndex", HTTP_GET, []() { | |
server.sendHeader("Connection", "close"); | |
server.send(200, "text/html", serverIndex); | |
}); | |
server.on("/concatened", HTTP_GET, []() { | |
server.sendHeader("Connection", "close"); | |
server.send(200, "text/html", concatened); | |
}); | |
/*handling uploading firmware file */ | |
server.on( | |
"/update", HTTP_POST, []() { | |
server.sendHeader("Connection", "close"); | |
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); | |
ESP.restart(); | |
}, []() { | |
HTTPUpload& upload = server.upload(); | |
if (upload.status == UPLOAD_FILE_START) { | |
Serial.printf("Update: %s\n", upload.filename.c_str()); | |
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size | |
Update.printError(Serial); | |
} | |
} else if (upload.status == UPLOAD_FILE_WRITE) { | |
/* flashing firmware to ESP*/ | |
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { | |
Update.printError(Serial); | |
} | |
} else if (upload.status == UPLOAD_FILE_END) { | |
if (Update.end(true)) { //true to set the size to the current progress | |
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); | |
} else { | |
Update.printError(Serial); | |
} | |
} | |
}); | |
print_wakeup_reason(); | |
Serial.println("\nSTARTED or RESTARTED at " + myDate()); | |
eraseFile = false; | |
// The following use delayFunction(10) to avoid | |
//voltage drop down and Brownout detector was triggered exception; | |
filldatArray(); | |
printf("\n6 minutes probing\n"); | |
aNewline(); | |
fillPlaceHolders(); | |
int a = capacitiveProbe(1, 20, 1, probe1); //0=réchauffe, 3min + 4 min | |
pinMode(relay, OUTPUT); | |
if (a < dryRedLightAlarm) { | |
tooDryRedLightAlarm(a);//3 minutes si en dessous de dryRedLight | |
} | |
server.begin(); | |
//relay drains ~50 mA : higher power could result in | |
//Brownout detector was triggered? error on connecting WIFI | |
lightTest(); | |
previouzMillis = millis(); | |
pageOn = 7800000; | |
Serial.printf("%s\n %d minutes.\n", awaken, (pageOn / 60000)); | |
Serial.println("\nsetup ended at:\n" + myDate()); | |
} | |
/* | |
*/ | |
void loop() | |
{ | |
server.handleClient(); | |
delayFunction(0.000001); | |
if ((millis() - previouzMillis) > pageOn) | |
{ | |
whyling(awaken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment