pleasedontcode

WiFi Relay rev_05

Aug 13th, 2025
205
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: WiFi Relay
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-08-13 12:09:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp12f with i2c display timer,press start relay on */
  21.     /* hold set time.after delay another delay time as */
  22.     /* per set start,after this lid open a message on */
  23.     /* display and off all.2 swich to up down time */
  24.     /* setting,another one swich to stop.all setting from */
  25.     /* mobile */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /********* User code review feedback **********
  30. #### Feedback 1 ####
  31. - ssd name is esp32,password is 12345678
  32. #### Feedback 2 ####
  33. - wifi not seen
  34.  
  35. ********* User code review feedback **********/
  36. /* START CODE */
  37. /****** DEFINITION OF LIBRARIES *****/
  38. #include <DS18B20.h>
  39. #include <Wire.h>             // For I2C display
  40. #include <LiquidCrystal_I2C.h> // For I2C LCD display
  41. #include <ESP8266WiFi.h>      // For WiFi connectivity
  42. #include <ESP8266WebServer.h> // For mobile control via web
  43.  
  44. /****** FUNCTION PROTOTYPES *****/
  45. void setup(void);
  46. void loop(void);
  47. void initializeDisplay();
  48. void updateDisplay(const String& message);
  49. void handleWebServer();
  50. void startRelay();
  51. void stopRelay();
  52. void openLid();
  53. void closeLid();
  54. void saveSettingsFromMobile();
  55. void handleSetDelays();
  56. void handleStartSystem();
  57. void handleStopSystem();
  58.  
  59. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  60. const uint8_t DS18B20_DQ_PIN = 1; // D1 pin on NodeMCU
  61.  
  62. // Pins for switches/buttons
  63. const uint8_t BUTTON_START_PIN = D2;   // GPIO 4
  64. const uint8_t BUTTON_UP_PIN = D3;      // GPIO 0
  65. const uint8_t BUTTON_DOWN_PIN = D4;    // GPIO 2
  66. const uint8_t BUTTON_STOP_PIN = D5;    // GPIO 14
  67.  
  68. // Relay control pin
  69. const uint8_t RELAY_PIN = D6;          // GPIO 12
  70.  
  71. // Lid sensor (optional)
  72. const uint8_t LID_SENSOR_PIN = D7;     // GPIO 13
  73.  
  74. //***** LIBRARIES CLASS INSTANCES *****/
  75. // DS18B20 instance
  76. DS18B20 temperatureSensor(DS18B20_DQ_PIN);
  77.  
  78. // LCD display instance (assuming 16x2 LCD with I2C address 0x27)
  79. LiquidCrystal_I2C lcd(0x27, 16, 2);
  80.  
  81. // Web server for mobile control
  82. ESP8266WebServer server(80);
  83.  
  84. // Timer variables
  85. unsigned long startDelayMillis = 0;
  86. unsigned long holdDelayMillis = 0;
  87. unsigned long startDelayDuration = 5000; // default 5 seconds
  88. unsigned long holdDelayDuration = 10000; // default 10 seconds
  89.  
  90. // State variables
  91. bool relayOn = false;
  92. bool lidOpened = false;
  93. bool systemRunning = false;
  94. unsigned long systemStartTime = 0;
  95.  
  96. // Mobile settings (to be set via web interface)
  97. unsigned long userSetStartDelay = 5000; // default 5 seconds
  98. unsigned long userSetHoldDelay = 10000; // default 10 seconds
  99.  
  100. // WiFi credentials
  101. const char* ssid = "esp12f";
  102. const char* password = "12345678";
  103.  
  104. // Function to initialize display
  105. void initializeDisplay() {
  106.   lcd.init();
  107.   lcd.backlight();
  108.   updateDisplay("System Ready");
  109. }
  110.  
  111. // Function to update display message
  112. void updateDisplay(const String& message) {
  113.   lcd.clear();
  114.   lcd.setCursor(0, 0);
  115.   lcd.print(message);
  116. }
  117.  
  118. // Setup function
  119. void setup() {
  120.   // Initialize serial for debugging
  121.   Serial.begin(9600);
  122.  
  123.   // Initialize pins
  124.   pinMode(DS18B20_DQ_PIN, INPUT);
  125.   pinMode(BUTTON_START_PIN, INPUT_PULLUP);
  126.   pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
  127.   pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
  128.   pinMode(BUTTON_STOP_PIN, INPUT_PULLUP);
  129.   pinMode(RELAY_PIN, OUTPUT);
  130.   pinMode(LID_SENSOR_PIN, INPUT_PULLUP);
  131.  
  132.   // Initialize display
  133.   initializeDisplay();
  134.  
  135.   // Connect to WiFi
  136.   WiFi.begin(ssid, password);
  137.   while (WiFi.status() != WL_CONNECTED) {
  138.     delay(500);
  139.     Serial.print(".");
  140.   }
  141.   Serial.println("\nWiFi connected");
  142.   updateDisplay("WiFi Connected");
  143.  
  144.   // Setup web server routes
  145.   server.on("/", handleWebServer);
  146.   server.on("/setDelays", handleSetDelays);
  147.   server.on("/start", handleStartSystem);
  148.   server.on("/stop", handleStopSystem);
  149.   server.begin();
  150.  
  151.   // Initialize other variables
  152.   startDelayMillis = millis();
  153.   holdDelayMillis = millis();
  154. }
  155.  
  156. // Loop function
  157. void loop() {
  158.   // Handle web server
  159.   handleWebServer();
  160.  
  161.   // Read buttons
  162.   if (digitalRead(BUTTON_START_PIN) == LOW) {
  163.     // Start system
  164.     systemRunning = true;
  165.     systemStartTime = millis();
  166.     updateDisplay("System Started");
  167.     startRelay();
  168.   }
  169.  
  170.   if (digitalRead(BUTTON_STOP_PIN) == LOW) {
  171.     // Stop system
  172.     systemRunning = false;
  173.     updateDisplay("System Stopped");
  174.     stopRelay();
  175.   }
  176.  
  177.   if (digitalRead(BUTTON_UP_PIN) == LOW) {
  178.     // Increase start delay
  179.     userSetStartDelay += 1000; // increase by 1 sec
  180.     Serial.print("Start Delay: ");
  181.     Serial.println(userSetStartDelay);
  182.   }
  183.  
  184.   if (digitalRead(BUTTON_DOWN_PIN) == LOW) {
  185.     // Decrease start delay
  186.     if (userSetStartDelay > 1000) {
  187.       userSetStartDelay -= 1000; // decrease by 1 sec
  188.     }
  189.     Serial.print("Start Delay: ");
  190.     Serial.println(userSetStartDelay);
  191.   }
  192.  
  193.   // Main logic
  194.   if (systemRunning) {
  195.     unsigned long currentTime = millis();
  196.  
  197.     // Check if delay before starting relay
  198.     if ((currentTime - systemStartTime) >= userSetStartDelay && !relayOn) {
  199.       startRelay();
  200.     }
  201.  
  202.     // After relay is on for hold delay, open lid and turn off relay
  203.     if (relayOn && (currentTime - (systemStartTime + userSetStartDelay)) >= userSetHoldDelay) {
  204.       openLid();
  205.       stopRelay();
  206.       updateDisplay("Lid Opened");
  207.     }
  208.  
  209.     // Optional: monitor lid sensor or other conditions
  210.     if (digitalRead(LID_SENSOR_PIN) == LOW && !lidOpened) {
  211.       // Lid opened
  212.       lidOpened = true;
  213.       updateDisplay("Lid is Open");
  214.     }
  215.   }
  216.  
  217.   // Read temperature
  218.   float tempC = temperatureSensor.getTempC();
  219.   Serial.print("Temperature: ");
  220.   Serial.print(tempC);
  221.   Serial.println(" C");
  222.  
  223.   delay(100); // loop delay
  224. }
  225.  
  226. // Handle web server requests
  227. void handleWebServer() {
  228.   server.handleClient();
  229.   // Additional web interface handling can be added here
  230. }
  231.  
  232. // Handle setting delays via web
  233. void handleSetDelays() {
  234.   if (server.hasArg("startDelay")) {
  235.     unsigned long delayVal = server.arg("startDelay").toInt();
  236.     userSetStartDelay = delayVal;
  237.   }
  238.   if (server.hasArg("holdDelay")) {
  239.     unsigned long delayVal = server.arg("holdDelay").toInt();
  240.     userSetHoldDelay = delayVal;
  241.   }
  242.   server.send(200, "text/plain", "Delays updated");
  243. }
  244.  
  245. // Handle start system
  246. void handleStartSystem() {
  247.   systemRunning = true;
  248.   systemStartTime = millis();
  249.   updateDisplay("System Started");
  250.   startRelay();
  251.   server.send(200, "text/plain", "System started");
  252. }
  253.  
  254. // Handle stop system
  255. void handleStopSystem() {
  256.   systemRunning = false;
  257.   updateDisplay("System Stopped");
  258.   stopRelay();
  259.   server.send(200, "text/plain", "System stopped");
  260. }
  261.  
  262. // Start relay
  263. void startRelay() {
  264.   digitalWrite(RELAY_PIN, HIGH);
  265.   relayOn = true;
  266.   Serial.println("Relay ON");
  267. }
  268.  
  269. // Stop relay
  270. void stopRelay() {
  271.   digitalWrite(RELAY_PIN, LOW);
  272.   relayOn = false;
  273.   Serial.println("Relay OFF");
  274. }
  275.  
  276. // Open lid (simulate with message)
  277. void openLid() {
  278.   updateDisplay("Lid is Open");
  279.   lidOpened = true;
  280. }
  281.  
  282. // Close lid (simulate)
  283. void closeLid() {
  284.   updateDisplay("Lid Closed");
  285.   lidOpened = false;
  286. }
  287.  
  288. // Save settings from mobile (via web)
  289. void saveSettingsFromMobile() {
  290.   // Parse web request to update delays
  291. }
  292.  
Advertisement
Add Comment
Please, Sign In to add comment