Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
- int calibrationTime = 30;
- //the time when the sensor outputs a low impulse
- long unsigned int lowIn;
- //the amount of milliseconds the sensor has to be low
- //before we assume all motion has stopped
- long unsigned int pause = 100;
- boolean lockLow = true;
- boolean takeLowTime;
- int pirPin = 16; //the digital pin connected to the PIR sensor's output // pin used to show the motion status
- const char* ssid = "AT GUEST - 3"; // wifi ssid
- const char* password = "NOPASSWORD"; // wifi password
- String auth = "YWxpMjIyMjJvbWFyQGdtYWlsLmNvbTpkZWVvd2ViCgo="; // Authentication credentials Create a string from <email_address>:<API_Password> and encode it base64
- // The sample:
- // String auth = "dXNlcjpwYXNzd29yZA=="
- // is the encoding for "user:password"
- char server[] = "www.ic2pro.com"; // Wiring Cloud host name www.ic2cloud.com
- int port = 80; // port
- String devId = "111-222-333"; // Device ID. CREATE YOUR OWN GUID; Use this http://www.guidgenerator.com/
- // You can leave this 111-222-333 if this is your first project with IC2Cloud.
- // If you change it then you have to change the ID on your cloud device and
- // on your mobile application
- void setup() {
- Serial.begin(9600);
- // initilize serial connection for debug
- //mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
- pinMode(pirPin, INPUT);
- digitalWrite(pirPin, LOW);
- //give the sensor some time to calibrate
- Serial.print("calibrating sensor ");
- for (int i = 0; i < calibrationTime; i++) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println(" done");
- Serial.println("SENSOR ACTIVE");
- delay(1000);
- delay(10);
- // setup GPIO pins
- // Start connection to a WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- //,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
- void sendEvent() {
- // send motion event to the wiring cloud at IC2Cloud platform
- Serial.print("connecting to ");
- Serial.println(server);
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- if (!client.connect(server, port)) {
- Serial.println("connection failed");
- return;
- }
- // Send motion signal to the server
- client.print("GET http://" + String(server) + ":" + String(port) + "/Wire/connector/set?id=" + devId + "&MOTION=YES HTTP/1.1\r\n" +
- "Host: " + server + "\r\n" +
- "Authorization: Basic " + auth + "\r\n" +
- "Connection: close\r\n\r\n");
- delay(10);
- // Read all the lines of the reply from server and print them to Serial
- while (client.available()) {
- String line = client.readStringUntil('\r');
- Serial.print(line);
- }
- Serial.println();
- Serial.println("closing connection");
- }
- void loop() {
- if (digitalRead(pirPin) == HIGH) {
- if (lockLow) {
- //makes sure we wait for a transition to LOW before any further output is made:
- lockLow = false;
- Serial.println("---");
- Serial.println("motion detected ");
- delay(50);
- }
- takeLowTime = true;
- }
- if (digitalRead(pirPin) == LOW) {
- if (takeLowTime) {
- lowIn = millis(); //save the time of the transition from high to LOW
- takeLowTime = false; //make sure this is only done at the start of a LOW phase
- }
- //if the sensor is low for more than the given pause,
- //we assume that no more motion is going to happen
- if (!lockLow && millis() - lowIn > pause) {
- //makes sure this block of code is only executed again after
- //a new motion sequence has been detected
- lockLow = true;
- Serial.println("motion ended "); //output
- delay(1000);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement