#include <DHT.h>
#define DHT_SENSOR_PIN 4 // ESP32 pin GPIO21 connected to DHT22 sensor
#define DHT_SENSOR_TYPE DHT22
TaskHandle_t Task1;
TaskHandle_t Task2;
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
#include <Wire.h>
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
void Task1code( void * pvParameter);
void Task2code( void * pvParameter);
void setup() {
Serial.begin(9600);
dht_sensor.begin(); // initialize the DHT sensor
xTaskCreatePinnedToCore(
Task1code, /* Function to implement the task */
"Task1", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
&Task1, /* Task handle. */
0); /* Core where the task should run */
xTaskCreatePinnedToCore(
Task2code, /* Function to implement the task */
"Task2", /* Name of the task */
20000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
&Task2, /* Task handle. */
1); /* Core where the task should run */
Wire.begin(21, 22, 100000); // sda, scl, clock speed
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU−6050)
Wire.endTransmission(true);
Serial.println("Setup complete");
}
void loop() {
// Empty loop as tasks are being used.
}
void Task1code( void * pvParameter) {
Serial.print("Task1 running on core ");
Serial.print(xPortGetCoreID());
Serial.print(". |");
for(;;) {
// read humidity
float humi = dht_sensor.readHumidity();
// read temperature in Celsius
float tempC = dht_sensor.readTemperature();
// read temperature in Fahrenheit
float tempF = dht_sensor.readTemperature(true);
// check whether the reading is successful or not
if ( isnan(tempC) || isnan(tempF) || isnan(humi)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");
}
// wait a 2 seconds between readings
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void Task2code(void *pvParameter) {
Serial.print("Task2 running on core ");
Serial.print(xPortGetCoreID());
Serial.print(". |");
for (;;) {
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_ADDR, 14, true); // request a total of 14 registers
// read the sensor values
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
// print the sensor values
Serial.print("AcX = "); Serial.print(AcX);
Serial.print(" | AcY = "); Serial.print(AcY);
Serial.print(" | AcZ = "); Serial.print(AcZ);
Serial.print(" | Tmp = "); Serial.print(Tmp / 605.0 + 31); // convert to Celsius
Serial.print("C | GyX = "); Serial.print(GyX);
Serial.print(" | GyY = "); Serial.print(GyY);
Serial.print(" | GyZ = "); Serial.println(GyZ);
// wait for a second before the next reading
vTaskDelay(pdMS_TO_TICKS(1000));
}
}