使用带有 DHT11 传感器的 W5100S-EVB-PICO 板创建了一个项目来收集天气数据。 利用 MQTT 通信根据天气推荐播放列表
转发: Recommended Weather-Appropriate Song Playlists by using W5x00-EVB-Pico with DHT11 & MQTT protocol
项目介绍
我创建了一个应用程序,通过 W5100S 从 DHT11 接收温度和湿度值,并推荐适合这些值的音乐。
对于这个项目,我们与 Wiznet 的 Hannah 合作。 她负责MQTT通信控制,而我专注于控制DHT11和处理数据。 我将讲解DHT11控制和数据处理相关的技术。
要引用项目文件,请单击下面。
要启动此项目,请连接 DHT11 和 W5100S。 请注意,DHT11 的 DATA PIN 号为 28。另外,将剩余的 3.3V 电压连接到地。
首先,在整个代码的顶部适当配置您的设备。 本项目不限于DHT11,通过修改代码也可以与其他版本的DHT一起使用。 如果您想使用不同的引脚号,请修改端口号。
// change this to match your setup static const dht_model_t DHT_MODEL = DHT22; static const uint DATA_PIN = 28; static float celsius_to_fahrenheit(float temperature) { return temperature * (9.0f / 5) + 32;
我们将这些部分分为三类:热、冷和暖。 使用基于温度的条件语句和底部 DHT11 传感器的数据,我们可以以不同的方式定制歌曲推荐数据输出。
stdio_init_all(); char dht_string[600]; char today_hot[500] = "Today's weather is Hot Summer! \nRecommended playlist: https://www.youtube.com/embed/Hf4A6O6q16c"; char today_cold[500] = "Today's weather is Cold! \nRecommended playlist: https://www.youtube.com/embed/K81dHzC0T_M"; char today_warm[500] = "Today's weather is Warm day! \nRecommended playlist: https://www.youtube.com/embed/DpcJP-wQWGI"; dht_t dht; dht_init(&dht, DHT_MODEL, pio0, DATA_PIN, true /* pull_up */); dht_start_measurement(&dht); float humidity; float temperature_c; dht_result_t result = dht_finish_measurement_blocking(&dht, &humidity, &temperature_c); if (result == DHT_RESULT_OK) { snprintf(dht_string, sizeof(dht_string), "%.1f C (%.1f F), %.1f%% humidity\n", temperature_c, celsius_to_fahrenheit(temperature_c), humidity); if(temperature_c > 30) strcat(dht_string, today_hot); else if (temperature_c < 10) strcat(dht_string, today_cold); else strcat(dht_string, today_warm); } else if (result == DHT_RESULT_TIMEOUT) { puts("DHT sensor not responding. Please check your wiring."); } else { assert(result == DHT_RESULT_BAD_CHECKSUM); puts("Bad checksum"); }
DHT11 和 W5100S 之间的通信机制非常简单。 主机设备向DHT11发送数据请求。 然后,DHT11 以 40 位数字数据的形式响应温度和湿度信息。 随后,主机设备解释该数据以提取温度和湿度值。 我们使用 PIO 代码实现了这个过程。 更详细的机制请参考以下信息。
通过上述过程,我们就可以使用MQTT通信将处理后的数据发送到应用程序。 下图代表了该项目的结果。
-
W5100S-MQTT-DHT11
整个代码,包括每个基本代码,可以在下面的Github链接中引用。