Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.36 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <RCSwitch.h>
  5. #include <SPI.h>
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_SSD1306.h>
  8.  
  9. #define OLED_SDA   D7  // MOSI
  10. #define OLED_SCL   D5  // CLK
  11. #define OLED_DC    D2  // D/C
  12. #define OLED_CS    12  // no need of connecting, just use some pin number
  13. #define OLED_RESET D1  // RES
  14. int a = 0;
  15. Adafruit_SSD1306 display(OLED_SDA,OLED_SCL, OLED_DC, OLED_RESET, OLED_CS);
  16. RCSwitch mySwitch = RCSwitch();
  17.  
  18. const char* ssid =   "xxx";
  19. const char* password = "xxx";
  20.  
  21. ESP8266WebServer server(80);
  22.  
  23. char* housecodes[] = {"00010", "00010", "00010", "00010", "00010", "10000"};
  24. char* socketcodes[] = {"10000", "01000", "00100", "00010", "00001", "10000"};
  25. char* socketnames[] = {"Stehlampe", "Weihnachtsbeleuchtung", "Schlafzimmerschrank", "Vitrine", "Raspberry", "Reserve"};
  26. int numofsockets = sizeof(socketcodes)/4;
  27.  
  28. String css = "body {background-color:#ffffff; color: #000000; font-family: 'Century Gothic', CenturyGothic, AppleGothic, sans-serif;}h1 {font-size: 2em;}";
  29. String head1 = "<!DOCTYPE html> <html> <head> <title>Steckdosensteuerung</title><meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'> <style>";
  30. String head2 = "</style></head><body><center>";
  31. String header = head1 + css + head2;
  32. String body = "";
  33.  
  34. String website(String h, String b){
  35.   String complete = h+b;
  36.   return complete;
  37. }
  38.  
  39. void setup(void){
  40.   pinMode(2, OUTPUT);
  41.   body = "<h1>Steckdosensteuerung</h1><br /></center><div style='padding-left: 20px;'>";
  42.   for(int i = 0; i < numofsockets; i++){
  43.     String namesocket = socketnames[i];  
  44.     body = body + "<p><a href=\"socket" + String(i) + "On?pw=\"><button>AN</button></a>&nbsp;<a href=\"socket" + String(i) + "Off?pw=\"><button>AUS</button></a> " + namesocket + " (" + housecodes[i] + ":" + socketcodes[i] + ")</p>";
  45.   }
  46.   body += "</center></body>";
  47.   WiFi.begin(ssid, password);
  48.  
  49.   display.begin(SSD1306_SWITCHCAPVCC);
  50.   display.display();
  51.   delay(500);
  52.   display.clearDisplay();
  53.  
  54.   display.setTextColor(WHITE);
  55.   display.setTextSize(1);
  56.   display.setCursor(25,11);
  57.   display.print("TRcoding.net");
  58.   display.display();
  59.   delay(2000);
  60.  
  61.   display.clearDisplay();
  62.   display.setCursor(0,11);
  63.   display.print("Verbindungsaufbau ...");
  64.   display.display();
  65.  
  66.   delay(4000);
  67.   display.clearDisplay();
  68.  
  69.   mySwitch.enableTransmit(2);
  70.   mySwitch.enableReceive(0);
  71.   delay(1000);
  72.   Serial.begin(115200);
  73.  
  74.   while (WiFi.status() != WL_CONNECTED) {
  75.     delay(500);
  76.     Serial.print(".");
  77.   }
  78.   Serial.println("");
  79.   Serial.print("Connected to ");
  80.   Serial.println(ssid);
  81.   Serial.print("IP: ");
  82.   Serial.println(WiFi.localIP());
  83.   display.clearDisplay();
  84.   display.setCursor(0,3);
  85.   display.print("Host:  ");
  86.   display.println(ssid);
  87.   display.print("IP: ");
  88.   display.println(WiFi.localIP());
  89.   display.display();
  90.  
  91.   server.on("/", [](){
  92.     String webPage = website(header, body);
  93.     server.send(200, "text/html", webPage);
  94.   });
  95.  
  96.   for(int i = 0; i < numofsockets; i++){
  97.     String pathOn = "/socket"+String(i)+"On";
  98.     const char* pathOnChar = pathOn.c_str();
  99.     String pathOff = "/socket"+String(i)+"Off";
  100.     const char* pathOffChar = pathOff.c_str();
  101.  
  102.     server.on(pathOnChar, [i](){
  103.       String get_password = server.arg("pw");
  104.       if (get_password == password)
  105.       {
  106.         String webPage = website(header, body);
  107.         server.send(200, "text/html", webPage);
  108.         mySwitch.switchOn(housecodes[i], socketcodes[i]);
  109.         Serial.print("Code AN: ");
  110.         Serial.println(socketcodes[i]);
  111.         display.clearDisplay();
  112.         display.setCursor(0,3);
  113.         display.print("IP: ");
  114.         display.println(WiFi.localIP());
  115.         display.print("Hauscode: ");
  116.         display.println(housecodes[i]);
  117.         display.print("Code: ");
  118.         display.print(socketcodes[i]);
  119.         display.println(" AN");
  120.         display.display();
  121.         delay(1000);
  122.       } else {
  123.         String webPage = website(header + "<h2 style=\"color: #ff0000\">Passwort fehlt (?pw=)</h2>", body);
  124.         server.send(200, "text/html", webPage);
  125.         Serial.print("Passwort fehlt (?pw=): ");
  126.         Serial.println(password);
  127.       }
  128.     });
  129.    
  130.     server.on(pathOffChar, [i](){
  131.       String get_password = server.arg("pw");
  132.       if (get_password == password)
  133.       {
  134.         String webPage = website(header, body);
  135.         server.send(200, "text/html", webPage);
  136.         mySwitch.switchOff(housecodes[i], socketcodes[i]);
  137.         Serial.print("Code AUS: ");
  138.         Serial.println(socketcodes[i]);
  139.         display.clearDisplay();
  140.         display.setCursor(0,3);
  141.         display.print("IP: ");
  142.         display.println(WiFi.localIP());
  143.         display.print("Hauscode: ");
  144.         display.println(housecodes[i]);
  145.         display.print("Code: ");
  146.         display.print(socketcodes[i]);
  147.         display.println(" AUS");
  148.         display.display();
  149.         delay(1000);
  150.       } else {
  151.         String webPage = website(header + "<h2 style=\"color: #ff0000\">Passwort fehlt (?pw=)</h2>", body);
  152.         server.send(200, "text/html", webPage);
  153.         Serial.print("Passwort fehlt (?pw=): ");
  154.         Serial.println(password);
  155.       }
  156.     });
  157.   }
  158.   server.begin();
  159.   Serial.println("HTTP-Server gestartet");
  160. }
  161.  
  162. void loop(void){
  163.   digitalWrite(2, HIGH);
  164.   server.handleClient();
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement