diff --git a/README.md b/README.md index d6dd320..5cc2fc0 100644 --- a/README.md +++ b/README.md @@ -1509,7 +1509,7 @@ For Arduino IDE create/update `platform.local.txt`: Add/Update the following line: ``` - compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX + compiler.cpp.extra_flags=-DASYNCWEBSERVER_REGEX ``` For platformio modify `platformio.ini`: diff --git a/examples/SmartSwitch/PinOut_Notes.txt b/examples/SmartSwitch/PinOut_Notes.txt index c8c2a50..2634a19 100644 --- a/examples/SmartSwitch/PinOut_Notes.txt +++ b/examples/SmartSwitch/PinOut_Notes.txt @@ -1,13 +1,56 @@ -ESP12 +This application: +D2 = 4; // DHT DATA I/O +D3 = 0; // BUTTON - most modules have it populated on PCB +D4 = 2; // LED (RELAY) - most modules have it populated on PCB -static const uint8_t D0 = 16; -static const uint8_t D1 = 5; -static const uint8_t D2 = 4; // DHT DATA I/O -static const uint8_t D3 = 0; // BUTTON - most modules have it populated on PCB -static const uint8_t D4 = 2; // LED (RELAY) - most modules have it populated on PCB -static const uint8_t D5 = 14; -static const uint8_t D6 = 12; -static const uint8_t D7 = 13; -static const uint8_t D8 = 15; -static const uint8_t D9 = 3; -static const uint8_t D10 = 1; \ No newline at end of file + + +Pinout ESP12 (8266) +D GPIO In Out Notes + +D0 16 no interrupt no PWM or I2C support HIGH at boot used to wake up from deep sleep +D1 5 OK OK often used as SCL (I2C) +D2 4 OK OK often used as SDA (I2C) +D3 0 PU OK pulled up connected to FLASH button, boot fails if pulled LOW +D4 2 PU OK pulled up HIGH at boot connected to on-board LED, boot fails if pulled LOW +D5 14 OK OK SPI (SCLK) +D6 12 OK OK SPI (MISO) +D7 13 OK OK SPI (MOSI) +D8 15 pulled to GND OK SPI (CS) Boot fails if pulled HIGH +RX 3 OK RX pin HIGH at boot +TX 1 TX pin OK HIGH at boot debug output at boot, boot fails if pulled LOW +A0 ADC0 Analog Input + + +Pinout ESP32 +IO In Out Notes + +0 PU OK pulled-up input, outputs PWM signal at boot +1 TX OK debug output at boot +2 OK OK connected to on-board LED +3 OK RX pin HIGH at boot +4 OK OK +5 OK OK outputs PWM signal at boot + +6-11 x x connected to the integrated SPI flash + +12 OK OK boot fail if pulled high +13 OK OK +14 OK OK outputs PWM signal at boot +15 OK OK outputs PWM signal at boot +16 OK OK +17 OK OK +18 OK OK +19 OK OK +21 OK OK +22 OK OK +23 OK OK +25 OK OK +26 OK OK +27 OK OK +32 OK OK +33 OK OK +34 OK input only +35 OK input only +36 OK input only +39 OK input only \ No newline at end of file diff --git a/examples/SmartSwitch/README.md b/examples/SmartSwitch/README.md index 1d5171a..fd8cd40 100644 --- a/examples/SmartSwitch/README.md +++ b/examples/SmartSwitch/README.md @@ -1,16 +1,17 @@ -![](1.PNG) ![](2.PNG) -## -![](3.PNG) ![](4.PNG) - -## SmartSwitch -* Remote Temperature Control application with schedule (example car block heater or battery charger) -* Based on ESP_AsyncFSBrowser example -* Wide browser compatibility, no extra server-side needed. -* HTTP server and WebSocket, single port -* Standalone, no JS dependencies for the browser from Internet (I hope), ace editor included -* Added ESPAsyncWiFiManager -* Real Time (NTP) w/ Time Zones -* Memorized settings to EEPROM -* Multiple clients can be connected at same time, they see each other' requests -* Default credentials smart:switch -* Use latest ESP8266 core lib (from Github) +![](1.PNG) ![](2.PNG) +## +![](3.PNG) ![](4.PNG) + +## SmartSwitch +* Remote Temperature Control application with schedule (example car block heater or battery charger) +* Based on ESP_AsyncFSBrowser example +* Wide browser compatibility, no extra server-side needed +* HTTP server and WebSocket, single port +* Standalone, no JS dependencies for the browser from Internet (I hope), ace editor included +* Added ESPAsyncWiFiManager +* Fallback to an own WIFI_AP, no Internet to sync but by a browser the clock can be set once +* Real Time (NTP) w/ Time Zones +* Memorized settings to EEPROM +* Multiple clients can be connected at same time, they see each other' requests +* Default credentials smart:switch +* Use latest ESP8266 or ESP32 core(from GitHub) diff --git a/examples/SmartSwitch/SmartSwitch.ino b/examples/SmartSwitch/SmartSwitch.ino index 90aca030..3dbc61b 100644 --- a/examples/SmartSwitch/SmartSwitch.ino +++ b/examples/SmartSwitch/SmartSwitch.ino @@ -1,498 +1,529 @@ -/* -SmartSwitch application -Based on ESP_AsyncFSBrowser -Temperature Control for heater with schedule -Main purpose - for winter outside car block heater or battery charger -Wide browser compatibility, no other server-side needed. -HTTP server and WebSocket, single port -Standalone, no JS dependencies for the browser from Internet (I hope) -Based on ESP_AsyncFSBrowser -Real Time (NTP) w/ Time Zones -Memorized settings to EEPROM -Multiple clients can be connected at same time, they see each other requests -Use latest ESP core lib (from Github) -*/ - -#define USE_WFM // to use ESPAsyncWiFiManager -//#define DEL_WFM // delete Wifi credentials stored - //(use once then comment and flash again), also HTTP /erase-wifi can do the same live - -#define USE_AUTH // .setAuthentication for all static - -#include -#ifdef ESP32 - #include - #include - #include - #include - #include -#elif defined(ESP8266) - #include - #include - #include -#endif -#include -#ifdef USE_WFM - #include "ESPAsyncWiFiManager.h" -#endif -#include -#include -#include -#include -#include -#define MYTZ TZ_America_Toronto -#define RTC_UTC_TEST 1577836800 // Some Date - -#define EESC 100 // fixed eeprom address for sched choice -#define EECH 104 // fixed eeprom address to keep selected active channel, only for reference here -#define EEBEGIN EECH + 1 -#define EEMARK 0x5A -#define MEMMAX 2 // 0,1,2... last max index (only 3 channels) -#define EEALL 512 - -#define HYST 0.5 // C +/- hysteresis - -// DHT -#define DHTTYPE DHT22 // DHT 11 // DHT 22, AM2302, AM2321 // DHT 21, AM2301 -#define DHTPIN 4 //D2 - -DHT dht(DHTPIN, DHTTYPE); - -// SKETCH BEGIN MAIN DECLARATIONS -Ticker tim; -AsyncWebServer server(80); //single port - easy for forwarding -AsyncWebSocket ws("/ws"); - -#ifdef USE_WFM - #ifdef USE_EADNS - AsyncDNSServer dns; - #else - DNSServer dns; - #endif -#else - const char* ssid = "MYSSD"; - const char* password = "MYPASSWD"; -#endif -const char * hostName = "smartsw"; -const char* http_username = "smart"; // for SPIFFSEditor (and static html) -const char* http_password = "switch"; - -// RTC -static timeval tv; -static time_t now; - -// HW I/O -const int btnPin = 0; //D3 -const int ledPin = 2; //D4 -int btnState = HIGH; - -// Globals -uint8_t count = 0; -uint8_t sched = 0; // automatic schedule -byte memch = 0; // select memory "channel" to work with -float t = 0; -float h = 0; -bool udht = false; -bool heat_enabled_prev = false; -int ledState; - -struct EE_bl { - byte memid; //here goes the EEMARK stamp - uint8_t hstart; - uint8_t mstart; - uint8_t hstop; - uint8_t mstop; - float tempe; -}; - -EE_bl ee = {0,0,0,0,0,0.1}; //populate as initial - -// SUBS -void writeEE() { - ee.memid = EEMARK; - //EEPROM.put(EESC, sched); // only separately when needed with commit() - //EEPROM.put(EECH, memch); // not need to store and retrieve memch - EEPROM.put(EEBEGIN + memch*sizeof(ee), ee); - EEPROM.commit(); //needed for ESP8266? -} - -void readEE() { - byte ChkEE; - if (memch > MEMMAX) memch = 0; - EEPROM.get(EEBEGIN + memch*sizeof(ee), ChkEE); - if (ChkEE == EEMARK){ //otherwise stays with defaults - EEPROM.get(EEBEGIN + memch*sizeof(ee), ee); - EEPROM.get(EESC, sched); - if (sched > MEMMAX + 1) sched = 0; - } -} - -void showTime() -{ - byte tmpch = 0; - bool heat_enabled = false; - - gettimeofday(&tv, nullptr); - now = time(nullptr); - const tm* tm = localtime(&now); - ws.printfAll("Now,Clock,%02d:%02d,%d", tm->tm_hour, tm->tm_min, tm->tm_wday); - if ((2==tm->tm_hour )&&(2==tm->tm_min)) { - configTime(MYTZ, "pool.ntp.org"); - Serial.print(F("Sync Clock at 02:02\n")); - } - Serial.printf("RTC: %02d:%02d\n", tm->tm_hour, tm->tm_min); - - if (sched == 0) { // automatic - if ((tm->tm_wday > 0)&&(tm->tm_wday < 6)) tmpch = 0; //Mon - Fri - else if (tm->tm_wday == 6) tmpch = 1; //Sat - else if (tm->tm_wday == 0) tmpch = 2; //Sun - } else { // manual - tmpch = sched - 1; //and stays - } - - if (tmpch != memch) { // update if different - memch = tmpch; - readEE(); - ws.printfAll("Now,Setting,%02d:%02d,%02d:%02d,%+2.1f", ee.hstart, ee.mstart, ee.hstop, ee.mstop, ee.tempe); - } - - // process smart switch by time and temperature - uint16_t xmi = (uint16_t)(60*tm->tm_hour) + tm->tm_min; // max 24h = 1440min, current time - uint16_t bmi = (uint16_t)(60*ee.hstart) + ee.mstart; // begin in minutes - uint16_t emi = (uint16_t)(60*ee.hstop) + ee.mstop; // end in minutes - - if (bmi == emi) heat_enabled = false; - else { //enable smart if different - - if (((bmi < emi)&&(bmi <= xmi)&&(xmi < emi))|| - ((emi < bmi)&&((bmi <= xmi)||(xmi < emi)))) { - heat_enabled = true; - } else heat_enabled = false; - } - - if (heat_enabled_prev) { // smart control (delayed one cycle) - if (((t - HYST) < ee.tempe)&&(ledState == HIGH)) { // OFF->ON once - ledState = LOW; - digitalWrite(ledPin, ledState); // apply change - ws.textAll("led,ledon"); - } - if ((((t + HYST) > ee.tempe)&&(ledState == LOW))||(!heat_enabled)) { // ON->OFF once, also turn off at end of period. - ledState = HIGH; - digitalWrite(ledPin, ledState); // apply change - ws.textAll("led,ledoff"); - } - Serial.printf(ledState ? "LED OFF" : "LED ON"); - Serial.print(F(", Smart enabled\n")); - } - heat_enabled_prev = heat_enabled; //update -} - -void updateDHT(){ - h = dht.readHumidity(); - t = dht.readTemperature(); //Celsius or dht.readTemperature(true) for Fahrenheit - if (isnan(h) || isnan(t)) { - Serial.print(F("Failed to read from DHT sensor!")); - h = 0; // debug w/o sensor - t = 0; - } -} - -void analogSample() -{ - ws.printfAll("wpMeter,Arduino,%+2.1f,%2.1f,%d", t, h, heat_enabled_prev); - Serial.printf("T/H.: %+2.1f°C/%2.1f%%,%d\n", t, h, heat_enabled_prev); -} - -void checkPhysicalButton() -{ - if (digitalRead(btnPin) == LOW) { - if (btnState != LOW) { // btnState is used to avoid sequential toggles - ledState = !ledState; - digitalWrite(ledPin, ledState); - if (ledState) ws.textAll("led,ledoff"); - else ws.textAll("led,ledon"); - } - btnState = LOW; - } else { - btnState = HIGH; - } -} - -void mytimer() { - ++count; //200ms increments - checkPhysicalButton(); - if ((count % 25) == 1) { // update temp every 5 seconds - analogSample(); - udht = true; - } - if ((count % 50) == 0) { // update temp every 10 seconds - ws.cleanupClients(); - } - if (count >= 150) { // cycle every 30 sec - showTime(); - count = 0; - } -} - -// server -void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ - if(type == WS_EVT_CONNECT){ - Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); - //client->printf("Hello Client %u :)", client->id()); - //client->ping(); - - IPAddress ip = client->remoteIP(); - Serial.printf("[%u] Connected from %d.%d.%d.%d\n", client->id(), ip[0], ip[1], ip[2], ip[3]); - showTime(); - analogSample(); - if (ledState) ws.textAll("led,ledoff"); - else ws.textAll("led,ledon"); - - ws.printfAll("Now,Setting,%02d:%02d,%02d:%02d,%+2.1f", ee.hstart, ee.mstart, ee.hstop, ee.mstop, ee.tempe); - ws.printfAll("Now,sched,%d", sched); - - } else if(type == WS_EVT_DISCONNECT){ - Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id()); - ws.textAll("Now,remoff"); - - } else if(type == WS_EVT_ERROR){ - Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); - } else if(type == WS_EVT_PONG){ - Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:""); - } else if(type == WS_EVT_DATA){ - AwsFrameInfo * info = (AwsFrameInfo*)arg; - String msg = ""; - if(info->final && info->index == 0 && info->len == len){ - //the whole message is in a single frame and we got all of it's data - Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len); - - if(info->opcode == WS_TEXT){ - for(size_t i=0; i < info->len; i++) { //debug - msg += (char) data[i]; - } - if(data[0] == 'L') { // LED - if(data[1] == '1') { - ledState = LOW; - ws.textAll("led,ledon"); // for others - } - else if(data[1] == '0') { - ledState = HIGH; - ws.textAll("led,ledoff"); - } - digitalWrite(ledPin, ledState); // apply change - - - } else if(data[0] == 'T') { // timeset - if (len > 11) { - data[3] = data[6] = data[9] = data[12] = 0; // cut strings - ee.hstart = (uint8_t) atoi((const char *) &data[1]); - ee.mstart = (uint8_t) atoi((const char *) &data[4]); - ee.hstop = (uint8_t) atoi((const char *) &data[7]); - ee.mstop = (uint8_t) atoi((const char *) &data[10]); - Serial.printf("[%u] Timer set %02d:%02d - %02d:%02d\n", client->id(), ee.hstart, ee.mstart, ee.hstop, ee.mstop); - writeEE(); - memch = 255; // to force showTime()to send Setting - showTime(); - } - } else if(data[0] == 'W') { // temperatureset - if (len > 3) { - if (ee.tempe != (float) atof((const char *) &data[1])){ - ee.tempe = (float) atof((const char *) &data[1]); - Serial.printf("[%u] Temp set %+2.1f\n", client->id(), ee.tempe); - writeEE(); - memch = 255; // to force showTime()to send Setting - showTime(); - } - } - } else if ((data[0] == 'Z')&&(len > 2)) { // sched - data[2] = 0; - if (sched != (uint8_t) atoi((const char *) &data[1])){ - sched = (uint8_t) atoi((const char *) &data[1]); - EEPROM.put(EESC, sched); //separately - EEPROM.commit(); //needed for ESP8266? - ws.printfAll("Now,sched,%d", sched); - showTime(); - } - } - - } else { - char buff[3]; - for(size_t i=0; i < info->len; i++) { - sprintf(buff, "%02x ", (uint8_t) data[i]); - msg += buff ; - } - } - Serial.printf("%s\n",msg.c_str()); - - if(info->opcode == WS_TEXT) - client->text("I got your text message"); - else - client->binary("I got your binary message"); - - } else { - //message is comprised of multiple frames or the frame is split into multiple packets - if(info->index == 0){ - if(info->num == 0) - Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary"); - Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len); - } - - Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len); - - if(info->opcode == WS_TEXT){ - for(size_t i=0; i < len; i++) { - msg += (char) data[i]; - } - } else { - char buff[3]; - for(size_t i=0; i < len; i++) { - sprintf(buff, "%02x ", (uint8_t) data[i]); - msg += buff ; - } - } - Serial.printf("%s\n",msg.c_str()); - - if((info->index + len) == info->len){ - Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len); - if(info->final){ - Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary"); - if(info->message_opcode == WS_TEXT) - client->text("I got your text message"); - else - client->binary("I got your binary message"); - } - } - } - } -} - - -// setup ----------------------------------- - -void setup(){ - Serial.begin(115200); - Serial.setDebugOutput(true); - -//Wifi -#ifdef USE_WFM - AsyncWiFiManager wifiManager(&server,&dns); - #ifdef DEL_WFM - wifiManager.resetSettings(); - #endif - wifiManager.autoConnect(hostName); -#else - //WiFi.mode(WIFI_AP_STA); // Core SVN 5179 use STA as default interface in mDNS (#7042) - //WiFi.softAP(hostName); // Core SVN 5179 use STA as default interface in mDNS (#7042) - WiFi.mode(WIFI_STA); // Core SVN 5179 use STA as default interface in mDNS (#7042) - WiFi.begin(ssid, password); - if (WiFi.waitForConnectResult() != WL_CONNECTED) { - Serial.print(F("STA: Failed!\n")); - WiFi.disconnect(false); - delay(1000); - WiFi.begin(ssid, password); - } -#endif - - Serial.print(F("*CONNECTED*\n")); - -//DHT - dht.begin(); - updateDHT(); //first reading takes time, hold here than the loop; - -//Real Time - time_t rtc = RTC_UTC_TEST; - timeval tv = { rtc, 0 }; - //timezone tz = { 0, 0 }; //(insert) <#5194 - settimeofday(&tv, nullptr); //settimeofday(&tv, &tz); // <#5194 - configTime(MYTZ, "pool.ntp.org"); - -//MDNS (not needed) - // MDNS.begin(hostName); - // MDNS.addService("http","tcp",80); // Core SVN 5179 use STA as default interface in mDNS (#7042) - -//I/O & DHT - pinMode(ledPin, OUTPUT); - pinMode(btnPin, INPUT_PULLUP); - -//EE - EEPROM.begin(EEALL); - //EEPROM.get(EECH, memch); //current channel, no need - readEE(); // populate structure if healthy - digitalWrite(ledPin, ledState); - Serial.printf("Timer set %02d:%02d - %02d:%02d\n", ee.hstart, ee.mstart, ee.hstop, ee.mstop); - Serial.printf("Temp set %+2.1f\n", ee.tempe); - -//SPIFFS - SPIFFS.begin(); - - ws.onEvent(onWsEvent); - server.addHandler(&ws); - -#ifdef ESP32 - server.addHandler(new SPIFFSEditor(SPIFFS, http_username,http_password)); -#elif defined(ESP8266) - server.addHandler(new SPIFFSEditor(http_username,http_password)); -#endif - - server.on("/free-ram", HTTP_GET, [](AsyncWebServerRequest *request){ // direct request->answer - request->send(200, "text/plain", String(ESP.getFreeHeap())); - }); - - server.on("/hw-reset", HTTP_GET, [](AsyncWebServerRequest *request){ - request->onDisconnect([]() { -#ifdef ESP32 - ESP.restart(); -#elif defined(ESP8266) - ESP.reset(); -#endif - }); - request->send(200, "text/plain","Restarting ..."); - }); - - server.on("/erase-wifi", HTTP_GET, [](AsyncWebServerRequest *request){ - request->onDisconnect([]() { - WiFi.disconnect(true); -#ifdef ESP32 - ESP.restart(); -#elif defined(ESP8266) - ESP.reset(); -#endif - }); - request->send(200, "text/plain","Erasing WiFi data ..."); - }); - -#ifdef USE_AUTH - server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm").setAuthentication(http_username,http_password); -#else - server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm"); -#endif - - server.onNotFound([](AsyncWebServerRequest *request){ // nothing known - Serial.print(F("NOT_FOUND: ")); - if (request->method() == HTTP_OPTIONS) request->send(200); //CORS - else request->send(404); - }); - - DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");//CORS - server.begin(); - - //Timer tick - tim.attach(0.2, mytimer); //every 0.2s - - //OTA - ArduinoOTA.setHostname(hostName); - ArduinoOTA.onStart([]() { - Serial.print(F("OTA Started ...\n")); - ws.textAll("Now,OTA"); // for all clients - }); - ArduinoOTA.begin(); -} // setup end - -// loop ----------------------------------- -void loop(){ - if (udht){ // 5sec - updateDHT(); - udht = false; - } - ArduinoOTA.handle(); -} +/* +SmartSwitch application +Based on ESP_AsyncFSBrowser +Temperature Control for heater with schedule +Main purpose - for winter outside car block heater or battery charger +Wide browser compatibility, no other server-side needed. +HTTP server and WebSocket, single port +Standalone, no JS dependencies for the browser from Internet (I hope) +Based on ESP_AsyncFSBrowser +Real Time (NTP) w/ Time Zones +Memorized settings to EEPROM +Multiple clients can be connected at same time, they see each other requests +Use latest ESP core lib (from Github) +*/ + +#define USE_WFM // to use ESPAsyncWiFiManager +//#define DEL_WFM // delete Wifi credentials stored + //(use once then comment and flash again), also HTTP /erase-wifi can do the same live + +#define USE_AUTH // .setAuthentication for all static + +#include +#ifdef ESP32 + #include + #include + #include + #include + #include +#elif defined(ESP8266) + #include + #include + #include +#endif +#include +#ifdef USE_WFM + #include "ESPAsyncWiFiManager.h" +#endif +#include +#include +#include +#include + +#define RTC_UTC_TEST 1577836800 // Some Date +#ifdef ESP32 + #define MYTZ -5*3600,3600 +#elif defined(ESP8266) + #include + #define MYTZ TZ_America_Toronto +#endif + +#define EESC 100 // fixed eeprom address for sched choice +#define EECH 104 // fixed eeprom address to keep selected active channel, only for reference here +#define EEBEGIN EECH + 1 +#define EEMARK 0x5A +#define MEMMAX 2 // 0,1,2... last max index (only 3 channels) +#define EEALL 512 + +#define HYST 0.5 // C +/- hysteresis + +// DHT +#define DHTTYPE DHT22 // DHT 11 // DHT 22, AM2302, AM2321 // DHT 21, AM2301 +#define DHTPIN 4 //D2 + +DHT dht(DHTPIN, DHTTYPE); + +// SKETCH BEGIN MAIN DECLARATIONS +Ticker tim; +AsyncWebServer server(80); //single port - easy for forwarding +AsyncWebSocket ws("/ws"); + +#ifdef USE_WFM + #ifdef USE_EADNS + AsyncDNSServer dns; + #else + DNSServer dns; + #endif + +//Fallback timeout in seconds allowed to config or it creates an own AP, then serves 192.168.4.1 + #define FBTO 120 + const char* fbssid = "FBSSW"; + const char* fbpassword = "FBpassword4"; + +#else + const char* ssid = "MYROUTERSSD"; + const char* password = "MYROUTERPASSWD"; +#endif +const char* hostName = "smartsw"; +const char* http_username = "smart"; // for SPIFFSEditor (and static html) +const char* http_password = "switch"; + +// RTC +static timeval tv; +static time_t now; + +// HW I/O +const int btnPin = 0; //D3 +const int ledPin = 2; //D4 +int btnState = HIGH; + +// Globals +uint8_t count = 0; +uint8_t sched = 0; // automatic schedule +byte memch = 0; // select memory "channel" to work with +float t = 0; +float h = 0; +bool udht = false; +bool heat_enabled_prev = false; +int ledState; + +struct EE_bl { + byte memid; //here goes the EEMARK stamp + uint8_t hstart; + uint8_t mstart; + uint8_t hstop; + uint8_t mstop; + float tempe; +}; + +EE_bl ee = {0,0,0,0,0,0.1}; //populate as initial + +// SUBS +void writeEE() { + ee.memid = EEMARK; + //EEPROM.put(EESC, sched); // only separately when needed with commit() + //EEPROM.put(EECH, memch); // not need to store and retrieve memch + EEPROM.put(EEBEGIN + memch*sizeof(ee), ee); + EEPROM.commit(); //needed for ESP8266? +} + +void readEE() { + byte ChkEE; + if (memch > MEMMAX) memch = 0; + EEPROM.get(EEBEGIN + memch*sizeof(ee), ChkEE); + if (ChkEE == EEMARK){ //otherwise stays with defaults + EEPROM.get(EEBEGIN + memch*sizeof(ee), ee); + EEPROM.get(EESC, sched); + if (sched > MEMMAX + 1) sched = 0; + } +} + +void showTime() +{ + byte tmpch = 0; + bool heat_enabled = false; + + gettimeofday(&tv, nullptr); + now = time(nullptr); + const tm* tm = localtime(&now); + ws.printfAll("Now,Clock,%02d:%02d,%d", tm->tm_hour, tm->tm_min, tm->tm_wday); + if ((2==tm->tm_hour )&&(2==tm->tm_min)) { + configTime(MYTZ, "pool.ntp.org"); + Serial.print(F("Sync Clock at 02:02\n")); + } + Serial.printf("RTC: %02d:%02d\n", tm->tm_hour, tm->tm_min); + + if (sched == 0) { // automatic + if ((tm->tm_wday > 0)&&(tm->tm_wday < 6)) tmpch = 0; //Mon - Fri + else if (tm->tm_wday == 6) tmpch = 1; //Sat + else if (tm->tm_wday == 0) tmpch = 2; //Sun + } else { // manual + tmpch = sched - 1; //and stays + } + + if (tmpch != memch) { // update if different + memch = tmpch; + readEE(); + ws.printfAll("Now,Setting,%02d:%02d,%02d:%02d,%+2.1f", ee.hstart, ee.mstart, ee.hstop, ee.mstop, ee.tempe); + } + + // process smart switch by time and temperature + uint16_t xmi = (uint16_t)(60*tm->tm_hour) + tm->tm_min; // max 24h = 1440min, current time + uint16_t bmi = (uint16_t)(60*ee.hstart) + ee.mstart; // begin in minutes + uint16_t emi = (uint16_t)(60*ee.hstop) + ee.mstop; // end in minutes + + if (bmi == emi) heat_enabled = false; + else { //enable smart if different + + if (((bmi < emi)&&(bmi <= xmi)&&(xmi < emi))|| + ((emi < bmi)&&((bmi <= xmi)||(xmi < emi)))) { + heat_enabled = true; + } else heat_enabled = false; + } + + if (heat_enabled_prev) { // smart control (delayed one cycle) + if (((t - HYST) < ee.tempe)&&(ledState == HIGH)) { // OFF->ON once + ledState = LOW; + digitalWrite(ledPin, ledState); // apply change + ws.textAll("led,ledon"); + } + if ((((t + HYST) > ee.tempe)&&(ledState == LOW))||(!heat_enabled)) { // ON->OFF once, also turn off at end of period. + ledState = HIGH; + digitalWrite(ledPin, ledState); // apply change + ws.textAll("led,ledoff"); + } + Serial.printf(ledState ? "LED OFF" : "LED ON"); + Serial.print(F(", Smart enabled\n")); + } + heat_enabled_prev = heat_enabled; //update +} + +void updateDHT(){ + h = dht.readHumidity(); + t = dht.readTemperature(); //Celsius or dht.readTemperature(true) for Fahrenheit + if (isnan(h) || isnan(t)) { + Serial.print(F("Failed to read from DHT sensor!")); + h = 0; // debug w/o sensor + t = 0; + } +} + +void analogSample() +{ + ws.printfAll("wpMeter,Arduino,%+2.1f,%2.1f,%d", t, h, heat_enabled_prev); + Serial.printf("T/H.: %+2.1f°C/%2.1f%%,%d\n", t, h, heat_enabled_prev); +} + +void checkPhysicalButton() +{ + if (digitalRead(btnPin) == LOW) { + if (btnState != LOW) { // btnState is used to avoid sequential toggles + ledState = !ledState; + digitalWrite(ledPin, ledState); + if (ledState) ws.textAll("led,ledoff"); + else ws.textAll("led,ledon"); + } + btnState = LOW; + } else { + btnState = HIGH; + } +} + +void mytimer() { + ++count; //200ms increments + checkPhysicalButton(); + if ((count % 25) == 1) { // update temp every 5 seconds + analogSample(); + udht = true; + } + if ((count % 50) == 0) { // update temp every 10 seconds + ws.cleanupClients(); + } + if (count >= 150) { // cycle every 30 sec + showTime(); + count = 0; + } +} + +// server +void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ + if(type == WS_EVT_CONNECT){ + Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); + //client->printf("Hello Client %u :)", client->id()); + //client->ping(); + + IPAddress ip = client->remoteIP(); + Serial.printf("[%u] Connected from %d.%d.%d.%d\n", client->id(), ip[0], ip[1], ip[2], ip[3]); + showTime(); + analogSample(); + if (ledState) ws.textAll("led,ledoff"); + else ws.textAll("led,ledon"); + + ws.printfAll("Now,Setting,%02d:%02d,%02d:%02d,%+2.1f", ee.hstart, ee.mstart, ee.hstop, ee.mstop, ee.tempe); + ws.printfAll("Now,sched,%d", sched); + + } else if(type == WS_EVT_DISCONNECT){ + Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id()); + ws.textAll("Now,remoff"); + + } else if(type == WS_EVT_ERROR){ + Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); + } else if(type == WS_EVT_PONG){ + Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:""); + } else if(type == WS_EVT_DATA){ + AwsFrameInfo * info = (AwsFrameInfo*)arg; + String msg = ""; + if(info->final && info->index == 0 && info->len == len){ + //the whole message is in a single frame and we got all of it's data + Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len); + + if(info->opcode == WS_TEXT){ + for(size_t i=0; i < info->len; i++) { //debug + msg += (char) data[i]; + } + if(data[0] == 'L') { // LED + if(data[1] == '1') { + ledState = LOW; + ws.textAll("led,ledon"); // for others + } + else if(data[1] == '0') { + ledState = HIGH; + ws.textAll("led,ledoff"); + } + digitalWrite(ledPin, ledState); // apply change + + + } else if(data[0] == 'T') { // timeset + if (len > 11) { + data[3] = data[6] = data[9] = data[12] = 0; // cut strings + ee.hstart = (uint8_t) atoi((const char *) &data[1]); + ee.mstart = (uint8_t) atoi((const char *) &data[4]); + ee.hstop = (uint8_t) atoi((const char *) &data[7]); + ee.mstop = (uint8_t) atoi((const char *) &data[10]); + Serial.printf("[%u] Timer set %02d:%02d - %02d:%02d\n", client->id(), ee.hstart, ee.mstart, ee.hstop, ee.mstop); + writeEE(); + memch = 255; // to force showTime()to send Setting + showTime(); + } + } else if(data[0] == 'W') { // temperatureset + if (len > 3) { + if (ee.tempe != (float) atof((const char *) &data[1])){ + ee.tempe = (float) atof((const char *) &data[1]); + Serial.printf("[%u] Temp set %+2.1f\n", client->id(), ee.tempe); + writeEE(); + memch = 255; // to force showTime()to send Setting + showTime(); + } + } + } else if ((data[0] == 'Z')&&(len > 2)) { // sched + data[2] = 0; + if (sched != (uint8_t) atoi((const char *) &data[1])){ + sched = (uint8_t) atoi((const char *) &data[1]); + EEPROM.put(EESC, sched); //separately + EEPROM.commit(); //needed for ESP8266? + ws.printfAll("Now,sched,%d", sched); + showTime(); + } + } + + } else { + char buff[3]; + for(size_t i=0; i < info->len; i++) { + sprintf(buff, "%02x ", (uint8_t) data[i]); + msg += buff ; + } + } + Serial.printf("%s\n",msg.c_str()); + + if(info->opcode == WS_TEXT) + client->text("I got your text message"); + else + client->binary("I got your binary message"); + + } else { + //message is comprised of multiple frames or the frame is split into multiple packets + if(info->index == 0){ + if(info->num == 0) + Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary"); + Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len); + } + + Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len); + + if(info->opcode == WS_TEXT){ + for(size_t i=0; i < len; i++) { + msg += (char) data[i]; + } + } else { + char buff[3]; + for(size_t i=0; i < len; i++) { + sprintf(buff, "%02x ", (uint8_t) data[i]); + msg += buff ; + } + } + Serial.printf("%s\n",msg.c_str()); + + if((info->index + len) == info->len){ + Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len); + if(info->final){ + Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary"); + if(info->message_opcode == WS_TEXT) + client->text("I got your text message"); + else + client->binary("I got your binary message"); + } + } + } + } +} + + +// setup ----------------------------------- + +void setup(){ + Serial.begin(115200); + Serial.setDebugOutput(true); + +//Wifi +#ifdef USE_WFM + AsyncWiFiManager wifiManager(&server,&dns); + #ifdef DEL_WFM + wifiManager.resetSettings(); + #endif + wifiManager.setTimeout(FBTO); // seconds to config or it creates an own AP, then browse 192.168.4.1 + if (!wifiManager.autoConnect(hostName)){ + Serial.print(F("*FALLBACK AP*\n")); + WiFi.mode(WIFI_AP); + WiFi.softAP(fbssid, fbpassword); + // MDNS.begin(fbssid); + // MDNS.addService("http","tcp",80); // Core SVN 5179 use STA as default interface in mDNS (#7042) + } + +#else +// Manual simple STA mode to connect to known router + //WiFi.mode(WIFI_AP_STA); // Core SVN 5179 use STA as default interface in mDNS (#7042) + //WiFi.softAP(hostName); // Core SVN 5179 use STA as default interface in mDNS (#7042) + WiFi.mode(WIFI_STA); // Core SVN 5179 use STA as default interface in mDNS (#7042) + WiFi.begin(ssid, password); + if (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.print(F("STA: Failed!\n")); + WiFi.disconnect(false); + delay(1000); + WiFi.begin(ssid, password); + } +#endif + + Serial.print(F("*CONNECTED*\n")); + +//DHT + dht.begin(); + updateDHT(); //first reading takes time, hold here than the loop; + +//Real Time + time_t rtc = RTC_UTC_TEST; + timeval tv = { rtc, 0 }; + //timezone tz = { 0, 0 }; //(insert) <#5194 + settimeofday(&tv, nullptr); //settimeofday(&tv, &tz); // <#5194 + configTime(MYTZ, "pool.ntp.org"); + +//MDNS (not needed) + // MDNS.begin(hostName); + // MDNS.addService("http","tcp",80); // Core SVN 5179 use STA as default interface in mDNS (#7042) + +//I/O & DHT + pinMode(ledPin, OUTPUT); + pinMode(btnPin, INPUT_PULLUP); + +//EE + EEPROM.begin(EEALL); + //EEPROM.get(EECH, memch); //current channel, no need + readEE(); // populate structure if healthy + digitalWrite(ledPin, ledState); + Serial.printf("Timer set %02d:%02d - %02d:%02d\n", ee.hstart, ee.mstart, ee.hstop, ee.mstop); + Serial.printf("Temp set %+2.1f\n", ee.tempe); + +//SPIFFS + SPIFFS.begin(); + + ws.onEvent(onWsEvent); + server.addHandler(&ws); + +#ifdef ESP32 + server.addHandler(new SPIFFSEditor(SPIFFS, http_username,http_password)); +#elif defined(ESP8266) + server.addHandler(new SPIFFSEditor(http_username,http_password)); +#endif + + server.on("/free-ram", HTTP_GET, [](AsyncWebServerRequest *request){ // direct request->answer + request->send(200, "text/plain", String(ESP.getFreeHeap())); + }); + + + server.on("/get-time", HTTP_GET, [](AsyncWebServerRequest *request){ + if(request->hasParam("btime")){ + time_t rtc = (request->getParam("btime")->value()).toInt(); + timeval tv = { rtc, 0 }; + settimeofday(&tv, nullptr); + } + request->send(200, "text/plain","Got browser time ..."); + }); + + + server.on("/hw-reset", HTTP_GET, [](AsyncWebServerRequest *request){ + request->onDisconnect([]() { +#ifdef ESP32 + ESP.restart(); +#elif defined(ESP8266) + ESP.reset(); +#endif + }); + request->send(200, "text/plain","Restarting ..."); + }); + + server.on("/erase-wifi", HTTP_GET, [](AsyncWebServerRequest *request){ + request->onDisconnect([]() { + WiFi.disconnect(true); +#ifdef ESP32 + ESP.restart(); +#elif defined(ESP8266) + ESP.reset(); +#endif + }); + request->send(200, "text/plain","Erasing WiFi data ..."); + }); + +#ifdef USE_AUTH + server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm").setAuthentication(http_username,http_password); +#else + server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm"); +#endif + + server.onNotFound([](AsyncWebServerRequest *request){ // nothing known + Serial.print(F("NOT_FOUND: ")); + if (request->method() == HTTP_OPTIONS) request->send(200); //CORS + else request->send(404); + }); + + DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");//CORS + server.begin(); + + //Timer tick + tim.attach(0.2, mytimer); //every 0.2s + + //OTA + ArduinoOTA.setHostname(hostName); + ArduinoOTA.onStart([]() { + Serial.print(F("OTA Started ...\n")); + ws.textAll("Now,OTA"); // for all clients + }); + ArduinoOTA.begin(); +} // setup end + +// loop ----------------------------------- +void loop(){ + if (udht){ // 5sec + updateDHT(); + udht = false; + } + ArduinoOTA.handle(); +} diff --git a/examples/SmartSwitch/data/ace.ico.gz b/examples/SmartSwitch/data/ace.ico.gz new file mode 100644 index 0000000..49e15bc Binary files /dev/null and b/examples/SmartSwitch/data/ace.ico.gz differ diff --git a/examples/SmartSwitch/data/acefull.js.gz b/examples/SmartSwitch/data/acefull.js.gz index 55e6039..0ff2a5f 100644 Binary files a/examples/SmartSwitch/data/acefull.js.gz and b/examples/SmartSwitch/data/acefull.js.gz differ diff --git a/examples/SmartSwitch/data/index.htm b/examples/SmartSwitch/data/index.htm index 65a7eaf..9f7da5b 100644 --- a/examples/SmartSwitch/data/index.htm +++ b/examples/SmartSwitch/data/index.htm @@ -1,6 +1,6 @@ + - Smart Switch @@ -54,6 +54,7 @@ appearance: none; } + .switch { position: relative; display: inline-block; @@ -116,6 +117,12 @@ .clk { font-size: 54px; color: #444; + cursor: pointer + } + + .clk2 { + font-size: 32px; + color: #444 } .clear:after, @@ -136,6 +143,7 @@ left: 0; margin: auto; max-width: 500px; + text-align: center; border: 0px solid #ccc } @@ -185,7 +193,7 @@ .container { display: inline-block; position: relative; - padding-left: 28px; + padding-left: 19px; padding-top: 4px; margin-top: 8px; margin-bottom: 8px; @@ -194,6 +202,9 @@ -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; + + width: 12.5%; + user-select: none } @@ -239,6 +250,7 @@ border-radius: 50%; background: white } + @@ -249,7 +261,7 @@
-