Hallo Zusammen,
habe hier im Forum Mals gesucht aber nicht viel zu dem Thema Telegram gefunden. (Telegram liefert eine API). Hoffe es kennt sich trotzdem jemand aus ;-).
Jetzt zu meinem Problem:
wenn ich einen Taster betätige soll ein Interrupt ausgelöst werden und anschließend will ich dann eine Nachricht von meinem Telegram Bot an eine Gruppe schicken.
Habe das auch alles geschafft. Also sobald ich den Taster drücke bekomme ich eine Nachricht.
ABER: Das ganze funktioniert 2 mal. Danach werden mir keine Nachrichten mehr geschickt. -> Interrupt wird aber noch ausgelöst. Das problem liegt also am verschicken der Nachricht. Wenn ich den ESP resette, funktioniert es wieder 2 mal...
Hier der Code:
#include <ssl_client.h>
#include <WiFiClientSecure.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <UniversalTelegramBot.h>
//---------- WiFi Settings ----------
const char *ssid = ""; // network SSID
const char *password = ""; // network key
//---------- Telegram config ----------
#define botToken "" // your Bot Token (Get from Botfather)
#define chatID "" // Chat ID of where you want the message to go (You can use MyIdBot to get the chat ID)
//---------- SSL Client ----------
WiFiClientSecure client;
//---------- TelegramBot ----------
UniversalTelegramBot bot(botToken, client);
volatile bool ButtonPressedFlag = false; //volatile bool ButtonPressedFlag = false;
volatile unsigned long alteZeit=0, entprellZeit=20; // Zum Taster Entprellen
String ipAddress = "";
void setup()
{
Serial.begin(115200);
//---------- Initialze the buttons ----------
mDisplay.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
mDisplay.display();
delay(2000);
mDisplay.clearDisplay();
mDisplay.setTextSize(1);
mDisplay.setTextColor(WHITE);
mDisplay.setCursor(0,0);
//---------- Initialze the buttons ----------
pinMode(32, INPUT_PULLUP); // Pin als Input
attachInterrupt(digitalPinToInterrupt(32), ButtonPressed, FALLING);
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// Attempt to connect to Wifi network:
Serial.print("Connecting to WiFi: ");
mDisplay.println("Connecting to WiFi: ");
Serial.println(ssid);
mDisplay.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
mDisplay.println("WiFi connected");
Serial.println("IP address: ");
mDisplay.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
mDisplay.println(ip);
mDisplay.display();
ipAddress = ip.toString();
}
//---------- ISR ----------
void ButtonPressed()
{
if((millis() - alteZeit) > entprellZeit) // Taster Entprellen
{
ButtonPressedFlag = true;
alteZeit = millis(); // letzte Schaltzeit merken
}
}
void sendTelegramMessage()
{
Serial.println("sending...");
if(bot.sendMessage(chatID, "ALARM"))
{
Serial.println("TELEGRAM Successfully sent");
}
else
{
Serial.println("FAILD");
}
ButtonPressedFlag = false;
}
void loop()
{
if (ButtonPressedFlag)
{
sendTelegramMessage();
}
}