c_cpp esp8266_telegram.cpp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp esp8266_telegram.cpp相关的知识,希望对你有一定的参考价值。
#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();
}
}
以上是关于c_cpp esp8266_telegram.cpp的主要内容,如果未能解决你的问题,请参考以下文章