SHARE
TWEET

Untitled

a guest Jul 17th, 2019 130 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3.  
  4. ESP8266WebServer server(80);
  5.  
  6. int R0 = 16;
  7. int R1 = D1;
  8. int R2 = D2;
  9. int R3 = D3;
  10. int R4 = D4;
  11. int R5 = D5;
  12. int R6 = D6;
  13. int R7 = D7;
  14. int R8 = D8;
  15.  
  16. int Switch[] = {R0, R1, R2, R3, R4, R5, R6, R7, R8};
  17. bool State[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
  18.  
  19. void setup() {
  20.   for(unsigned int a = 0; a < sizeof(Switch); a = a + 1){
  21.     Serial.println(a);
  22.     Serial.println(Switch[a]);
  23.     pinMode(Switch[a], OUTPUT);
  24.   }
  25.   Serial.begin(115200);
  26.   WiFi.begin("ROUTERNAME", "PASSWORD");
  27.   while (WiFi.status() != WL_CONNECTED) {
  28.     delay(500);
  29.     Serial.println("waiting to connect...");
  30.   }
  31.   Serial.print("IP address: ");
  32.   Serial.println(WiFi.localIP());
  33.  
  34.   server.on("/switchOn", handleSwitchOn);
  35.   server.on("/switchOff", handleSwitchOff);
  36.   server.on("/switchStatus", handleSwitchStatus);
  37.   server.begin();
  38.   Serial.println("server listening");
  39. }
  40.  
  41. void loop() {
  42.   server.handleClient();
  43. }
  44.  
  45. void handleSwitchOn(){
  46.   String t = server.arg("switchId");
  47.   int id = std::atoi(t.c_str());
  48.   Serial.println("Switching on");
  49.   State[id] = HIGH;
  50.   digitalWrite(Switch[id], HIGH);
  51.   server.send(200, "text/plain", "1");
  52. }
  53.  
  54. void handleSwitchOff(){
  55.   String t = server.arg("switchId");
  56.   int id = std::atoi(t.c_str());
  57.   Serial.println("Switching off");
  58.   Serial.println(Switch[id]);
  59.   State[id] = LOW;
  60.   digitalWrite(Switch[id], LOW);
  61.   server.send(200, "text/plain", "0");
  62. }
  63.  
  64. void handleSwitchStatus(){
  65.   String t = server.arg("switchId");
  66.   int id = std::atoi(t.c_str());
  67.   String s = String(State[id]);
  68.   server.send(200, "text/plain", s);
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top