Last active
October 25, 2019 17:48
-
-
Save yeffrimic/62ef8ee2d8df78c96d350201f64d2b6e 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
/* | |
Sensor de gas | |
Este sketch tiene conectado a un pin digital | |
un sensor mq2 que puede detectar el humo en el ambiente | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#define wifiSSID "nelprro" //nombre del wifi | |
#define wifiPassword "estalibre" // contrase;a del wifi | |
#define broker "broker.mqttdashboard.com" // broker en internet, nos permite intercambiar los mensajes | |
#define alertTopic "/alertaHumo" // topico al que se envia la informacion de la alerta | |
#define msgAlert "Humo detectado" //mensaje que se envia al momento de detectar la alarma | |
#define brokerPort 1883 | |
#define clientID "alertaHumo" // clientID to connect to the broker | |
#define sensorPin D5 | |
boolean changeDetected = false; | |
boolean sendAlert = false; | |
boolean alertSent = false; | |
int publishFails = 0; | |
int alertFails = 0; | |
WiFiClient wemosClient; | |
PubSubClient mqttClient(wemosClient); | |
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) { | |
} | |
void mqttReconnect() { | |
while (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"); | |
} | |
} | |
} | |
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() { | |
checkSensor(); | |
if (mqttClient.connected() == false) { | |
mqttReconnect(); | |
} else { | |
mqttClient.loop(); | |
if (sendAlert) { | |
delay(500); | |
if (!mqttClient.publish(alertTopic, msgAlert)) { | |
alertFails++; | |
} | |
sendAlert = false; | |
alertSent = true; | |
} | |
} | |
} |
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
[ | |
{ | |
"id": "c8aa19ef.4eb458", | |
"type": "tab", | |
"label": "Flow 1", | |
"disabled": false, | |
"info": "" | |
}, | |
{ | |
"id": "952eaf09.c89c2", | |
"type": "ui_toast", | |
"z": "c8aa19ef.4eb458", | |
"position": "dialog", | |
"displayTime": "3", | |
"highlight": "", | |
"sendall": true, | |
"outputs": 1, | |
"ok": "OK", | |
"cancel": "", | |
"raw": false, | |
"topic": "", | |
"name": "", | |
"x": 490, | |
"y": 240, | |
"wires": [ | |
[] | |
] | |
}, | |
{ | |
"id": "4f5ddfd2.0569a8", | |
"type": "ui_gauge", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"group": "44546030.6f0b38", | |
"order": 1, | |
"width": 0, | |
"height": 0, | |
"gtype": "gage", | |
"title": "Temperatura", | |
"label": "units", | |
"format": "{{value}}", | |
"min": 0, | |
"max": "100", | |
"colors": [ | |
"#00b500", | |
"#e6e600", | |
"#ca3838" | |
], | |
"seg1": "", | |
"seg2": "", | |
"x": 690, | |
"y": 360, | |
"wires": [] | |
}, | |
{ | |
"id": "973fb04f.2c682", | |
"type": "ui_gauge", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"group": "e43ad7ae.a0c9f", | |
"order": 1, | |
"width": 0, | |
"height": 0, | |
"gtype": "gage", | |
"title": "Humedad", | |
"label": "units", | |
"format": "{{value}}", | |
"min": 0, | |
"max": "100", | |
"colors": [ | |
"#00b500", | |
"#e6e600", | |
"#ca3838" | |
], | |
"seg1": "", | |
"seg2": "", | |
"x": 700, | |
"y": 400, | |
"wires": [] | |
}, | |
{ | |
"id": "8ccc966c.f03df", | |
"type": "inject", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"topic": "", | |
"payload": "", | |
"payloadType": "date", | |
"repeat": "1", | |
"crontab": "", | |
"once": true, | |
"onceDelay": 0.1, | |
"x": 270, | |
"y": 360, | |
"wires": [ | |
[ | |
"ff036bec.eead7" | |
] | |
] | |
}, | |
{ | |
"id": "4186f757.7d3638", | |
"type": "inject", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"topic": "", | |
"payload": "", | |
"payloadType": "date", | |
"repeat": "3", | |
"crontab": "", | |
"once": true, | |
"onceDelay": 0.1, | |
"x": 270, | |
"y": 400, | |
"wires": [ | |
[ | |
"4c6f95b4.3dd48c" | |
] | |
] | |
}, | |
{ | |
"id": "ff036bec.eead7", | |
"type": "function", | |
"z": "c8aa19ef.4eb458", | |
"name": "numeros aleatorios", | |
"func": "msg.payload = Math.floor((Math.random() * 100) + 1);\nreturn msg;", | |
"outputs": 1, | |
"noerr": 0, | |
"x": 470, | |
"y": 360, | |
"wires": [ | |
[ | |
"4f5ddfd2.0569a8" | |
] | |
] | |
}, | |
{ | |
"id": "4c6f95b4.3dd48c", | |
"type": "function", | |
"z": "c8aa19ef.4eb458", | |
"name": "numeros aleatorios", | |
"func": "msg.payload = Math.floor((Math.random() * 100) + 1);\nreturn msg;", | |
"outputs": 1, | |
"noerr": 0, | |
"x": 470, | |
"y": 400, | |
"wires": [ | |
[ | |
"973fb04f.2c682" | |
] | |
] | |
}, | |
{ | |
"id": "19560c4c.730514", | |
"type": "ui_gauge", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"group": "3209770f.67e818", | |
"order": 0, | |
"width": 0, | |
"height": 0, | |
"gtype": "wave", | |
"title": "Nivel de agua", | |
"label": "units", | |
"format": "{{value}}", | |
"min": 0, | |
"max": "100", | |
"colors": [ | |
"#00b500", | |
"#e6e600", | |
"#ca3838" | |
], | |
"seg1": "", | |
"seg2": "", | |
"x": 700, | |
"y": 460, | |
"wires": [] | |
}, | |
{ | |
"id": "c84f1767.73c88", | |
"type": "inject", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"topic": "", | |
"payload": "", | |
"payloadType": "date", | |
"repeat": "10", | |
"crontab": "", | |
"once": true, | |
"onceDelay": 0.1, | |
"x": 270, | |
"y": 460, | |
"wires": [ | |
[ | |
"be9db9c7.f7be68" | |
] | |
] | |
}, | |
{ | |
"id": "be9db9c7.f7be68", | |
"type": "function", | |
"z": "c8aa19ef.4eb458", | |
"name": "numeros aleatorios", | |
"func": "msg.payload = Math.floor((Math.random() * 100) + 1);\nreturn msg;", | |
"outputs": 1, | |
"noerr": 0, | |
"x": 470, | |
"y": 460, | |
"wires": [ | |
[ | |
"19560c4c.730514" | |
] | |
] | |
}, | |
{ | |
"id": "ae55b927.cf18e", | |
"type": "ui_text", | |
"z": "c8aa19ef.4eb458", | |
"group": "3c36db81.b6682c", | |
"order": 1, | |
"width": 0, | |
"height": 0, | |
"name": "", | |
"label": "Ultima vez humo detectado", | |
"format": "{{msg.payload}}", | |
"layout": "col-center", | |
"x": 760, | |
"y": 140, | |
"wires": [] | |
}, | |
{ | |
"id": "98189da5.f92ca8", | |
"type": "function", | |
"z": "c8aa19ef.4eb458", | |
"name": "Crear fecha", | |
"func": "fecha = new Date();\nmsg.payload = fecha.toLocaleString()\nreturn msg;", | |
"outputs": 1, | |
"noerr": 0, | |
"x": 490, | |
"y": 140, | |
"wires": [ | |
[ | |
"ae55b927.cf18e", | |
"52ab7304.1e4cbc", | |
"1aa30df7.25cbda" | |
] | |
] | |
}, | |
{ | |
"id": "52ab7304.1e4cbc", | |
"type": "debug", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"active": true, | |
"tosidebar": true, | |
"console": false, | |
"tostatus": false, | |
"complete": "false", | |
"x": 710, | |
"y": 180, | |
"wires": [] | |
}, | |
{ | |
"id": "b8fbc059.206c38", | |
"type": "mqtt in", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"topic": "/alertaHumo", | |
"qos": "2", | |
"datatype": "auto", | |
"broker": "a89ec2e.bd92c4", | |
"x": 290, | |
"y": 200, | |
"wires": [ | |
[ | |
"98189da5.f92ca8", | |
"952eaf09.c89c2" | |
] | |
] | |
}, | |
{ | |
"id": "d280fae5.090d88", | |
"type": "ui_chart", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"group": "548b8a61.607c2c", | |
"order": 1, | |
"width": 0, | |
"height": 0, | |
"label": "historial", | |
"chartType": "line", | |
"legend": "false", | |
"xformat": "HH:mm:ss", | |
"interpolate": "linear", | |
"nodata": "", | |
"dot": false, | |
"ymin": "", | |
"ymax": "", | |
"removeOlder": 1, | |
"removeOlderPoints": "", | |
"removeOlderUnit": "3600", | |
"cutout": 0, | |
"useOneColor": false, | |
"colors": [ | |
"#1f77b4", | |
"#aec7e8", | |
"#ff7f0e", | |
"#2ca02c", | |
"#98df8a", | |
"#d62728", | |
"#ff9896", | |
"#9467bd", | |
"#c5b0d5" | |
], | |
"useOldStyle": false, | |
"outputs": 1, | |
"x": 710, | |
"y": 540, | |
"wires": [ | |
[] | |
] | |
}, | |
{ | |
"id": "6fd64f17.029808", | |
"type": "inject", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"topic": "", | |
"payload": "", | |
"payloadType": "date", | |
"repeat": "1", | |
"crontab": "", | |
"once": true, | |
"onceDelay": 0.1, | |
"x": 250, | |
"y": 540, | |
"wires": [ | |
[ | |
"4dc2affc.36b918" | |
] | |
] | |
}, | |
{ | |
"id": "4dc2affc.36b918", | |
"type": "function", | |
"z": "c8aa19ef.4eb458", | |
"name": "numeros aleatorios", | |
"func": "msg.payload = Math.floor((Math.random() * 100) + 1);\nreturn msg;", | |
"outputs": 1, | |
"noerr": 0, | |
"x": 450, | |
"y": 540, | |
"wires": [ | |
[ | |
"d280fae5.090d88" | |
] | |
] | |
}, | |
{ | |
"id": "4d285a1a.6a0f7c", | |
"type": "telegram receiver", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"bot": "13949797.b53bc", | |
"saveDataDir": "", | |
"x": 230, | |
"y": 620, | |
"wires": [ | |
[ | |
"761cd68e.d7ecf8" | |
], | |
[] | |
] | |
}, | |
{ | |
"id": "761cd68e.d7ecf8", | |
"type": "debug", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"active": true, | |
"tosidebar": true, | |
"console": false, | |
"tostatus": false, | |
"complete": "false", | |
"x": 540, | |
"y": 620, | |
"wires": [] | |
}, | |
{ | |
"id": "d3aa55cc.e589", | |
"type": "telegram sender", | |
"z": "c8aa19ef.4eb458", | |
"name": "", | |
"bot": "13949797.b53bc", | |
"x": 1030, | |
"y": 220, | |
"wires": [ | |
[] | |
] | |
}, | |
{ | |
"id": "1aa30df7.25cbda", | |
"type": "function", | |
"z": "c8aa19ef.4eb458", | |
"name": "crear mensaje telegram", | |
"func": "msg.payload = {type: \"message\", \n chatId: 761322092,\n content: \"alerta de gas \" + msg.payload};\nreturn msg;", | |
"outputs": 1, | |
"noerr": 0, | |
"x": 730, | |
"y": 220, | |
"wires": [ | |
[ | |
"d3aa55cc.e589" | |
] | |
] | |
}, | |
{ | |
"id": "44546030.6f0b38", | |
"type": "ui_group", | |
"z": "", | |
"name": "temperatura", | |
"tab": "2d11720c.56ad3e", | |
"order": 1, | |
"disp": false, | |
"width": "6", | |
"collapse": false | |
}, | |
{ | |
"id": "e43ad7ae.a0c9f", | |
"type": "ui_group", | |
"z": "", | |
"name": "humedad", | |
"tab": "2d11720c.56ad3e", | |
"order": 2, | |
"disp": true, | |
"width": "6", | |
"collapse": false | |
}, | |
{ | |
"id": "3209770f.67e818", | |
"type": "ui_group", | |
"z": "", | |
"name": "nivel de agua", | |
"tab": "2d11720c.56ad3e", | |
"order": 3, | |
"disp": false, | |
"width": "6", | |
"collapse": false | |
}, | |
{ | |
"id": "3c36db81.b6682c", | |
"type": "ui_group", | |
"z": "", | |
"name": "Alertas", | |
"tab": "2d11720c.56ad3e", | |
"disp": true, | |
"width": "6", | |
"collapse": false | |
}, | |
{ | |
"id": "a89ec2e.bd92c4", | |
"type": "mqtt-broker", | |
"z": "", | |
"name": "", | |
"broker": "broker.mqttdashboard.com", | |
"port": "1883", | |
"clientid": "", | |
"usetls": false, | |
"compatmode": false, | |
"keepalive": "60", | |
"cleansession": true, | |
"birthTopic": "", | |
"birthQos": "0", | |
"birthPayload": "", | |
"closeTopic": "", | |
"closeQos": "0", | |
"closePayload": "", | |
"willTopic": "", | |
"willQos": "0", | |
"willPayload": "" | |
}, | |
{ | |
"id": "548b8a61.607c2c", | |
"type": "ui_group", | |
"z": "", | |
"name": "historial", | |
"tab": "2d11720c.56ad3e", | |
"order": 5, | |
"disp": true, | |
"width": "6", | |
"collapse": false | |
}, | |
{ | |
"id": "13949797.b53bc", | |
"type": "telegram bot", | |
"z": "", | |
"botname": "seminario2019bot", | |
"usernames": "", | |
"chatids": "", | |
"baseapiurl": "", | |
"updatemode": "polling", | |
"pollinterval": "300", | |
"usesocks": false, | |
"sockshost": "", | |
"socksport": "6667", | |
"socksusername": "anonymous", | |
"sockspassword": "", | |
"bothost": "", | |
"localbotport": "8443", | |
"publicbotport": "8443", | |
"privatekey": "", | |
"certificate": "", | |
"useselfsignedcertificate": false, | |
"verboselogging": false | |
}, | |
{ | |
"id": "2d11720c.56ad3e", | |
"type": "ui_tab", | |
"z": "", | |
"name": "informacion", | |
"icon": "dashboard", | |
"order": 1, | |
"disabled": false, | |
"hidden": false | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment