pleasedontcode

Embedded Control rev_06

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