pleasedontcode

Relay Controller rev_04

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