Last active
August 29, 2015 14:10
-
-
Save LuisFDuarte/010f074a5d659a58b42d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Spark IDE. | |
#include "HttpClient/HttpClient.h" | |
HttpClient http; | |
#define VARIABLE_ID "544e4sda8f7625426e332b1e8e"//REPLACE HERE WITH YOUR OWN VARIABLE'S ID | |
#define TOKEN "40RwMVjyvD9H3HOu7p191MsdoqSHg4"//REPLACE HERE WITH YOUR OWN TOKEN | |
int people = 0; | |
int tiempo=0; | |
int presence=0; | |
// Headers needed by the Ubidots API | |
http_header_t headers[] = { | |
{ "Content-Type", "application/json" }, | |
{ "X-Auth-Token" , TOKEN }, | |
{ NULL, NULL } // NOTE: Always terminate headers will NULL | |
}; | |
http_request_t request; | |
http_response_t response; | |
void setup() { | |
pinMode(D1, INPUT); | |
request.hostname = "things.ubidots.com"; | |
request.port = 80; | |
Serial.begin(9600); | |
} | |
void loop() { | |
presence=digitalRead(D1); | |
Serial.print("presence"); | |
Serial.println(presence); | |
if(presence) | |
{ | |
people=people+1; | |
do{ | |
presence=digitalRead(D1); | |
}while(presence);// wait until motion sensor sends a "0" | |
//delay(2000); | |
} | |
tiempo=tiempo+1; | |
delay(1000); | |
if(tiempo==10)//every ten seconds the count is sent to Ubidots | |
{ | |
Serial.println(people); | |
request.path = "/api/v1.6/variables/"VARIABLE_ID"/values"; | |
request.body = "{\"value\":" + String(people) + "}"; | |
http.post(request, response, headers);// Post request to send the value of the counter | |
Serial.println(response.status); | |
Serial.println(response.body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment