//================================================================================================================================= // NAME: ESP_8266_Local Server basic // Author Ugo Schiara // Date 05/02/2023 // Purpose: to configure the NodeMCU8266 Mini D1 from AZ-Delivery as a local WiFi Access Point (AP) to allow a MIT AI2 app // to send messages with the aim of driving actuators (i.e. LED's or relays. The PAD is a client. // Version 1.0 Acquires a PIR sensor to swith on/of a relay. The relay can be forced On/Off by the WiFi from the PAD (running AI2) // Version 2.0 10/02/2023 Adds to the communication to two STA nodes NODEmcu8266 each one drives a relay every 5s ON/OFF // This AP receives every 250 ms a "NEWS" request by the client PAD and responds with the status of the PIR //================================================================================================================================= //#include //#include #include ESP8266WebServer server(80); //Port 80 is standard #define TBASE 4000 // The filter in time to avoid multiple PIR lectures #define T_on 5 // How many seconds the relay shall stay ON if energized = T_on * TBASE const char* ap_id = "esp_ap"; // SSID of server const char* ap_pswd = "pwd"; // password for server (not used) unsigned long tempo = 0; // elapsed time bwtween millis() bool beccato = false; // PIR detected bool forced = false; // Relay forcefully commanded by external app int T_on_Count = 0; // Counter of TBASE of millis() cycles with relay in ON status String head = "ESP8266 Server"; // Server name String body = "

Home Control

"; // what the server is intended for String footer = "
"; String html; int Max_ClientNum = 0; //==================================================================================== void base_page() { body = "Home Control"; html = head + body + footer; server.send(200, "text/html", html); } //==================================================================================== void RelayON() { digitalWrite(D2,HIGH); // switch ON the relay body = "RELAY ON"; // answer to the server html = head + body + footer; server.send(200, "text/html", html); beccato = false; // do not enter the PIR loop forced = true; // force the status of relay } //==================================================================================== void Relay2ON() { digitalWrite(D0,HIGH); // switch ON the relay body = "RELAY2 ON"; // answer to the server html = head + body + footer; server.send(200, "text/html", html); } //==================================================================================== void RelayOFF() { digitalWrite(D2,LOW); // switch OFF the relay body = "RELAY OFF"; // answer to the server html = head + body + footer; server.send(200, "text/html", html); beccato = false; // do not enter the PIR loop forced = false; // cease to force the status of relay } //==================================================================================== void Relay2OFF() { digitalWrite(D0,LOW); // switch OFF the relay body = "RELAY2 OFF"; // answer to the server html = head + body + footer; server.send(200, "text/html", html); } //==================================================================================== void News_To_Pad() { if(beccato) body = "PIR catched"; // message to the server else body = "Nothing to say"; html = head + body + footer; server.send(200, "text/html", html); } //==================================================================================== void setup() { Serial.begin(115200); // baud to serial monitor //ESP.wdtDisable(); pinMode(D1, INPUT); // PIR input (0 = no match; 3.3 Vdc = match) pinMode(D2, OUTPUT); // Relay command line pinMode(D0, OUTPUT); // Relay command line digitalWrite(D2,HIGH); // relay test at power_on delay(1000); digitalWrite(D2,LOW); digitalWrite(D0,HIGH); // relay test at power_on delay(1000); digitalWrite(D0,LOW); WiFi.softAP(ap_id); // initialize WiFi of 8266 as an access point IPAddress ip(192, 168, 4, 2); // default address is .4.1; in this case is set at .4.2 IPAddress gateway(192, 168, 4, 9); // gateway 4.9 IPAddress subnet(255, 255, 255, 0); WiFi.softAPConfig(ip, gateway, subnet); // configure the port IPAddress ap_ip = WiFi.softAPIP(); // get back the real IP address (should be = to the one set by us) Serial.println(ap_ip); server.on("/",base_page); // browswer's base page server.on("/RELE/1/on",RelayON); // assign callback functions server.on("/RELE/1/off",RelayOFF); // assign callback functions server.on("/RELE/2/on",Relay2ON); // assign callback functions server.on("/RELE/2/off",Relay2OFF); // assign callback functions server.on("/NEWS",News_To_Pad); // assign callback functions server.begin(); // startr server ESP.wdtFeed(); // keep calm the WD delay(3000); // wait for WiFi sabilization Serial.println("Server online"); // to PC monitor Max_ClientNum = WiFi.softAPgetStationNum(); Serial.print("Client num. : "); Serial.println(Max_ClientNum); } //==================================================================================== void loop() { // while ( (millis() - tempo) < 2000) {ESP.wdtFeed();a=a;} ; if ( (millis() - tempo) < TBASE) // read always but decides only every TBASE seconds if energising the relay { server.handleClient(); // any activity ? if((WiFi.softAPgetStationNum() > Max_ClientNum) || (WiFi.softAPgetStationNum() < Max_ClientNum) ) { Max_ClientNum = WiFi.softAPgetStationNum(); Serial.print("Client num. : "); Serial.println(Max_ClientNum); } if (digitalRead(D1) == HIGH) // PIR detected a movement { beccato = true; // warns the part of code executed every TBASE seconds } } else // part executed every TBASE secondi { tempo = millis(); // updates the delta time every TBASE seconds Serial.println(tempo); // debug if(beccato == true) // movement detected by PIR { Serial.println("Beccato"); // debug if(T_on_Count == T_on) T_on_Count = 0; // the Relay On timer starts only if not already on the run beccato = false; // remove the "PIR detected" flag } if(T_on_Count < T_on) // if Relay On period not elapsed yet { digitalWrite(D2,HIGH); // keep relay On T_on_Count++; // Relay On time runs } else if (!forced) digitalWrite(D2,LOW); // Relay On time elapsed, switches off the relay } }