pleasedontcode

Embedded Controller rev_02

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