DS18B20双温度监测控制冰酒发酵-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 785|回复: 5

[分享] DS18B20双温度监测控制冰酒发酵

[复制链接]
发表于 2022-3-25 09:23 | 显示全部楼层 |阅读模式
本帖最后由 armduino 于 2022-3-25 09:25 编辑

用一个温度传感器程序比较简单,用两个或多个DS18B20传感器只需要一根总线,但是涉及到地址识别问题,程序略微复杂。
结合自己的实际需要,程序增改如下:

  1. /* *****************************************************************
  2. * DS18B20是Dallas Semiconductor Corp.生产的1-Wire接口温度传感器,该独特的1-Wire接口仅需要一个用于与微控制器的双向通信的数字针
  3. * DS18B20温度传感器相当精确,无需外部组件即可工作,它可以测量-55°C至+ 125°C的温度,精度为±0.5°C.
  4. * 用户可以将温度传感器的分辨率配置为9,10,11或12位.但是,上电时的默认分辨率为12位(即0.0625°C精度)
  5. * 该传感器可以由3V至5.5V电源供电,并且在主动温度转换期间仅消耗1mA电流
  6. * 使用点灯科技的APP开发控制界面
  7. * *****************************************************************/
  8. #define BLINKER_WIFI
  9. //#define BLINKER_ESP_SMARTCONFIG
  10. #include <Blinker.h>

  11. #include<OneWire.h>
  12. #include<DallasTemperature.h>
  13. #define TEMPERATURE_PRECISION 10
  14. #define BUS2 2        //for NodeMCU GPIO13=D7,温度传感器黄色线接针脚D7(或者GPIO2),然后并一个4.7K电阻接到3V3
  15.                       //DS18B20的针脚定义:面朝印字面,左为GND,右为VCC,中间为数字输出引脚(接4.7-10k上拉电阻)
  16. OneWire onewire(BUS2);//通过将传感器的信号引脚传递到其构造函数来创建单线对象,这个单线对象让我们与任何单线设备进行通信,而不仅仅是DS18B20

  17. DallasTemperature sensors(&onewire);//为了与DS18B20传感器进行通信,我们需要创建DallasTemperature库的对象,并将单线对象的引用作为参数传递

  18. // arrays to hold device address
  19. DeviceAddress insideThermometer, outsideThermometer;//定义地址变量

  20. //联接好两个温度传感器后,可以先用示例程序oneWireSearch上传到MCU 获取传感器的地址如下,也就是说可以直接赋参
  21. //DeviceAddress insideThermometer = {0x28,0x61,0x64,0x11,0xB8,0x6A,0x69,0x8E};
  22. //DeviceAddress outsideThermometer = {0x28,0x45,0xDB,0x07,0xD6,0x01,0x3C,0x39};

  23. char auth[] ="6d8268190a67";// "Your Device Secret Key";
  24. char ssid[] = "TP-LINK_2F98";//Your WiFi network SSID or name";
  25. char pswd[] = "xxxxxx";//Your WiFi network WPA password or WEP key";
  26. uint32_t read_time = 0;
  27. float temp_read1=0;
  28. float temp_read2=0;

  29. // 新建组件对象
  30. BlinkerButton Button1("cooler1");
  31. BlinkerButton Button2("cooler2");
  32. BlinkerNumber TEMP1("temp1");
  33. BlinkerNumber TEMP2("temp2");

  34. bool b1_state=false;
  35. bool b2_state=false;
  36. int relayPin1=5;  //LC-ESP12F开发板占用引脚5用于继电器触发信号,GPIO5=D1 for NodeMCU
  37. int relayPin2=12; //备用 GPIO12=D6

  38. // 按下按键即会执行该函数
  39. void button1_callback(const String & state){
  40.     BLINKER_LOG("get button1 state: ", state);
  41.     //digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  42.     if (state=="on" ){
  43.       b1_state=true;
  44.       digitalWrite(relayPin1,HIGH);
  45.       Button1.print("on");
  46.     }
  47.     else if (state=="off"){
  48.       b1_state=false;
  49.       digitalWrite(relayPin1,LOW);
  50.       Button1.print("off");
  51.     }
  52. }

  53. void button2_callback(const String & state) {
  54.     BLINKER_LOG("get button2 state: ", state);
  55.     //digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  56.     if(state=="on"){
  57.       b2_state=true;
  58.       digitalWrite(relayPin2,HIGH);
  59.       Button2.print("on");
  60.     }
  61.     else if (state=="off"){
  62.       b2_state=false;
  63.       digitalWrite(relayPin2,LOW);
  64.       Button2.print("off");
  65.     }
  66. }

  67. // 如果未绑定的组件被触发,则会执行其中内容
  68. void dataRead(const String & data){
  69.      BLINKER_LOG("Blinker readString: ", data);
  70.      digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  71.      Blinker.vibrate();
  72.      uint32_t BlinkerTime = millis();
  73.      Blinker.print("millis", BlinkerTime);
  74. }

  75. void heartbeat(){     
  76.      TEMP1.print(temp_read1);
  77.      TEMP2.print(temp_read2);
  78. }

  79. void dataStorage(){
  80.      Blinker.dataStorage("temp1", temp_read1);
  81.      Blinker.dataStorage("temp2", temp_read2);
  82. }

  83. // function to print a device address
  84. void printAddress(DeviceAddress deviceAddress)
  85. {
  86.   for (uint8_t i = 0; i < 8; i++)
  87.   {
  88.     // zero pad the address if necessary
  89.     if (deviceAddress[i] < 16) Serial.print("0");
  90.     Serial.print(deviceAddress[i], HEX);
  91.   }
  92. }

  93. // function to print the temperature for a device
  94. void printTemperature(DeviceAddress deviceAddress)
  95. {
  96.   float tempC = sensors.getTempC(deviceAddress);
  97.   if(tempC == DEVICE_DISCONNECTED_C)
  98.   {
  99.     Serial.println("Error: Could not read temperature data");
  100.     return;
  101.   }
  102.   Serial.print("Temp C: ");
  103.   Serial.print(tempC);
  104.   Serial.print(" | Temp F: ");
  105.   Serial.print(DallasTemperature::toFahrenheit(tempC));
  106. }

  107. // function to print a device's resolution
  108. void printResolution(DeviceAddress deviceAddress)
  109. {
  110.   Serial.print("Resolution: ");
  111.   Serial.print(sensors.getResolution(deviceAddress));
  112.   Serial.println();
  113. }

  114. // main function to print information about a device
  115. void printData(DeviceAddress deviceAddress)
  116. {
  117.   Serial.print("Device Address: ");
  118.   printAddress(deviceAddress);
  119.   Serial.print(" ");
  120.   printTemperature(deviceAddress);
  121.   Serial.println();
  122. }


  123. void setup()
  124. {
  125.     Serial.begin(115200);
  126.     BLINKER_DEBUG.stream(Serial);
  127.     pinMode(relayPin1,OUTPUT);
  128.     pinMode(relayPin2,OUTPUT);
  129.     pinMode(LED_BUILTIN, OUTPUT);
  130.     digitalWrite(LED_BUILTIN, HIGH);

  131.     Blinker.begin(auth,ssid, pswd);
  132.     Blinker.attachData(dataRead);
  133.     Blinker.attachHeartbeat(heartbeat);
  134.     Blinker.attachDataStorage(dataStorage);
  135.     Button1.attach(button1_callback);
  136.     Button2.attach(button2_callback);

  137.     BLINKER_LOG("Awesome! Uploading is OK!");
  138.     sensors.begin();  //该功能搜索总线上连接的传感器,并为每个传感器设置 位分辨率(12位)

  139.     //sensors.setResolution( TEMPERATURE_PRECISION );

  140.     // locate devices on the bus
  141.     Serial.print("\nLocating devices...");
  142.     Serial.print("Found ");
  143.     Serial.print(sensors.getDeviceCount(), DEC); //获取传感器设备数量
  144.     Serial.println(" devices.");

  145.   // report parasite power requirements
  146.   // bool isParasitePowerMode(void)返回是否需要总线寄生供电,需要则返回true
  147.     Serial.print("Parasite power is: ");
  148.       if (sensors.isParasitePowerMode()) Serial.println("ON");
  149.       else Serial.println("OFF");

  150.   // Search for devices on the bus and assign based on an index. Ideally,
  151.   // you would do this to initially discover addresses on the bus and then
  152.   // use those addresses and manually assign them (see above) once you know
  153.   // the devices on your bus (and assuming they don't change).
  154.   //
  155.   // method 1: by index
  156.   if (!sensors.getAddress(outsideThermometer, 0))  Serial.println("Unable to find address for Device 0");
  157.   if (!sensors.getAddress(insideThermometer, 1))   Serial.println("Unable to find address for Device 1");

  158.   // method 2: search()
  159.   // search() looks for the next device. Returns 1 if a new address has been
  160.   // returned. A zero might mean that the bus is shorted, there are no devices,
  161.   // or you have already retrieved all of them. It might be a good idea to
  162.   // check the CRC to make sure you didn't get garbage. The order is
  163.   // deterministic. You will always get the same devices in the same order
  164.   //
  165.   // Must be called before search()
  166.   //oneWire.reset_search();
  167.   // assigns the first address found to insideThermometer
  168.   //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
  169.   // assigns the seconds address found to outsideThermometer
  170.   //if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");

  171.   // show the addresses we found on the bus
  172.     Serial.print("Device 0 Address: ");
  173.     printAddress(outsideThermometer); //28616411B86A698E
  174.     Serial.println();

  175.     Serial.print("Device 1 Address: ");
  176.     printAddress(insideThermometer); //2845DB07D6013C39
  177.     Serial.println();

  178.   // set the resolution to 9 bit per device
  179.     sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
  180.     sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);

  181.     Serial.print("Device 0 Resolution: ");
  182.     Serial.print(sensors.getResolution(outsideThermometer), DEC); //12
  183.     Serial.println();

  184.     Serial.print("Device 1 Resolution: ");
  185.     Serial.print(sensors.getResolution(insideThermometer), DEC); //9
  186.     Serial.println();

  187. }


  188. void loop(){
  189.     Blinker.run();
  190.     // call sensors.requestTemperatures() to issue a global temperature
  191.     // request to all devices on the bus
  192.     Serial.print("Requesting temperatures...");
  193.     sensors.requestTemperatures(); //该功能向总线上的所有传感器发送命令以执行温度转换
  194.     Serial.println("DONE");
  195.     // print the device information
  196.     //printData(insideThermometer);
  197.     //printData(outsideThermometer);

  198.     uint16_t data_time= 3000;
  199.     if (read_time == 0 || (millis() - read_time) >= data_time) {
  200.         read_time = millis();
  201.         float t1 = sensors.getTempC (insideThermometer);
  202.         float t2 = sensors.getTempC (outsideThermometer);
  203.         //float t1 = sensors.getTempCByIndex(1);  //该功能读取并返回传感器的温度读数.deviceIndex只是总线上传感器的位置,如果您仅在总线上使用一个DS18B20,请将其设置为0
  204.         //float t2 = sensors.getTempCByIndex(0);
  205.         //static char temperatureTemp[7];
  206.         //dtostrf(t1, 6, 1, temperatureTemp);  //data to string function 把浮点数或整型数转换成字符串,保留一位小数

  207.         temp_read1 = t1;
  208.         temp_read2 = t2;
  209.         Serial.print("当前温度 t1 = ");
  210.         Serial.print(t1);
  211.         Serial.print((char)176);//shows degrees character
  212.         Serial.print("C  |  ");
  213.         Serial.print("当前温度 t2 = ");
  214.         Serial.print(t2);
  215.         Serial.print((char)176);//shows degrees character
  216.         Serial.println("C  |  ");

  217.        //print the temperature in Fahrenheit
  218.        //Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  219.        //Serial.print((char)176);//shows degrees character
  220.        //Serial.println("F");

  221.         if (t1>=8){
  222.         digitalWrite(relayPin1,HIGH);  //制冷机启动
  223.         }
  224.         else if (t1<=5){
  225.         digitalWrite(relayPin1,LOW);
  226.         }
  227.     }      

  228.     /*
  229.      * DallasTemperature.h库中的其他有用功能
  230.      * DallasTemperature对象可以使用一些有用的功能,下面列出了其中几个:
  231.      * setResolution() 该功能将DS18B20的内部模数转换器的分辨率设置为9位,10位,11位或12位,分别对应于0.5°C,0.25°C,0.125°C和0.0625°C的增量
  232.      * bool getWaitForConversion()函数返回waitForConversion标志的值.当您要检查温度转换是否完成时,此功能很有用.
  233.      * setHighAlarmTemp()&setLowAlarmTemp()功能可设置设备的内部高温和低温警报(以摄氏度为单位),有效范围是-55至125°C
  234.      * bool hasAlarm() 如果温度超过上限和下限警报温度设置时设备处于警报状态,此功能将返回true.

  235.      */
  236. }
复制代码


 楼主| 发表于 2022-3-25 09:26 | 显示全部楼层
微信图片_20220324230411.png
 楼主| 发表于 2022-3-25 09:27 | 显示全部楼层
微信图片_20220324231830.jpg
 楼主| 发表于 2022-3-25 09:29 | 显示全部楼层
微信图片_20220324232016.jpg
发表于 2022-4-1 12:10 | 显示全部楼层
楼主 为什么我看有的 是上拉到5v上啊 而且官方文档也是上拉到5v的
 楼主| 发表于 2022-4-2 10:15 | 显示全部楼层
我看到都是上拉3.3,另外,esp板子输出引脚都是3.3V。
板子内置了上拉电阻,可以不要外接电阻,抽空再测试一下。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 05:31 , Processed in 0.083224 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表