Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hello All,
- Ok I am sorry for my typos and all. I guess I was tired from all my debugging.
- This first script where I use void PaintRoom() after making some global variables the latest error during compile is
- 'String relayState(int)' redeclared as different kind of entity
- The second script works fine without adding the void PaintRoom()
- //FIRST SCRIPT WITH void PaintRoom() DOES NOT COMPILE GIVES ERROR 'String relayState(int)' redeclared as different kind of entity
- /*********
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp8266-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.
- *********/
- // Import required libraries
- #include "ESP8266WiFi.h"
- #include "ESPAsyncWebServer.h"
- //My Edits
- String buttons ="";
- int i;
- int relayStateValue;
- int relayValue;
- String relayState;
- // Set to true to define Relay as Normally Open (NO)
- #define RELAY_NO true
- // Set number of relays
- //#define NUM_RELAYS 5
- // Set number of relays
- #define NUM_RELAYS 5
- // Assign each GPIO to a relay
- //int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13};
- // Assign each GPIO to a relay
- int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13};
- // Replace with your network credentials
- //const char* ssid = "REPLACE_WITH_YOUR_SSID";
- //const char* password = "REPLACE_WITH_YOUR_PASSWORD";
- const char* ssid = "xxxxxxxxxx";
- const char* password = "xxxxxxxxxx";
- 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(
- <!DOCTYPE HTML><html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <style>
- html {font-family: Arial; display: inline-block; text-align: center;}
- h2 {font-size: 3.0rem;}
- p {font-size: 3.0rem;}
- body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
- .switch {position: relative; display: inline-block; width: 120px; height: 68px}
- .switch input {display: none}
- .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
- .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
- input:checked+.slider {background-color: #2196F3}
- input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
- </style>
- </head>
- <body>
- <h2>Hoffman Collision ESP Web Server</h2>
- %BUTTONPLACEHOLDER%
- <script>function toggleCheckbox(element) {
- var xhr = new XMLHttpRequest();
- if(element.checked){ xhr.open("GET", "/update?relay="+element.id+"&state=1", true); }
- else { xhr.open("GET", "/update?relay="+element.id+"&state=0", true); }
- xhr.send();
- }</script>
- </body>
- </html>
- )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);
- Serial.println (relayGPIOs[i-1]);
- //My Edit if i = 1
- if (i==1)
- {
- PaintRoom();
- }
- else
- {
- buttons+= "<h4>Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
- }
- }
- 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);
- //My Edit
- String paintroom = "Paint Room";
- // 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], HIGH);
- }
- else{
- digitalWrite(relayGPIOs[i-1], LOW);
- }
- }
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi..");
- }
- // Print ESP8266 Local IP Address
- Serial.println(WiFi.localIP());
- // 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 <ESP_IP>/update?relay=<inputMessage>&state=<inputMessage2>
- server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
- String inputMessage;
- String inputParam;
- String inputMessage2;
- String inputParam2;
- // GET input1 value on <ESP_IP>/update?relay=<inputMessage>
- 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();
- }
- void PaintRoom()
- {
- //buttons+= "<h4>Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + " Paint Room " + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
- buttons+= "<h4>Relay #" + String(1) + " - GPIO " + relayGPIOs[1] + " Paint Room " + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(1) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
- //Serial.println (String(1));
- Serial.println ("Paint Room");
- }
- void loop() {
- }
- //=======================================================================================================================
- //SECOND SCRIPT THIS ONE WORKS FINE.
- //====================================================================================================================
- //THIS SCRIPT WORKS
- /*********
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp8266-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.
- *********/
- // Import required libraries
- #include "ESP8266WiFi.h"
- #include "ESPAsyncWebServer.h"
- // Set to true to define Relay as Normally Open (NO)
- #define RELAY_NO true
- // Set number of relays
- //#define NUM_RELAYS 5
- // Set number of relays
- #define NUM_RELAYS 5
- // Assign each GPIO to a relay
- //int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13};
- // Assign each GPIO to a relay
- int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13};
- // Replace with your network credentials
- //const char* ssid = "REPLACE_WITH_YOUR_SSID";
- //const char* password = "REPLACE_WITH_YOUR_PASSWORD";
- const char* ssid = "xxxxxxxxxx";
- const char* password = "xxxxxxxxxx";
- 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(
- <!DOCTYPE HTML><html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <style>
- html {font-family: Arial; display: inline-block; text-align: center;}
- h2 {font-size: 3.0rem;}
- p {font-size: 3.0rem;}
- body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
- .switch {position: relative; display: inline-block; width: 120px; height: 68px}
- .switch input {display: none}
- .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
- .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
- input:checked+.slider {background-color: #2196F3}
- input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
- </style>
- </head>
- <body>
- <h2>Hoffman Collision ESP Web Server</h2>
- %BUTTONPLACEHOLDER%
- <script>function toggleCheckbox(element) {
- var xhr = new XMLHttpRequest();
- if(element.checked){ xhr.open("GET", "/update?relay="+element.id+"&state=1", true); }
- else { xhr.open("GET", "/update?relay="+element.id+"&state=0", true); }
- xhr.send();
- }</script>
- </body>
- </html>
- )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);
- //My Edit if i = 1
- if (i==1)
- {
- buttons+= "<h4>Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + " Paint Room " + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
- }
- else
- {
- buttons+= "<h4>Relay #" + String(i) + " - GPIO " + relayGPIOs[i-1] + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";
- }
- }
- 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);
- // 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], HIGH);
- }
- else{
- digitalWrite(relayGPIOs[i-1], LOW);
- }
- }
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi..");
- }
- // Print ESP8266 Local IP Address
- Serial.println(WiFi.localIP());
- // 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 <ESP_IP>/update?relay=<inputMessage>&state=<inputMessage2>
- server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
- String inputMessage;
- String inputParam;
- String inputMessage2;
- String inputParam2;
- // GET input1 value on <ESP_IP>/update?relay=<inputMessage>
- 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();
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment