pleasedontcode

ESP32 Essentials rev_02

Aug 13th, 2025
218
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: ESP32 Essentials
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-08-13 12:39:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp32 with i2c display,one relay out,one swich for */
  21.     /* start, one swich for stop, one swich for time */
  22.     /* setting up,one swich for time setting down,one */
  23.     /* ds18b20 temperature sensor. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - not working
  30.  
  31. ********* User code review feedback **********/
  32.  
  33. /* START CODE */
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <Wire.h>
  36. #include <Adafruit_SSD1306.h>
  37. #include <OneWire.h>
  38. #include <DallasTemperature.h>
  39.  
  40. // Define display parameters
  41. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  42. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  43. #define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  44.  
  45. // Instantiate display object
  46. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  47.  
  48. // Define pins for switches
  49. const int startSwitchPin = 2;   // Start button
  50. const int stopSwitchPin = 3;    // Stop button
  51. const int timeUpSwitchPin = 4;  // Time setting up
  52. const int timeDownSwitchPin = 5; // Time setting down
  53.  
  54. // Define pin for relay
  55. const int relayPin = 6;
  56.  
  57. // Define pin for DS18B20 temperature sensor
  58. const int oneWireBusPin = 7;
  59.  
  60. // Create OneWire and DallasTemperature objects
  61. OneWire oneWire(oneWireBusPin);
  62. DallasTemperature sensors(&oneWire);
  63.  
  64. // Variables for system state
  65. bool systemRunning = false;
  66. int temperature = 0;
  67. int setTimeMinutes = 0;
  68.  
  69. void setup() {
  70.   // Initialize serial communication
  71.   Serial.begin(115200);
  72.  
  73.   // Initialize display
  74.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
  75.     Serial.println(F("SSD1306 allocation failed"));
  76.     for(;;); // Loop forever
  77.   }
  78.   display.clearDisplay();
  79.   display.setTextSize(1);
  80.   display.setTextColor(SSD1306_WHITE);
  81.   display.setCursor(0,0);
  82.   display.println("Initializing...");
  83.   display.display();
  84.  
  85.   // Initialize pins
  86.   pinMode(startSwitchPin, INPUT_PULLUP);
  87.   pinMode(stopSwitchPin, INPUT_PULLUP);
  88.   pinMode(timeUpSwitchPin, INPUT_PULLUP);
  89.   pinMode(timeDownSwitchPin, INPUT_PULLUP);
  90.   pinMode(relayPin, OUTPUT);
  91.  
  92.   // Initialize relay
  93.   digitalWrite(relayPin, LOW); // Relay off
  94.  
  95.   // Initialize temperature sensor
  96.   sensors.begin();
  97.  
  98.   // Initial display update
  99.   updateDisplay();
  100. }
  101.  
  102. void loop() {
  103.   handleSwitches();
  104.   readTemperature();
  105.   updateDisplay();
  106.   delay(200); // Loop delay
  107. }
  108.  
  109. void handleSwitches() {
  110.   // Read switch states
  111.   bool startState = digitalRead(startSwitchPin) == LOW;
  112.   bool stopState = digitalRead(stopSwitchPin) == LOW;
  113.   bool timeUpState = digitalRead(timeUpSwitchPin) == LOW;
  114.   bool timeDownState = digitalRead(timeDownSwitchPin) == LOW;
  115.  
  116.   if (startState) {
  117.     systemRunning = true;
  118.     digitalWrite(relayPin, HIGH); // Turn relay on
  119.   }
  120.   if (stopState) {
  121.     systemRunning = false;
  122.     digitalWrite(relayPin, LOW); // Turn relay off
  123.   }
  124.   if (timeUpState) {
  125.     setTimeMinutes++;
  126.   }
  127.   if (timeDownState) {
  128.     if (setTimeMinutes > 0) {
  129.       setTimeMinutes--;
  130.     }
  131.   }
  132. }
  133.  
  134. void readTemperature() {
  135.   sensors.requestTemperatures();
  136.   temperature = sensors.getTempCByIndex(0);
  137. }
  138.  
  139. void updateDisplay() {
  140.   display.clearDisplay();
  141.   display.setCursor(0,0);
  142.   display.println("ESP32 I2C Display");
  143.   display.println("-----------------");
  144.   display.print("Temp: ");
  145.   display.print(temperature);
  146.   display.println(" C");
  147.   display.print("Set Time: ");
  148.   display.print(setTimeMinutes);
  149.   display.println(" min");
  150.   display.println(systemRunning ? "Status: RUNNING" : "Status: STOPPED");
  151.   display.display();
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment