Created
April 28, 2017 23:23
-
-
Save ubi-gists/e68bab308f5d719662904bd26f80c3cb 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
// Made by: Maria Carlina Hernandez | |
/**************************************** | |
* Include Libraries | |
****************************************/ | |
#include "UbidotsESPMQTT.h" | |
/**************************************** | |
* Define Constants | |
****************************************/ | |
#define TOKEN "..." // Your Ubidots TOKEN | |
#define WIFINAME "..." //Your SSID | |
#define WIFIPASS "..." // Your Wifi Pass | |
#define MQTTCLIENTNAME "..." // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name | |
#define DEVICE "pir-sensor" // Assign the device label | |
#define VARIABLE "motion" // Assign the variable label | |
#define LED 2 | |
#define SENSOR D6 | |
uint8_t contador=0; | |
unsigned long estado = 0; | |
Ubidots client(TOKEN, MQTTCLIENTNAME); | |
/**************************************** | |
* Auxiliar Functions | |
****************************************/ | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i=0;i<length;i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
} | |
/**************************************** | |
* Main Functions | |
****************************************/ | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
pinMode(SENSOR, INPUT); | |
pinMode(LED, OUTPUT); | |
client.wifiConnection(WIFINAME, WIFIPASS); | |
client.begin(callback); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if (!client.connected()) { | |
digitalWrite(LED, LOW); | |
client.reconnect(); | |
digitalWrite(LED, HIGH); | |
} else { | |
digitalWrite(LED, HIGH); | |
} | |
uint8_t sensorValue = digitalRead(SENSOR); | |
bool flag = false; | |
if(sensorValue>0){ | |
for(uint8_t espera=0; espera<=4; espera++){ | |
sensorValue = digitalRead(SENSOR); | |
Serial.println(sensorValue); | |
if(sensorValue==1){ | |
contador++; | |
} | |
if(contador>3){ | |
flag = true; | |
} | |
delay(500); | |
} | |
} | |
Serial.println("sending data"); | |
uint8_t value; | |
if(flag){ | |
value = 1; | |
client.add(VARIABLE, value); | |
client.ubidotsPublish(DEVICE); | |
}else{ | |
value = 0; | |
if(estado == 10000){ | |
client.add(VARIABLE, value); | |
client.ubidotsPublish(DEVICE); | |
estado = 0; | |
} | |
} | |
estado++; | |
client.loop(); | |
contador = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment