SHARE
TWEET

ESP8266 433MHz

a guest Apr 29th, 2016 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5. #include <RCSwitch.h>
  6. #ifdef ESP8266
  7. extern "C" {
  8. #include "user_interface.h"
  9. }
  10. #endif
  11. RCSwitch mySwitch = RCSwitch();
  12. MDNSResponder mdns;
  13. // Replace with your network credentials
  14. const char* ssid = "ssid";
  15. const char* password = "password";
  16. char* myHostname = "hostname";
  17. ESP8266WebServer server(80);
  18. // replace with your values
  19. char* housecode = "00000";
  20. char* socketcodes[] = {"10000", "01000", "00100"};
  21. char* socketnames[] = {"Wohnzimmer", "Schlafzimmer", "Verstaerker"};
  22. int numofsockets = sizeof(socketcodes)/4;
  23. // you can write your own css and html code (head) here
  24. String css = "body {background-color:#ffffff; color: #000000; font-family: 'Century Gothic', CenturyGothic, AppleGothic, sans-serif;}h1 {font-size: 2em;}a{font-family: Century Gothic, CenturyGothic, AppleGothic, sans-serif; font-size: 2em; text-decoration: none; background-color: #EEEEEE; color: #333333; padding: 2px 6px 2px 6px; border-top: 1px solid #CCCCCC; border-right: 1px solid #333333; border-bottom: 1px solid #333333; border-left: 1px solid #CCCCCC;} p{ font-family: Century Gothic, CenturyGothic, AppleGothic, sans-serif; font-size: 2em;}";
  25. String head1 = "<!DOCTYPE html> <html> <head> <meta name=\"viewport\" content=\"initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"> <title>Steckdosensteuerung</title> <style>";
  26. String head2 = "</style></head><body><center>";
  27. String header = head1 + css + head2;
  28. String body = "";
  29. String website(String h, String b){
  30.   String complete = h+b;
  31.   return complete;
  32. }
  33. void setup(void){
  34.   // if you want to modify body part of html start here
  35.   body = "<h1>Steckdosensteuerung</h1>";
  36.   // socket names and buttons are created dynamical
  37.   for(int i = 0; i < numofsockets; i++){
  38.     String namesocket = socketnames[i];
  39.     body = body + "<p>" + namesocket + " </p> <a href=\"socket" + String(i) + "On\">AN</a>&nbsp;<a href=\"socket" + String(i) + "Off\">AUS</a>";
  40.   }
  41.   body += "</center></body>";
  42.   mySwitch.enableTransmit(2);
  43.   delay(1000);
  44.   Serial.begin(115200);
  45.   wifi_station_set_hostname(myHostname);
  46.   WiFi.begin(ssid, password);
  47.   Serial.println("");
  48.   // Wait for connection
  49.   while (WiFi.status() != WL_CONNECTED) {
  50.     delay(500);
  51.     Serial.print(".");
  52.   }
  53.   // serial output of connection details
  54.   Serial.println("");
  55.   Serial.print("Connected to ");
  56.   Serial.println(ssid);
  57.   Serial.print("IP address: ");
  58.   Serial.println(WiFi.localIP());
  59.   #ifdef ESP8266
  60.   Serial.print("wifi_station_get_hostname: ");
  61.   Serial.println(wifi_station_get_hostname());
  62.   #endif
  63.   if (mdns.begin("esp8266", WiFi.localIP())) {
  64.     Serial.println("MDNS responder started");
  65.   }
  66.   // this page is loaded when accessing the root of esp8266´s IP
  67.   server.on("/", [](){
  68.     String webPage = website(header, body);
  69.     server.send(200, "text/html", webPage);
  70.   });
  71.   // pages for all your sockets are created dynamical
  72.   for(int i = 0; i < numofsockets; i++){
  73.     String pathOn = "/socket"+String(i)+"On";
  74.     const char* pathOnChar = pathOn.c_str();
  75.     String pathOff = "/socket"+String(i)+"Off";
  76.     const char* pathOffChar = pathOff.c_str();
  77.     server.on(pathOnChar, [i](){
  78.       String webPage = website(header, body);
  79.       server.send(200, "text/html", webPage);
  80.       mySwitch.switchOn(housecode, socketcodes[i]);
  81.       delay(1000);
  82.     });
  83.     server.on(pathOffChar, [i](){
  84.       String webPage = website(header, body);
  85.       server.send(200, "text/html", webPage);
  86.       mySwitch.switchOff(housecode, socketcodes[i]);
  87.       delay(1000);
  88.     });
  89.   }
  90.   server.begin();
  91.   Serial.println("HTTP server started");
  92. }
  93. void loop(void){
  94.   server.handleClient();
  95. }
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