ESP-BOT
/
esp8266.ino
342 строки · 13.8 Кб
1#include <WiFiClient.h>2#include <ArduinoJson.h>3#define FB_WITH_LOCATION4#include <datatypes.h>5#include <utils.h>6#include <string>7#include <FastBot.h>8#include <ESP8266HTTPClient.h>9#include <ESP8266WiFi.h>10#define WIFI_SSID "TP-Link_AC5C"11#define WIFI_PASS "99931728"12#define BOT_TOKEN "6435083940:AAGMSQN1fn1891vLicfYMAGbLk0cDCq_LjM"13FastBot bot(BOT_TOKEN);14//переменные
15String API = "8b92943f5d8168a3668a52206d787396";16String regionID = "20174";17String serverPath = "https://yandex.com/time/sync.json?geo=" + regionID;18String monday[8] = { "0.Разговор о важном", "1.Алгебра", "2.Англ.яз", "3.ТВИС", "4.Алгебра", "5.История", "6.Литература", "7.Физика" };19String tuesday[8] = { "0", "1.Геометрия", "2.Физ-ра", "3.Русский яз.", "4.Общество", "5.Физика", "6.Биология", "7.Индивидуальный проект" };20String wednesday[8] = { "0", "1", "2.Литература", "3.Англ.яз", "4.Геометрия", "5.Химия", "6.Физика", "7.История" };21String thursday[8] = { "0.Профмин", "1.Информатика", "2.Русский.яз", "3.ОБЖ", "4.Алгебра", "5.Алгебра", "6.Физика", "7.Общество" };22String friday[8] = { "0", "1.Англ.яз", "2.Литература", "3.География", "4.Физика", "5.Геометрия", "6.Физ-ра", "7.КНКБР" };23String call[8] = { "0.08:00-08:20", "1.08:30-09:10", "2.09:20-10:00", "3.10:15-10:55", "4.11:10-11:50", "5.12:05-12:45", "6.13:00-13:40", "7.13:55-14:35" };24StaticJsonDocument<1000> wea;25WiFiClient client;26HTTPClient http;27
28
29void setup() {30connectWiFi();31bot.attach(new2Msg);32}
33
34
35void new2Msg(FB_msg& msg) {36if (msg.OTA && msg.chatID == "718856532") bot.update();37else if (msg.text == "/start") {38bot.sendMessage("Привет, это бот созданный для физмата 10А класса", msg.chatID);39} else if (msg.text == "/lesson0schedule") {40FB_Time t(msg.unix, 3); // передали unix и часовой пояс41uint8_t day = t.dayWeek;42if (day == 1) {43int i;44for (i = 0; i < 8; i = i + 1) {45bot.sendMessage(monday[i], msg.chatID);46}47} else if (day == 2) {48int i;49for (i = 0; i < 8; i = i + 1) {50bot.sendMessage(tuesday[i], msg.chatID);51}52} else if (day == 3) {53int i;54for (i = 0; i < 8; i = i + 1) {55bot.sendMessage(wednesday[i], msg.chatID);56}57} else if (day == 4) {58int i;59for (i = 0; i < 8; i = i + 1) {60bot.sendMessage(thursday[i], msg.chatID);61}62} else if (day == 5) {63int i;64for (i = 0; i < 8; i = i + 1) {65bot.sendMessage(friday[i], msg.chatID);66}67} else if (day > 5) {68bot.sendMessage("Уроков нет", msg.chatID);69}70} else if (msg.text == "/call0schedule") {71int i;72for (i = 0; i < 8; i = i + 1) {73bot.sendMessage(call[i], msg.chatID);74}75} else if (msg.text == "/next0lesson") {76FB_Time t(msg.unix, 3);77String d[8];78if (t.dayWeek == 1) {79for (int i = 0; i < 9; i++) {80d[i] = monday[i];81}82} else if (t.dayWeek == 2) {83for (int i = 0; i < 9; i++) {84d[i] = tuesday[i];85}86} else if (t.dayWeek == 3) {87for (int i = 0; i < 9; i++) {88d[i] = wednesday[i];89}90} else if (t.dayWeek == 4) {91for (int i = 0; i < 9; i++) {92d[i] = thursday[i];93}94} else if (t.dayWeek == 5) {95for (int i = 0; i < 9; i++) {96d[i] = friday[i];97}98}99if (t.hour == 7) bot.sendMessage(d[0], msg.chatID);100else if (t.hour == 8 && t.minute < 30) bot.sendMessage(d[1], msg.chatID);101else if ((t.hour == 8 && t.minute >= 30) or (t.hour == 9 && t.minute < 20)) bot.sendMessage(d[2], msg.chatID);102else if ((t.hour == 9 && t.minute >= 20) or (t.hour == 10 && t.minute < 15)) bot.sendMessage(d[3], msg.chatID);103else if ((t.hour == 10 && t.minute >= 15) or (t.hour == 11 && t.minute < 10)) bot.sendMessage(d[4], msg.chatID);104else if ((t.hour == 11 && t.minute >= 10) or (t.hour == 12 && t.minute < 5)) bot.sendMessage(d[5], msg.chatID);105else if ((t.hour == 12 && t.minute >= 5) or (t.hour == 13 && t.minute == 0)) bot.sendMessage(d[6], msg.chatID);106else if ((t.hour == 13 && t.minute >= 0) and (t.hour == 13 && t.minute < 55)) bot.sendMessage(d[7], msg.chatID);107else if ((t.hour == 13 && t.minute >= 55) or (t.hour == 14 && t.minute < 35)) bot.sendMessage("Домой", msg.chatID);108else bot.sendMessage("Уроков нет", msg.chatID);109} else if (msg.text == "/how0long0until0the0bell") {110FB_Time t(msg.unix, 3);111if (t.hour == 7) {112byte hour, minute, minute1, hour1;113String ost, ost1;114hour = 8 * 60 + 0 - t.hour * 60 - t.minute;115hour1 = 8 * 60 + 0 - t.hour * 60 - t.minute - 15;116minute = hour % 60;117minute1 = hour1 % 60;118hour = hour / 60;119hour1 = hour1 / 60;120ost += hour;121ost += "ч";122ost += minute;123ost += "м";124ost1 += hour1;125ost1 += "ч";126ost1 += minute1;127ost1 += "м";128bot.sendMessage(("До следующего урока: " + ost), msg.chatID);129bot.sendMessage(("До конца урока: " + ost1), msg.chatID);130} else if (t.hour == 8 && t.minute < 30) {131byte hour, minute, minute1, hour1;132String ost, ost1;133hour = 8 * 60 + 30 - t.hour * 60 - t.minute;134hour1 = 8 * 60 + 30 - t.hour * 60 - t.minute - 15;135minute = hour % 60;136minute1 = hour1 % 60;137hour = hour / 60;138hour1 = hour1 / 60;139ost += hour;140ost += "ч";141ost += minute;142ost += "м";143ost1 += hour1;144ost1 += "ч";145ost1 += minute1;146ost1 += "м";147bot.sendMessage(("До следующего урока: " + ost), msg.chatID);148bot.sendMessage(("До конца урока: " + ost1), msg.chatID);149} else if ((t.hour == 8 && t.minute >= 30) or (t.hour == 9 && t.minute < 20)) {150byte hour, minute, minute1, hour1;151String ost, ost1;152hour = 9 * 60 + 20 - t.hour * 60 - t.minute;153hour1 = 9 * 60 + 20 - t.hour * 60 - t.minute - 15;154minute = hour % 60;155minute1 = hour1 % 60;156hour = hour / 60;157hour1 = hour1 / 60;158ost += hour;159ost += "ч";160ost += minute;161ost += "м";162ost1 += hour1;163ost1 += "ч";164ost1 += minute1;165ost1 += "м";166bot.sendMessage(("До следующего урока: " + ost), msg.chatID);167bot.sendMessage(("До конца урока: " + ost1), msg.chatID);168} else if ((t.hour == 9 && t.minute >= 20) or (t.hour == 10 && t.minute < 15)) {169byte hour, minute, minute1, hour1;170String ost, ost1;171hour = 10 * 60 + 15 - t.hour * 60 - t.minute;172hour1 = 10 * 60 + 15 - t.hour * 60 - t.minute - 15;173minute = hour % 60;174minute1 = hour1 % 60;175hour = hour / 60;176hour1 = hour1 / 60;177ost += hour;178ost += "ч";179ost += minute;180ost += "м";181ost1 += hour1;182ost1 += "ч";183ost1 += minute1;184ost1 += "м";185bot.sendMessage(("До следующего урока: " + ost), msg.chatID);186bot.sendMessage(("До конца урока: " + ost1), msg.chatID);187} else if ((t.hour == 10 && t.minute >= 15) or (t.hour == 11 && t.minute < 10)) {188byte hour, minute, minute1, hour1;189String ost, ost1;190hour = 11 * 60 + 10 - t.hour * 60 - t.minute;191hour1 = 11 * 60 + 10 - t.hour * 60 - t.minute - 15;192minute = hour % 60;193minute1 = hour1 % 60;194hour = hour / 60;195hour1 = hour1 / 60;196ost += hour;197ost += "ч";198ost += minute;199ost += "м";200ost1 += hour1;201ost1 += "ч";202ost1 += minute1;203ost1 += "м";204bot.sendMessage(("До следующего урока: " + ost), msg.chatID);205bot.sendMessage(("До конца урока: " + ost1), msg.chatID);206} else if ((t.hour == 11 && t.minute >= 10) or (t.hour == 12 && t.minute < 5)) {207byte hour, minute, minute1, hour1;208String ost, ost1;209hour = 12 * 60 + 5 - t.hour * 60 - t.minute;210hour1 = 12 * 60 + 5 - t.hour * 60 - t.minute - 15;211minute = hour % 60;212minute1 = hour1 % 60;213hour = hour / 60;214hour1 = hour1 / 60;215ost += hour;216ost += "ч";217ost += minute;218ost += "м";219ost1 += hour1;220ost1 += "ч";221ost1 += minute1;222ost1 += "м";223bot.sendMessage(("До следующего урока: " + ost), msg.chatID);224bot.sendMessage(("До конца урока: " + ost1), msg.chatID);225} else if ((t.hour == 12 && t.minute >= 5) or (t.hour == 13 && t.minute == 0)) {226byte hour, minute, minute1, hour1;227String ost, ost1;228hour = 13 * 60 - t.hour * 60 - t.minute;229hour1 = 13 * 60 - t.hour * 60 - t.minute - 15;230minute = hour % 60;231minute1 = hour1 % 60;232hour = hour / 60;233hour1 = hour1 / 60;234ost += hour;235ost += "ч";236ost += minute;237ost += "м";238ost1 += hour1;239ost1 += "ч";240ost1 += minute1;241ost1 += "м";242bot.sendMessage(("До следующего урока: " + ost), msg.chatID);243bot.sendMessage(("До конца урока: " + ost1), msg.chatID);244} else if ((t.hour == 13 && t.minute >= 0) or (t.hour == 13 && t.minute < 55)) {245byte hour, minute, minute1, hour1;246String ost, ost1;247hour = 13 * 60 + 55 - t.hour * 60 - t.minute;248hour1 = 13 * 60 + 55 - t.hour * 60 - t.minute - 15;249minute = hour % 60;250minute1 = hour1 % 60;251hour = hour / 60;252hour1 = hour1 / 60;253ost += hour;254ost += "ч";255ost += minute;256ost += "м";257ost1 += hour1;258ost1 += "ч";259ost1 += minute1;260ost1 += "м";261bot.sendMessage(("До следующего урока: " + ost), msg.chatID);262bot.sendMessage(("До конца урока: " + ost1), msg.chatID);263} else if ((t.hour == 13 && t.minute >= 55) or (t.hour == 14 && t.minute < 35)) {264byte hour, minute, minute1, hour1;265String ost, ost1;266hour = 14 * 60 + 35 - t.hour * 60 - t.minute;267hour1 = 14 * 60 + 35 - t.hour * 60 - t.minute - 15;268minute = hour % 60;269minute1 = hour1 % 60;270hour = hour / 60;271hour1 = hour1 / 60;272ost += hour;273ost += "ч";274ost += minute;275ost += "м";276ost1 += hour1;277ost1 += "ч";278ost1 += minute1;279ost1 += "м";280bot.sendMessage(("До следующего урока: " + ost), msg.chatID);281bot.sendMessage(("До конца урока: " + ost1), msg.chatID);282} else bot.sendMessage("Уроков нет", msg.chatID);283} else if (msg.location.latitude.length() > 0 && msg.location.longitude.length() > 0) {284//bot.sendMessage("Lat: " + msg.location.latitude + ", Lon: " + msg.location.longitude, msg.chatID);285//String weather = "https://api.openweathermap.org/data/2.5/weather?lat=" + String(msg.location.latitude) + "&lon=" + String(msg.location.longitude) + "&units=metric&lang=ru&appid=" + API;286String url = "/data/2.5/weather?lat=" + msg.location.latitude + "&lon=" + msg.location.longitude + "&units=metric&lang=ru&appid=8b92943f5d8168a3668a52206d787396";287http.begin(client, "api.openweathermap.org", 80, url);288int httpCode = http.GET();289if (httpCode > 0) {290String spon = http.getString();291DeserializationError error = deserializeJson(wea, spon);292if (error) {293String errorStr = error.c_str();294bot.sendMessage(errorStr, msg.chatID);295} else {296//bot.sendMessage("deserializeJson() без ошибок.", msg.chatID);297//bot.sendMessage(spon, msg.chatID);298// Deserialize the JSON document299deserializeJson(wea, spon);300const char* locate = wea["name"];301bot.sendMessage(("Местоположение: " + String(locate)), msg.chatID);302//bot.sendMessage(url, msg.chatID);303JsonObject weather_0 = wea["weather"][0];304const char* description = weather_0["description"]; // "небольшой дождь"305const char* smile1 = weather_0["icon"]; // "10n"306String smile = smile1;307if (smile == "01d" or smile == "01n") bot.sendMessage(("Погода: " + String(description) + " ☀️"), msg.chatID);308else if (smile == "02d" or smile == "02n") bot.sendMessage(("Погода: " + String(description) + " ⛅"), msg.chatID);309else if (smile == "03d" or smile == "03n" or smile == "04d" or smile == "04n") bot.sendMessage(("Погода: " + String(description) + " ☁️"), msg.chatID);310else if (smile == "09d" or smile == "09n") bot.sendMessage(("Погода: " + String(description) + " ⛈️"), msg.chatID);311else if (smile == "10d" or smile == "10n") bot.sendMessage(("Погода: " + String(description) + " 🌧️"), msg.chatID);312else if (smile == "11d" or smile == "11n") bot.sendMessage(("Погода: " + String(description) + " 🌩️"), msg.chatID);313else if (smile == "13d" or smile == "13n") bot.sendMessage(("Погода: " + String(description) + " ❄️"), msg.chatID);314else if (smile == "50d" or smile == "50n") bot.sendMessage(("Погода: " + String(description) + " 🌫️"), msg.chatID);315float temp = wea["main"]["temp"];316bot.sendMessage(("Температура: " + String(temp)), msg.chatID);317byte hum = wea["main"]["humidity"];318bot.sendMessage(("Влажность: " + String(hum)), msg.chatID);319http.end();320}321}322}323}
324
325
326void connectWiFi() { //подключение к интернету327delay(2000);328Serial.begin(115200);329WiFi.begin(WIFI_SSID, WIFI_PASS);330while (WiFi.status() != WL_CONNECTED) {331delay(500);332Serial.print(".");333if (millis() > 15000) ESP.restart();334}335}
336
337//функции команд
338
339
340void loop() {341bot.tick();342}
343