42 Weather update on Telegram with NodeMCU

 Introduction:

We are going to interface BME280 sensor with NodeMCU. BME280 is a 3 in 1 sensor like Humidity, Biometric pressure and Ambient temperature. NodeMCU send these data to Telegram messenger then we can get  update on telegram like this picture.

BME280:

BME280 is a human sensor especially developed for mobile application and wearables where size and low power consumption are key design parameters. The unit combines high linearity and high accuracy sensors and is perfectly feasible for low current consumption, long-term stability and high EMC robustness. The humidity sensor offers an extremely fast response time and therefore supports performance requirements  for emerging applications such as context awareness, and high accuracy over a wide temperature range.

Schematic Diagram:

PinOut:
NodeMCU  BME280
 Vin  Vin
 GND  GND
    SCL  D2
 SDA  D1

Create Telegram Bot:

Open Telegram app  in your smartphone. and search "BotFather" then you will get a bot, Click there.
Now click on "Start". Then you will get this message.
Click on  /newbot to create bot.
Now type channel name then press enter. Now you have to choose username. it must contain "bot" in end. example xyxbot.
I choose "MBEIOTbot". It is accepted and created Bot. We get bit url "t.me/MBEIOTbot" and API key.
select this API key then copy it.

Library Installation:

Open you Arduino IDE, Then we have to install some libraries that is "BME280", "CTBot" and "Arduino Json". Go to  Sketch >> Include Library >>  Manage Libraries.  search here BME280 then you will get BME280 library created by Adafruit, Install it.
Now search "CTBot" then install.
Again search "ArduinoJson". You will get ArduinoJson library created by "Benoit Blanchon". Install it.

Let us code:

We have to include library "CTBot" for telegram bot, <Wire.h> for I2C protocol,  <Adafruit_Sensor.h> and <Adafruit_BME280.h> for BME280 sensor. 
 #include "CTBot.h"  
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
bme, myBot, msg  are objects. t, p and h are float type variables which store temperature, pressure and humidity data. replace "priyo"  and "Priyo789" with your ssid and password of your WiFi router. Replace "mytoken" with your Telegram bot token or API key.
 Adafruit_BME280 bme;  
float t = 0;
float p = 0;
float h = 0;
CTBot myBot;
TBMessage msg;
String ssid = "priyo"; // REPLACE mySSID WITH YOUR WIFI SSID
String pass = "Priyo789"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "1555013970:AAGFAvmd20zqzQX9Q_M3c26psToEGDcm0Jo"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
bme.begin() function initialized BME280 sensor, 0x76 is I2C address of BME280 sensor. once it initialized then it data will be store in status variable. If sensor will be not connected with properly with board then it will be failed and print on serial monitor "Could not find a valid BME280 sensor, check wiring, address, sensor ID!" . myBot.wifiConnect function help to connect board with internet by using ssid and password. myBot.setTelegramToken function help  to connect with telegram bot by using Bot token number (API key). If telegram will be successfully connected then it will print  "\ntestConnection OK" on serial monitor otherwise it will print  "\ntestConnection NOK" 
 status = bme.begin(0x76, &Wire);  
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
Serial.println("-- Default Test --");
Serial.println();
Serial.println("Starting TelegramBot...");
myBot.wifiConnect(ssid, pass);
myBot.setTelegramToken(token);
if (myBot.testConnection()){
Serial.println("\ntestConnection OK");
}
else
{
Serial.println("\ntestConnection NOK");
}
}
printValues()  function will print Temperature, Humidity, Altitude and Biometric pressure on serial monitor.
 void printValues() {  
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}
variable t, h and p store data from BME280 sensor. t store temperature in degree Celsius, h store humidity in % and p store pressure in hPa.
 t = bme.readTemperature();  
h = bme.readHumidity();
p = bme.readPressure() / 100.0F; // in hPa
myBot.getNewMessage() function check if it receives any  new message then again verify command by msg.text.equalIsIgnoreCase() function. If message match with any command for example new message is /humidity then it will response Humidity in Telegram bot.
  if (myBot.getNewMessage(msg)) {  
if (msg.text.equalsIgnoreCase("/humidity")) {
myBot.sendMessage(msg.sender.id, humid);
}
if (msg.text.equalsIgnoreCase("/temperature")) {
myBot.sendMessage(msg.sender.id, temp);
}
if (msg.text.equalsIgnoreCase("/pressure")) {
myBot.sendMessage(msg.sender.id, pres);
}
if (msg.text.equalsIgnoreCase("/weather")) {
myBot.sendMessage(msg.sender.id, data);
}
}

Code:
 #include "CTBot.h"  
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
float t = 0;
float p = 0;
float h = 0;
CTBot myBot;
TBMessage msg;
String ssid = "priyo"; // REPLACE mySSID WITH YOUR WIFI SSID
String pass = "Priyo789"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
String token = "1555013970:AAGFAvmd20zqzQX9Q_M3c26psToEGDcm0Jo"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
void setup() {
Serial.begin(9600);
while (!Serial); // time to get serial running
Serial.println(F("BME280 test"));
// client.wifiConnection(WIFISSID, PASSWORD);
unsigned status;
// default settings
status = bme.begin();
// You can also pass in a Wire library object like &Wire2
status = bme.begin(0x76, &Wire);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
Serial.println("-- Default Test --");
Serial.println();
Serial.println("Starting TelegramBot...");
myBot.wifiConnect(ssid, pass);
myBot.setTelegramToken(token);
if (myBot.testConnection()){
Serial.println("\ntestConnection OK");
}
else
{
Serial.println("\ntestConnection NOK");
}
}
void loop() {
t = bme.readTemperature();
h = bme.readHumidity();
p = bme.readPressure() / 100.0F; // in hPa
printValues();
String humid = "Humidity: " + String(h) + " % \n";
String temp = "Temperature: " + String(t) + "°C \n";
String pres = "Pressure: " + String(p) + " hPa \n";
String data = "Temperature: " + String(t) + "°C, " + "Humidity: " + String(h) + " %, " + "Pressure: " + String(p) + " hPa \n";
if (myBot.getNewMessage(msg)) {
if (msg.text.equalsIgnoreCase("/humidity")) {
myBot.sendMessage(msg.sender.id, humid);
}
if (msg.text.equalsIgnoreCase("/temperature")) {
myBot.sendMessage(msg.sender.id, temp);
}
if (msg.text.equalsIgnoreCase("/pressure")) {
myBot.sendMessage(msg.sender.id, pres);
}
if (msg.text.equalsIgnoreCase("/weather")) {
myBot.sendMessage(msg.sender.id, data);
}
}
delay(10);
}
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}

👇👇Watch this video for more details👇👇
Thanks for visiting💖 🙏
Please Donate to help me and afford new equipment & components to make more videos and blogs.
Please Donate us:

No comments:

Powered by Blogger.