Source:http://telegramiotbot.com/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const byte interruptPin = 2;
const char[] SSID="YOUR WIFI SSID";
const char[] PASSWORD="YOUR WIFI PASSWORD";
const char[] BASEURI="http://telegramiotbot.com/api/notify?token=";
const char[] TOKEN_NOTIFY="YOUT TOKEN";
bool interrupted=false;
/*
interrupt handler
*/
void inputChanged(){
interrupted=true;
}
void notify(const char* token){
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin(BASEURI+token);
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
if(httpCode) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
} else {
Serial.print("[HTTP] GET... failed, no connection or no HTTP server\n");
}
}
bool connect(){
WiFi.begin(SSID,PASSWORD);
Serial.print("[Connecting]");
int tries=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 30){
Serial.println();
Serial.print("[connecting failed]");
Serial.println();
return false;
}
}
Serial.print("You're connected to the network");
Serial.println();
return true;
}
void setup() {
Serial.begin(115200);
conect();
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), inputChanged, FALLING);
}
void loop() {
if(interrupted){
if((WiFi.status() == WL_CONNECTED)) {
notify(TOKEN_NOTIFY);
interrupted=false;
}
}
}