Advertisement
Guest User

Untitled

a guest
Sep 8th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
  3. int calibrationTime = 30;
  4.  
  5. //the time when the sensor outputs a low impulse
  6. long unsigned int lowIn;
  7.  
  8. //the amount of milliseconds the sensor has to be low
  9. //before we assume all motion has stopped
  10. long unsigned int pause = 100;
  11.  
  12. boolean lockLow = true;
  13. boolean takeLowTime;
  14.  
  15. int pirPin = 16; //the digital pin connected to the PIR sensor's output // pin used to show the motion status
  16.  
  17.  
  18.  
  19.  
  20. const char* ssid = "AT GUEST - 3"; // wifi ssid
  21. const char* password = "NOPASSWORD"; // wifi password
  22. String auth = "YWxpMjIyMjJvbWFyQGdtYWlsLmNvbTpkZWVvd2ViCgo="; // Authentication credentials Create a string from <email_address>:<API_Password> and encode it base64
  23. // The sample:
  24. // String auth = "dXNlcjpwYXNzd29yZA=="
  25. // is the encoding for "user:password"
  26.  
  27.  
  28. char server[] = "www.ic2pro.com"; // Wiring Cloud host name www.ic2cloud.com
  29. int port = 80; // port
  30. String devId = "111-222-333"; // Device ID. CREATE YOUR OWN GUID; Use this http://www.guidgenerator.com/
  31. // You can leave this 111-222-333 if this is your first project with IC2Cloud.
  32. // If you change it then you have to change the ID on your cloud device and
  33. // on your mobile application
  34.  
  35.  
  36.  
  37. void setup() {
  38. Serial.begin(9600);
  39.  
  40. // initilize serial connection for debug
  41. //mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
  42. pinMode(pirPin, INPUT);
  43. digitalWrite(pirPin, LOW);
  44.  
  45. //give the sensor some time to calibrate
  46. Serial.print("calibrating sensor ");
  47. for (int i = 0; i < calibrationTime; i++) {
  48. Serial.print(".");
  49. delay(1000);
  50. }
  51. Serial.println(" done");
  52. Serial.println("SENSOR ACTIVE");
  53. delay(1000);
  54.  
  55.  
  56.  
  57.  
  58. delay(10);
  59.  
  60. // setup GPIO pins
  61.  
  62. // Start connection to a WiFi network
  63. Serial.println();
  64. Serial.println();
  65. Serial.print("Connecting to ");
  66. Serial.println(ssid);
  67.  
  68. WiFi.begin(ssid, password);
  69.  
  70. while (WiFi.status() != WL_CONNECTED) {
  71. delay(500);
  72. Serial.print(".");
  73. }
  74.  
  75. Serial.println("");
  76. Serial.println("WiFi connected");
  77. Serial.println("IP address: ");
  78. Serial.println(WiFi.localIP());
  79.  
  80. }
  81. //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  82.  
  83. void sendEvent() {
  84. // send motion event to the wiring cloud at IC2Cloud platform
  85. Serial.print("connecting to ");
  86. Serial.println(server);
  87.  
  88. // Use WiFiClient class to create TCP connections
  89. WiFiClient client;
  90. if (!client.connect(server, port)) {
  91. Serial.println("connection failed");
  92. return;
  93. }
  94.  
  95. // Send motion signal to the server
  96. client.print("GET http://" + String(server) + ":" + String(port) + "/Wire/connector/set?id=" + devId + "&MOTION=YES HTTP/1.1\r\n" +
  97. "Host: " + server + "\r\n" +
  98. "Authorization: Basic " + auth + "\r\n" +
  99. "Connection: close\r\n\r\n");
  100. delay(10);
  101.  
  102. // Read all the lines of the reply from server and print them to Serial
  103. while (client.available()) {
  104. String line = client.readStringUntil('\r');
  105. Serial.print(line);
  106. }
  107.  
  108. Serial.println();
  109. Serial.println("closing connection");
  110. }
  111.  
  112. void loop() {
  113.  
  114. if (digitalRead(pirPin) == HIGH) {
  115. if (lockLow) {
  116. //makes sure we wait for a transition to LOW before any further output is made:
  117. lockLow = false;
  118. Serial.println("---");
  119. Serial.println("motion detected ");
  120. delay(50);
  121. }
  122. takeLowTime = true;
  123. }
  124.  
  125. if (digitalRead(pirPin) == LOW) {
  126.  
  127. if (takeLowTime) {
  128. lowIn = millis(); //save the time of the transition from high to LOW
  129. takeLowTime = false; //make sure this is only done at the start of a LOW phase
  130. }
  131. //if the sensor is low for more than the given pause,
  132. //we assume that no more motion is going to happen
  133. if (!lockLow && millis() - lowIn > pause) {
  134. //makes sure this block of code is only executed again after
  135. //a new motion sequence has been detected
  136. lockLow = true;
  137. Serial.println("motion ended "); //output
  138.  
  139. delay(1000);
  140. }
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement