Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Relay Control**
- - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
- - Source Code created on: 2025-03-09 04:23:37
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* let the relay work on the basis of the setting of */
- /* hours stored in flash memory, configured in web- */
- /* intefeys, take the current time from the internet, */
- /* transfer the wifi address via telegram */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Relay.h> //https://github.com/rafaelnsantos/Relay
- #include <WiFi.h> // Library for WiFi connection
- #include <NTPClient.h> // Library to get time from NTP server
- #include <WiFiUdp.h> // Library for UDP communication
- #include <TelegramBot.h> // Library for Telegram Bot
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs();
- void connectToWiFi();
- void setupNTP();
- void sendWiFiAddress();
- void checkTimeAndControlRelay();
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t myRelay_RelayModule_Signal_PIN_D1 = 1;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool myRelay_RelayModule_Signal_PIN_D1_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float myRelay_RelayModule_Signal_PIN_D1_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // NTPClient instance
- WiFiUDP udp;
- NTPClient timeClient(udp, "pool.ntp.org", 0, 60000); // NTP client to get time every minute
- // Telegram Bot instance
- const char* botToken = "YOUR_TELEGRAM_BOT_TOKEN"; // Replace with your bot token
- TelegramBot bot(botToken, WiFi); // Create Telegram bot instance
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(myRelay_RelayModule_Signal_PIN_D1, OUTPUT);
- connectToWiFi(); // Connect to WiFi
- setupNTP(); // Setup NTP client
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- timeClient.update(); // Update time from NTP server
- checkTimeAndControlRelay(); // Check time and control relay
- updateOutputs(); // Refresh output data
- }
- void updateOutputs()
- {
- digitalWrite(myRelay_RelayModule_Signal_PIN_D1, myRelay_RelayModule_Signal_PIN_D1_rawData);
- }
- void connectToWiFi() {
- WiFi.begin("YOUR_SSID", "YOUR_PASSWORD"); // Replace with your SSID and password
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- }
- sendWiFiAddress(); // Send WiFi address via Telegram
- }
- void setupNTP() {
- timeClient.begin(); // Start the NTP client
- }
- void sendWiFiAddress() {
- String message = "Connected to WiFi: " + String(WiFi.localIP().toString());
- bot.sendMessage("YOUR_CHAT_ID", message); // Replace with your chat ID
- }
- void checkTimeAndControlRelay() {
- // Example logic to control the relay based on time
- int currentHour = timeClient.getHours();
- if (currentHour >= 8 && currentHour < 20) { // Relay on between 8 AM and 8 PM
- myRelay_RelayModule_Signal_PIN_D1_rawData = true; // Turn on relay
- } else {
- myRelay_RelayModule_Signal_PIN_D1_rawData = false; // Turn off relay
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement