Advertisement
safwan092

Untitled

May 15th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include "CTBot.h"
  2. #include "DHT.h"
  3.  
  4. #define dht11_PIN D4
  5. DHT dht(dht11_PIN, DHT11);
  6. CTBot myBot;
  7.  
  8. float temp_Value;
  9. float hum_Value;
  10.  
  11. String ssid = "network"; // REPLACE mySSID WITH YOUR WIFI SSID
  12. String pass = "123456789"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
  13. String token = "5316933044:AAFP4Huo7KpzrlPaDV-o3BuZUKaBlBIR16U";
  14. //"5303605044:AAGU8H0XDaYFlxNSlqWiULycmVNfFXP6aMw"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
  15. uint8_t led1 = D1;
  16. uint8_t led2 = D2; // the onboard ESP8266 LED.
  17. // If you have a NodeMCU you can use the BUILTIN_LED pin
  18. // (replace 2 with BUILTIN_LED)
  19.  
  20. void setup() {
  21. // initialize the Serial
  22. Serial.begin(115200);
  23. Serial.println("Starting TelegramBot...");
  24.  
  25. // connect the ESP8266 to the desired access point
  26. myBot.wifiConnect(ssid, pass);
  27.  
  28. // set the telegram bot token
  29. myBot.setTelegramToken(token);
  30.  
  31. // check if all things are ok
  32. if (myBot.testConnection())
  33. Serial.println("\ntestConnection OK");
  34. else
  35. Serial.println("\ntestConnection NOK");
  36.  
  37.  
  38. }
  39.  
  40. void loop() {
  41. // a variable to store telegram message data
  42. TBMessage msg;
  43. temp_Value = checkTemp();
  44. hum_Value = checkHum();
  45. // if there is an incoming message...
  46. if (myBot.getNewMessage(msg)) {
  47.  
  48. if (msg.text.equalsIgnoreCase("temp")||msg.text.equalsIgnoreCase("Temp")) { // if the received message is "LIGHT ON"...
  49. float value_1 = checkTemp();
  50. float value_2 = checkHum();
  51. String reply1;
  52. String reply2;
  53. reply1 = (String)"Temprature reading is : " + String(value_1);
  54. reply2 = (String)"Humidity reading is : " + String(value_2);
  55. myBot.sendMessage(msg.sender.id, "Temprature reading is : " + String(value_1));
  56. myBot.sendMessage(msg.sender.id, "Humidity reading is : " + String(value_2));
  57. }
  58. //else { // otherwise...
  59. // generate the message for the sender
  60. //String reply;
  61. //reply = (String)"Welcome " + msg.sender.username + (String)". Try LIGHT ON or LIGHT OFF.";
  62. //myBot.sendMessage(msg.sender.id, reply); // and send it
  63. //}
  64. }
  65. if (temp_Value>30){
  66. myBot.sendMessage(msg.sender.id, "HIGH Tempreture detected !!!");
  67. }
  68. // wait 500 milliseconds
  69. delay(500);
  70. }//end of LOOP
  71.  
  72.  
  73. float checkTemp() {
  74. float value1 = dht.readTemperature();
  75. //Serial.print("temprature value: ");
  76. //Serial.println(value1);
  77. return value1;
  78. }
  79. float checkHum() {
  80. float value2 = dht.readHumidity();
  81. //Serial.print("humidity value: ");
  82. //Serial.println(value2);
  83. return value2;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement