diff --git a/examples/ControlAssist-ConnectionCtrl/ControlAssist-ConnectionCtrl.ino b/examples/ControlAssist-ConnectionCtrl/ControlAssist-ConnectionCtrl.ino new file mode 100644 index 0000000..f58df19 --- /dev/null +++ b/examples/ControlAssist-ConnectionCtrl/ControlAssist-ConnectionCtrl.ino @@ -0,0 +1,204 @@ +#if defined(ESP32) + #include + #include + #define WEB_SERVER WebServer + #define ADC_PIN 36 +#else + #include + #include + #define WEB_SERVER ESP8266WebServer + #define ADC_PIN A0 +#endif + +#define LOGGER_LOG_LEVEL 5 // Define log level for this module +#include // Control assist class + +const char st_ssid[]="mdk3"; // Put connection SSID here. On empty an AP will be started +const char st_pass[]="2843028858"; // Put your wifi passowrd. +unsigned long pingMillis = millis(); // Ping millis +unsigned long disconnMillis = millis(); // Wifi disconnect millis + +ControlAssist ctrl; // Control assist class +WEB_SERVER server(80); // Web server on port 80 + +time_t disconTime = 10 * 1000; // Wifi disconnection duration + +PROGMEM const char HTML_BODY[] = R"=====( + + +
+
+

ControlAssist Websockets connection

+
+
+ + + + + + + + + + + + + + + + + + +
MacAddressAA:DD:CC:EE:FF:KK
Wifi RSSI
Disconnect +  WiFi for +  Seconds +
 
 
Disconnected
+
+
+ + + +)====="; + +// Disconnect WiFi +void disconnect(){ + LOG_D("Disconnect WiFi for %s seconds\n", String(disconTime / 1000L).c_str()); + ctrl.put("wifi_rssi", -120 ); + ctrl.loop(); + ctrl.sendSystemMsg("C"); + time_t s = millis(); + while(millis() - s < 100 ) + ctrl.loop(); + WiFi.disconnect(); + disconnMillis = millis(); +} + +// Change handler to handle websockets changes +void changeHandler(uint8_t ndx){ + String key = ctrl[ndx].key; + if(key == "sleep_time" ) + disconTime = ctrl[key].toInt() * 1000; + else if(key == "disconnect_button") + disconnect(); + + LOG_D("changeHandler: ndx: %02i, key: %s = %s\n",ndx, key.c_str(), ctrl[key].c_str()); +} +// Connect WiFi +void connect(){ + if(WiFi.status() == WL_CONNECTED ) return; + // Connect WIFI ? + if(strlen(st_ssid)>0){ + LOG_E("Connect Wifi to %s.\n", st_ssid); + WiFi.mode(WIFI_STA); + WiFi.begin(st_ssid, st_pass); + uint32_t startAttemptTime = millis(); + while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 20000) { + Serial.print("."); + delay(500); + Serial.flush(); + } + Serial.println(); + } + + // Check connection + if(WiFi.status() == WL_CONNECTED ){ + LOG_I("Wifi AP SSID: %s connected, use 'http://%s' to connect\n", st_ssid, WiFi.localIP().toString().c_str()); + }else{ + LOG_E("Connect failed.\n"); + LOG_I("Starting AP.\n"); + String mac = WiFi.macAddress(); + mac.replace(":",""); + String hostName = "ControlAssist_" + mac.substring(6); + WiFi.mode(WIFI_AP); + WiFi.softAP(hostName.c_str(),"",1); + LOG_I("Wifi AP SSID: %s started, use 'http://%s' to connect\n", WiFi.softAPSSID().c_str(), WiFi.softAPIP().toString().c_str()); + if (MDNS.begin(hostName.c_str())) LOG_V("AP MDNS responder Started\n"); + } +} + +void setup() { + Serial.begin(115200); + Serial.print("\n\n\n\n"); + Serial.flush(); + LOG_I("Starting..\n"); + // Connect WiFi + connect(); + + // Control assist setup + ctrl.setHtmlBody(HTML_BODY); + ctrl.bind("mac_address", WiFi.macAddress().c_str() ); + ctrl.bind("wifi_rssi"); + ctrl.bind("sleep_time", disconTime / 1000); + ctrl.bind("disconnect_button"); + ctrl.setAutoSendOnCon(true); + // Every time a variable changed changeHandler will be called + ctrl.setGlobalCallback(changeHandler); + + // Start web sockets + ctrl.begin(); + //ctrl.put("sleep_time", disconTime / 1000,true); + LOG_V("ControlAssist started.\n"); + + // Setup webserver + server.on("/", []() { + server.setContentLength(CONTENT_LENGTH_UNKNOWN); + String res = ""; + res.reserve(CTRLASSIST_STREAM_CHUNKSIZE); + while( ctrl.getHtmlChunk(res)){ + server.sendContent(res); + } + }); + + // Start webs server + server.begin(); + LOG_V("HTTP server started\n"); +} + +void loop() { + // Change html control values + if (millis() - pingMillis >= 3000){ + // Update control assist variables + ctrl.put("wifi_rssi", WiFi.RSSI() ); + + pingMillis = millis(); + } + // Change html control values + if ( WiFi.status() != WL_CONNECTED && millis() - disconnMillis >= disconTime){ + // Re connect + connect(); + disconnMillis = millis(); + } + #if not defined(ESP32) + if(MDNS.isRunning()) MDNS.update(); // Handle MDNS + #endif + // Handler webserver clients + server.handleClient(); + // Handle websockets + ctrl.loop(); +} \ No newline at end of file