Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Embedded Control
- - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
- - Source Code created on: 2025-08-13 12:15:41
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* esp12f with i2c display timer,press start relay on */
- /* hold set time.after delay another delay time as */
- /* per set start,after this lid open a message on */
- /* display and off all.2 swich to up down time */
- /* setting,another one swich to stop.all setting from */
- /* mobile */
- /****** END SYSTEM REQUIREMENTS *****/
- /********* User code review feedback **********
- #### Feedback 1 ####
- - ssd name is esp32,password is 12345678
- #### Feedback 2 ####
- - wifi not seen
- #### Feedback 3 ####
- - Compilation error: 'esp' was not declared in this scope; did you
- mean 'exp'?
- ********* User code review feedback **********/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <DS18B20.h>
- #include <Wire.h> // For I2C display
- #include <LiquidCrystal_I2C.h> // For I2C LCD display
- #include <ESP8266WiFi.h> // For WiFi connectivity
- #include <ESP8266WebServer.h> // For mobile control via web
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeDisplay();
- void updateDisplay(const String& message);
- void handleWebServer();
- void startRelay();
- void stopRelay();
- void openLid();
- void closeLid();
- void saveSettingsFromMobile();
- void handleSetDelays();
- void handleStartSystem();
- void handleStopSystem();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t DS18B20_DQ_PIN = 1; // D1 pin on NodeMCU
- // Pins for switches/buttons
- const uint8_t BUTTON_START_PIN = D2; // GPIO 4
- const uint8_t BUTTON_UP_PIN = D3; // GPIO 0
- const uint8_t BUTTON_DOWN_PIN = D4; // GPIO 2
- const uint8_t BUTTON_STOP_PIN = D5; // GPIO 14
- // Relay control pin
- const uint8_t RELAY_PIN = D6; // GPIO 12
- // Lid sensor (optional)
- const uint8_t LID_SENSOR_PIN = D7; // GPIO 13
- //***** LIBRARIES CLASS INSTANCES *****/
- // DS18B20 instance
- DS18B20 temperatureSensor(DS18B20_DQ_PIN);
- // LCD display instance (assuming 16x2 LCD with I2C address 0x27)
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- // Web server for mobile control
- ESP8266WebServer server(80);
- // Timer variables
- unsigned long startDelayMillis = 0;
- unsigned long holdDelayMillis = 0;
- unsigned long startDelayDuration = 5000; // default 5 seconds
- unsigned long holdDelayDuration = 10000; // default 10 seconds
- // State variables
- bool relayOn = false;
- bool lidOpened = false;
- bool systemRunning = false;
- unsigned long systemStartTime = 0;
- // Mobile settings (to be set via web interface)
- unsigned long userSetStartDelay = 5000; // default 5 seconds
- unsigned long userSetHoldDelay = 10000; // default 10 seconds
- // WiFi credentials
- const char* ssid = "esp12f";
- const char* password = "12345678";
- // Function to initialize display
- void initializeDisplay() {
- lcd.init();
- lcd.backlight();
- updateDisplay("System Ready");
- }
- // Function to update display message
- void updateDisplay(const String& message) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(message);
- }
- // Setup function
- void setup() {
- // Initialize serial for debugging
- Serial.begin(9600);
- // Initialize pins
- pinMode(DS18B20_DQ_PIN, INPUT);
- pinMode(BUTTON_START_PIN, INPUT_PULLUP);
- pinMode(BUTTON_UP_PIN, INPUT_PULLUP);
- pinMode(BUTTON_DOWN_PIN, INPUT_PULLUP);
- pinMode(BUTTON_STOP_PIN, INPUT_PULLUP);
- pinMode(RELAY_PIN, OUTPUT);
- pinMode(LID_SENSOR_PIN, INPUT_PULLUP);
- // Initialize display
- initializeDisplay();
- // Connect to WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("\nWiFi connected");
- updateDisplay("WiFi Connected");
- // Setup web server routes
- server.on("/", handleWebServer);
- server.on("/setDelays", handleSetDelays);
- server.on("/start", handleStartSystem);
- server.on("/stop", handleStopSystem);
- server.begin();
- // Initialize other variables
- startDelayMillis = millis();
- holdDelayMillis = millis();
- }
- // Loop function
- void loop() {
- // Handle web server
- handleWebServer();
- // Read buttons
- if (digitalRead(BUTTON_START_PIN) == LOW) {
- // Start system
- systemRunning = true;
- systemStartTime = millis();
- updateDisplay("System Started");
- startRelay();
- }
- if (digitalRead(BUTTON_STOP_PIN) == LOW) {
- // Stop system
- systemRunning = false;
- updateDisplay("System Stopped");
- stopRelay();
- }
- if (digitalRead(BUTTON_UP_PIN) == LOW) {
- // Increase start delay
- userSetStartDelay += 1000; // increase by 1 sec
- Serial.print("Start Delay: ");
- Serial.println(userSetStartDelay);
- }
- if (digitalRead(BUTTON_DOWN_PIN) == LOW) {
- // Decrease start delay
- if (userSetStartDelay > 1000) {
- userSetStartDelay -= 1000; // decrease by 1 sec
- }
- Serial.print("Start Delay: ");
- Serial.println(userSetStartDelay);
- }
- // Main logic
- if (systemRunning) {
- unsigned long currentTime = millis();
- // Check if delay before starting relay
- if ((currentTime - systemStartTime) >= userSetStartDelay && !relayOn) {
- startRelay();
- }
- // After relay is on for hold delay, open lid and turn off relay
- if (relayOn && (currentTime - (systemStartTime + userSetStartDelay)) >= userSetHoldDelay) {
- openLid();
- stopRelay();
- updateDisplay("Lid Opened");
- }
- // Optional: monitor lid sensor or other conditions
- if (digitalRead(LID_SENSOR_PIN) == LOW && !lidOpened) {
- // Lid opened
- lidOpened = true;
- updateDisplay("Lid is Open");
- }
- }
- // Read temperature
- float tempC = temperatureSensor.getTempC();
- Serial.print("Temperature: ");
- Serial.print(tempC);
- Serial.println(" C");
- delay(100); // loop delay
- }
- // Handle web server requests
- void handleWebServer() {
- server.handleClient();
- // Additional web interface handling can be added here
- }
- // Handle setting delays via web
- void handleSetDelays() {
- if (server.hasArg("startDelay")) {
- unsigned long delayVal = server.arg("startDelay").toInt();
- userSetStartDelay = delayVal;
- }
- if (server.hasArg("holdDelay")) {
- unsigned long delayVal = server.arg("holdDelay").toInt();
- userSetHoldDelay = delayVal;
- }
- server.send(200, "text/plain", "Delays updated");
- }
- // Handle start system
- void handleStartSystem() {
- systemRunning = true;
- systemStartTime = millis();
- updateDisplay("System Started");
- startRelay();
- server.send(200, "text/plain", "System started");
- }
- // Handle stop system
- void handleStopSystem() {
- systemRunning = false;
- updateDisplay("System Stopped");
- stopRelay();
- server.send(200, "text/plain", "System stopped");
- }
- // Start relay
- void startRelay() {
- digitalWrite(RELAY_PIN, HIGH);
- relayOn = true;
- Serial.println("Relay ON");
- }
- // Stop relay
- void stopRelay() {
- digitalWrite(RELAY_PIN, LOW);
- relayOn = false;
- Serial.println("Relay OFF");
- }
- // Open lid (simulate with message)
- void openLid() {
- updateDisplay("Lid is Open");
- lidOpened = true;
- }
- // Close lid (simulate)
- void closeLid() {
- updateDisplay("Lid Closed");
- lidOpened = false;
- }
- // Save settings from mobile (via web)
- void saveSettingsFromMobile() {
- // Parse web request to update delays
- }
Advertisement
Add Comment
Please, Sign In to add comment