shurillu / CTBot

A simple (and easy to use) Arduino Telegram BOT Library for ESP8266/ESP32
MIT License
147 stars 34 forks source link

sendMessage not work #109

Closed sigurroses closed 4 months ago

sigurroses commented 1 year ago

Hello, currently I'm doing a project that uses the Nodemcu esp8266 Amica board microcontroller and a magnetic door sensor. The bot will send me a message if the sensor value where the door is open is 0.

I've tried a variety of myBot.sendMessage-related lines, but nothing works. However, my bot responds the message when I use Echobot to try to send it a message. after adding the sensor value statement below echobot line, but still, the function never send a message when the value sensor is 0 .

Here is the code :

#include"CTBot.h"
#define pinreed 5
CTBot myBot;

String ssid  = "xxxxx";
String pass  = "xxxxxx";
String token = "xxxxxxxxxxxxxxxxxxxxxxxx";
const int chat_id = xxxxxxxxxxxx;
int reed = digitalRead(pinreed);

void setup() {
// initialize the Serial
    Serial.begin(9600);
        pinMode(pinreed, OUTPUT);
    Serial.println("Starting TelegramBot...");

    // connect the ESP8266 to the desired access point
    myBot.wifiConnect(ssid, pass);

    // set the telegram bot token
    myBot.setTelegramToken(token);

    // check if all things are ok
    if (myBot.testConnection())
        Serial.println("\ntestConnection OK");
    else
        Serial.println("\ntestConnection NOK");
}

void loop() {
 TBMessage msg; // a variable to store telegram message data
    // if there is an incoming message...
    if (myBot.getNewMessage(msg)) {
        // ...forward it to the sender
        myBot.sendMessage(msg.sender.id, msg.text);
         }
    // wait 500 milliseconds
    delay(500);

  if (reed == 0)
  {
    myBot.sendMessage(chat_id, "DOOR OPEN");
  }
  else
  {
    myBot.sendMessage(chat_id, "DOOR CLOSED");
  }
}
G-Remo commented 1 year ago

Hello, If you read a data try INPUT in pinMode then try to ad the read function in loop ++ for Yes / No result try bool ++ use delay or equivalent in loop to prevent flood response

shurillu commented 1 year ago

Hello, sigurroses, first, the chat_id should be an INT 64 as Telegram says here. Try using directly your chat ID in the sendMessageand if it works, modify the const int chat_id = xxxxxxxxxxxx; in const int64_t chat_id = xxxxxxxxxxxx; Second, you should send a message only when the door sensor change his status, i.e. when it pass from closed to open and vice versa. In the snippet that you posted here, you "flood" your chat with messages because the code sends messages at every cycle. Try something like that:

#include"CTBot.h"
#define pinreed 5
CTBot myBot;

String ssid  = "xxxxx";
String pass  = "xxxxxx";
String token = "xxxxxxxxxxxxxxxxxxxxxxxx";
const int64_t chat_id = xxxxxxxxxxxx;
int reed;
int prev_reed;

void setup() {
// initialize the Serial
    Serial.begin(9600);
        pinMode(pinreed, INPUT); // you should read something form here, so the pin is INPUT
    Serial.println("Starting TelegramBot...");

    // connect the ESP8266 to the desired access point
    myBot.wifiConnect(ssid, pass);

    // set the telegram bot token
    myBot.setTelegramToken(token);

    // check if all things are ok
    if (myBot.testConnection())
        Serial.println("\ntestConnection OK");
    else
        Serial.println("\ntestConnection NOK");

   // read the actual state of the reed sensor
   reed = digitalRead(pinreed);
   prev_reed = reed;
}

void loop() {
 TBMessage msg; // a variable to store telegram message data
    // if there is an incoming message...
    if (myBot.getNewMessage(msg)) {
        // ...forward it to the sender
        myBot.sendMessage(msg.sender.id, msg.text);
         }
    // wait 500 milliseconds
    delay(500);

  reed = digitalRead(pinReed); // you have to update the state of the reed sensor at every cycle
  if (reed!=prev_reed) // only if there is a status change you have to send a message
  {
    if (reed == 0)
       myBot.sendMessage(chat_id, "DOOR OPEN");
   else
      myBot.sendMessage(chat_id, "DOOR CLOSED");
   prev_reed = reed; // now you have to update the last status of the reed sensor
  }
}

In these days I can't check that code, but it should work. Try it and let me know if it solve your issue.

Cheers

Stefano

sigurroses commented 1 year ago

hi yall, sorry for the late reply, and thanks for replying to my issue. i just tried the code that you give me, the result is when the code is executed, the bot sends a message right away. However, when I move the sensor to test it, the bot transmits messages so slowly. I moved the sensor once again, but the bot made no attempt to send me a message.

so i reset the board, after connecting to the network the bot sends no message at all. i thought that the responses of the bot was not consistent at all because the outcomes varied from attempt to attempt.

is it possible the library version and the board manager version is the cause? currently I'm using board : manager esp8266 version 3.1.1 arduinojson version 6.19 ctbot 2.1.9

shurillu commented 1 year ago

Hello sigurroses, I've not currently tested the ESP8266 v 3.1.1 (released just yesterday!) but I saw that it have minor updates. The sketch that I posted before is just an example: I don't know how your sensor works and I do not have had the possibility to test it. Speaking on the speed: keep on mind that bots are intended for a non realtime environment. In other words, it is possible to wait seconds between the event (opening/closing the sensor) and the notification message. Anyway, my suggestion is: try writing a sketch that works only with the sensor (i.e. a sketch that write a message on the serial when the sensor is opened/closed). Once this sketch works correctly, add the telegram functionalities. Cheers,

Stefano