Advertisement
metalx1000

ESP8266 Arduino IDE Code

Apr 4th, 2017
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.11 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5. #include <ESP8266HTTPClient.h>
  6.  
  7.  
  8. const char* ssid = "<SSID>";
  9. const char* password = "<PASSWORD>";
  10.  
  11. ESP8266WebServer server(80);
  12.  
  13. int p = 0;
  14. int dataPin = 13;
  15.  
  16. #include <RCSwitch.h>
  17.  
  18. RCSwitch mySwitch = RCSwitch();
  19.  
  20. // constants won't change. They're used here to
  21. // set pin numbers:
  22. const int buttonPin = 0;     // the number of the pushbutton pin
  23. const int ledPin =  2;      // the number of the LED pin
  24.  
  25. // variables will change:
  26. int buttonState = 0;         // variable for reading the pushbutton status
  27.  
  28. void handleRoot() {
  29.   if (server.args() > 0 ) {
  30.     for ( uint8_t i = 0; i < server.args(); i++ ) {
  31.       if (server.argName(i) == "switch") {
  32.         server.send(200, "text/plain", server.arg(i));
  33.         int code = server.arg(i).toInt();
  34.         /* Same switch as above, but using decimal code */
  35.         mySwitch.send(code, 24);
  36.         delay(1000);
  37.        
  38.       }
  39.     }
  40.   }else{
  41.     server.send(200, "text/plain", "hello from esp8266!");
  42.   }
  43. }
  44.  
  45. void handleNotFound(){
  46.   String message = "File Not Found\n\n";
  47.   message += "URI: ";
  48.   message += server.uri();
  49.   message += "\nMethod: ";
  50.   message += (server.method() == HTTP_GET)?"GET":"POST";
  51.   message += "\nArguments: ";
  52.   message += server.args();
  53.   message += "\n";
  54.   for (uint8_t i=0; i<server.args(); i++){
  55.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  56.   }
  57.   server.send(404, "text/plain", message);
  58. }
  59.  
  60. void ring(){
  61.   Serial.println("Ringing Bell");
  62.   HTTPClient http;
  63.   http.begin("http://<server IP>/cgi-bin/doorbell.cgi");
  64.   int httpCode = http.GET();
  65.   http.end();
  66.   delay(5000);
  67. }
  68. void setup() {
  69.  
  70.   // initialize the LED pin as an output:
  71.   pinMode(ledPin, OUTPUT);
  72.   // initialize the pushbutton pin as an input:
  73.   pinMode(buttonPin, INPUT);
  74.  
  75.   Serial.begin(9600);
  76.   WiFi.begin(ssid, password);
  77.   Serial.println("");
  78.  
  79.   // Wait for connection
  80.   while (WiFi.status() != WL_CONNECTED) {
  81.     delay(500);
  82.     Serial.print(".");
  83.   }
  84.   Serial.println("");
  85.   Serial.print("Connected to ");
  86.   Serial.println(ssid);
  87.   Serial.print("IP address: ");
  88.   Serial.println(WiFi.localIP());
  89.  
  90.   if (MDNS.begin("esp8266")) {
  91.     Serial.println("MDNS responder started");
  92.   }
  93.  
  94.   server.on("/", handleRoot);
  95.  
  96.   server.onNotFound(handleNotFound);
  97.  
  98.   server.begin();
  99.   Serial.println("HTTP server started");
  100.   // Transmitter is connected to Arduino Pin 'dataPin'  
  101.   mySwitch.enableTransmit(dataPin);
  102.  
  103.   // Optional set pulse length.
  104.    mySwitch.setPulseLength(185);
  105. }
  106.  
  107. void loop() {
  108.   // read the state of the pushbutton value:
  109.   buttonState = digitalRead(buttonPin);
  110.  
  111.   // check if the pushbutton is pressed.
  112.   // if it is, the buttonState is HIGH:
  113.   if (buttonState == HIGH) {
  114.     // turn LED on:
  115.     p=0;
  116.     digitalWrite(ledPin, HIGH);
  117.   } else {
  118.     // turn LED off:
  119.     digitalWrite(ledPin, LOW);
  120.     p++;
  121.     Serial.println(p);
  122.     if(p > 50){
  123.       Serial.println("Ring!!!");
  124.       p=0;
  125.       ring();
  126.     }
  127.      
  128.   }
  129.  
  130.   //webserver
  131.   server.handleClient();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement