งานที่มอบหมาย ครูประภาส

บทความประกอบการเรียนรู้ => IOT : Internet of Thing (ESP8266, NodeMCU, WeMos D1 mini) => ข้อความที่เริ่มโดย: admin ที่ พฤศจิกายน 10, 2017, 11:39:40 PM

หัวข้อ: งานครั้งที่ 19 การเขียนโปรแกรมอ่านค่าอุณหภูมิจาก DS18B20
เริ่มหัวข้อโดย: admin ที่ พฤศจิกายน 10, 2017, 11:39:40 PM
การเขียนโปรแกรมอ่านค่าอุณหภูมิจาก DS18B20

ไลบรารี่สำหรับ DS18B20  เนื่องจากโปรแกรม Arduino IDE ไม่ได้มีการติดตั้งมาให้ตั้งแต่เริ่มต้นจึงจำเป็นต้องมีการติดตั้งเพิ่มเติ่มเข้าไปในโปรแกรมซึ่งต้องใช้ไลบรารี่ 2 ตัวและอยู่คนละเวปไซต์ ขั้นตอนดังนี้

1. ดาวน์โหลดไฟล์ zip
-OneWire.h จากเวปไซด์https://github.com/PaulStoffregen/OneWire (https://github.com/PaulStoffregen/OneWire)
-DallasTemperature.h จากเวปไซด์ https://github.com/milesburton/Arduino-Temperature-Control-Library (https://github.com/milesburton/Arduino-Temperature-Control-Library)


(http://www.praphas.com/PhotoForum/iot/Lab-19-DS18B20/01.png)

2. ทำการเพิ่มไลบรารี่ลงในโปรแกรม Arduino IDE โดยการเพิ่มจากไฟล์ zip แล้วทำการหาไฟล์ zip ที่ได้จากการดาวน์โหลดในข้อ 1
(http://www.praphas.com/PhotoForum/arduino/Lab-16-b.png)

โจทย์โปรแกรม

-เขียนโปรแกรมอ่านค่าอุณหภูมิจาก DS18B20 แสดงผลที่อุปกรณ์ต่าง ๆ
-โปรแกรมรายละเอียดพิเศษรายกลุ่ม (แจ้งให้ทราบเมื่อถึงชั่วโมงเรียน)

วงจรที่ใช้ทดลอง
กรณีใช้บอร์ดรุ่น NodeMCU
(http://www.praphas.com/PhotoForum/iot/Lab-19-DS18B20/02.png)
กรณีใช้บอร์ดรุ่น WeMos D1 mini
(http://www.praphas.com/PhotoForum/iot/Lab-19-DS18B20/03.png)

ตัวอย่างโปรแกรม
โค๊ด: [Select]
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(D2);                 //Connect to D2
DallasTemperature sensors(&oneWire);
void setup(void)
{
  Serial.begin(115200);
  Serial.println("Dallas Temperature IC DS18B20");
  sensors.begin();
}
void loop(void)
{
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures();            // Send the command to get temperatures
  Serial.println("DONE");
  Serial.print("Temperature is: ");
  Serial.print(sensors.getTempCByIndex(0));
  Serial.println(" Degrees C");
  Serial.print("Temperature is: ");
  Serial.print(sensors.getTempFByIndex(0));
  Serial.println(" Degrees F");
}

ผลการรันจากโปรแกรมตัวอย่าง
(http://www.praphas.com/PhotoForum/iot/Lab-19-DS18B20/04.png)