//Including the four libraries
#include <UniversalTelegramBot.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
//------- WiFi Settings -------
const char* ssid = "";
const char* password = "";
const char* mqtt_server = "";
const char* mqtt_username = "";
const char* mqtt_password = "";
// ------- 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;
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;
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
if (client.connect(clientId.c_str(), mqtt_username , mqtt_password)) {
Serial.println("connected");
client.publish("SmartToilet", "welcome");
// ... and resubscribe
client.subscribe("SmartToilet");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(gasAnalogPin, INPUT);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
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);
long now = millis();
if (now - lastMsg > 60000) {
lastMsg = now;
++value;
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...
if (ratio > 1.00) { //good stage 1
Serial.println("Publish MQTT Message: ");
client.publish("SmartToilet", val);
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
Serial.print("Publish MQTT Message: ");
client.publish("SmartToilet", val);
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
Serial.print("Publish MQTT Message: ");
client.publish("SmartToilet", val);
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
Serial.print("Publish MQTT Message: ");
client.publish("SmartToilet", val);
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;
}
}
}
}
}
My MQTT now publish as plain message format , how do i change to json publish format?