stspringer

ESP8266 Script add a void PaintRoom()

Jul 15th, 2023 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.58 KB | None | 0 0
  1. Hello All,
  2.  
  3. Ok I am sorry for my typos and all. I guess I was tired from all my debugging.
  4.  
  5. This first script where I use void PaintRoom() after making some global variables the latest error during compile is
  6. 'String relayState(int)' redeclared as different kind of entity
  7.  
  8. The second script works fine without adding the void PaintRoom()
  9.  
  10.  
  11.  
  12.  
  13.  
  14. //FIRST SCRIPT WITH void PaintRoom() DOES NOT COMPILE GIVES ERROR 'String relayState(int)' redeclared as different kind of entity
  15.  
  16. /*********
  17. Rui Santos
  18. Complete project details at https://RandomNerdTutorials.com/esp8266-relay-module-ac-web-server/
  19.  
  20. The above copyright notice and this permission notice shall be included in all
  21. copies or substantial portions of the Software.
  22. *********/
  23.  
  24. // Import required libraries
  25. #include "ESP8266WiFi.h"
  26. #include "ESPAsyncWebServer.h"
  27.  
  28. //My Edits
  29. String buttons ="";
  30. int i;
  31. int relayStateValue;
  32. int relayValue;
  33. String relayState;
  34. // Set to true to define Relay as Normally Open (NO)
  35. #define RELAY_NO true
  36.  
  37. // Set number of relays
  38. //#define NUM_RELAYS 5
  39.  
  40. // Set number of relays
  41. #define NUM_RELAYS 5
  42.  
  43. // Assign each GPIO to a relay
  44. //int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13};
  45.  
  46. // Assign each GPIO to a relay
  47. int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13};
  48.  
  49. // Replace with your network credentials
  50. //const char* ssid = "REPLACE_WITH_YOUR_SSID";
  51. //const char* password = "REPLACE_WITH_YOUR_PASSWORD";
  52.  
  53. const char* ssid = "xxxxxxxxxx";
  54. const char* password = "xxxxxxxxxx";
  55.  
  56. const char* PARAM_INPUT_1 = "relay";
  57. const char* PARAM_INPUT_2 = "state";
  58.  
  59. // Create AsyncWebServer object on port 80
  60. AsyncWebServer server(80);
  61.  
  62. const char index_html[] PROGMEM = R"rawliteral(
  63. <!DOCTYPE HTML><html>
  64. <head>
  65. <meta name="viewport" content="width=device-width, initial-scale=1">
  66. <style>
  67. html {font-family: Arial; display: inline-block; text-align: center;}
  68. h2 {font-size: 3.0rem;}
  69. p {font-size: 3.0rem;}
  70. body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
  71. .switch {position: relative; display: inline-block; width: 120px; height: 68px}
  72. .switch input {display: none}
  73. .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
  74. .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
  75. input:checked+.slider {background-color: #2196F3}
  76. input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
  77. </style>
  78. </head>
  79. <body>
  80.  
  81. <h2>Hoffman Collision ESP Web Server</h2>
  82. %BUTTONPLACEHOLDER%
  83. <script>function toggleCheckbox(element) {
  84. var xhr = new XMLHttpRequest();
  85. if(element.checked){ xhr.open("GET", "/update?relay="+element.id+"&state=1", true); }
  86. else { xhr.open("GET", "/update?relay="+element.id+"&state=0", true); }
  87. xhr.send();
  88. }</script>
  89. </body>
  90. </html>
  91. )rawliteral";
  92.  
  93. // Replaces placeholder with button section in your web page
  94. String processor(const String& var){
  95. //Serial.println(var);
  96. if(var == "BUTTONPLACEHOLDER"){
  97. String buttons ="";
  98. for(int i=1; i<=NUM_RELAYS; i++){
  99. String relayStateValue = relayState(i);
  100. Serial.println (relayGPIOs[i-1]);
  101.  
  102. //My Edit if i = 1
  103. if (i==1)
  104. {
  105. PaintRoom();
  106. }
  107. else
  108. {
  109. 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>";
  110. }
  111. }
  112.  
  113. return buttons;
  114.  
  115. }
  116. return String();
  117. }
  118.  
  119. String relayState(int numRelay){
  120. if(RELAY_NO){
  121. if(digitalRead(relayGPIOs[numRelay-1])){
  122. return "";
  123. }
  124. else {
  125. return "checked";
  126. }
  127. }
  128. else {
  129. if(digitalRead(relayGPIOs[numRelay-1])){
  130. return "checked";
  131. }
  132. else {
  133. return "";
  134. }
  135. }
  136. return "";
  137. }
  138.  
  139. void setup(){
  140. // Serial port for debugging purposes
  141. Serial.begin(115200);
  142. //My Edit
  143. String paintroom = "Paint Room";
  144. // 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
  145. for(int i=1; i<=NUM_RELAYS; i++){
  146. pinMode(relayGPIOs[i-1], OUTPUT);
  147. if(RELAY_NO){
  148. digitalWrite(relayGPIOs[i-1], HIGH);
  149. }
  150. else{
  151. digitalWrite(relayGPIOs[i-1], LOW);
  152. }
  153. }
  154.  
  155. // Connect to Wi-Fi
  156. WiFi.begin(ssid, password);
  157. while (WiFi.status() != WL_CONNECTED) {
  158. delay(1000);
  159. Serial.println("Connecting to WiFi..");
  160. }
  161.  
  162. // Print ESP8266 Local IP Address
  163. Serial.println(WiFi.localIP());
  164.  
  165. // Route for root / web page
  166. server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  167. request->send_P(200, "text/html", index_html, processor);
  168. });
  169.  
  170. // Send a GET request to <ESP_IP>/update?relay=<inputMessage>&state=<inputMessage2>
  171. server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
  172. String inputMessage;
  173. String inputParam;
  174. String inputMessage2;
  175. String inputParam2;
  176. // GET input1 value on <ESP_IP>/update?relay=<inputMessage>
  177. if (request->hasParam(PARAM_INPUT_1) & request->hasParam(PARAM_INPUT_2)) {
  178. inputMessage = request->getParam(PARAM_INPUT_1)->value();
  179. inputParam = PARAM_INPUT_1;
  180. inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
  181. inputParam2 = PARAM_INPUT_2;
  182. if(RELAY_NO){
  183. Serial.print("NO ");
  184. digitalWrite(relayGPIOs[inputMessage.toInt()-1], !inputMessage2.toInt());
  185. }
  186. else{
  187. Serial.print("NC ");
  188. digitalWrite(relayGPIOs[inputMessage.toInt()-1], inputMessage2.toInt());
  189. }
  190. }
  191. else {
  192. inputMessage = "No message sent";
  193. inputParam = "none";
  194. }
  195. Serial.println(inputMessage + inputMessage2);
  196. request->send(200, "text/plain", "OK");
  197. });
  198. // Start server
  199. server.begin();
  200. }
  201.  
  202. void PaintRoom()
  203. {
  204.  
  205. //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>";
  206. 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>";
  207. //Serial.println (String(1));
  208. Serial.println ("Paint Room");
  209. }
  210.  
  211. void loop() {
  212.  
  213. }
  214. //=======================================================================================================================
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. //SECOND SCRIPT THIS ONE WORKS FINE.
  227. //====================================================================================================================
  228. //THIS SCRIPT WORKS
  229. /*********
  230. Rui Santos
  231. Complete project details at https://RandomNerdTutorials.com/esp8266-relay-module-ac-web-server/
  232.  
  233. The above copyright notice and this permission notice shall be included in all
  234. copies or substantial portions of the Software.
  235. *********/
  236.  
  237. // Import required libraries
  238. #include "ESP8266WiFi.h"
  239. #include "ESPAsyncWebServer.h"
  240.  
  241. // Set to true to define Relay as Normally Open (NO)
  242. #define RELAY_NO true
  243.  
  244. // Set number of relays
  245. //#define NUM_RELAYS 5
  246.  
  247. // Set number of relays
  248. #define NUM_RELAYS 5
  249.  
  250. // Assign each GPIO to a relay
  251. //int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13};
  252.  
  253. // Assign each GPIO to a relay
  254. int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13};
  255.  
  256. // Replace with your network credentials
  257. //const char* ssid = "REPLACE_WITH_YOUR_SSID";
  258. //const char* password = "REPLACE_WITH_YOUR_PASSWORD";
  259.  
  260. const char* ssid = "xxxxxxxxxx";
  261. const char* password = "xxxxxxxxxx";
  262.  
  263. const char* PARAM_INPUT_1 = "relay";
  264. const char* PARAM_INPUT_2 = "state";
  265.  
  266. // Create AsyncWebServer object on port 80
  267. AsyncWebServer server(80);
  268.  
  269. const char index_html[] PROGMEM = R"rawliteral(
  270. <!DOCTYPE HTML><html>
  271. <head>
  272. <meta name="viewport" content="width=device-width, initial-scale=1">
  273. <style>
  274. html {font-family: Arial; display: inline-block; text-align: center;}
  275. h2 {font-size: 3.0rem;}
  276. p {font-size: 3.0rem;}
  277. body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
  278. .switch {position: relative; display: inline-block; width: 120px; height: 68px}
  279. .switch input {display: none}
  280. .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
  281. .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
  282. input:checked+.slider {background-color: #2196F3}
  283. input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
  284. </style>
  285. </head>
  286. <body>
  287.  
  288. <h2>Hoffman Collision ESP Web Server</h2>
  289. %BUTTONPLACEHOLDER%
  290. <script>function toggleCheckbox(element) {
  291. var xhr = new XMLHttpRequest();
  292. if(element.checked){ xhr.open("GET", "/update?relay="+element.id+"&state=1", true); }
  293. else { xhr.open("GET", "/update?relay="+element.id+"&state=0", true); }
  294. xhr.send();
  295. }</script>
  296. </body>
  297. </html>
  298. )rawliteral";
  299.  
  300. // Replaces placeholder with button section in your web page
  301. String processor(const String& var){
  302. //Serial.println(var);
  303. if(var == "BUTTONPLACEHOLDER"){
  304. String buttons ="";
  305. for(int i=1; i<=NUM_RELAYS; i++){
  306. String relayStateValue = relayState(i);
  307. //My Edit if i = 1
  308. if (i==1)
  309. {
  310. 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>";
  311. }
  312. else
  313. {
  314. 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>";
  315. }
  316. }
  317. return buttons;
  318. }
  319. return String();
  320. }
  321.  
  322. String relayState(int numRelay){
  323. if(RELAY_NO){
  324. if(digitalRead(relayGPIOs[numRelay-1])){
  325. return "";
  326. }
  327. else {
  328. return "checked";
  329. }
  330. }
  331. else {
  332. if(digitalRead(relayGPIOs[numRelay-1])){
  333. return "checked";
  334. }
  335. else {
  336. return "";
  337. }
  338. }
  339. return "";
  340. }
  341.  
  342. void setup(){
  343. // Serial port for debugging purposes
  344. Serial.begin(115200);
  345.  
  346. // 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
  347. for(int i=1; i<=NUM_RELAYS; i++){
  348. pinMode(relayGPIOs[i-1], OUTPUT);
  349. if(RELAY_NO){
  350. digitalWrite(relayGPIOs[i-1], HIGH);
  351. }
  352. else{
  353. digitalWrite(relayGPIOs[i-1], LOW);
  354. }
  355. }
  356.  
  357. // Connect to Wi-Fi
  358. WiFi.begin(ssid, password);
  359. while (WiFi.status() != WL_CONNECTED) {
  360. delay(1000);
  361. Serial.println("Connecting to WiFi..");
  362. }
  363.  
  364. // Print ESP8266 Local IP Address
  365. Serial.println(WiFi.localIP());
  366.  
  367. // Route for root / web page
  368. server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  369. request->send_P(200, "text/html", index_html, processor);
  370. });
  371.  
  372. // Send a GET request to <ESP_IP>/update?relay=<inputMessage>&state=<inputMessage2>
  373. server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
  374. String inputMessage;
  375. String inputParam;
  376. String inputMessage2;
  377. String inputParam2;
  378. // GET input1 value on <ESP_IP>/update?relay=<inputMessage>
  379. if (request->hasParam(PARAM_INPUT_1) & request->hasParam(PARAM_INPUT_2)) {
  380. inputMessage = request->getParam(PARAM_INPUT_1)->value();
  381. inputParam = PARAM_INPUT_1;
  382. inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
  383. inputParam2 = PARAM_INPUT_2;
  384. if(RELAY_NO){
  385. Serial.print("NO ");
  386. digitalWrite(relayGPIOs[inputMessage.toInt()-1], !inputMessage2.toInt());
  387. }
  388. else{
  389. Serial.print("NC ");
  390. digitalWrite(relayGPIOs[inputMessage.toInt()-1], inputMessage2.toInt());
  391. }
  392. }
  393. else {
  394. inputMessage = "No message sent";
  395. inputParam = "none";
  396. }
  397. Serial.println(inputMessage + inputMessage2);
  398. request->send(200, "text/plain", "OK");
  399. });
  400. // Start server
  401. server.begin();
  402. }
  403.  
  404. void loop() {
  405.  
  406. }
Advertisement
Add Comment
Please, Sign In to add comment