In this code, the temperature and humidity values are sent to the telegram bot. I want it to send a message to enter a number when the /Limit option is selected?
There are two problems here:
- It only asks for a value when /limit is selected and reads it
- It will query whether the entered value is integer and warn if this value is not an integer or not in the desired range(10-30).
same topic is here, but no solution
https://forum.arduino.cc/t/how-to-get-value-from-telegram-messagge-to-arduino-program/1012589
It works this way too, but not as I like.
Thank you
//My project is on wokwi
https://wokwi.com/projects/343810495276384852
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
//#include "DHT.h"
#include<DHTesp.h>
#define DHTPIN 15
DHTesp dht;
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Wifi network station credentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "5711693392:AAEUASm_bSmfnqUdoef3hN8ZjUnp4dWHV6A"
#define CHAT_ID "542689770"
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
unsigned long bot_lasttime; // last time messages' scan has been done
unsigned long temptime;
float temperature;
float limit;
float humidity;
void setup()
{
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.setup(DHTPIN, DHTesp::DHT22);
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
TempAndHumidity data;
if(millis() - temptime > BOT_MTBS)
{
data = dht.getTempAndHumidity();
temperature = data.temperature;
humidity = data.humidity;
}
if (millis() - bot_lasttime > BOT_MTBS)
{
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages)
{
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
}
void handleNewMessages(int numNewMessages)
{
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID )
{
bot.sendMessage(chat_id, "Unauthorized user", "");
}
else
{
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
if (text == "/tempC")
{ String msg = "Temperature is ";
msg += msg.concat(temperature);
msg += "C";
bot.sendMessage(chat_id,msg, "");
}
if (text == "/limit")
{
String msg = "Set Temperature limit : \n";
// msg += "/10 /13 /16 /18 /19 /20 /21 /22 /23 /24 /25 /26 /28 /30";
msg +="between 10 and 30" ;
bot.sendMessage(chat_id,msg, "");
String tt = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
Serial.println(tt);
}
if (text == "/humidity")
{
String msg = "Humidity is ";
msg += msg.concat(humidity);
msg += "%";
bot.sendMessage(chat_id,msg, "");
}
if (text == "/start")
{
Serial.println(temperature);
Serial.println(i);
String welcome = "DHTxx sensor readings.\n";
welcome += "/tempC : Temperature in celcius \n";
welcome += "/limit : Set Temperature limit is\n";
welcome += "/humidity : Humidity\n";
bot.sendMessage(chat_id, welcome, "");
}
Serial.println(text.toInt());
}
}
}