Use c# to write esp32 microcontroller to obtain DHT11 temperature sensor parameters

Welcome lovers of c#, in this article we will use the nanoframework framework of C# to write and get the temperature and humidity of the DHT11 sensor on the esp32 microcontroller

To achieve this, we need to prepare and configure the esp32 environment. You can take a look at the esp32 written before to build a nanoframework framework development environment.

Then buy DHT11 for a few dollars (some treasure) as shown in the picture

 Then we can start developing.

The first step is to create a nanoframework project Demo

 

 Click on NuGet

 Search for nanoFramework.Iot.Device.Dhtxx.Esp32 and install it into the project

 

 

 

 The installed nanoFramework.Iot.Device.Dhtxx.Esp32 depends on the nanoFramework.CoreLibrary version needs to be consistent and must be consistent to copy the code block

using Iot.Device.DHTxx.Esp32;
using System.Diagnostics;

namespace DemoDHT11
{
    public class Program
    {
        public static void Main()
        {
            //12,24 代表针角
            using (Dht11 dht = new Dht11(12, 14))
            {
                var temperature = dht.Temperature;//获取温度
                var humidity = dht.Humidity;//获取湿度百分比
                if (dht.IsLastReadSuccessful)//是否获取成功
                {
                    Debug.WriteLine($"温度: {temperature.DegreesCelsius} \u00B0C, 湿度百分比: {humidity.Percent} %");
                }
                else
                {
                    Debug.WriteLine("读取DHT传感器错误");
                }
            }
        }
    }
}

Connect the cables as shown in the figure: 12 and 14 are out (data) and connect two lines

 

 Select the device and run the program

 Running result: temperature: 20.8 °C, humidity percentage: 64 %

 Thank you for liking c# programming xd I hope more and more people like c# and even like to use c# to write MCU programming

nanoFramework official website:

.NET nanoFramework | nanoFramework Documentation

nanoFrameworkGitHub:

.NET nanoFramework (github.com)

Guess you like

Origin blog.csdn.net/xiaohucxy/article/details/122180660