Dtynin
Dtynin
DIIDevHeads IoT Integration Server
Created by Dtynin on 8/20/2024 in #code-review
Initialize AVR32UC with Zephyr OS for DHT22 Sensor Communication and Resolve Data Fetch Failure
Hey guys, how can I initialize the AVR32UC microcontroller with Zephyr OS to communicate with the DHT22 temperature and humidity sensor, and address the issue of failing to fetch sensor data? I have been able to configure Zephyr OS for the AVR32UC and ensured all necessary drivers are enabled. The DHT22 sensor is connected correctly, and the initial code for reading temperature and humidity data is implemented. However, the microcontroller still fails to fetch data from the DHT22. what could be wrong? here is my code snippet:
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <sys/printk.h>

const struct device *dht22_dev;

void main(void)
{
printk("System initialized successfully.\n");

dht22_dev = device_get_binding(DT_LABEL(DT_INST(0, aosong_dht22)));
if (!dht22_dev) {
printk("DHT22: Device driver not found.\n");
return;
}

struct sensor_value temp, hum;

if (sensor_sample_fetch(dht22_dev) < 0) {
printk("DHT22: Failed to fetch data.\n");
return;
}

sensor_channel_get(dht22_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
sensor_channel_get(dht22_dev, SENSOR_CHAN_HUMIDITY, &hum);

printk("Temperature: %d.%d C\n", temp.val1, temp.val2);
printk("Humidity: %d.%d %%\n", hum.val1, hum.val2);
}
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <sys/printk.h>

const struct device *dht22_dev;

void main(void)
{
printk("System initialized successfully.\n");

dht22_dev = device_get_binding(DT_LABEL(DT_INST(0, aosong_dht22)));
if (!dht22_dev) {
printk("DHT22: Device driver not found.\n");
return;
}

struct sensor_value temp, hum;

if (sensor_sample_fetch(dht22_dev) < 0) {
printk("DHT22: Failed to fetch data.\n");
return;
}

sensor_channel_get(dht22_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
sensor_channel_get(dht22_dev, SENSOR_CHAN_HUMIDITY, &hum);

printk("Temperature: %d.%d C\n", temp.val1, temp.val2);
printk("Humidity: %d.%d %%\n", hum.val1, hum.val2);
}
If the implementation is successful, the AVR32UC microcontroller should initialize successfully with Zephyr OS and communicate with the DHT22 sensor, providing accurate temperature and humidity readings.
2 replies