/********* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp32-relay-module-ac-web-server/ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Modification GL 01/2022 *********/ // Import required libraries #include "WiFi.h" #include "ESPAsyncWebServer.h" #include #include #include // Set to true to define Relay as Normally Open (NO) #define RELAY_NO false // Set number of relays #define NUM_RELAYS 1 // Assign each GPIO to a relay int relayGPIOs[NUM_RELAYS] = {15}; // Replace with your network credentials const char* ssid = "xxxxxxxxxxxxxxxx"; const char* password = "xxxxxxxxxxxxxxxxxx"; /* // Wifi castellab const char* ssid = "castellab"; const char* password = "Castel37110$"; */ void Wifi_connected(WiFiEvent_t event, WiFiEventInfo_t info){ Serial.println("Successfully connected to Access Point"); } void Get_IPAddress(WiFiEvent_t event, WiFiEventInfo_t info){ Serial.println("WIFI is connected!"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void Wifi_disconnected(WiFiEvent_t event, WiFiEventInfo_t info){ Serial.println("Disconnected from WIFI access point"); Serial.print("WiFi lost connection. Reason: "); Serial.println(info.disconnected.reason); Serial.println("Reconnecting..."); WiFi.begin(ssid, password); } const char* PARAM_INPUT_1 = "relay"; const char* PARAM_INPUT_2 = "state"; // Create AsyncWebServer object on port 80 AsyncWebServer server(80); const char index_html[] PROGMEM = R"rawliteral(

Castellab commande chauffage

%BUTTONPLACEHOLDER% )rawliteral"; // Replaces placeholder with button section in your web page String processor(const String& var){ //Serial.println(var); if(var == "BUTTONPLACEHOLDER"){ String buttons =""; for(int i=1; i<=NUM_RELAYS; i++){ String relayStateValue = relayState(i-1); buttons+= "

Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + "

"; } return buttons; } return String(); } String relayState(int numRelay){ if(RELAY_NO){ if(digitalRead(relayGPIOs[numRelay-1])){ return ""; } else { return "checked"; } } else { if(digitalRead(relayGPIOs[numRelay-1])){ return "checked"; } else { return ""; } } return ""; } void setup(){ // Serial port for debugging purposes Serial.begin(115200); Serial.println("Booting"); pinMode(relayGPIOs[0], OUTPUT); digitalWrite(relayGPIOs[0],LOW); if(RELAY_NO){ digitalWrite(relayGPIOs[0], LOW); } else{ digitalWrite(relayGPIOs[0], HIGH); } // Set all relays to off when the program starts - if set to Normally Open (NO), the relay is off when you set the relay to HIGH /*for(int i=1; i<=NUM_RELAYS; i++){ pinMode(relayGPIOs[i-1], OUTPUT); if(RELAY_NO){ digitalWrite(relayGPIOs[i-1], LOW); } else{ digitalWrite(relayGPIOs[i-1], HIGH); } } */ // Connect to Wi-Fi WiFi.mode(WIFI_STA); WiFi.disconnect(true); delay(1000); WiFi.onEvent(Wifi_connected,SYSTEM_EVENT_STA_CONNECTED); WiFi.onEvent(Get_IPAddress, SYSTEM_EVENT_STA_GOT_IP); WiFi.onEvent(Wifi_disconnected, SYSTEM_EVENT_STA_DISCONNECTED); WiFi.begin(ssid, password); Serial.println("Waiting for WIFI network..."); /* WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } */ /* while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } */ // Route for root / web page server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); // Send a GET request to /update?relay=&state= server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) { String inputMessage; String inputParam; String inputMessage2; String inputParam2; // GET input1 value on /update?relay= if (request->hasParam(PARAM_INPUT_1) & request->hasParam(PARAM_INPUT_2)) { inputMessage = request->getParam(PARAM_INPUT_1)->value(); inputParam = PARAM_INPUT_1; inputMessage2 = request->getParam(PARAM_INPUT_2)->value(); inputParam2 = PARAM_INPUT_2; if(RELAY_NO){ Serial.print("NO "); digitalWrite(relayGPIOs[inputMessage.toInt()-1], !inputMessage2.toInt()); } else{ Serial.print("NC "); digitalWrite(relayGPIOs[inputMessage.toInt()-1], inputMessage2.toInt()); } } else { inputMessage = "No message sent"; inputParam = "none"; } Serial.println(inputMessage + inputMessage2); request->send(200, "text/plain", "OK"); }); // Start server server.begin(); ArduinoOTA .onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) type = "sketch"; else // U_SPIFFS type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() Serial.println("Start updating " + type); }) .onEnd([]() { Serial.println("\nEnd"); }) .onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }) .onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); /* Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); */ } void loop() { ArduinoOTA.handle(); delay(1000); }