Last active
January 20, 2021 09:39
-
-
Save myyc/81d4fa3d6e25065454b52e09bec6542b to your computer and use it in GitHub Desktop.
esp32_telegram_hi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* "params.h" | |
* const char* ssid = "your SSID"; | |
* const char* password = "wpa password"; | |
* const String token = "telegram_bot_token"; | |
* const String to_id = "numeric telegram id of the recipient"; | |
*/ | |
#include "params.h" | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
String sendMessage(String msg) { | |
HTTPClient http; | |
String url = "https://api.telegram.org/bot" + token + "/sendMessage?chat_id=" + to_id + "&parse_mode=Markdown&text=" + msg; | |
http.begin(url.c_str()); | |
int httpResponseCode = http.GET(); | |
String payload; | |
if (httpResponseCode>0) { | |
payload = http.getString(); | |
Serial.println(payload); | |
} | |
else { | |
String payload = "{\"ok\":false,\"result\":{},\"error\":\"Error " + String(httpResponseCode) + "\"}"; | |
} | |
http.end(); | |
return payload; | |
} | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
Serial.println("Connecting"); | |
while(WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to '" + String(ssid) + "' with IP address "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
if(WiFi.status() == WL_CONNECTED) { | |
sendMessage("lols"); | |
delay(10000); | |
} | |
else { | |
Serial.println("No WiFi connection available"); | |
delay(2000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment