· 8 months ago · Mar 09, 2025, 04:30 AM
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 *****/
36void setup(void);
37void loop(void);
38void updateOutputs();
39void connectToWiFi();
40void setupNTP();
41void sendWiFiAddress();
42void checkTimeAndControlRelay();
43
44/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
45const uint8_t myRelay_RelayModule_Signal_PIN_D1 = 1;
46
47/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
48bool myRelay_RelayModule_Signal_PIN_D1_rawData = 0;
49
50/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
51float myRelay_RelayModule_Signal_PIN_D1_phyData = 0.0;
52
53/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
54// NTPClient instance
55WiFiUDP udp;
56NTPClient timeClient(udp, "pool.ntp.org", 0, 60000); // NTP client to get time every minute
57
58// Telegram Bot instance
59const char* botToken = "YOUR_TELEGRAM_BOT_TOKEN"; // Replace with your bot token
60TelegramBot bot(botToken, WiFi); // Create Telegram bot instance
61
62void 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
70void 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
78void updateOutputs()
79{
80 digitalWrite(myRelay_RelayModule_Signal_PIN_D1, myRelay_RelayModule_Signal_PIN_D1_rawData);
81}
82
83void 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
91void setupNTP() {
92 timeClient.begin(); // Start the NTP client
93}
94
95void 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
100void 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 */