Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // wifi
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266mDNS.h>
  5. #include <ArduinoOTA.h>
  6.  
  7. const char* ssid = "test";
  8. const char* password = "1234567890";
  9.  
  10. IPAddress ip(192, 168, 222, 50); // the ip address of your ESP8266
  11. IPAddress dns(192, 168, 222, 1); // the ip address of your DNS server. Mostlikely the same as gateway
  12. IPAddress gateway(192, 168, 222, 1); // the ip of your gateway.
  13. IPAddress subnet(255, 255, 255, 0); // probably this. if not you will know what this is anyway
  14.  
  15. // motion sensor
  16.  
  17. WiFiClient client;
  18. /*
  19.     The IP address of your Raspberry Pi or where ever you are running the plugin
  20. */
  21. IPAddress motionServer(192, 168, 222, 25);
  22.  
  23. /*
  24.     The port number you assigned to the plugin
  25. */
  26. #define REMOTE_PORT_NUMBER 10200
  27.  
  28. /*
  29.     the time you want to wait until this calls your raspberry pi again
  30.     I find 10.000 ms (10 s) to be reasonable.
  31.  
  32. #define TIMESPAN_MOTION 10000
  33.  
  34.     The pin where you connect the motion sensor to the ESP8266
  35. */
  36. #define HCSR501PIN 4
  37.  
  38. /* unsigned long lastMotionDetected = 0; */
  39.  
  40.  
  41. void setup() {
  42.     Serial.begin(9600);
  43.  
  44.     while (!Serial); // wait for serial attach
  45.     WiFi.mode(WIFI_STA);
  46.     WiFi.begin(ssid, password);
  47.     WiFi.config(ip, dns, gateway, subnet);
  48.     if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  49.         Serial.println("WiFi Connect Failed! Rebooting...");
  50.         delay(1000);
  51.         ESP.restart();
  52.     }
  53.     ArduinoOTA.begin();
  54.  
  55.     pinMode(HCSR501PIN, INPUT);
  56. }
  57.  
  58. void loop() {
  59.     ArduinoOTA.handle();
  60.  
  61.     int buttonState = digitalRead(HCSR501PIN);
  62.  
  63.     /* unsigned long now = millis(); */
  64.  
  65.     if (buttonState == 1){
  66.         /* lastMotionDetected = now; */
  67.         Serial.println("Motion detected");
  68.         if (client.connect(motionServer, REMOTE_PORT_NUMBER)) {
  69.             Serial.println("connected");
  70.             // Make a HTTP request:
  71.             client.println("GET /motion HTTP/1.1");
  72.             client.println();
  73.         }
  74.         delay(10000);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement