Advertisement
pleasedontcode

Smart Climate rev_01

Sep 28th, 2024
89
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: Smart Climate
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-09-28 18:53:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Connect to wifi, print ip address on first line of */
  21.     /* display, monitor temperatures and humidity for all */
  22.     /* 3 zones and print each sensor on a new line,  when */
  23.     /* temperature lower than 21C, activate corresponding */
  24.     /* relay, when temp over 23 C, turn off the relay */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* emulate a webserver that will print values for */
  27.     /* each zone and include a toggle switch and for each */
  28.     /* relay, in the webpage add the ability to turn on */
  29.     /* relays based on temperature or a timer */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <Wire.h>
  34. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  35. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  36. #include <Adafruit_SSD1306.h> // Include the Adafruit SSD1306 library
  37. #include <WiFi.h> // Include WiFi library
  38. #include <WebServer.h> // Include WebServer library
  39. #include <DHT.h> // Include DHT sensor library
  40.  
  41. /****** FUNCTION PROTOTYPES *****/
  42. void setup(void);
  43. void loop(void);
  44. void updateOutputs();
  45. void handleRoot(); // Function to handle root URL
  46. void readSensors(); // Function to read temperature and humidity
  47.  
  48. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  49. const uint8_t Sensor1_DHT11_DOUT_PIN_D4     = 4;
  50. const uint8_t Sensor2_DHT11_DOUT_PIN_D13        = 13;
  51.  
  52. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  53. const uint8_t Relay1_RelayModule_Signal_PIN_D16     = 16;
  54. const uint8_t Relay2_RelayModule_Signal_PIN_D17     = 17;
  55.  
  56. /***** DEFINITION OF I2C PINS *****/
  57. const uint8_t Display1_SSD1306OledDisplay_I2C_PIN_SDA_D21       = 21;
  58. const uint8_t Display1_SSD1306OledDisplay_I2C_PIN_SCL_D22       = 22;
  59. const uint8_t Display1_SSD1306OledDisplay_I2C_SLAVE_ADDRESS     = 60;
  60.  
  61. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  62. bool Relay1_RelayModule_Signal_PIN_D16_rawData      = 0;
  63. bool Relay2_RelayModule_Signal_PIN_D17_rawData      = 0;
  64.  
  65. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  66. float Relay1_RelayModule_Signal_PIN_D16_phyData     = 0.0;
  67. float Relay2_RelayModule_Signal_PIN_D17_phyData     = 0.0;
  68.  
  69. // DHT sensor instances
  70. DHT dht1(Sensor1_DHT11_DOUT_PIN_D4, DHT11);
  71. DHT dht2(Sensor2_DHT11_DOUT_PIN_D13, DHT11);
  72.  
  73. // Create relay instances for Relay1 and Relay2
  74. Relay relay1(Relay1_RelayModule_Signal_PIN_D16, 1); // Relay on pin 16
  75. Relay relay2(Relay2_RelayModule_Signal_PIN_D17, 1); // Relay on pin 17
  76.  
  77. // Create an instance of the Adafruit SSD1306 display
  78. Adafruit_SSD1306 display(Display1_SSD1306OledDisplay_I2C_SLAVE_ADDRESS, Display1_SSD1306OledDisplay_I2C_PIN_SDA_D21, Display1_SSD1306OledDisplay_I2C_PIN_SCL_D22);
  79.  
  80. // Create an instance of the U8G2_FOR_ADAFRUIT_GFX class
  81. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
  82.  
  83. // WiFi credentials
  84. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  85. const char* password = "your_PASSWORD"; // Replace with your WiFi Password
  86.  
  87. // Web server instance
  88. WebServer server(80);
  89.  
  90. // Variables to store sensor readings
  91. float temperature1, humidity1;
  92. float temperature2, humidity2;
  93.  
  94. void setup(void)
  95. {
  96.     // Initialize serial communication
  97.     Serial.begin(115200);
  98.  
  99.     // Connect to WiFi
  100.     WiFi.begin(ssid, password);
  101.     while (WiFi.status() != WL_CONNECTED) {
  102.         delay(1000);
  103.         Serial.println("Connecting to WiFi...");
  104.     }
  105.     Serial.println("Connected to WiFi");
  106.     Serial.print("IP Address: ");
  107.     Serial.println(WiFi.localIP());
  108.  
  109.     // Initialize the display
  110.     display.begin(SSD1306_SWITCHCAPVCC); // Initialize the SSD1306 display
  111.     u8g2_for_adafruit_gfx.begin(display); // Connect u8g2 procedures to Adafruit GFX
  112.  
  113.     // Initialize relays
  114.     relay1.setRelayMode(relayModeAutomatic); // Set relay1 to automatic mode
  115.     relay2.setRelayMode(relayModeAutomatic); // Set relay2 to automatic mode
  116.  
  117.     // Initialize DHT sensors
  118.     dht1.begin();
  119.     dht2.begin();
  120.  
  121.     // Set up web server routes
  122.     server.on("/", handleRoot);
  123.     server.begin();
  124. }
  125.  
  126. void loop(void)
  127. {
  128.     // Refresh output data
  129.     updateOutputs();
  130.     relay1.loop(); // Call loop function for relay1
  131.     relay2.loop(); // Call loop function for relay2
  132.  
  133.     // Read sensors
  134.     readSensors();
  135.  
  136.     // Example of using the display
  137.     display.clearDisplay(); // Clear the display
  138.     u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf); // Set font
  139.     u8g2_for_adafruit_gfx.setCursor(0, 20); // Set cursor position
  140.     u8g2_for_adafruit_gfx.print("IP: ");
  141.     u8g2_for_adafruit_gfx.print(WiFi.localIP()); // Print IP address
  142.     u8g2_for_adafruit_gfx.setCursor(0, 40); // Set cursor position
  143.     u8g2_for_adafruit_gfx.print("T1: "); u8g2_for_adafruit_gfx.print(temperature1); u8g2_for_adafruit_gfx.print("C");
  144.     u8g2_for_adafruit_gfx.setCursor(0, 60); // Set cursor position
  145.     u8g2_for_adafruit_gfx.print("T2: "); u8g2_for_adafruit_gfx.print(temperature2); u8g2_for_adafruit_gfx.print("C");
  146.     display.display(); // Display the content
  147.     delay(2000); // Wait for 2 seconds
  148.  
  149.     // Handle client requests
  150.     server.handleClient();
  151. }
  152.  
  153. void updateOutputs()
  154. {
  155.     // Control relays based on temperature thresholds
  156.     if (temperature1 < 21) {
  157.         Relay1_RelayModule_Signal_PIN_D16_rawData = 1; // Turn on relay 1
  158.     } else if (temperature1 > 23) {
  159.         Relay1_RelayModule_Signal_PIN_D16_rawData = 0; // Turn off relay 1
  160.     }
  161.  
  162.     if (temperature2 < 21) {
  163.         Relay2_RelayModule_Signal_PIN_D17_rawData = 1; // Turn on relay 2
  164.     } else if (temperature2 > 23) {
  165.         Relay2_RelayModule_Signal_PIN_D17_rawData = 0; // Turn off relay 2
  166.     }
  167.  
  168.     digitalWrite(Relay1_RelayModule_Signal_PIN_D16, Relay1_RelayModule_Signal_PIN_D16_rawData);
  169.     digitalWrite(Relay2_RelayModule_Signal_PIN_D17, Relay2_RelayModule_Signal_PIN_D17_rawData);
  170. }
  171.  
  172. void readSensors() {
  173.     // Read temperature and humidity from DHT sensors
  174.     humidity1 = dht1.readHumidity();
  175.     temperature1 = dht1.readTemperature();
  176.     humidity2 = dht2.readHumidity();
  177.     temperature2 = dht2.readTemperature();
  178. }
  179.  
  180. void handleRoot() {
  181.     // Handle root URL and send HTML response
  182.     server.send(200, "text/html", "<h1>Temperature and Humidity</h1><p>Zone 1: T: " + String(temperature1) + "C, H: " + String(humidity1) + "%</p><p>Zone 2: T: " + String(temperature2) + "C, H: " + String(humidity2) + "%</p>");
  183. }
  184.  
  185. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement