Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- Adafruit_BME280 bme;
- #define RELAY1 0 //D2
- #define RELAY2 4 //D3
- #define SDA 14 //D5
- #define SCL 12 //D6
- #define ON HIGH
- #define OFF LOW
- #define DELAY 3000
- #define address 0x76
- #define address2 0x77
- // Initialize Wifi connection to the router
- char ssid[] = "********"; // your network SSID (name)
- char password[] = "*********"; // your network key
- // Initialize Telegram BOT
- #define BOTtoken "*********************************" // your Bot Token (Get from Botfather)
- //Initialize temperature, humidity and pressure
- float temp;
- float hum;
- float pressure;
- WiFiClientSecure client;
- UniversalTelegramBot bot(BOTtoken, client);
- unsigned int Bot_mtbs = 1000; //mean time between scan messages
- unsigned long Bot_lasttime; //last time messages' scan has been done
- 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 from_id = String(bot.messages[i].from_id);
- String text = bot.messages[i].text;
- if (from_id == "***********") // here is my tellegram id
- {
- if (text == "/temp")
- {
- temp = bme.readTemperature();
- bot.sendMessage(chat_id, String("Температура в комнате: ") + String(temp) + String("˚C"));
- }
- else if (text == "/hum")
- {
- hum = bme.readHumidity();
- bot.sendMessage(chat_id, String("Влажность в комнате: ") + String(hum) + String("%"));
- }
- else if (text == "/press")
- {
- pressure = bme.readPressure() / 133.0;
- bot.sendMessage(chat_id, String("Атмосферное давление: ") + String(pressure) + String("мм.рт.ст"));
- }
- else if (text == "/1on")
- {
- digitalWrite(RELAY1, ON);
- bot.sendMessage(chat_id, String("Relay 1 now is ON"));
- }
- else if (text == "/1off")
- {
- digitalWrite(RELAY1, OFF);
- bot.sendMessage(chat_id, String("Relay 1 now is OFF"));
- }
- else if (text == "/2on")
- {
- digitalWrite(RELAY2, ON);
- bot.sendMessage(chat_id, String("Relay 2 now is ON"));
- }
- else if (text == "/2off")
- {
- digitalWrite(RELAY2, OFF);
- bot.sendMessage(chat_id, String("Relay 2 now is OFF"));
- }
- else if (text == "/allon")
- {
- digitalWrite(RELAY1, ON);
- digitalWrite(RELAY2, ON);
- bot.sendMessage(chat_id, String("Relay 1 now is ON \nRelay 2 now is ON"));
- }
- else if (text == "/alloff")
- {
- digitalWrite(RELAY1, OFF);
- digitalWrite(RELAY2, OFF);
- bot.sendMessage(chat_id, String("Relay 1 now is OFF \nRelay 2 now is OFF"));
- }
- else if (text == "/status")
- {
- if ((!digitalRead(RELAY1)) && (!digitalRead(RELAY2)))
- {
- bot.sendMessage(chat_id, String("Relay 1 now is OFF \nRelay 2 now is OFF"));
- }
- else if ((digitalRead(RELAY1)) && (digitalRead(RELAY2)))
- {
- bot.sendMessage(chat_id, String("Relay 1 now is ON \nRelay 2 now is ON"));
- }
- else if ((!digitalRead(RELAY1)) && (digitalRead(RELAY2)))
- {
- bot.sendMessage(chat_id, String("Relay 1 now is OFF \nRelay 2 now is ON"));
- }
- else if ((digitalRead(RELAY1)) && (!digitalRead(RELAY2)))
- {
- bot.sendMessage(chat_id, String("Relay 1 now is ON \nRelay 2 now is OFF"));
- }
- }
- else
- {
- bot.sendMessage(chat_id, String("Command ") + String('"') + String(text) + String('"') + String(" is unknown!"));
- }
- }
- else
- {
- bot.sendMessage(chat_id, String("Access denied!"));
- }
- }
- }
- void setup()
- {
- Serial.begin(115200);
- Wire.begin(SDA, SCL);
- //Set pins mode and default state
- pinMode(RELAY1, OUTPUT);
- digitalWrite(RELAY1, OFF);
- pinMode(RELAY2, OUTPUT);
- digitalWrite(RELAY2, OFF);
- if (!bme.begin(address))
- {
- Serial.println("Could not find a valid BME280 sensor, check wiring or try another address!");
- while (1)
- {
- yield();
- delay(DELAY);
- }
- }
- // bme.begin();
- // 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());
- }
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement