Created
September 23, 2019 21:01
-
-
Save yeffrimic/5620326f067e048c7b5827ad869166af 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
/* | |
Magnetic door sensor with time alarm | |
This sensor sends alerts when the door was opened, closed and when | |
the time alarm was exceeded | |
**next steps, use the wifimanager with custom parameters to update | |
the alarm time. | |
when there is no internet save the times went opened, closed and the time alarm | |
updatable from ota | |
custom parametes updates | |
*/ | |
/************* | |
Libraries | |
************/ | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <NTPClient.h> | |
#include <WiFiUdp.h> | |
/************************ | |
constants definition | |
***********************/ | |
#define wifiSSID "Xibalba Hackerspace" | |
#define wifiPassword "IOT1234567" | |
#define broker "192.168.1.2" // broker url | |
#define brokerPort 1883 //broker port | |
#define clientID "prueba" // clientID to connect to the broker | |
#define sensorPin D5 // | |
#define alertTime 60000 // miliseconds | |
int count = 0;// to sleep after some tries | |
/********************** | |
object declaration | |
*********************/ | |
//wifi and mqtt objects | |
WiFiClient wemosClient; | |
PubSubClient mqttClient(wemosClient); | |
//udp client use it in NTP requests | |
WiFiUDP ntpUDP; | |
int32_t utc = -6 * 3600; | |
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", utc, 60000); | |
/************* | |
functions | |
************/ | |
//this is just for the good work of the library | |
void callback(char* topic, byte* payload, unsigned int length) { | |
} | |
//Connect and reconnect to the MQTT broker | |
void mqttReconnect() { | |
if (mqttClient.connected() == false) { | |
Serial.print("conectando al Broker"); | |
Serial.println(broker); | |
if (mqttClient.connect(clientID)) { | |
Serial.println("Conectado al broker"); | |
} else { | |
Serial.println("falla numero: "); | |
Serial.println(mqttClient.state()); | |
Serial.println("Intentando Reconexion en 5 segundos"); | |
delay(5000); | |
} | |
} | |
} | |
boolean wifiConnect() { | |
if (WiFi.status() != WL_CONNECTED) { | |
return false; | |
} | |
Serial.println("Conectado al Wifi"); | |
Serial.println("Direccion IP"); | |
Serial.println(WiFi.localIP()); | |
return true; | |
} | |
/* | |
Send Data | |
receive the topic and the payload to send the data to the broker | |
char* Topic, topic to send the data | |
String payload, the data to send with the topic to the broker | |
if there is no way to connect the device goes to deepsleep to save power | |
return true if the data was sent else false | |
*/ | |
boolean sendData(char* topic, String payload) { | |
char auxPayload[payload.length() + 1]; | |
payload.toCharArray(auxPayload, payload.length() + 1); | |
while (!mqttClient.connected()) { | |
mqttReconnect(); | |
count ++; | |
if (count > 50) { | |
ESP.deepSleep(0); | |
} | |
} | |
mqttClient.loop(); | |
Serial.print("enviando datos "); | |
Serial.print(topic); | |
Serial.print(", "); | |
Serial.print(auxPayload); | |
if (mqttClient.publish(topic, auxPayload)) { | |
Serial.println(" enviada"); | |
return true; | |
} else { | |
Serial.println(" no enviada"); | |
return false; | |
} | |
} | |
/* | |
setup | |
the core of the project | |
this configure all the system and watch the sensor | |
when the magnetic sensor is open sends a reset signal | |
and the device connect to wifi, send the alert and | |
wait for close or when the time alarms ends send the alert | |
then goto sleep to save power | |
*/ | |
void setup() { | |
pinMode(sensorPin, INPUT_PULLUP); | |
Serial.begin(115200); | |
Serial.println("Conectando al WiFi"); | |
Serial.println(wifiSSID); | |
WiFi.begin(wifiSSID, wifiPassword); | |
while (wifiConnect() != true) { | |
Serial.println("."); | |
delay(500); | |
} | |
mqttClient.setServer(broker, brokerPort); | |
mqttClient.setCallback(callback); | |
timeClient.begin(); | |
timeClient.update(); | |
int timeDoorOpened = millis();//count the miliseconts when the devices was reset | |
Serial.println("ntp"); | |
Serial.println(timeClient.getEpochTime()); | |
Serial.println("tiempo transcurrido"); | |
Serial.println(int(timeDoorOpened / 1000)); | |
Serial.println(int(timeClient.getEpochTime()) - (int(timeDoorOpened / 1000))); | |
String auxData = String(int(timeClient.getEpochTime()) - (int(timeDoorOpened / 1000))); | |
sendData("/abrir", auxData); | |
while ( digitalRead(sensorPin) != LOW) { | |
delay(10); | |
if (millis() > timeDoorOpened + alertTime) { | |
timeClient.update(); | |
auxData = String(timeClient.getEpochTime()); | |
sendData("/alertaTiempo", auxData); | |
delay(1000); | |
ESP.deepSleep(0); | |
} | |
} | |
timeClient.update(); | |
auxData = String(timeClient.getEpochTime()); | |
sendData("/cerrar", auxData); | |
delay(1000); | |
ESP.deepSleep(0); | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment