//Including the four libraries
#include <UniversalTelegramBot.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
//------- WiFi Settings -------
const char* ssid = "";
const char* password = "";
char thingsboardServer[] = "1";
#define TOKEN ""
// ------- Telegram config --------
#define BOT_TOKEN "" // your Bot Token (Get from Botfather)
#define CHAT_ID "" // Chat ID of where you want the message to go (You can use MyIdBot to get the chat ID)
const int gasAnalogPin = 32;
long Bot_lasttime;
int bulk_messages_mtbs = 12000; // testing to delay 6sec to detecting another distance and which message been sent .
long lastMsg = 0;
int value = 0;
String val_str; //see last code block below use these to convert the float that you get back from DHT to a string =str
char val[50];
WiFiClientSecure espClient;
UniversalTelegramBot bot(BOT_TOKEN, espClient);
WiFiClient Client;
PubSubClient client(Client);
String ipAddress = "";
volatile bool telegramButton1PressedFlag = false;
volatile bool telegramButton2PressedFlag = false;
volatile bool telegramButton3PressedFlag = false;
volatile bool telegramButton4PressedFlag = false;
int status = WL_IDLE_STATUS;
unsigned long lastSend;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
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();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
Serial.print("Connecting to ThingsBoard node ...");
// Attempt to connect (clientId, username, password)
if ( client.connect("ESP32 Device", TOKEN, NULL) ) {
Serial.println( "[DONE]" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}
void setup() {
Serial.begin(115200);
pinMode(gasAnalogPin, INPUT);
setup_wifi();
client.setServer( thingsboardServer, 1883 );
lastSend = 0;
}
void sendTelegramMessage1() {
String message = "Air was good @ 27 Mandai Estate Meeting Room!";
if(bot.sendMessage(CHAT_ID, message, "Markdown")){
Serial.println("TELEGRAM Message 1 Successfully sent");
}
telegramButton1PressedFlag = false;
}
void sendTelegramMessage2() {
String message2 = "Air was poor, please turn on the aircondition fan @ 27 Mandai Estate Meeting Room!";
if(bot.sendMessage(CHAT_ID, message2, "Markdown")){
Serial.println("TELEGRAM Message 2 Successfully sent");
}
telegramButton2PressedFlag = false;
}
void sendTelegramMessage3() {
String message3 = "Air was bad, please leave the room @ 27 Mandai Estate Meeting Room!";
if(bot.sendMessage(CHAT_ID, message3, "Markdown")){
Serial.println("TELEGRAM Message 3 Successfully sent");
}
telegramButton3PressedFlag = false;
}
void sendTelegramMessage4() {
String message4 = "Air was terrible, please emergency evaculation @ 27 Mandai Estate Meeting Room!";
if(bot.sendMessage(CHAT_ID, message4, "Markdown")){
Serial.println("TELEGRAM Message 4 Successfully sent");
}
telegramButton4PressedFlag = false;
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
float myfloat;
float V0 = 750;
float Rs = 0;
float R0 = 0;
float Voltage;
float Vout = 1183;
float ratio = 0;
static bool value_reach = false;
static bool value_reach2 = false;
R0 = ((20000*5000/V0)-40000);
Rs = ((20000*5000/analogRead(gasAnalogPin))-40000);
ratio = Rs/R0; //Replace R0 with the value found using the sketch above
Serial.println(R0);
Serial.println(Rs);
Serial.println(ratio); // How to calculate PPM?
Serial.println("ratio");
delay(1000);
val_str = String(ratio); //converting ftemp (the float variable above) to a string
val_str.toCharArray(val, val_str.length() + 1); //packaging up the data to publish to mqtt whoa...
// Prepare a JSON payload string
String payload = "{";
payload += "\"val\":"; payload += val; payload += ",";
payload += "}";
// {"ts":1451649600512, "values":{"key1":"value1", "key2":"value2"}};
// Send payload
char attributes[100];
payload.toCharArray( attributes, 100 );
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
if (ratio > 1.00) { //good stage 1
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
Serial.println("checking ratio (1) again");
delay(3000);
if(!value_reach)
{
if(ratio > 1.00){
Serial.println("Send notification 1 to Telegram");
sendTelegramMessage1();
value_reach = true;
Bot_lasttime = 0;
}
}
} else if (ratio > 0.50 && ratio < 1.00) { //bad stage 2
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
Serial.println("checking ratio (2) again");
delay(3000);
if(value_reach)
{
if(ratio > 0.50 && ratio < 1.00){
Serial.println("Send notification 2 to Telegram");
sendTelegramMessage2();
Bot_lasttime = 0;
value_reach = false;
}
}
} else if (ratio > 0.10 && ratio <0.50){ //poor stage 3
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
Serial.println("checking ratio (3) again");
delay(3000);
if(!value_reach2)
{
if (ratio > 0.10 && ratio <0.50){
Serial.println("Send notification 3 to Telegram");
sendTelegramMessage3();
value_reach2 = true;
Bot_lasttime = 0;
}
}
} else if (ratio < 0.10){ //terrible stage 4
client.publish( "v1/devices/me/telemetry", attributes );
Serial.println( attributes );
Serial.println("checking ratio (4) again");
delay(3000);
if(value_reach2)
{
if (ratio < 0.10){
Serial.println("Send notification 4 to Telegram");
sendTelegramMessage4();
Bot_lasttime = 0;
value_reach2 = false;
}
}
}
}
}
After connecting up my think board server , my esp32 will reboot automatically .
Serial monitor shown :"Attempting MQTT connection...Connecting to ThingsBoard node ...[DONE] Guru Meditation Error: Core 1 panic'ed (Integer Divide By Zero). Exception was unhandled. Core 1 register dump: PC : 0x400d2162 PS : 0x00060430 A0 : 0x800d68f4 A1 : 0x3ffb1f00 A2 : 0x05f5e100 A3 : 0x3ffc0b98 A4 : 0x00000020 A5 : 0x80000020 A6 : 0x00000000 A7 : 0x3ffba3d8 A8 : 0x800d215c A9 : 0x3ffb1ee0 A10 : 0x00000000 A11 : 0x47b64aaa A12 : 0x00003a98 A13 : 0x00000cee A14 : 0x00000001 A15 : 0x00000000 SAR : 0x0000001c EXCCAUSE: 0x00000006 EXCVADDR: 0x00000000 LBEG : 0x400d602d LEND : 0x400d6039 LCOUNT : 0x00000000 Backtrace: 0x400d2162:0x3ffb1f00 0x400d68f1:0x3ffb1fb0 0x4008eab5:0x3ffb1fd0"