safwan092

Untitled

Jun 3rd, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4.  
  5. #define LIGHT_SENSOR_PIN 34
  6.  
  7. #define WIFI_SSID "Team2 project"
  8. #define WIFI_PASSWORD "123454321"
  9. #define BOT_TOKEN "6094929450:AAFapzcEPmso7a5VoEUOpb8lOc-BF4AejRM"
  10. String chat_id_1 = "-994605295";
  11. int total = 0;
  12. int stateChange = 0;
  13. WiFiClientSecure secured_client;
  14. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  15.  
  16. void setup()
  17. {
  18. Serial.begin(115200);
  19. connectToWiFiAndTelegramServer();
  20. total = 0;
  21. bot.sendMessage(chat_id_1, "Thank you for Saving the Environment");
  22. bot.sendMessage(chat_id_1, "Curent Balance: " + String(total) + " (SAR)");
  23. }
  24.  
  25. void loop()
  26. {
  27. int analogValue = analogRead(LIGHT_SENSOR_PIN);
  28. Serial.println(analogValue);
  29. if (analogValue < 3000 && stateChange == 0) { //700
  30. stateChange = 1;
  31. total += 1;
  32. bot.sendMessage(chat_id_1, "Curent Balance: " + String(float(total)) + " (SAR)");
  33. delay(1000);
  34. }
  35. else if(analogValue > 3000 && stateChange == 1){
  36. stateChange = 0;
  37. delay(1000);
  38. }
  39. }
  40.  
  41.  
  42. void connectToWiFiAndTelegramServer() {
  43. Serial.println();
  44. Serial.print("Connecting to Wifi SSID ");
  45. Serial.print(WIFI_SSID);
  46. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  47. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
  48. while (WiFi.status() != WL_CONNECTED)
  49. {
  50. Serial.print(".");
  51. delay(500);
  52. }
  53. Serial.print("\nWiFi connected. IP address: ");
  54. Serial.println(WiFi.localIP());
  55.  
  56. Serial.print("Retrieving time: ");
  57. configTime(0, 0, "pool.ntp.org");
  58.  
  59. time_t now = time(nullptr);
  60. while (now < 24 * 3600)
  61. {
  62. Serial.print(".");
  63. delay(100);
  64. now = time(nullptr);
  65. }
  66. Serial.println(now);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment