|
| 1 | +/******************************************************************* |
| 2 | + * An example of bot that receives commands and turns on and off * |
| 3 | + * an LED. * |
| 4 | + * * |
| 5 | + * written by Giacarlo Bacchio (Gianbacchio on Github) * |
| 6 | + * adapted by Brian Lough * |
| 7 | + *******************************************************************/ |
| 8 | +#include <ESP8266WiFi.h> |
| 9 | +#include <WiFiClientSecure.h> |
| 10 | +#include <TimeLib.h> |
| 11 | +#include <WiFiUdp.h> |
| 12 | +#include <ArduinoJson.h> |
| 13 | +#include "FS.h" |
| 14 | +#include <AccelStepper.h> |
| 15 | +#include "DHT.h" |
| 16 | +#include <ArduinoOTA.h> |
| 17 | +#include <UniversalTelegramBot.h> |
| 18 | + |
| 19 | +// Initialize Wifi connection to the router |
| 20 | +char ssid[] = "1a3496"; // your network SSID (name) |
| 21 | +char password[] = "278833924"; // your network key |
| 22 | + |
| 23 | +// Initialize Telegram BOT |
| 24 | +#define BOTtoken "326858637:AAGDZGkaKP_Vrhzs8mkd--0BnGQQ6AioF4M" // your Bot Token (Get off Botfather) |
| 25 | + |
| 26 | +WiFiClientSecure client; |
| 27 | +UniversalTelegramBot bot(BOTtoken, client); |
| 28 | + |
| 29 | +int Bot_mtbs = 1000; //mean time between scan messages |
| 30 | +long Bot_lasttime; //last time messages' scan has been done |
| 31 | +bool Start = false; |
| 32 | + |
| 33 | +const int ledPin = 13; |
| 34 | +int ledStatus = 0; |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +// PINS SETUP |
| 40 | +#define REED_WINDOW_CLOSED D1 |
| 41 | +#define REED_WINDOW_MAX_OPEN D2 |
| 42 | +#define STEPPER_DRIVER_ON_OFF D3 |
| 43 | +#define DHTPIN D7 |
| 44 | +#define RESET_CONFIG_BUTTON D8 |
| 45 | + |
| 46 | +// TEMP SETUP |
| 47 | +#define DHTTYPE DHT22 |
| 48 | + |
| 49 | +String apiKey = "xxxxxxxxxxxxx"; |
| 50 | +const char* statistic_server = "api.thingspeak.com"; |
| 51 | + |
| 52 | +int wifi_connect_tries = 0; |
| 53 | + |
| 54 | +const int is_in_office = 0; |
| 55 | +const int is_in_dev_room = 0; |
| 56 | + |
| 57 | +// sleep for this many seconds |
| 58 | +const int sleepSeconds = 10; |
| 59 | + |
| 60 | +AccelStepper Stepper1(1,D5,D6); |
| 61 | +WiFiServer server(80); |
| 62 | + |
| 63 | +String setup_log = ""; |
| 64 | +String loop_log = ""; |
| 65 | + |
| 66 | +// NTP Servers: |
| 67 | +static const char ntpServerName[] = "us.pool.ntp.org"; |
| 68 | +//static const char ntpServerName[] = "time.nist.gov"; |
| 69 | +//static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov"; |
| 70 | +//static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov"; |
| 71 | +//static const char ntpServerName[] = "time-c.timefreq.bldrdoc.gov"; |
| 72 | + |
| 73 | +const int timeZone = 2; // Central European Time |
| 74 | + |
| 75 | +WiFiUDP Udp; |
| 76 | +unsigned int localPort = 8888; // local port to listen for UDP packets |
| 77 | + |
| 78 | +time_t getNtpTime(); |
| 79 | +void digitalClockDisplay(); |
| 80 | +void printDigits(int digits); |
| 81 | +void sendNTPpacket(IPAddress &address); |
| 82 | + |
| 83 | +DHT dht(DHTPIN, DHTTYPE); |
| 84 | + |
| 85 | +int automatic_mode = 1; |
| 86 | +float current_window_degree = 0; |
| 87 | +int automatic_window_step = 2; //degrees |
| 88 | +int dir_open = -1; |
| 89 | +int dir_close = 1; |
| 90 | + |
| 91 | +int keep_temp_cold_room = 23; // (+-1) |
| 92 | +time_t last_motor_action; |
| 93 | + |
| 94 | +bool motor_working = false; |
| 95 | +String current_motor_action = ""; |
| 96 | +bool debug = false; |
| 97 | +bool config_found = true; |
| 98 | +bool closing_window_config_not_found = false; |
| 99 | +bool need_redirect_to_main_page = false; |
| 100 | + |
| 101 | +String s = "LOG"; |
| 102 | + |
| 103 | +bool sketch_updating = false; |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +void handleNewMessages(int numNewMessages) { |
| 109 | + Serial.println("handleNewMessages"); |
| 110 | + Serial.println(String(numNewMessages)); |
| 111 | + |
| 112 | + for (int i=0; i<numNewMessages; i++) { |
| 113 | + String chat_id = String(bot.messages[i].chat_id); |
| 114 | + String text = bot.messages[i].text; |
| 115 | + |
| 116 | + String from_name = bot.messages[i].from_name; |
| 117 | + if (from_name == "") from_name = "Guest"; |
| 118 | + |
| 119 | + if (text == "/ledon") { |
| 120 | + digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) |
| 121 | + ledStatus = 1; |
| 122 | + bot.sendMessage(chat_id, "Led is ON", ""); |
| 123 | + } |
| 124 | + |
| 125 | + if (text == "/ledoff") { |
| 126 | + ledStatus = 0; |
| 127 | + digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level) |
| 128 | + bot.sendMessage(chat_id, "Led is OFF", ""); |
| 129 | + } |
| 130 | + |
| 131 | + if (text == "/status") { |
| 132 | + String file_content = "Ninja\n\n"; |
| 133 | + |
| 134 | + file_content += "mode - Change mode (automatic or manual)\n"; |
| 135 | + |
| 136 | + file_content += "open_window_1 - Open window by 1 degrees\n"; |
| 137 | + file_content += "open_window_3 - Open window by 3 degrees\n"; |
| 138 | + file_content += "open_window_5 - Open window by 5 degrees\n"; |
| 139 | + file_content += "open_window_10 - Open window by 10 degrees\n\n"; |
| 140 | + |
| 141 | + file_content += "close_window_1 - Close window by 1 degrees\n"; |
| 142 | + file_content += "close_window_3 - Close window by 3 degrees\n"; |
| 143 | + file_content += "close_window_5 - Close window by 5 degrees\n"; |
| 144 | + file_content += "close_window_10 - Close window by 10 degrees\n\n"; |
| 145 | + |
| 146 | + file_content += "mode - Change mode (automatic or manual)\n\n"; |
| 147 | + |
| 148 | + file_content += "open_window_1 - Open window by 1 degrees\n"; |
| 149 | + file_content += "open_window_3 - Open window by 3 degrees\n"; |
| 150 | + file_content += "open_window_5 - Open window by 5 degrees\n"; |
| 151 | + file_content += "open_window_10 - Open window by 10 degrees\n\n"; |
| 152 | + |
| 153 | + file_content += "close_window_1 - Close window by 1 degrees\n"; |
| 154 | + file_content += "close_window_3 - Close window by 3 degrees\n"; |
| 155 | + file_content += "close_window_5 - Close window by 5 degrees\n"; |
| 156 | + file_content += "close_window_10 - Close window by 10 degrees"; |
| 157 | + |
| 158 | + bot.sendMessage(chat_id, file_content); |
| 159 | + } |
| 160 | + |
| 161 | + if (text == "/start") { |
| 162 | + String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n"; |
| 163 | + welcome += "This is Flash Led Bot example.\n\n"; |
| 164 | + welcome += "/ledon : to switch the Led ON\n"; |
| 165 | + welcome += "/ledoff : to switch the Led OFF\n"; |
| 166 | + welcome += "/status : Returns current status of LED\n"; |
| 167 | + bot.sendMessage(chat_id, welcome, "Markdown"); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +void setup() { |
| 174 | + Serial.begin(115200); |
| 175 | + |
| 176 | + // Set WiFi to station mode and disconnect from an AP if it was Previously |
| 177 | + // connected |
| 178 | + WiFi.mode(WIFI_STA); |
| 179 | + WiFi.disconnect(); |
| 180 | + delay(100); |
| 181 | + |
| 182 | + // attempt to connect to Wifi network: |
| 183 | + Serial.print("Connecting Wifi: "); |
| 184 | + Serial.println(ssid); |
| 185 | + WiFi.begin(ssid, password); |
| 186 | + |
| 187 | + while (WiFi.status() != WL_CONNECTED) { |
| 188 | + Serial.print("."); |
| 189 | + delay(500); |
| 190 | + } |
| 191 | + |
| 192 | + Serial.println(""); |
| 193 | + Serial.println("WiFi connected"); |
| 194 | + Serial.print("IP address: "); |
| 195 | + Serial.println(WiFi.localIP()); |
| 196 | + |
| 197 | + pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output. |
| 198 | + delay(10); |
| 199 | + digitalWrite(ledPin, LOW); // initialize pin as off |
| 200 | +} |
| 201 | + |
| 202 | +void loop() { |
| 203 | + if (millis() > Bot_lasttime + Bot_mtbs) { |
| 204 | + int numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 205 | + |
| 206 | + while(numNewMessages) { |
| 207 | + Serial.println("got response"); |
| 208 | + handleNewMessages(numNewMessages); |
| 209 | + numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 210 | + } |
| 211 | + |
| 212 | + Bot_lasttime = millis(); |
| 213 | + } |
| 214 | +} |
0 commit comments