I want to create a simple alert from several sensors, integrated with Telegram and Thingspeak. So, this code is handling 3 functions that run simultaneously, that's why I use millis(). Based on the https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158/2 I made a simple code to handle timing loops without adding any action but printing on serial monitor and it can work properly. here is the simple code.
The problem is when I add some code to the timing loops to read the sensor, handle telegram messages, and send data to Thingspeak. it has a delay that's not what I intended to do and didn't occur on the sample code above.
here is my code :
const unsigned long interval = 1000;
const unsigned long readinterval = 1000;
const unsigned long sendinterval = 20000;
unsigned long currentMillis = 0;
unsigned long SensorReadMillis = 0;
unsigned long last_call = 0;
unsigned long last_sendmillis = 0;
WiFiSSLClient securedClient;
WiFiClient client;
UniversalTelegramBot bot(BOT_API_TOKEN, securedClient);
void handleMessages(int num_new_messages) {
for (int i = 0; i < num_new_messages; i++) {
message_id = bot.messages[i].message_id;
chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
from_name = bot.messages[i].from_name;
if (from_name == "") {
from_name = "Unknown";
}
if (text == "/start") {
bot.sendMessage(chat_id, "Haloo " + (from_name == "Unknown" ? "" : from_name) + ", Welcome", "");
} else if (text == "/status"){
togglestatus();
}
Serial.println(from_name);
Serial.println(chat_id);
Serial.println(text);
}
}
void setup() {
Serial.begin(115200);
pinMode(buzzer, OUTPUT);
lcd1.init();
lcd2.init();
lcd1.backlight();
lcd2.backlight();
ThingSpeak.begin(client);
initflame();
initdht11();
initmq2();
connectToWiFi();
Serial.print("WiFi connected. IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
currentMillis = millis();
readmessage();
deteksi();
lcdscreen();
senddata();
}
void readmessage(){
if (currentMillis - last_call > interval) {
int num_new_messages = bot.getUpdates(bot.last_message_received + 1);
while (num_new_messages) {
Serial.println("Message received");
handleMessages(num_new_messages);
num_new_messages = bot.getUpdates(bot.last_message_received + 1);
}
last_call = millis();
}
}
void deteksi(){
flamedetected=readFLAME();
smokedetected=readSMOKE();
if (currentMillis - SensorReadMillis >= readinterval) {
if (flamedetected==LOW){
bot.sendMessage(chat_id, "Flame detected!!", "");
Serial.println("flame");
tone(buzzer, 300, 3000);
} else if (smokedetected==HIGH){
bot.sendMessage(chat_id, "Smoke detected!!", "");
Serial.println("Smoke");
tone(buzzer, 200, 3000);
} else if (suhucelsius() > batassuhu){
bot.sendMessage(chat_id, "Suhu melebihi batas!", "");
}
else {
noTone(buzzer);
}
SensorReadMillis = millis();
}
}
void senddata(){
if (currentMillis - last_sendmillis >= sendinterval){
ThingSpeak.setField(1, suhucelsius());
ThingSpeak.setField(2, kelembapan());
ThingSpeak.setField(3, mq2value());
ThingSpeak.setField(4, readFLAME());
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println(x);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
last_sendmillis = millis();
}
}
My expected timing in each function is 1 sec for handling telegram, 1 sec for reading sensor, and 20 sec for sending data to thingspeak. this current code has an unexpected delay around 20 sec for readingsensor. is there any mistake on my code?
thanks in advance for your help.



