nanoFramework.Iot.Device.Ds18b20 1.0.564

前缀已保留
dotnet add package nanoFramework.Iot.Device.Ds18b20 --version 1.0.564                
NuGet\Install-Package nanoFramework.Iot.Device.Ds18b20 -Version 1.0.564                
此命令旨在在 Visual Studio 的包管理器控制台中使用,因为它使用了 NuGet 模块的 Install-Package 版本。
<PackageReference Include="nanoFramework.Iot.Device.Ds18b20" Version="1.0.564" />                
对于支持 PackageReference 的项目,请将此 XML 节点复制到项目文件中,以引用此包。
paket add nanoFramework.Iot.Device.Ds18b20 --version 1.0.564                
#r "nuget: nanoFramework.Iot.Device.Ds18b20, 1.0.564"                
#r 指令可用于 F# Interactive 和多语言笔记本。将其复制到交互工具或脚本的源代码中,以引用该包。
// Install nanoFramework.Iot.Device.Ds18b20 as a Cake Addin
#addin nuget:?package=nanoFramework.Iot.Device.Ds18b20&version=1.0.564

// Install nanoFramework.Iot.Device.Ds18b20 as a Cake Tool
#tool nuget:?package=nanoFramework.Iot.Device.Ds18b20&version=1.0.564                

Ds18b20 - 温度传感器

DS18B20 数字温度计提供 9 位到 12 位的摄氏度温度测量,并且具有一个报警功能,具有非易失性的用户可编程的上限和下限触发点。

文档

产品数据表可在 此处 找到

传感器图像

sensor

电路

circuit

  • DATA - RX2 + TX2 短接
  • VCC - 3.3V
  • GND - GND

重要: 确保您已将传感器的数据引脚连接到板上的两个引脚上!

用法

重要: 在创建 OneWireHost 之前,确保正确设置 Rx/Tx 引脚(特别是对于 ESP32),并确保已安装 nanoFramework.Hardware.ESP32 nuget

Configuration.SetPinFunction(16, DeviceFunction.COM3_RX);
Configuration.SetPinFunction(17, DeviceFunction.COM3_TX);

库支持从 Ds18b20 传感器读取数据的三种方法

  1. 从 OneWire 总线上找到的第一个传感器或作为构造函数第二个参数传入的地址的传感器读取
 private static void ReadingFromOneSensor()
 {
     OneWireHost oneWire = new OneWireHost();


     Ds18b20 ds18b20 = new Ds18b20(oneWire, null, false, TemperatureResolution.VeryHigh);

     ds18b20.IsAlarmSearchCommandEnabled = false;
     if (ds18b20.Initialize())
     {
         Console.WriteLine($"Is sensor parasite powered?:{ds18b20.IsParasitePowered}");
         string devAddrStr = "";
         foreach (var addrByte in ds18b20.Address)
         {
             devAddrStr += addrByte.ToString("X2");
         }

         Console.WriteLine($"Sensor address:{devAddrStr}");

         while (true)
         {
             if (!ds18b20.TryReadTemperature(out var currentTemperature))
             {
                 Console.WriteLine("Can't read!");
             }
             else
             {
                 Console.WriteLine($"Temperature: {currentTemperature.DegreesCelsius.ToString("F")}\u00B0C");
             }

             Thread.Sleep(5000);
         }
     }


     oneWire.Dispose();
 }
  1. 从 OneWire 总线上找到的第一个传感器或作为构造函数第二个参数传入的地址的传感器读取的通知模式
private static void NotificationWhenValueHasChanged()
{
    OneWireHost oneWire = new OneWireHost();

    Ds18b20 ds18b20 = new Ds18b20(oneWire, null, false, TemperatureResolution.VeryHigh);

    if (ds18b20.Initialize())
    {
        ds18b20.SensorValueChanged += (currentTemperature) =>
        {
            Console.WriteLine($"Temperature: {currentTemperature.DegreesCelsius.ToString("F")}\u00B0C");
        };
        ds18b20.BeginTrackChanges(TimeSpan.FromMilliseconds(2000));
        // do whatever you want or sleep
        Thread.Sleep(60000);
        ds18b20.EndTrackChanges();
    }

    oneWire.Dispose();
}
  1. 使用警报 - 警报是一种模式,当传感器值高于或低于定义的阈值时。
private static void UsingAlarms()
{
    using OneWireHost oneWire = new OneWireHost();

    Ds18b20 ds18b20 = new Ds18b20(oneWire, null, false, TemperatureResolution.VeryHigh);

    if (ds18b20.Initialize())
    {
        for (int i = 0; i < ds18b20.AddressNet.Length; i++)
        {
            string devAddrStr = "";
            ds18b20.Address = ds18b20.AddressNet[i];

            foreach (var addrByte in ds18b20.AddressNet[i])
            {
                devAddrStr += addrByte.ToString("X2");
            }

            Console.WriteLine("18b20-" + i.ToString("X2") + " " + devAddrStr);

            ds18b20.ConfigurationRead(false);
            Console.WriteLine("Alarm set-points before changes:");
            Console.WriteLine("Hi alarm = " + ds18b20.TemperatureHighAlarm.DegreesCelsius + " C");
            Console.WriteLine("Lo alarm = " + ds18b20.TemperatureLowAlarm.DegreesCelsius + " C");
            SetAlarmSetting();
        }
        alarmSearch();
    }
    else
    {
        Console.WriteLine("No devices found.");
    }

    oneWire.Dispose();

    void alarmSearch()
    {
        int loopRead = 1000;
        ds18b20.IsAlarmSearchCommandEnabled = true;

        while (loopRead > 0)
        {
            Console.WriteLine("LoopRead " + loopRead);

            if (ds18b20.SearchForAlarmCondition())
            {
                for (int index = 0; index < ds18b20.AddressNet.Length; index++)
                {
                    ds18b20.Address = ds18b20.AddressNet[index];
                    if (ds18b20.TryReadTemperature(out var currentTemperature))
                    {
                        break;
                    }

                    string devAddrStr = "";
                    foreach (var addrByte in ds18b20.AddressNet[index]) devAddrStr += addrByte.ToString("X2");
                    Console.WriteLine("DS18B20[" + devAddrStr + "] Sensor reading in One-Shot-mode; T = " + currentTemperature.DegreesCelsius.ToString("f2") + " C");

                    ds18b20.ConfigurationRead(false);
                    Console.WriteLine("Alarm set-points:");
                    Console.WriteLine("Hi alarm = " + ds18b20.TemperatureHighAlarm.DegreesCelsius + " C");
                    Console.WriteLine("Lo alarm = " + ds18b20.TemperatureLowAlarm.DegreesCelsius + " C");
                }
            }
            else
            {
                Console.WriteLine("***** No devices in alarm ****");
            }

            loopRead--;
        }

        Console.WriteLine("");
    }

    void SetAlarmSetting()
    {
        ds18b20.TemperatureHighAlarm = Temperature.FromDegreesCelsius(30);
        ds18b20.TemperatureLowAlarm = Temperature.FromDegreesCelsius(25);
        // Write configuration on ScratchPad.
        ds18b20.ConfigurationWrite(false);
        // Write configuration on EEPROM too.
        ds18b20.ConfigurationWrite(true);
        // Read configuration to check if changes were applied
        ds18b20.ConfigurationRead(true);
        Console.WriteLine("Alarm set-points after changes:");
        Console.WriteLine("Hi alarm = " + ds18b20.TemperatureHighAlarm.DegreesCelsius.ToString("F") + " C");
        Console.WriteLine("Lo alarm = " + ds18b20.TemperatureLowAlarm.DegreesCelsius.ToString("F") + " C");
    }
}

查看“样本项目”以获取更多使用示例。

产品 兼容的以及额外的计算目标框架版本。
.NET 框架 net 兼容。
兼容的目标框架
包含的目标框架(在包中)
了解有关目标框架.NET 标准的更多信息。

NuGet 包

此包未由任何 NuGet 包使用。

GitHub 仓库

此包未由任何流行的 GitHub 仓库使用。

版本 下载 最后更新
1.0.564 60 8/9/2024
1.0.557 43 8/2/2024
1.0.552 50 7/26/2024
1.0.541 77 7/17/2024
1.0.524 92 6/19/2024
1.0.487 128 4/15/2024
1.0.465 112 3/22/2024
1.0.445 104 2/28/2024
1.0.425 126 1/24/2024
1.0.413 151 1/5/2024
1.0.409 127 12/20/2023
1.0.387 131 11/10/2023
1.0.367 94 11/8/2023
1.0.354 142 10/6/2023
1.0.347 119 9/27/2023
1.0.335 147 9/6/2023
1.0.329 158 8/16/2023
1.0.320 139 8/2/2023
1.0.314 150 7/28/2023
1.0.308 142 7/19/2023
1.0.305 121 7/14/2023
1.0.296 144 6/21/2023
1.0.292 128 6/14/2023
1.0.288 141 6/7/2023
1.0.286 153 6/2/2023
1.0.280 147 5/26/2023
1.0.253 165 5/10/2023
1.0.248 133 5/3/2023
1.0.224 271 3/17/2023
1.0.218 264 3/10/2023
1.0.214 256 3/8/2023
1.0.210 285 2/27/2023
1.0.207 259 2/24/2023
1.0.204 273 2/22/2023
1.0.173 316 1/9/2023
1.0.168 331 1/6/2023
1.0.159 301 1/3/2023
1.0.154 323 12/28/2022
1.0.110 394 11/14/2022
1.0.104 399 11/5/2022
1.0.38 510 9/15/2022
1.0.14 441 9/3/2022
1.0.3 450 8/17/2022
1.0.1 418 8/16/2022