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 @JorgeTrincado 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:

just copy:

https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[MY_CHANNEL_NAME]&text=[MY_MESSAGE_TEXT]

and modify it, where:

  • BOT_API_KEY is the API Key generated by BotFather when you created your bot
  • MY_CHANNEL_NAME is the handle of your channel (e.g. @my_channel_name)
  • MY_MESSAGE_TEXT is the message you want to send (URL-encoded)

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:

I tried the latest type of “Telegram send message” endpoints, but I didn’t get the notification like it should. can someone help me to solve this problem?

#include <ThingerESP8266.h>
#include <DHT.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#define USERNAME "xx"
#define DEVICE_ID "xx"
#define DEVICE_CREDENTIAL "xx"

#define SSID "Setsu"
#define SSID_PASSWORD "12345678"

static const int RXPin = 4, TXPin = 5;   // GPIO 4=D2(conneect Tx of GPS) and GPIO 5=D1(Connect Rx of GPS
static const uint32_t GPSBaud = 9600;

int vs= D0;
TinyGPSPlus gps; // The TinyGPS++ object

SoftwareSerial ss(RXPin, TXPin);

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

#define DHTPIN 0          // What digital pin we're connected to
#define DHTTYPE DHT11     // DHT 11
DHT dht(DHTPIN, DHTTYPE);
float t,h;

void setup() {
  //pinMode(LED_BUILTIN, OUTPUT);
   dht.begin();
   ss.begin(GPSBaud);
   pinMode(vs, INPUT);

  thing.add_wifi(SSID, SSID_PASSWORD);



  // digital pin control example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  //thing["led"] << digitalPin(LED_BUILTIN);

  // DHT11 Sensor
    //thing["Suhu"] >> outputValue(dht.readTemperature());
    //thing["Humd"] >> outputValue(dht.readHumidity());
    thing["DHT11"] >> [](pson& out){
      out["Suhu"] = dht.readTemperature();
      out["Humd"] = dht.readHumidity();
     };
    
  //vibrate sensor
    thing["getar"] >> outputValue(vibration());

}

unsigned long lastCheck = 0;
void loop() {

  while (ss.available() > 0) 
    {
      // sketch displays information every time a new sentence is correctly encoded.
      if (gps.encode(ss.read()))
        displayInfo();
  }
  
  thing.handle();
   unsigned long currentTs = millis();
    if(currentTs-lastCheck>=1*60*1000){
        lastCheck = currentTs;
        if(dht.readTemperature()>30){
            thing.call_endpoint("high_temp_telegram");
        }
    }
}

void displayInfo()
{ 

  if (gps.location.isValid() ) 
  {
    thing["location"] >> [](pson& out){
      out["lat"] = gps.location.lat();
      out["lon"] = gps.location.lng();
};
  }
  
  Serial.println();
}

long vibration(){
  long data=pulseIn(vs, HIGH);
  return data;
  
  }

Hmm there seems to be some bug in this endpoint, we checked it out this week. At the moment you can use the full api as explained above

best