[ Raspberry Pi ] 透過 Python 傳送溫溼度資訊到 ThingSpeak

與 IoT 相關的雲端服務為數眾多,例如:ThingSpeakWoT.CityAWSIBM BluemixMediaTek MCSGoogle Firebase 等,並在透過這些雲端服務可在對資料進行資料視覺化、資料分析與其它的應用,而本文透過 Raspberry Pi 將溫溼度資料傳送到 ThingSpeak 。


執行步驟


前置準備作業

1.準備 Raspberry Pi 3 Model B 開發板

2.準備 Micro SD (已安裝 Raspbian) - 【 透過 MacOS 安裝 Raspbian 教學 】

3.將 Micro SD 裝入到 Raspberry Pi 中

4.已設定網路

5.安裝 GrovePi+

6.安裝 Grove – Temperature and Humidity Sensor (D4)

7.安裝 GROVE - LCD RGB BACKLIGHT (I2C-2)

8.安裝 IDE ( Sublime Text or Visual Studio Code )

9.Windows 平台,請安裝 Putty 與 FileZilla
http://oranwind.org/-linkit/

10.其它前置作業
請參考 [ Raspberry Pi ] 透過 Python 顯示溫溼度資訊到 Grove LCD


Sensor 、 LCD 、 GrovePi+ 與 Raspberry Pi 連接圖


ThingSpeak 端

Step 1. 到 ThingSpeak 網站申請帳號


Step 2. 登入 ThingSpeak 網站


Step 3. 點擊上方 Channels ➙ My Channels


Step 4. 點擊左方的 New Channel


Step 5. 設定 Channel 的名字、描述、設定欄位名稱、Enable Make Public


Step 6. 設定完成畫面


Step 7. 點擊中間的 API Keys,以取得 API Key


Raspberry Pi 端

Step 1. 透過 Python 讀取溫溼度資訊並傳送到 ThingSpeak

  • 請更新程式中 thinkSpeakApiKey 的值
#**************************************************** 
# Import Package                                                                           
#**************************************************** 

import sys  
import time  
import httplib, urllib  
sys.path.append('/home/pi/rpi/code/Package')  
import grovepi  
from grove_rgb_lcd import *

#**************************************************** 
# Set Pin No, ThingSpeak Key                                                                          
#**************************************************** 

sensor = 4  
blue = 0    # The Blue colored sensor.  
white = 1   # The White colored sensor.

thingSpeakApiKey = "XDFR4PLTEFD2NEFL"

#**************************************************** 
# Set ThingSpeak Connection                                                   
#**************************************************** 

def post_to_thingspeak(payload):  
    headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
    not_connected = 1
    while (not_connected):
        try:
            conn = httplib.HTTPConnection("api.thingspeak.com:80")
            conn.connect()
            not_connected = 0
        except (httplib.HTTPException, socket.error) as ex:
            print "Error: %s" % ex
            time.sleep(10)  # sleep 10 seconds

    conn.request("POST", "/update", payload, headers)
    response = conn.getresponse()
    print( response.status, response.reason, payload, time.strftime("%c"))
    data = response.read()
    conn.close()

#**************************************************** 
# Post ThingSpeak                                                  
#**************************************************** 

while True:  
    [temp,humidity] = grovepi.dht(sensor,blue)  
    print("temp = %.02f C humidity =%.02f%%"%(temp, humidity))
    params = urllib.urlencode({'field1': temp, 'field2': humidity, 'key': thingSpeakApiKey})
    post_to_thingspeak(params)
    time.sleep(5)


Step 2. 將 Python Code 傳送到 Raspberry Pi


Step 3. 執行剛傳到 Raspberry Pi 中的 Python Code

python 檔名.py  

Step 4. Console 執行畫面


Step 5. ThingSpeak 畫面


GitHub
List of blogs

Archer

Having being a full stack engineer. Experience with C, Python, Objective-C, Swift, Node.js, Lua, Linkit Smart 7688, Raspberry Pi, ARM mbed, Arduino, IoT solutions. Contact us : [email protected]

ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io