/** * DHT11 Sensor, Analog Standard Servo and LED strip APA102 * This sketch reads temperature and humidity data from the DHT11 sensor and prints the values to the serial port. * It moves servo based on humidity and sends data to serial plotter * It turns on LED strip based on temperature */ // Include the DHT11 library for interfacing with the sensor. Include the ESP32 library to interface with the servo. Include the FastLed to interface with the LED strip. #include #include #include #include #include #include LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows //Define pins #define DHTPIN 4 #define SERVO_PIN 9 #define DATA_PIN 20 #define NUM_LEDS 40 // Create Servo instance Servo myservo; // Create LED strip Adafruit_NeoPixel pixels(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800); // Create an instance of the DHT11 class. // - For Arduino: Connect the sensor to Digital I/O Pin 2. // - For ESP32-C3: Connect the sensor to pin 4. // - For ATtiny: pin = 0 DHT11 dht11(4); const char * ssid = "NIELS Arbejdsmobil"; const char * password = "Leakris1"; //const char* ssid = "Niels OnePlus"; //const char* password = "Leakris1"; String server = "http://maker.ifttt.com"; String eventName = "The_Fish"; String IFTTT_Key = "gYJ3Qq0AjbhTHxH0ETlQ2"; String IFTTTUrl = "https://maker.ifttt.com/trigger/The_Fish/with/key/gYJ3Qq0AjbhTHxH0ETlQ2"; int value1; int value2; int value3; unsigned long previousMillis = 0; unsigned long previousMillisPrint = 0; void setup() { // Uncomment/edit one of the following lines for your leds arrangement. // ## Clockless types ## pixels.begin(); //FastLED.addLeds(leds, NUM_LEDS); // BGR ordering is typical // Initialize serial communication to allow debugging and data readout. // Using a baud rate of 9600 bps. Serial.begin(9600); int pos = 0; // variable to store the servo position //For ATtiny1614, pin:10 //ForESP32-C3, pin:9 //For ESP-C3 small board, pin:3 myservo.attach(9); // attaches the servo on pin 9 to the servo object WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Viola, Connected !!!"); //LCD lcd.init(); // initialize the lcd lcd.backlight(); } void loop() { unsigned long currentMillis = millis(); int temperature = 0; int humidity = 0; // Attempt to read the temperature and humidity values from the DHT11 sensor. int result = dht11.readTemperatureHumidity(temperature, humidity); // Check the results of the readings. // If the reading is successful, print the temperature and humidity values. // If there are errors, print the appropriate error messages. if (result == 0) { Serial.print("Temperature: "); Serial.print(temperature); Serial.print(" °C\tHumidity: "); Serial.print(humidity); Serial.println(" %"); value1 = temperature; value2 = humidity; } else { // Print error message based on the error code. Serial.println(DHT11::getErrorString(result)); } //LCD displays humidity and temperature lcd.clear(); // check whether the reading is successful or not if (isnan(temperature) || isnan(humidity)) { lcd.setCursor(0, 0); lcd.print("Failed"); } else { lcd.setCursor(0, 0); // display position lcd.print("Temp: "); lcd.print(temperature); // display the temperature //lcd.print("°C"); lcd.print((char)223); lcd.setCursor(0, 1); // display position lcd.print("Humi: "); lcd.print(humidity); // display the humidity lcd.print("%"); } // Move the servo motor based on humidity if (humidity >= 60) { for (int pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 90 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(50); // waits 15ms for the servo to reach the position } if (humidity <= 58) { for (int pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } } //Turn LED on based on temperature. 18 - 23 = blue. 24 - 27 = Green. 28 - 31 = Red if (temperature >= 18 && temperature <= 23) { setLedsColor(0, 0, 255); } else if (temperature >= 24 && temperature <= 27) { setLedsColor(0, 255, 0); } else if (temperature >= 28 && temperature <= 31) { setLedsColor(255, 0, 0); } else { pixels.clear(); } pixels.show(); if (currentMillis - previousMillis >= 10000) { Serial.println("sending"); // save the last time you blinked the LED previousMillis = currentMillis; sendDataToSheet(); } delay(1000); } void setLedsColor(int R, int G, int B) { for (int i = 0; i < NUM_LEDS; i++) { pixels.setPixelColor(i, pixels.Color(R, G, B)); } } void printData(int t, int h) { Serial.print("Temperature: "); Serial.print(t); Serial.print(" °C\tHumidity: "); Serial.print(h); Serial.println(" %"); } void sendDataToSheet(void) { String url = server + "/trigger/" + eventName + "/with/key/" + IFTTT_Key + "?value1=" + String((int)value1) + "&value2=" + String((int)value2) + "&value3=" + String((int)value3); Serial.println(url); //Start to send data to IFTTT HTTPClient http; Serial.print("[HTTP] begin...\n"); http.begin(url); //HTTP Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); // httpCode will be negative on error if (httpCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); }