Advertisement
Guest User

Untitled

a guest
Mar 9th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. #include <Wire.h>
  5. #include <Adafruit_Sensor.h>
  6. #include <Adafruit_BME280.h>
  7. Adafruit_BME280 bme;
  8.  
  9. #define RELAY1 0  //D2
  10. #define RELAY2 4  //D3
  11. #define SDA 14    //D5
  12. #define SCL 12    //D6
  13. #define ON HIGH
  14. #define OFF LOW
  15. #define DELAY 3000
  16. #define address 0x76
  17. #define address2 0x77
  18.  
  19. // Initialize Wifi connection to the router
  20. char ssid[] = "********";        // your network SSID (name)
  21. char password[] = "*********"; // your network key
  22. // Initialize Telegram BOT
  23. #define BOTtoken "*********************************" // your Bot Token (Get from Botfather)
  24. //Initialize temperature, humidity and pressure
  25. float temp;
  26. float hum;
  27. float pressure;
  28.  
  29. WiFiClientSecure client;
  30. UniversalTelegramBot bot(BOTtoken, client);
  31.  
  32. unsigned int Bot_mtbs = 1000; //mean time between scan messages
  33. unsigned long Bot_lasttime;   //last time messages' scan has been done
  34.  
  35. void handleNewMessages(int numNewMessages)
  36. {
  37.   Serial.println("handleNewMessages");
  38.   Serial.println(String(numNewMessages));
  39.  
  40.   for (int i = 0; i < numNewMessages; i++)
  41.   {
  42.     String chat_id = String(bot.messages[i].chat_id);
  43.     String from_id = String(bot.messages[i].from_id);
  44.     String text = bot.messages[i].text;
  45.  
  46.     if (from_id == "***********") // here is my tellegram id
  47.     {
  48.       if (text == "/temp")
  49.       {
  50.         temp = bme.readTemperature();
  51.         bot.sendMessage(chat_id, String("Температура в комнате: ") + String(temp) + String("˚C"));
  52.       }
  53.       else if (text == "/hum")
  54.       {
  55.         hum = bme.readHumidity();
  56.         bot.sendMessage(chat_id, String("Влажность в комнате: ") + String(hum) + String("%"));
  57.       }
  58.       else if (text == "/press")
  59.       {
  60.         pressure = bme.readPressure() / 133.0;
  61.         bot.sendMessage(chat_id, String("Атмосферное давление: ") + String(pressure) + String("мм.рт.ст"));
  62.       }
  63.       else if (text == "/1on")
  64.       {
  65.         digitalWrite(RELAY1, ON);
  66.         bot.sendMessage(chat_id, String("Relay 1 now is ON"));
  67.       }
  68.       else if (text == "/1off")
  69.       {
  70.         digitalWrite(RELAY1, OFF);
  71.         bot.sendMessage(chat_id, String("Relay 1 now is OFF"));
  72.       }
  73.       else if (text == "/2on")
  74.       {
  75.         digitalWrite(RELAY2, ON);
  76.         bot.sendMessage(chat_id, String("Relay 2 now is ON"));
  77.       }
  78.       else if (text == "/2off")
  79.       {
  80.         digitalWrite(RELAY2, OFF);
  81.         bot.sendMessage(chat_id, String("Relay 2 now is OFF"));
  82.       }
  83.       else if (text == "/allon")
  84.       {
  85.         digitalWrite(RELAY1, ON);
  86.         digitalWrite(RELAY2, ON);
  87.         bot.sendMessage(chat_id, String("Relay 1 now is ON \nRelay 2 now is ON"));
  88.       }
  89.       else if (text == "/alloff")
  90.       {
  91.         digitalWrite(RELAY1, OFF);
  92.         digitalWrite(RELAY2, OFF);
  93.         bot.sendMessage(chat_id, String("Relay 1 now is OFF \nRelay 2 now is OFF"));
  94.       }
  95.       else if (text == "/status")
  96.       {
  97.         if ((!digitalRead(RELAY1)) && (!digitalRead(RELAY2)))
  98.         {
  99.           bot.sendMessage(chat_id, String("Relay 1 now is OFF \nRelay 2 now is OFF"));
  100.         }
  101.         else if ((digitalRead(RELAY1)) && (digitalRead(RELAY2)))
  102.         {
  103.           bot.sendMessage(chat_id, String("Relay 1 now is ON \nRelay 2 now is ON"));
  104.         }
  105.         else if ((!digitalRead(RELAY1)) && (digitalRead(RELAY2)))
  106.         {
  107.           bot.sendMessage(chat_id, String("Relay 1 now is OFF \nRelay 2 now is ON"));
  108.         }
  109.         else if ((digitalRead(RELAY1)) && (!digitalRead(RELAY2)))
  110.         {
  111.           bot.sendMessage(chat_id, String("Relay 1 now is ON \nRelay 2 now is OFF"));
  112.         }
  113.       }
  114.       else
  115.       {
  116.         bot.sendMessage(chat_id, String("Command ") + String('"') + String(text) + String('"') + String(" is unknown!"));
  117.       }
  118.     }
  119.     else
  120.     {
  121.       bot.sendMessage(chat_id, String("Access denied!"));
  122.     }
  123.   }
  124. }
  125.  
  126. void setup()
  127. {
  128.   Serial.begin(115200);
  129.   Wire.begin(SDA, SCL);
  130.   //Set pins mode and default state
  131.   pinMode(RELAY1, OUTPUT);
  132.   digitalWrite(RELAY1, OFF);
  133.   pinMode(RELAY2, OUTPUT);
  134.   digitalWrite(RELAY2, OFF);
  135.   if (!bme.begin(address))
  136.   {
  137.     Serial.println("Could not find a valid BME280 sensor, check wiring or try another address!");
  138.     while (1)
  139.     {
  140.       yield();
  141.       delay(DELAY);
  142.     }
  143.   }
  144.   // bme.begin();
  145.   // Set WiFi to station mode and disconnect from an AP if it was Previously
  146.   // connected
  147.   WiFi.mode(WIFI_STA);
  148.   WiFi.disconnect();
  149.   delay(100);
  150.   // attempt to connect to Wifi network:
  151.   Serial.print("Connecting Wifi: ");
  152.   Serial.println(ssid);
  153.   WiFi.begin(ssid, password);
  154.   while (WiFi.status() != WL_CONNECTED)
  155.   {
  156.     Serial.print(".");
  157.     delay(500);
  158.   }
  159.   Serial.println("");
  160.   Serial.println("WiFi connected");
  161.   Serial.print("IP address: ");
  162.   Serial.println(WiFi.localIP());
  163. }
  164. void loop()
  165. {
  166.   if (millis() > Bot_lasttime + Bot_mtbs)
  167.   {
  168.     int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  169.     while (numNewMessages)
  170.     {
  171.       Serial.println("got response");
  172.       handleNewMessages(numNewMessages);
  173.       numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  174.     }
  175.     Bot_lasttime = millis();
  176.   }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement