Skip to content

Instantly share code, notes, and snippets.

@myyc
Last active January 20, 2021 09:39
Show Gist options
  • Save myyc/81d4fa3d6e25065454b52e09bec6542b to your computer and use it in GitHub Desktop.
Save myyc/81d4fa3d6e25065454b52e09bec6542b to your computer and use it in GitHub Desktop.
esp32_telegram_hi
/*
* "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