-->
Page 1 of 1

Weather Monitoring Station Using Telegram

PostPosted: Sat Jan 21, 2023 11:19 pm
by bebekbengil
Hello everyone, I want to make weather monitoring using the Wemos D1R2 microcontroller and several types of sensors. This project uses Telegram bots to find out the conditions at the place and uses blynk cloud to see weather trends. The problem is the coding of the direction and wind speed sensors cannot run on Telegram (I don't know where the problem is) it can only run on the Blynk Cloud web. For other sensors, it can run on Telegram and the Blynk Cloud web. Thank you all, I hope there is someone willing to help me. For example sketch code I attach below:

#define BLYNK_TEMPLATE_ID "TMPLLbVmeb_W"
#define BLYNK_DEVICE_NAME "Monitoring Pacung"
#define BLYNK_AUTH_TOKEN "5ncqWXnlEQhZBGsARLy0CoR19gipJuTA"
#define TLG_TOKEN "5867588680:AAE5s322NLJc8RE1Q9MYJ3Iy_lc5FmpUHag" // token anakkendali_bot
#define WIFI_SSID "apah"
#define WIFI_PASS "yoi123321"


#include <Wire.h>
#include <BH1750.h>
#include <Adafruit_BMP085.h>
#define BLYNK_PRINT Serial
#include <string.h>
// Device libraries (Arduino ESP32/ESP8266 Cores)
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <utlgbotlib.h>

#include "DHT.h"
#include <SoftwareSerial.h>
#define DHTPIN D6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
BH1750 GY30;
Adafruit_BMP085 bmp;
float h, t, l, p;

#define MAX_CONN_FAIL 50
#define MAX_LENGTH_WIFI_SSID 31
#define MAX_LENGTH_WIFI_PASS 63
char auth[] = BLYNK_AUTH_TOKEN;
#define DEBUG_LEVEL_UTLGBOT 0

BlynkTimer timer;
SoftwareSerial dataserial(2, 14);
String data, arah_angin, s_angin;
int a, b;
volatile byte rpmcount; // count signals
volatile unsigned long last_micros;
unsigned long timeold;
unsigned long timemeasure = 5.00; // seconds
int timetoSleep = 1; // minutes
unsigned long sleepTime = 15; // minutes
unsigned long timeNow;
int countThing = 0;
int GPIO_pulse = D7; // NodeMCU = D5
float rpm, rps; // frequencies
float radius = 0.1; // meters - measure of the lenght of each the anemometer wing
float velocity_kmh; // km/h
float velocity_ms; //m/s
float omega = 0; // rad/s
float calibration_value = 2.0;
volatile boolean flag = false;

const char TEXT_START[] =
"Halo, saya adalah Bot Monitoring Perubahan Cuaca di Desa Pacung.\n"
"\n"
"Klik /info untuk melihat perubahan cuaca di Desa Pacung.";

char buff[100];
boolean state_t, state_h;

const char TEXT_HELP[] =
"Available Commands:\n"
"\n"
"/start - Show start text.\n"
"/info - Show actual text.\n"
"/relayon - Turn on the RELAY.\n"
"/statustekanan - Show actual pressure status.\n"
"/statuscahaya - Show actual lux status.\n"
"/statussuhu - Show actual temperature status.\n"
"/statuskelembaban - Show actual Humidity status.\n";

void wifi_init_stat(void);
bool wifi_handle_connection(void);


// Create Bot object
uTLGBot Bot(TLG_TOKEN);

void ICACHE_RAM_ATTR rpm_anemometer()
{
flag = true;
}

void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
float l = GY30.readLightLevel();
float p = bmp.readPressure();

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (dataserial.available()) // Jika ada data yang diterima dari sensor
{
data = dataserial.readString(); // data yang diterima dari sensor berawalan tanda * dan diakhiri tanda #, contoh *1#
a = data.indexOf("*"); // a adalah index tanda *
b = data.indexOf("#"); // b adalah index tanda #
s_angin = data.substring(a + 1, b); // membuang tanda * dan # sehingga di dapat nilai dari arah angin
if (s_angin.equals("1")) { // jika nilai dari sensor 1 maka arah angin utara
arah_angin = "utara ";
}
if (s_angin.equals("2")) {
arah_angin = "timur laut";
}
if (s_angin.equals("3")) {
arah_angin = "timur ";
}
if (s_angin.equals("4")) {
arah_angin = "tenggara ";
}
if (s_angin.equals("5")) {
arah_angin = "selatan ";
}
if (s_angin.equals("6")) {
arah_angin = "barat daya";
}
if (s_angin.equals("7")) {
arah_angin = "barat ";
}
if (s_angin.equals("8")) {
arah_angin = "barat laut";
}
Serial.println(arah_angin);
}

if (flag == true) // don't really need the == true but makes intent clear for new users
{
if (long(micros() - last_micros) >= 5000)
{ // time to debounce measures
rpmcount++;
last_micros = micros();
}
flag = false; // reset flag
}
//Measure RPM
if ((millis() - timeold) >= timemeasure * 1000)
{
countThing++;
detachInterrupt(digitalPinToInterrupt(GPIO_pulse)); // Disable interrupt when calculating
rps = float(rpmcount) / float(timemeasure); // rotations per second
rpm = 60 * rps; // rotations per minute
omega = 2 * PI * rps; // rad/s
velocity_ms = omega * radius * calibration_value; // m/s
velocity_kmh = velocity_ms * 3.6; // km/h

if (countThing == 1) // Send data per 25 seconds
{
countThing = 0;
}
timeold = millis();
rpmcount = 0;
attachInterrupt(digitalPinToInterrupt(GPIO_pulse), rpm_anemometer, RISING); // enable interrupt
}

// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
Blynk.virtualWrite(V2, l);
Blynk.virtualWrite(V3, p);
Blynk.virtualWrite(V4, arah_angin);
Blynk.virtualWrite(V5, velocity_kmh);
Serial.print("Temperature : ");
Serial.print(t);
Serial.print(" Humidity : ");
Serial.println(h);
}

void setup(void)
{
Serial.begin(9600);
dataserial.begin(9600);
Wire.begin();
GY30.begin();
dht.begin();
Serial.println(F("BH1750 Test begin"));
Bot.getMe();
pinMode(GPIO_pulse, INPUT_PULLUP);
digitalWrite(GPIO_pulse, LOW);
detachInterrupt(digitalPinToInterrupt(GPIO_pulse)); // force to initiate Interrupt on zero
attachInterrupt(digitalPinToInterrupt(GPIO_pulse), rpm_anemometer, RISING); //Initialize the intterrupt pin
rpmcount = 0;
rpm = 0;
timeold = 0;
timeNow = 0;
Blynk.begin(auth, WIFI_SSID, WIFI_PASS);
timer.setInterval(100L, sendSensor);
Bot.set_debug(DEBUG_LEVEL_UTLGBOT);

wifi_init_stat();
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
Serial.println("Waiting for WiFi connection.");
while (!wifi_handle_connection())
{
Serial.print(".");
delay(500);
}
}

void loop()
{
if (!wifi_handle_connection())
{
// Wait 100ms and check again
delay(100);
return;
}

// Check for Bot received messages
while (Bot.getUpdates())
{
Serial.println("Received message:");
Serial.println(Bot.received_msg.text);
Serial.println(Bot.received_msg.chat.id);

if (strncmp(Bot.received_msg.text, "/start", strlen("/start")) == 0)
{
Bot.sendMessage(Bot.received_msg.chat.id, TEXT_START);
}

else if (strncmp(Bot.received_msg.text, "/info", strlen("/info")) == 0)
{
l = GY30.readLightLevel();
String msg = "Status Cahaya: " + String (l) + " lx \n";

t = dht.readTemperature();
msg += "Status Suhu: " + String (t) + " °C \n";

h = dht.readHumidity();
msg += "Status Kelembaban: " + String (h) + " %Rh \n";


msg += "Arah angin saat ini " + String (arah_angin) + " \n";

p = bmp.readPressure();
msg += "Status Tekanan: " + String (p) + " pa \n";

msg += "Velocity: " + String (velocity_ms) + " m/s \n";



msg.toCharArray(buff, 1000);
Bot.sendMessage(Bot.received_msg.chat.id, buff);
}

// Feed the Watchdog
yield();
}

if (t > 30 && state_t == 0) {
state_t = 1;
String msg = "Status Suhu :";
msg += t;
msg += " °C\n";
msg += "Hati Hati Panas.";

msg.toCharArray(buff, 100);
Bot.sendMessage("945680895", buff);
}
else if (t <= 30) {
state_t = 0;

}

if (h < 60 && state_h == 0) {
state_h = 1;
String msg = "Status Kelembaban :";
msg += h;
msg += " %Rh\n";
msg += "Hati Hati Kering.";

msg.toCharArray(buff, 100);
Bot.sendMessage("556538872", buff);
}
else if (h >= 60) {
state_h = 0;
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
delay(1000);
Blynk.run();
timer.run();
}

// Init WiFi interface
void wifi_init_stat(void)
{
Serial.println("Initializing TCP-IP adapter...");
Serial.print("Wifi connecting to SSID: ");
Serial.println(WIFI_SSID);

WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);

Serial.println("TCP-IP adapter successfuly initialized.");
}


bool wifi_handle_connection(void)
{
static bool wifi_connected = false;

// Device is not connected
if (WiFi.status() != WL_CONNECTED)
{
// Was connected
if (wifi_connected)
{
Serial.println("WiFi disconnected.");
wifi_connected = false;
}

return false;
}
// Device connected
else
{
// Wasn't connected
if (!wifi_connected)
{
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

wifi_connected = true;
}

return true;
}
}

Re: Weather Monitoring Station Using Telegram

PostPosted: Tue Jan 24, 2023 10:47 am
by rooppoorali
Which wind speed sensor are you using? Is it the Grove-anemometer?

Re: Weather Monitoring Station Using Telegram

PostPosted: Wed Jan 25, 2023 7:51 am
by rooppoorali
If you're facing issues with the anemometer, you can try this tutorial: https://www.pcbway.com/project/sharepro ... 20ba7.html
Though he is using Arduino Nano, the basic principle is the same for all boards.