Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <RCSwitch.h>
- #include <SPI.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_SDA D7 // MOSI
- #define OLED_SCL D5 // CLK
- #define OLED_DC D2 // D/C
- #define OLED_CS 12 // no need of connecting, just use some pin number
- #define OLED_RESET D1 // RES
- int a = 0;
- Adafruit_SSD1306 display(OLED_SDA,OLED_SCL, OLED_DC, OLED_RESET, OLED_CS);
- RCSwitch mySwitch = RCSwitch();
- const char* ssid = "xxx";
- const char* password = "xxx";
- ESP8266WebServer server(80);
- char* housecodes[] = {"00010", "00010", "00010", "00010", "00010", "10000"};
- char* socketcodes[] = {"10000", "01000", "00100", "00010", "00001", "10000"};
- char* socketnames[] = {"Stehlampe", "Weihnachtsbeleuchtung", "Schlafzimmerschrank", "Vitrine", "Raspberry", "Reserve"};
- int numofsockets = sizeof(socketcodes)/4;
- String css = "body {background-color:#ffffff; color: #000000; font-family: 'Century Gothic', CenturyGothic, AppleGothic, sans-serif;}h1 {font-size: 2em;}";
- String head1 = "<!DOCTYPE html> <html> <head> <title>Steckdosensteuerung</title><meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'> <style>";
- String head2 = "</style></head><body><center>";
- String header = head1 + css + head2;
- String body = "";
- String website(String h, String b){
- String complete = h+b;
- return complete;
- }
- void setup(void){
- pinMode(2, OUTPUT);
- body = "<h1>Steckdosensteuerung</h1><br /></center><div style='padding-left: 20px;'>";
- for(int i = 0; i < numofsockets; i++){
- String namesocket = socketnames[i];
- body = body + "<p><a href=\"socket" + String(i) + "On?pw=\"><button>AN</button></a> <a href=\"socket" + String(i) + "Off?pw=\"><button>AUS</button></a> " + namesocket + " (" + housecodes[i] + ":" + socketcodes[i] + ")</p>";
- }
- body += "</center></body>";
- WiFi.begin(ssid, password);
- display.begin(SSD1306_SWITCHCAPVCC);
- display.display();
- delay(500);
- display.clearDisplay();
- display.setTextColor(WHITE);
- display.setTextSize(1);
- display.setCursor(25,11);
- display.print("TRcoding.net");
- display.display();
- delay(2000);
- display.clearDisplay();
- display.setCursor(0,11);
- display.print("Verbindungsaufbau ...");
- display.display();
- delay(4000);
- display.clearDisplay();
- mySwitch.enableTransmit(2);
- mySwitch.enableReceive(0);
- delay(1000);
- Serial.begin(115200);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP: ");
- Serial.println(WiFi.localIP());
- display.clearDisplay();
- display.setCursor(0,3);
- display.print("Host: ");
- display.println(ssid);
- display.print("IP: ");
- display.println(WiFi.localIP());
- display.display();
- server.on("/", [](){
- String webPage = website(header, body);
- server.send(200, "text/html", webPage);
- });
- for(int i = 0; i < numofsockets; i++){
- String pathOn = "/socket"+String(i)+"On";
- const char* pathOnChar = pathOn.c_str();
- String pathOff = "/socket"+String(i)+"Off";
- const char* pathOffChar = pathOff.c_str();
- server.on(pathOnChar, [i](){
- String get_password = server.arg("pw");
- if (get_password == password)
- {
- String webPage = website(header, body);
- server.send(200, "text/html", webPage);
- mySwitch.switchOn(housecodes[i], socketcodes[i]);
- Serial.print("Code AN: ");
- Serial.println(socketcodes[i]);
- display.clearDisplay();
- display.setCursor(0,3);
- display.print("IP: ");
- display.println(WiFi.localIP());
- display.print("Hauscode: ");
- display.println(housecodes[i]);
- display.print("Code: ");
- display.print(socketcodes[i]);
- display.println(" AN");
- display.display();
- delay(1000);
- } else {
- String webPage = website(header + "<h2 style=\"color: #ff0000\">Passwort fehlt (?pw=)</h2>", body);
- server.send(200, "text/html", webPage);
- Serial.print("Passwort fehlt (?pw=): ");
- Serial.println(password);
- }
- });
- server.on(pathOffChar, [i](){
- String get_password = server.arg("pw");
- if (get_password == password)
- {
- String webPage = website(header, body);
- server.send(200, "text/html", webPage);
- mySwitch.switchOff(housecodes[i], socketcodes[i]);
- Serial.print("Code AUS: ");
- Serial.println(socketcodes[i]);
- display.clearDisplay();
- display.setCursor(0,3);
- display.print("IP: ");
- display.println(WiFi.localIP());
- display.print("Hauscode: ");
- display.println(housecodes[i]);
- display.print("Code: ");
- display.print(socketcodes[i]);
- display.println(" AUS");
- display.display();
- delay(1000);
- } else {
- String webPage = website(header + "<h2 style=\"color: #ff0000\">Passwort fehlt (?pw=)</h2>", body);
- server.send(200, "text/html", webPage);
- Serial.print("Passwort fehlt (?pw=): ");
- Serial.println(password);
- }
- });
- }
- server.begin();
- Serial.println("HTTP-Server gestartet");
- }
- void loop(void){
- digitalWrite(2, HIGH);
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement