Preparation:
- Node-red installation
- MQTT Broker
- Chatbot plugin: https://flows.nodered.org/node/node-red-contrib-chatbot
- Create a bot, Telegram in this example: https://core.telegram.org/bots
- Arduino IDE
- Solder deep sleep jumper on the underside of the board, see build instruction.
Here is an example code for node-red for Undervoltage alert to Telegram.
You need to add:
*A bot to the sender node,
*Put the text you want to be sent out in the text node,
*Add chatid in conversation node: https://firstwarning.net/vanilla/discussion/4/create-telegram-bot-and-get-bots-token-and-the-groups-chat-id
*Your topic and Broker to the MQTT node.
[ { "id": "a0b2ef4c.ac08d", "type": "mqtt in", "z": "4e61ffbd.293758", "name": "", "topic": "device_topic/voltage", "qos": "2", "broker": "74f4f305.79b8e4", "x": 210, "y": 179, "wires": [ [ "2838f77a.391648" ] ] }, { "id": "2838f77a.391648", "type": "function", "z": "4e61ffbd.293758", "name": "", "func": "\n \nvar voltage = parseInt(msg.payload) / 1000;\n\nvar msg = {payload:voltage}\n\nreturn msg;", "outputs": "1", "noerr": 0, "x": 396, "y": 179, "wires": [ [ "d899ef9c.f29bf8" ] ] }, { "id": "e695c610.d8b18", "type": "chatbot-telegram-send", "z": "4e61ffbd.293758", "bot": "cfed462d.fcf59", "track": false, "parseMode": "", "outputs": 0, "x": 1112, "y": 179, "wires": [] }, { "id": "19c9b344.512d35", "type": "chatbot-message", "z": "4e61ffbd.293758", "name": "", "message": [ { "message": "Battery under 3.3V on unit XYZ" } ], "answer": false, "track": false, "x": 914, "y": 178, "wires": [ [ "e695c610.d8b18" ] ] }, { "id": "57bb9322.d0eb8c", "type": "chatbot-conversation", "z": "4e61ffbd.293758", "name": "", "chatId": "", "transport": "telegram", "messageId": "", "contextMessageId": false, "store": "", "x": 748, "y": 178, "wires": [ [ "19c9b344.512d35" ] ] }, { "id": "d899ef9c.f29bf8", "type": "falling-edge", "z": "4e61ffbd.293758", "name": "", "threshold": "3.3", "x": 559, "y": 178, "wires": [ [ "57bb9322.d0eb8c" ] ] }, { "id": "74f4f305.79b8e4", "type": "mqtt-broker", "z": "", "broker": "localhost", "port": "1883", "clientid": "", "usetls": false, "compatmode": true, "keepalive": "60", "cleansession": true, "willTopic": "", "willQos": "0", "willPayload": "", "birthTopic": "", "birthQos": "0", "birthPayload": "" }, { "id": "cfed462d.fcf59", "type": "chatbot-telegram-node", "z": "", "botname": "", "usernames": "", "polling": "1000", "log": "" } ]
Arduino code:
Basic work: Wifi connect -> MQTT Broker connect -> Measure Voltage -> Send Voltage over MQTT -> deep sleep "sleepTimsS" seconds
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // MQTT
#define sleepTimeS 600 //Time between updates
// CHANGE
const char* ssid = "ssid";
const char* password = "pass";
const char* mqtt_server = "IP";
String device_topic = "your_topic";
float cal_volt = 6.85; // Calibrate this value if the voltage reading is of
String voltage_topic = device_topic+"/voltage";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
//Wifi
WiFi.begin(ssid, password);
int wifiTryConnectCounter = 0;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
wifiTryConnectCounter++;
if (wifiTryConnectCounter >= 10)
{
ESP.deepSleep(sleepTimeS * 1000000);
}
}
//MQTT
client.setServer(mqtt_server, 1883);
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
//Success
} else {
ESP.deepSleep(sleepTimeS * 1000000);
}
//Sensor
digitalWrite(15, LOW);
int sensorValue = analogRead(A0);
digitalWrite(15, HIGH);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (cal_volt / 1023.0);
//Send
snprintf (msg, 75, "%ld", (int)(voltage * 1000));
client.publish(voltage_topic.c_str(), msg);
//Sleep and repeat
client.loop(); // MQTT work
ESP.deepSleep(sleepTimeS * 1000000);
}
void loop() {
// Will not run
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.