2015年2月5日 星期四

Arduno動手做 - 使用Ds18b20監測冰箱溫度並自動上傳數據


實驗室有個用來存放樣本的家用型冰箱
從冷凍的區域拿樣本出來時常常發現是退冰的狀態
所以一直很好奇在大家頻繁的開開關關之下到底溫度是如何變化
於是決定動手來實測...

這次使用的是arduino nano
溫度感測器則是選擇了防水封裝後的ds18b20
有三條線,一正一負和一條資料線
資料線接至數位pin D2
同時資料線必須用4.7k電阻併連至5v電源上拉


程式碼的部分如下

必須先安裝OneWire和DallasTemperature這兩個libary

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2  // 定義資料線為D2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}
 
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0)); 
  // 取得溫度並print到serial
  // index0的意思為第一個one wire裝置(因為one wire允許同時有很多個裝置串在同一條資料線上)

  delay(1000);

}


做到這裡打開serial應該就能正常看到溫度的偵測顯示了


接下來就是要如何把資料傳到thinkspeak並輸出圖表呢?

這邊要介紹的是利用esp8266 wifi晶片將資料上傳

上次已經簡述過如何將esp8266接上arduino所以就不贅述


直接看code吧:

//DS18B20設定
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

//esp8266設定
#define SSID       "你的無線網路SSID"
#define PASSWORD   "你的無線網路密碼"

#include "uartWIFI.h"
#include <SoftwareSerial.h>
WIFI wifi;

#define IP "184.106.153.149" // thingspeak.com
String GET = "GET /update?key=填入你的thinhspeakAPI金鑰";

void setup()
{
  sensors.begin(); // Start up the ds18b20 library
  wifi.begin();
  bool b = wifi.Initialize(STA, SSID, PASSWORD);
  if(!b)
  {
    DebugSerial.println("Init error");
  }
  delay(8000);  //make sure the module can have enough time to get an IP address 
  String ipstring  = wifi.showIP();
  DebugSerial.println("My IP address:");
  DebugSerial.println(ipstring);        //show the ip address of module
  
  String wifistring  = wifi.showJAP();
  DebugSerial.println(wifistring);      //show the name of current wifi access port

}
void loop()
{ 
  //DS18B20讀取
  sensors.requestTemperatures();
  float sensor_ds18temp = sensors.getTempCByIndex(0);

  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  Serial.println(cmd);
  delay(2000);
  if(Serial.find("Error")){
    return;
  }
  cmd = GET;
  cmd += "&field1=";
  cmd += sensor_ds18temp;
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if(Serial.find(">")){
    Serial.print(cmd);
  }else{
    Serial.println("AT+CIPCLOSE");
  }
  
}



如果沒有什麼問題應該就能在thingspeak上看到資料了
若沒斷訊的話即時圖表在此:


測試結果截圖:


從結果看得出來,其實冷凍溫度僅有15度左右

和實驗室常規需求的-20其實有段差距,而且一旦有打開冰箱的動作(上圖11:00時)

溫度就會上探最高到-5度左右

且得等上將近2個小時(13:00)才能恢復

換句話說就是只要兩個小時內只要有人開一次冰箱,冷凍溫度就只能維持在-5~ -10度

若開關頻率更高,不難想像會突破0度

樣本退冰自然也是有可能發生的事情


考慮之後可以在上面接個LED燈並設定門檻

譬如若目前溫度高過-5度就亮LED燈提醒使用者開關冰箱門的動作要加快一點



如果還不知道如何設定thingspeak的朋友別擔心

下一篇會來介紹Thingspeak的各種功能

6 則留言:

  1. AT+RST
    Module is ready
    reboot wifi is OK
    My IP address:
    192.168.1.143OK

    192.168.1.143
    AT+CIPSTART="TCP","184.106.153.149",80
    AT+CIPSEND=47
    AT+CIPCLOSE
    AT+CIPSTART="TCP","184.106.153.149",80
    AT+CIPSEND=47
    AT+CIPCLOSE
    AT+CIPSTART="TCP","184.106.153.149
    不知為什麼溫度沒有傳上去請問是什麼問題

    回覆刪除
    回覆
    1. 可以貼上程式碼參考一下嗎?

      刪除
    2. #include
      #include
      #define ONE_WIRE_BUS 2
      OneWire oneWire(ONE_WIRE_BUS);
      DallasTemperature sensors(&oneWire);
      \
      //esp8266設定
      #define SSID "Lucia"
      #define PASSWORD "27960203"
      #define MEGA
      #include "uartWIFI.h"
      #include
      WIFI wifi;

      #define IP "184.106.153.149" // thingspeak.com
      String GET = "GET /update?key=V0AKPG02ENCV932C";

      void setup()
      {
      sensors.begin(); // Start up the ds18b20 library
      wifi.begin();
      bool b = wifi.Initialize(STA, SSID, PASSWORD);
      if(!b)
      {
      DebugSerial.println("Init error");
      }
      delay(8000); //make sure the module can have enough time to get an IP address
      String ipstring = wifi.showIP();
      DebugSerial.println("My IP address:");
      DebugSerial.println(ipstring); //show the ip address of module

      String wifistring = wifi.showJAP();
      DebugSerial.println(wifistring); //show the name of current wifi access port

      }
      void loop()
      {
      //DS18B20讀取
      sensors.requestTemperatures();
      float sensor_ds18temp = sensors.getTempCByIndex(0);

      String cmd = "AT+CIPSTART=\"TCP\",\"";
      cmd += IP;
      cmd += "\",80";
      Serial.println(cmd);
      delay(2000);
      if(Serial.find("Error")){
      return;
      }
      cmd = GET;
      cmd += "&field1=";
      cmd += sensor_ds18temp;
      cmd += "\r\n";
      Serial.print("AT+CIPSEND=");
      Serial.println(cmd.length());
      if(Serial.find(">")){
      Serial.print(cmd);
      }else{
      Serial.println("AT+CIPCLOSE");
      }
      delay(6000); // 60 second
      }
      我適用esp8266-ep01

      刪除
  2. 您好
    程式是否只能使用MEGA才可寫入?
    若要使用Uno版,要如何更動?
    關於Library內容可是有看沒有懂...
    感謝

    回覆刪除
  3. 不好意思 我也遇到類似樓上的問題,而我是用MEGA ESP8266則是跟作者您同一片,以下是我遇到德問題,資料似乎沒有上傳到網頁上
    AT+RST
    Module have no response
    AT+RST
    Module is ready
    reboot wifi is OK
    My IP address:
    OK+CIFSR:STAIP,"192.168.1.205"+CIFSR:STAMAC,"18:fe:34:9f:f4:e2"OK

    AP:"704"
    AT+CIPSTART="TCP","184.106.153.149",80
    AT+CIPSEND=46
    AT+CIPCLOSE
    AT+CIPSTART="TCP","184.106.153.149",80
    AT+CIPSEND=46
    AT+CIPCLOSE
    AT+CIPSTART="TCP","184.106.153.149",80

    回覆刪除
  4. JAMUARY 25, 2020 | Wynn Resorts Ltd - JMT Hub
    JAMUARY 인천광역 출장안마 25, 2020. 4:00 PM JAMUARY 25, 2020. 3:15 AM. JAMUARY 25, 2020. 3:15 경상북도 출장안마 AM. 화성 출장안마 JAMUARY 25, 2020. 3:20 AM. JAMUARY 25, 2020. 3:25 AM. 논산 출장마사지 JAMUARY 25, 춘천 출장샵 2020.

    回覆刪除