Advertisement
pleasedontcode

"Thermostat Control" rev_02

Jan 21st, 2025
45
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: "Thermostat Control"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-21 14:47:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* a thermostat with an offset on and offset off */
  21.     /* logic using Freertos.  The thermostat works */
  22.     /* according to the recipe, which is stored in the */
  23.     /* EEprom memory. The recipe has the name, the step */
  24.     /* number, the step temperature, and the step time in */
  25.     /* minutes. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <DS18B20.h>    // https://github.com/matmunk/DS18B20
  32. #include <OneWire.h>
  33. #include <EEPROM.h>
  34. #include <FreeRTOS.h>
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39. void thermostatTask(void *pvParameters);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4;
  43.  
  44. /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  45. #define ONE_WIRE_BUS 2
  46. OneWire oneWire(ONE_WIRE_BUS);
  47. DS18B20 sensor(&oneWire);
  48.  
  49. // Recipe structure
  50. struct RecipeStep {
  51.     String name;
  52.     float temperature;
  53.     int time; // in minutes
  54. };
  55.  
  56. // Example recipe
  57. RecipeStep recipe[] = {
  58.     {"Step 1", 25.0, 10},
  59.     {"Step 2", 30.0, 15},
  60.     {"Step 3", 35.0, 20}
  61. };
  62.  
  63. // Current step index
  64. int currentStep = 0;
  65.  
  66. // Offset values
  67. float offsetOn = 0.5;
  68. float offsetOff = 0.5;
  69.  
  70. void setup(void)
  71. {
  72.     Serial.begin(115200);
  73.     pinMode(Temp_Sensor_DS18B20_DQ_PIN_D4, INPUT);
  74.     sensor.begin();
  75.  
  76.     // Create FreeRTOS task for thermostat
  77.     xTaskCreate(thermostatTask, "ThermostatTask", 2048, NULL, 1, NULL);
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // Main loop can remain empty as the thermostat logic is handled in the FreeRTOS task
  83. }
  84.  
  85. void thermostatTask(void *pvParameters)
  86. {
  87.     while (true) {
  88.         sensor.requestTemperatures();
  89.         float currentTemp = sensor.getTempC();
  90.  
  91.         // Check if we need to turn on or off the thermostat
  92.         if (currentStep < sizeof(recipe) / sizeof(recipe[0])) {
  93.             if (currentTemp < (recipe[currentStep].temperature - offsetOn)) {
  94.                 // Turn on heating
  95.                 Serial.println("Heating ON");
  96.             } else if (currentTemp > (recipe[currentStep].temperature + offsetOff)) {
  97.                 // Turn off heating
  98.                 Serial.println("Heating OFF");
  99.                 currentStep++; // Move to the next step
  100.             }
  101.         } else {
  102.             Serial.println("Recipe completed.");
  103.             vTaskDelete(NULL); // Delete the task if the recipe is completed
  104.         }
  105.  
  106.         // Delay for a while before the next temperature check
  107.         vTaskDelay(pdMS_TO_TICKS(1000));
  108.     }
  109. }
  110.  
  111. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement