Advertisement
pleasedontcode

**Relay Control** rev_01

Mar 8th, 2025
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Relay Control**
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-03-09 04:23:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* let the relay work on the basis of the setting of */
  21.     /* hours stored in flash memory, configured in web- */
  22.     /* intefeys, take the current time from the internet, */
  23.     /* transfer the wifi address via telegram */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  30. #include <WiFi.h>   // Library for WiFi connection
  31. #include <NTPClient.h> // Library to get time from NTP server
  32. #include <WiFiUdp.h> // Library for UDP communication
  33. #include <TelegramBot.h> // Library for Telegram Bot
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void updateOutputs();
  39. void connectToWiFi();
  40. void setupNTP();
  41. void sendWiFiAddress();
  42. void checkTimeAndControlRelay();
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t myRelay_RelayModule_Signal_PIN_D1 = 1;
  46.  
  47. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  48. bool myRelay_RelayModule_Signal_PIN_D1_rawData = 0;
  49.  
  50. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  51. float myRelay_RelayModule_Signal_PIN_D1_phyData = 0.0;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. // NTPClient instance
  55. WiFiUDP udp;
  56. NTPClient timeClient(udp, "pool.ntp.org", 0, 60000); // NTP client to get time every minute
  57.  
  58. // Telegram Bot instance
  59. const char* botToken = "YOUR_TELEGRAM_BOT_TOKEN"; // Replace with your bot token
  60. TelegramBot bot(botToken, WiFi); // Create Telegram bot instance
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     pinMode(myRelay_RelayModule_Signal_PIN_D1, OUTPUT);
  66.     connectToWiFi(); // Connect to WiFi
  67.     setupNTP(); // Setup NTP client
  68. }
  69.  
  70. void loop(void)
  71. {
  72.     // put your main code here, to run repeatedly:
  73.     timeClient.update(); // Update time from NTP server
  74.     checkTimeAndControlRelay(); // Check time and control relay
  75.     updateOutputs(); // Refresh output data
  76. }
  77.  
  78. void updateOutputs()
  79. {
  80.     digitalWrite(myRelay_RelayModule_Signal_PIN_D1, myRelay_RelayModule_Signal_PIN_D1_rawData);
  81. }
  82.  
  83. void connectToWiFi() {
  84.     WiFi.begin("YOUR_SSID", "YOUR_PASSWORD"); // Replace with your SSID and password
  85.     while (WiFi.status() != WL_CONNECTED) {
  86.         delay(1000);
  87.     }
  88.     sendWiFiAddress(); // Send WiFi address via Telegram
  89. }
  90.  
  91. void setupNTP() {
  92.     timeClient.begin(); // Start the NTP client
  93. }
  94.  
  95. void sendWiFiAddress() {
  96.     String message = "Connected to WiFi: " + String(WiFi.localIP().toString());
  97.     bot.sendMessage("YOUR_CHAT_ID", message); // Replace with your chat ID
  98. }
  99.  
  100. void checkTimeAndControlRelay() {
  101.     // Example logic to control the relay based on time
  102.     int currentHour = timeClient.getHours();
  103.     if (currentHour >= 8 && currentHour < 20) { // Relay on between 8 AM and 8 PM
  104.         myRelay_RelayModule_Signal_PIN_D1_rawData = true; // Turn on relay
  105.     } else {
  106.         myRelay_RelayModule_Signal_PIN_D1_rawData = false; // Turn off relay
  107.     }
  108. }
  109.  
  110. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement