Advertisement
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: Smart Climate
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-09-28 18:53:06
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Connect to wifi, print ip address on first line of */
- /* display, monitor temperatures and humidity for all */
- /* 3 zones and print each sensor on a new line, when */
- /* temperature lower than 21C, activate corresponding */
- /* relay, when temp over 23 C, turn off the relay */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* emulate a webserver that will print values for */
- /* each zone and include a toggle switch and for each */
- /* relay, in the webpage add the ability to turn on */
- /* relays based on temperature or a timer */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <Relay.h> //https://github.com/rafaelnsantos/Relay
- #include <U8g2_for_Adafruit_GFX.h> //https://github.com/olikraus/U8g2_for_Adafruit_GFX
- #include <Adafruit_SSD1306.h> // Include the Adafruit SSD1306 library
- #include <WiFi.h> // Include WiFi library
- #include <WebServer.h> // Include WebServer library
- #include <DHT.h> // Include DHT sensor library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs();
- void handleRoot(); // Function to handle root URL
- void readSensors(); // Function to read temperature and humidity
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Sensor1_DHT11_DOUT_PIN_D4 = 4;
- const uint8_t Sensor2_DHT11_DOUT_PIN_D13 = 13;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t Relay1_RelayModule_Signal_PIN_D16 = 16;
- const uint8_t Relay2_RelayModule_Signal_PIN_D17 = 17;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t Display1_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21;
- const uint8_t Display1_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22;
- const uint8_t Display1_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool Relay1_RelayModule_Signal_PIN_D16_rawData = 0;
- bool Relay2_RelayModule_Signal_PIN_D17_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float Relay1_RelayModule_Signal_PIN_D16_phyData = 0.0;
- float Relay2_RelayModule_Signal_PIN_D17_phyData = 0.0;
- // DHT sensor instances
- DHT dht1(Sensor1_DHT11_DOUT_PIN_D4, DHT11);
- DHT dht2(Sensor2_DHT11_DOUT_PIN_D13, DHT11);
- // Create relay instances for Relay1 and Relay2
- Relay relay1(Relay1_RelayModule_Signal_PIN_D16, 1); // Relay on pin 16
- Relay relay2(Relay2_RelayModule_Signal_PIN_D17, 1); // Relay on pin 17
- // Create an instance of the Adafruit SSD1306 display
- Adafruit_SSD1306 display(Display1_SSD1306OledDisplay_I2C_SLAVE_ADDRESS, Display1_SSD1306OledDisplay_I2C_PIN_SDA_D21, Display1_SSD1306OledDisplay_I2C_PIN_SCL_D22);
- // Create an instance of the U8G2_FOR_ADAFRUIT_GFX class
- U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
- // WiFi credentials
- const char* ssid = "your_SSID"; // Replace with your WiFi SSID
- const char* password = "your_PASSWORD"; // Replace with your WiFi Password
- // Web server instance
- WebServer server(80);
- // Variables to store sensor readings
- float temperature1, humidity1;
- float temperature2, humidity2;
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(115200);
- // Connect to WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP());
- // Initialize the display
- display.begin(SSD1306_SWITCHCAPVCC); // Initialize the SSD1306 display
- u8g2_for_adafruit_gfx.begin(display); // Connect u8g2 procedures to Adafruit GFX
- // Initialize relays
- relay1.setRelayMode(relayModeAutomatic); // Set relay1 to automatic mode
- relay2.setRelayMode(relayModeAutomatic); // Set relay2 to automatic mode
- // Initialize DHT sensors
- dht1.begin();
- dht2.begin();
- // Set up web server routes
- server.on("/", handleRoot);
- server.begin();
- }
- void loop(void)
- {
- // Refresh output data
- updateOutputs();
- relay1.loop(); // Call loop function for relay1
- relay2.loop(); // Call loop function for relay2
- // Read sensors
- readSensors();
- // Example of using the display
- display.clearDisplay(); // Clear the display
- u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf); // Set font
- u8g2_for_adafruit_gfx.setCursor(0, 20); // Set cursor position
- u8g2_for_adafruit_gfx.print("IP: ");
- u8g2_for_adafruit_gfx.print(WiFi.localIP()); // Print IP address
- u8g2_for_adafruit_gfx.setCursor(0, 40); // Set cursor position
- u8g2_for_adafruit_gfx.print("T1: "); u8g2_for_adafruit_gfx.print(temperature1); u8g2_for_adafruit_gfx.print("C");
- u8g2_for_adafruit_gfx.setCursor(0, 60); // Set cursor position
- u8g2_for_adafruit_gfx.print("T2: "); u8g2_for_adafruit_gfx.print(temperature2); u8g2_for_adafruit_gfx.print("C");
- display.display(); // Display the content
- delay(2000); // Wait for 2 seconds
- // Handle client requests
- server.handleClient();
- }
- void updateOutputs()
- {
- // Control relays based on temperature thresholds
- if (temperature1 < 21) {
- Relay1_RelayModule_Signal_PIN_D16_rawData = 1; // Turn on relay 1
- } else if (temperature1 > 23) {
- Relay1_RelayModule_Signal_PIN_D16_rawData = 0; // Turn off relay 1
- }
- if (temperature2 < 21) {
- Relay2_RelayModule_Signal_PIN_D17_rawData = 1; // Turn on relay 2
- } else if (temperature2 > 23) {
- Relay2_RelayModule_Signal_PIN_D17_rawData = 0; // Turn off relay 2
- }
- digitalWrite(Relay1_RelayModule_Signal_PIN_D16, Relay1_RelayModule_Signal_PIN_D16_rawData);
- digitalWrite(Relay2_RelayModule_Signal_PIN_D17, Relay2_RelayModule_Signal_PIN_D17_rawData);
- }
- void readSensors() {
- // Read temperature and humidity from DHT sensors
- humidity1 = dht1.readHumidity();
- temperature1 = dht1.readTemperature();
- humidity2 = dht2.readHumidity();
- temperature2 = dht2.readTemperature();
- }
- void handleRoot() {
- // Handle root URL and send HTML response
- 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>");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement