/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include "DHTesp.h"
#include "Ticker.h"
#include <ESP32Servo.h>
#include <Stepper.h>
unsigned long interval=1000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
float h_temp = 0;
float h_hum = 0;
int pos = 0;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 26, 25, 33, 32);
const int DHT_PIN = 15;
const int servoPin = 18;
Servo servo;
DHTesp dhtSensor;
Ticker blink ;
//float h_temp;
bool ledState = false;
void dht(float h_temp,float h_hum){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float current_temperature = data.temperature;
float current_humidity = data.humidity;
Serial.println("Temp_current: " + String(current_temperature, 2) + "°C");
Serial.println("Humidity_current: " + String(current_humidity, 1) + "%");
Serial.println("---");
if(current_temperature > h_temp){
h_temp = current_temperature;
}
if(current_humidity > h_hum){
h_hum = current_humidity;
}
Serial.println("Temp_highest: " + String(h_temp, 2) + "°C");
Serial.println("Humidity_highest: " + String(h_hum, 1) + "%");
Serial.println("---");
//delay(4000);
dht( h_temp,h_hum);
}
void led(){
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(15);
}
}
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
servo.attach(servoPin, 500, 2400);
myStepper.setSpeed(60);
// TempAndHumidity data = dhtSensor.getTempAndHumidity();
// float current_temperature = data.temperature;
// float current_humidity = data.humidity;
// Serial.println("Temp: " + String(current_temperature, 2) + "°C");
// Serial.println("Humidity: " + String(current_humidity, 1) + "%");
// Serial.println("---");
// float h_temp = current_temperature;
// float h_hum = current_humidity;
pinMode(2, OUTPUT);
digitalWrite(2, ledState);
//led();
//dht(current_temperature,current_humidity);
blink.attach(1,led);
}
void loop() {
unsigned long currentMillis = millis(); // grab current time
// check if "interval" time has passed (1000 milliseconds)
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
float current_temperature = data.temperature;
float current_humidity = data.humidity;
Serial.println("Temp_current: " + String(current_temperature, 2) + "°C");
Serial.println("Humidity_current: " + String(current_humidity, 1) + "%");
Serial.println("---");
if(current_temperature > h_temp){
h_temp = current_temperature;
}
if(current_humidity > h_hum){
h_hum = current_humidity;
}
Serial.println("Temp_highest: " + String(h_temp, 2) + "°C");
Serial.println("Humidity_highest: " + String(h_hum, 1) + "%");
Serial.println("---");
ledState = !ledState; // "toggles" the state
digitalWrite(2, ledState); // sets the LED based on ledState
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
previousMillis = millis();
}
}