Send notification with telegram

hi,.so my project create temperature monitoring system using nodeMcu and DHT11 sensor,.and add telegram notification if temperature above normal,
the proble is,the code not work along between thinger code and telegram send message code…
if i disable thinger source code (thing.handle() function.),program successfully send message to telegram,especially
below my source code

#include "CTBot.h"
#include "DHT.h"
#include <ThingerESP8266.h>

#define DHTPIN D7
#define DHTTYPE DHT11
#define USERNAME "xx" //Username thinger.io
#define DEVICE_ID "xx" 
#define DEVICE_CREDENTIAL "xx
#define SSID "xx" 
#define SSID_PASSWORD "xx"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

DHT dht(DHTPIN, DHTTYPE);
CTBot myBot;
String ssid = "xx";
String pass = "xx";
String token = "xx";
const int id =xx;
int counter,i;
float hum,temp;

void setup() 
{
  Serial.begin(9600);
  Serial.println("Starting TelegramBot...");
  myBot.wifiConnect(ssid, pass);
  myBot.setTelegramToken(token);

  if (myBot.testConnection()) 
  {
    Serial.println("wifi terhubung");
  } else 
  {
    Serial.println("wifi tidak terhubung");
  }

  myBot.sendMessage(id, "test message");
  Serial.println("message sent);

  dht.begin();
  delay(5000);
  
  thing.add_wifi(SSID, SSID_PASSWORD);
  thing["dht11"] >> [](pson& out)
  {
    out["humidity"] = hum;
    out["celsius"] = temp;
  };
}


void loop() 
{   
   thing.handle();    

  float h = dht.readHumidity();
  float t = dht.readTemperature(); 

  hum = h;
  temp = t;
  
  delay(5000);
  Serial.print("temperature : ");
  Serial.print(t);
  Serial.println(" *C");
  delay(3000);
  
     if(t>24)
     {
      myBot.sendMessage(id, "check your sever");
     }
   


}

Hello @Prema_Pradana, this is a really interesting project, congratulations!

Let’s see if we can help you make it works, I have two suggestions:

  1. delete delay instructions from the loop routine, this will make your device lose the connection with the router, so thing. handle routine will need to reconnect every time. You can implement the same logic by using something like:
if(millis()%(5000)==0){
  Serial.print("temperature : ");
  Serial.print(t);
  Serial.println(" *C");
}
  1. try to stop thinger communication before calling myBot.sendMessage() instruction using WiFi.disconnect(); and also would be interesting to include a mechanism to prevent the device to call this instruction ALWAYS the temperature is over the threshold, for example:
 if(t>24 && called==0){
     WiFi.disconnect();`
     myBot.sendMessage(id, "check your sever");
     called==1;
 }if(t<=24) called=0;

Hope it helps!!

Very cool project!

I guess it could be something with the connection, you declare two wifi setups (one for telegram, one for thinger), but of course is just one.

Reading @jtrinc26 response, it could work, if the instruction myBot.sendMessage(id, "check your sever"); tries to connect wifi with telegram established wifi credentials.

The only doubt I have is if after sending the telegram message, it could run the thinger process, because the wifi interface will be active with the telegram client, I think it is needed an instruccion to disable this client and left the wifi adapter available again for the thinger process.

Of course it would be really good just the need of one wifi setup and both process run over the same wifi connection, don’t know if this could be done easily, I’ve done something like this, running two different clouds clients at the same micrcocontroller by the same wifi connection, but just left the thinger’s wifi setup and modified the other client to run over this connection, nothing hard, just verify it is connected and send data, the thinger client was on charge to warrantee there is a connection always.

Hope this helps

Hi,many thanks for your reply and suggestion,.
i have try your suggestion,especially for insert WiFi.disconnect();`before sending telegram message. result is :

  1. on thinger.io device,say connected
  2. data not show on thinger.io dashboard & data bucket
  3. telegram notification successfully sent

i think like @ega mentioned,the wifi interface still connected on telegram client,.

hi @ega thank you for your reply,do you have any idea to release the wifi interface ?or how to use just one wifi connection for both telegram and thinger?

Hello @Prema_Pradana,

If you only want to send a message, I think that will be better to use an Endpoint integration. I have just tested telegram API and works well:

you just have to introduce your token and your chat id

Hope it helps

Working with the endpoint is a better solution than implement the client at the microcontroller.

But in case you still want to apply the microcontroller’s client, what I would do is to modify the telegram’s library to avoid the attempt connection to wifi and just verify that it is connected to it (this will be after the thinger process runs, of course), so it will check that it is connected and should send the message with no issue, and everything with the thinger’s client wifi connection.

i didn’t ever know there is such a feature like this,.and yeeaayyy finally now i can show data on thinger dasboard and send telegram notification…many thanks for your support…
and antoher thing,.
how to make the notification text that will be sent via telegram dinamically get data from the sensor…
for example &text=your server temperature is : [temp_value].
i hope you understand what i mean…:smile::smile: