[RPi] เริ่มต้นกับ Raspberry Pi ตอนที่ 7 : ทำ Temperature Monitoring ผ่าน ThingSpeak.com

Temperature Monitoring คืออะไร ?

Sathittham (Phoo) Sangthong
SS Blog
4 min readAug 2, 2015

--

  • อ่านข้อมูลสภาพอุณหภูมิปัจจุบัน ด้วยเซ็นเซอร์วัดอุณหภูมิแบบ I2C
  • ทั้งยังส่งข้อมูลขึ้นไปบนอินเตอร์เน็ตเซิร์ฟเวอร์ หรือ Cloud Server เพื่อดูออนไลน์ได้อีกด้วย

อุปกรณ์ที่ต้องใช้

  • Raspberry Pi
  • เซ็นเซอร์วัดอุณหภูมิ
  • โปรโตบอร์ด
  • สายจั๊ม

วีธีทำ Temperature Monitoring ผ่าน ThingSpeak.com

Step 1 : การต่อสายไฟ

Picture2
Picture1

Step 2 : ติดตั้ง smbus

  • เข้าที่ Terminal พิมพ์คำสั่ง
sudo  apt-get  install  python-smbus
Picture3
  • กด Enter

Step 3 : ตั้งค่า I2C

  • เข้าที่ Terminal พิมพ์คำสั่ง
sudo  raspi-config
Picture4
Picture5
  • กด Enter
  • เลือกเมนู Advance Options
  • เลือกเมนู I2C
Picture6
Picture7
  • เลือก YES แล้วกด Enter
  • OK (กด Enter)
Picture8
Picture9
  • เลือก YES แล้วกด Enter
  • OK (กด Enter)

Step 4 : ตั้งค่า I2C

  • เข้าที่ Terminal พิมพ์คำสั่ง
sudo  nano  /etc/modules
  • เพิ่ม 2 บรรทัดดังต่อไปนี้
i2c-bcm2708
i2c-dev
  • บันทึกด้วย Control-x
  • กด y เพื่อยืนยัน แล้วกด Enter
  • เข้าที่ Terminal พิมพ์คำสั่ง
sudo  nano  /boot/config.txt
  • เพิ่ม บรรทัดดังต่อไปนี้
dtparam=i2c1=on
  • บันทึกด้วย Control-x
  • กด y เพื่อยืนยัน แล้วกด Enter
Picture11

Step 5 : ทดสอบ I2C

  • เข้าที่ Terminal พิมพ์คำสั่ง
sudo  i2cdetect  –y  1
Picture12

Step 6 : การใช้งาน ThingSpeak

  • https://thingspeak.com/
  • กรอกข้อมูลเพื่อสร้าง Account
  • สร้าง Channel สำหรับเก็บข้อมูล
  • ตั้งชื่อกราฟ
  • Make Public
  • ตั้งชื่อข้อมูลที่ Field 1
  • Save Channel
Picture22

Step 7 : เขียนโปรแกรม

  • เรียกใช้งาน Library
  • ประกาศตัวแปล
  • เรียกใช้ I2C bus
  • ตั้งค่าเซนเซอร์อุณหภูมิ
import smbus		# Add the smbus library to a Python sketch
import time # Add the time library to a Pythonsketch
import httplib, urllib # Add the httplib, urllib library to a Python sketch
temp_add=0x48 # Temperature sensor address
temp_config_add=0x01 # Temperature sensor configuration address
temp_raw_add=0x00 # Temperature sensor data address
bus=smbus.SMBus(1) # Force I2C1
bus.write_byte_data(temp_add,temp_config_add,0x60) # Temperature sensor address
  • While Loop
  • อ่านอุณหภูมิจากเซนเซอร์
while True:    
templaw=bus.read_word_data(temp_add,temp_raw_add) # Read Raw Data
templow=(templaw&0xff00)>>8
temphigh=templaw&0x00ff
temp=(((temphigh*256)+templow)>>4)*.0625
temp=round(temp,2) #Data float 2 point
print "Temp = "+str(temp)
time.sleep(3)
  • While Loop (ต่อ)
  • ตั้งค่า field ของข้อมูลกราฟ
  • ตั้งค่าข้อมูลอุณหภูมิ
  • ตั้งค่า API keys ของกราฟ
# temp is the data you will be sending to the thingspeak channel for plotting the graph.    
# You can add more than one channel and plot more graphs
params = urllib.urlencode({'field1':temp,'key':'AMTRS8TFRQ1P3J2S'})
# use your API key generated in the thingspeak channels for the value of 'key'
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
  • ดู API key ของกราฟ
  • API Keys
  • Write API Key
  • While Loop (ต่อ)
  • ส่งข้อมูลขึ้น Cloud Server

(Thingspeak)

  • หน่วงเวลา 20 วินาที
try:        
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.status, response.reason # Response send status
data = response.read()
conn.close()
except:
print "connection failed"
time.sleep(20) # Delay 20 seccound

โค๊ดทั้งหมด pi_temp.py

import smbus			#Add the smbus library to a Python sketch
import time #Add the time library to a Python sketch
import httplib, urllib #Add the httplib, urllib library to a Python sketch
temp_add=0x48 #Temperature sensor address
temp_config_add=0x01 #Temperature sensor configuration address
temp_raw_add=0x00 #Temperature sensor data address
bus=smbus.SMBus(1) # Force I2C1
bus.write_byte_data(temp_add,temp_config_add,0x60) #Temperature sensor address
while True:
templaw=bus.read_word_data(temp_add,temp_raw_add) #Read Raw Data
templow=(templaw&0xff00)>>8
temphigh=templaw&0x00ff
temp=(((temphigh*256)+templow)>>4)*.0625
temp=round(temp,2) #Data float 2 point
print("Temp = "+str(temp))

# temp is the data you will be sending to the thingspeak channel for plotting the graph.
# You can add more than one channel and plot more graphs
# Add your API KEY at YOUR_API_KEY
params = urllib.urlencode({'field1':temp,'key':'YOUR_API_KEY'})
# use your API key generated in the thingspeak channels for the value of 'key'
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()
except:
print "connection failed"
time.sleep(60) #Delay 1 minute
Step 8 : ทดสอบ
  • รันโปรแกรมด้วยคำสั่ง
sudo  python  TemperatureLogger.py
Picture21
หากส่งข้อมูลสำเร็จจะแสดงสถานะ 200 OK*400 Bad Request เกิดจากการส่งข้อมูลไม่สำเร็จ อาจเกิดจาก API Key ไม่ถูกต้อง
Step 9 : ติดตั้งให้โปรแกรมทำงานอัตโนมัติ
  • เข้าที่ Terminal พิมพ์คำสั่ง
sudo   crontab   –e
  • เพิ่ม บรรทัดดังต่อไปนี้
@reboot  python  /home/pi/Deaktop/Code/TemperatureLogger.py
  • บันทึกด้วย Control-x แล้ว Y แล้ว Enter

ข้อมูลอ้างอิง

--

--

Sathittham (Phoo) Sangthong
SS Blog

Hi! It's me Phoo! I’m a Software Developer 👨‍💻 , a Startup Entrepreneur 📱 and a Runner 🏃 . Currently, I’m a Co-Founder and CTO of a Startup name “Urbanice”.