Last active
January 11, 2021 19:31
-
-
Save abonec/a5052e15edd4a1f5fa54a60aa245fdb9 to your computer and use it in GitHub Desktop.
This file contains 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
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <UniversalTelegramBot.h> | |
// Initialize Wifi connection to the router | |
char ssid[] = "xxx"; // your network SSID (name) | |
char password[] = "xxx"; // your network key | |
// Initialize Telegram BOT | |
#define BOTtoken "xxx" // your Bot Token (Get from Botfather) | |
#define GROUIP "xxx" | |
WiFiClientSecure client; | |
UniversalTelegramBot bot(BOTtoken, client); | |
int Bot_mtbs = 1000; //mean time between scan messages | |
long Bot_lasttime; //last time messages' scan has been done | |
bool Start = false; | |
const int ledPin = 2; | |
int ledStatus = 0; | |
void handleNewMessages(int numNewMessages) | |
{ | |
Serial.println("handleNewMessages"); | |
Serial.println(String(numNewMessages)); | |
for (int i = 0; i < numNewMessages; i++) | |
{ | |
String chat_id = String(bot.messages[i].chat_id); | |
String text = bot.messages[i].text; | |
String from_name = bot.messages[i].from_name; | |
if (from_name == "") | |
from_name = "Guest"; | |
if (text == "/test") | |
{ | |
for (int i = 0; i++; i <= 18) | |
{ | |
pinMode(i, OUTPUT); | |
digitalWrite(i, HIGH); | |
delay(1000); | |
digitalWrite(i, LOW); | |
char buf[5]; | |
sprintf(buf, "test #%d", i); | |
bot.sendMessage(chat_id, buf, ""); | |
} | |
} | |
if (text == "/turn1") | |
{ | |
digitalWrite(1, LOW); | |
delay(1000); | |
digitalWrite(1, HIGH); | |
bot.sendMessage(chat_id, "tested 1", ""); | |
} | |
if (text == "/turn0") | |
{ | |
digitalWrite(0, LOW); | |
delay(1000); | |
digitalWrite(0, HIGH); | |
bot.sendMessage(chat_id, "tested 1", ""); | |
} | |
if (text == "/turn2") | |
{ | |
digitalWrite(2, LOW); | |
delay(1000); | |
digitalWrite(2, HIGH); | |
bot.sendMessage(chat_id, "tested 2", ""); | |
} | |
if (text == "/turn3") | |
{ | |
digitalWrite(3, LOW); | |
delay(1000); | |
digitalWrite(3, HIGH); | |
bot.sendMessage(chat_id, "tested 3", ""); | |
} | |
if (text == "/turn4") | |
{ | |
digitalWrite(4, LOW); | |
delay(1000); | |
digitalWrite(4, HIGH); | |
bot.sendMessage(chat_id, "tested 4", ""); | |
} | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
// 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 Wifi: "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
pinMode(0, OUTPUT); // initialize digital ledPin as an output. | |
pinMode(1, OUTPUT); // initialize digital ledPin as an output. | |
pinMode(2, OUTPUT); // initialize digital ledPin as an output. | |
pinMode(3, OUTPUT); // initialize digital ledPin as an output. | |
pinMode(4, OUTPUT); // initialize digital ledPin as an output. | |
delay(10); | |
// digitalWrite(ledPin, LOW); // initialize pin as off | |
// for (int i = 1; i++; i < 18) | |
// { | |
// pinMode(i, OUTPUT); | |
// } | |
bot.sendMessage(GROUIP, "CONNECTED", ""); | |
} | |
void loop() | |
{ | |
if (millis() > Bot_lasttime + Bot_mtbs) | |
{ | |
int numNewMessages = bot.getUpdates(bot.last_message_received + 1); | |
while (numNewMessages) | |
{ | |
Serial.println("got response"); | |
handleNewMessages(numNewMessages); | |
numNewMessages = bot.getUpdates(bot.last_message_received + 1); | |
} | |
Bot_lasttime = millis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment