Skip to content

Instantly share code, notes, and snippets.

@yeffrimic
Created July 17, 2019 05:09
Show Gist options
  • Save yeffrimic/057748344cb88dab80f91c5e9b9c46d4 to your computer and use it in GitHub Desktop.
Save yeffrimic/057748344cb88dab80f91c5e9b9c46d4 to your computer and use it in GitHub Desktop.
/*
Sensores flexibles
Este sketch es un ejemplo de como hacer
que nuestro dispositivo trabaje con cualquier
sensor digital booleano, es decir todo aquel sensor que cambie
su estado cuando detecta algo, o pasa el umbral
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define wifiSSID "Xibalba Hackerspace"
#define wifiPassword "IOT1234567"
#define broker "broker.mqtt-dashboard.com"
#define subscribeTopic "/holi"
#define updateTopic1 "/msg"
#define alertTopic "/alertmsg"
#define msgAlert "alerta"
#define brokerPort 1883
#define clientID "prueba" // clientID to connect to the broker
#define updateInterval 2000 // miliseconds
#define sensorPin D5
boolean changeDetected = false;
boolean sendAlert = false;
boolean alertSent = false;
int publishFails = 0;
int alertFails = 0;
unsigned long nowTime = 0;
unsigned long lastTime = 0;
WiFiClient wemosClient;
PubSubClient mqttClient(wemosClient);
int volatile count = 0;
boolean publishString(String topic, String data) {
char tempTopic[topic.length()];
topic.toCharArray(tempTopic, topic.length() + 1);
char tempData[data.length()];
data.toCharArray(tempData, data.length() + 1);
return mqttClient.publish(tempTopic, tempData);
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Recibiendo Información de ");
Serial.println(topic);
Serial.println("payload = ");
for (int i = 0; i < length; i++) {
Serial.println((char) payload[i]);
}
}
void mqttReconnect() {
while (mqttClient.connected() == false) {
Serial.print("conectando al Broker");
Serial.println(broker);
if (mqttClient.connect(clientID)) {
Serial.println("Conectado al broker");
mqttClient.subscribe(subscribeTopic);
} else {
Serial.println("falla numero: ");
Serial.println(mqttClient.state());
Serial.println("Intentando Reconexion en 5 segundos");
}
}
}
boolean checkSensor() {
boolean sensorValue = digitalRead(sensorPin);
if (sensorValue) {
if (changeDetected == false) {
Serial.println("start detection");
changeDetected = true;
sendAlert = true;
alertSent = false;
}
} else {
if (changeDetected == true) {
Serial.println("end Detection");
changeDetected = false;
}
}
return changeDetected;
}
boolean wifiConnect() {
if (WiFi.status() != WL_CONNECTED) {
return false;
}
Serial.println("Conectado al Wifi");
Serial.println("Direccion IP");
Serial.println(WiFi.localIP());
return true;
}
void setup() {
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);
}
void loop() {
nowTime = millis();
checkSensor();
if (mqttClient.connected() == false) {
mqttReconnect();
} else {
mqttClient.loop();
if (sendAlert) {
if (!mqttClient.publish(alertTopic, msgAlert)) {
alertFails++;
}
sendAlert = false;
alertSent = true;
}
if (nowTime - lastTime > updateInterval) {
lastTime = nowTime;
String demo = "this is a demo msg # ";
demo += String(count);
Serial.println(demo);
if (!publishString(updateTopic1, demo)) {
publishFails++;
}
count++;
}
}
if (publishFails > 200) {
//reset or do something check the wifi
}
if (alertFails > 200) {
//reset or do something check the wifi
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment