How to Fetch and Display Data from DHT22 Sensor on AVR32UC with Zephyr OS?

hey guys, still trying to advance on my work, I've been working on fetching and displaying data from the DHT22 sensor using the AVR32UC microcontroller, but I'm encountering a persistent data fetch failure. Here’s the code I’ve implemented:
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <sys/printk.h>

const struct device *sensor_dev;

void main(void)
{
struct sensor_value temp, humidity;

sensor_dev = device_get_binding(DT_LABEL(DT_INST(0, ams_dht22)));
if (!sensor_dev) {
printk("Failed to bind DHT22 sensor\n");
return;
}

if (sensor_sample_fetch(sensor_dev) < 0) {
printk("Failed to fetch data\n");
return;
}

sensor_channel_get(sensor_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
sensor_channel_get(sensor_dev, SENSOR_CHAN_HUMIDITY, &humidity);

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

const struct device *sensor_dev;

void main(void)
{
struct sensor_value temp, humidity;

sensor_dev = device_get_binding(DT_LABEL(DT_INST(0, ams_dht22)));
if (!sensor_dev) {
printk("Failed to bind DHT22 sensor\n");
return;
}

if (sensor_sample_fetch(sensor_dev) < 0) {
printk("Failed to fetch data\n");
return;
}

sensor_channel_get(sensor_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
sensor_channel_get(sensor_dev, SENSOR_CHAN_HUMIDITY, &humidity);

printk("Temperature: %d.%d C\n", temp.val1, temp.val2);
printk("Humidity: %d.%d %%\n", humidity.val1, humidity.val2);
}
regardless of confirming the sensor's physical connections, verifying its initialization, ensuring the DHT22 sensor driver is correctly configured in prj.conf, and checking the device tree (dts file) for accurate sensor definitions, I still receive the error Failed to fetch data. How can I successfully fetch and display temperature and humidity data from the DHT22 sensor on the AVR32UC microcontroller?
Solution:
finally gushh😂 , I choked it, I enabled detailed logging, which revealed an I/O error. I then double-checked the GPIO configuration, verified d pin setup, and added a pull-up resistor. After increasing delays and adding debug prints in d driver code, I finally moved the sensor to a different GPIO pin. I think I'm making progress in this, the sensor is now successfully fetching and displaying data. see this ```[00:00:00.000,000] <inf> dht: DHT driver initialized [00:00:00.000,001] <dbg> dht: Starting data fetch [00:00:00.000,002] <dbg> dht: Starting measurement [00:00:00.000,003] <dbg> dht: Reading data Temperature: 25.4 C...
Jump to solution
9 Replies
Sterling
Sterling4mo ago
@Dtynin Have you tried adding a debug prints inside the driver code to trace where the failure occurs. It's necessary to know if the driver is correctly communicating with the sensor.
Enthernet Code
Enthernet Code4mo ago
hello @Dtynin did u wrap ur queue operations with mutex locks. something like this 👇
SemaphoreHandle_t xMutex;

xMutex = xSemaphoreCreateMutex();

void readSensorTask(void *pvParameters) {
sensor_data_t sensorData;
for (;;) {

if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
xQueueSend(sensorQueue, &sensorData, portMAX_DELAY);
xSemaphoreGive(xMutex);
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

void processDataTask(void *pvParameters) {
sensor_data_t sensorData;
for (;;) {
if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
if (xQueueReceive(sensorQueue, &sensorData, portMAX_DELAY)) {

}
xSemaphoreGive(xMutex);
}
vTaskDelay(pdMS_TO_TICKS(10000));
}
}
SemaphoreHandle_t xMutex;

xMutex = xSemaphoreCreateMutex();

void readSensorTask(void *pvParameters) {
sensor_data_t sensorData;
for (;;) {

if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
xQueueSend(sensorQueue, &sensorData, portMAX_DELAY);
xSemaphoreGive(xMutex);
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}

void processDataTask(void *pvParameters) {
sensor_data_t sensorData;
for (;;) {
if (xSemaphoreTake(xMutex, portMAX_DELAY)) {
if (xQueueReceive(sensorQueue, &sensorData, portMAX_DELAY)) {

}
xSemaphoreGive(xMutex);
}
vTaskDelay(pdMS_TO_TICKS(10000));
}
}
Enthernet Code
Enthernet Code4mo ago
also check that your processDataTask has a lower priority than readSensorTask since it operates less frequently and also handle errors by xQueueSend and xQueueReceive se the ESP32 debugging tools to identify the exact line where the crash occurs. The crash log suggests a memory access violation, which could be due to an invalid pointer or accessing memory out of bounds.
Dtynin
Dtynin4mo ago
Thanks for getting back to me, @Enthernet Code I didn't use mutex locks in my implementation because I'm not working with FreeRTOS. Instead, I'm using Zephyr OS, which comes with its own synchronization tools. Here's my prj.conf file
CONFIG_SENSOR=y
CONFIG_DHT=y
CONFIG_DHT_TRIGGER=y
CONFIG_SENSOR=y
CONFIG_DHT=y
CONFIG_DHT_TRIGGER=y
I’ll try adding debug prints in the driver code next @Sterling
ZacckOsiemo
ZacckOsiemo4mo ago
Zephyr does support mutexes, along other tools as well.
Dtynin
Dtynin4mo ago
You’re right @ZacckOsiemo , Zephyr does support mutexes along with other tools. But I opted to use Zephyr’s native sync mechanisms because they fit my needs better.
ZacckOsiemo
ZacckOsiemo4mo ago
that's valid.
Solution
Dtynin
Dtynin4mo ago
finally gushh😂 , I choked it, I enabled detailed logging, which revealed an I/O error. I then double-checked the GPIO configuration, verified d pin setup, and added a pull-up resistor. After increasing delays and adding debug prints in d driver code, I finally moved the sensor to a different GPIO pin. I think I'm making progress in this, the sensor is now successfully fetching and displaying data. see this
[00:00:00.000,000] <inf> dht: DHT driver initialized
[00:00:00.000,001] <dbg> dht: Starting data fetch
[00:00:00.000,002] <dbg> dht: Starting measurement
[00:00:00.000,003] <dbg> dht: Reading data
Temperature: 25.4 C
Humidity: 60.7 %
[00:00:00.000,000] <inf> dht: DHT driver initialized
[00:00:00.000,001] <dbg> dht: Starting data fetch
[00:00:00.000,002] <dbg> dht: Starting measurement
[00:00:00.000,003] <dbg> dht: Reading data
Temperature: 25.4 C
Humidity: 60.7 %
Dtynin
Dtynin4mo ago
merci guys:salute:
Want results from more Discord servers?
Add your server