pleasedontcode

Embedded Control rev_03

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