怎么用樹莓派搭建傳感器物聯(lián)網(wǎng)應(yīng)用

這篇文章將為大家詳細(xì)講解有關(guān)怎么用樹莓派搭建傳感器物聯(lián)網(wǎng)應(yīng)用,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)公司專注于正定企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),電子商務(wù)商城網(wǎng)站建設(shè)。正定網(wǎng)站建設(shè)公司,為正定等地區(qū)提供建站服務(wù)。全流程按需定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

1. 掛載DS18B20

sudo modprobe w1-gpio
sudo modprobe w1-therm

2. 安裝Python的庫單總線溫度計(jì)訪問庫: w1thermsensor

pip install w1thermsensor

3. 新建一個(gè)iot.py文件敲入以下代碼

#!/usr/bin/env python
#coding=utf8

import httplib, urllib

from w1thermsensor import W1ThermSensor
import time
import socket, serial, time
import httplib
import json

HOST = "open.lewei50.com"

PORT = 80
 

user_key = '樂為物聯(lián)的userkey'

def send_data_to_yeelink(temp):

		httpClient = None
		try:
			params = json.dumps({'value': temp})
			headers = {"Content-type": "application/json"
                    , "Accept": "text/plain",'U-ApiKey': 'Yeelink的api key'}
 
			print params
			httpClient = httplib.HTTPConnection('api.yeelink.net', 80, timeout=30)
			httpClient.request("POST", "/v1.1/device/設(shè)備id/sensor/傳感器id/datapoints", params, headers)
 
			response = httpClient.getresponse()
			print response.status
			print response.reason
			print response.read()
			print response.getheaders() #獲取頭信息
		except Exception, e:
			print e
		finally:
			if httpClient:
				httpClient.close()
 

def send_data(temp):

		httpClient = None
		try:
			params = json.dumps([{'Name': '傳感器名稱', 'Value': temp}])
			headers = {"Content-type": "application/x-www-form-urlencoded"
                    , "Accept": "text/plain","userkey":user_key}
 
			print params
			httpClient = httplib.HTTPConnection(HOST, 80, timeout=30)
			httpClient.request("POST", "/api/V1/Gateway/UpdateSensors/01", params, headers)
 
			response = httpClient.getresponse()
			print response.status
			print response.reason
			print response.read()
			print response.getheaders() #獲取頭信息
		except Exception, e:
			print e
		finally:
			if httpClient:
				httpClient.close()
while True:
	for sensor in W1ThermSensor.get_available_sensors([W1ThermSensor.THERM_SENSOR_DS18B20]):
		temp=sensor.get_temperature()
		print("Sensor %s has temperature %.2f" % (sensor.id,temp ))
		send_data(temp)
		send_data_to_yeelink(temp)
	time.sleep(120)

4. 運(yùn)行python腳本

python iot.py

5. 如果溫度讀取成功且上傳成功會(huì)打印相關(guān)消息.

關(guān)于“怎么用樹莓派搭建傳感器物聯(lián)網(wǎng)應(yīng)用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)頁名稱:怎么用樹莓派搭建傳感器物聯(lián)網(wǎng)應(yīng)用
網(wǎng)址分享:http://bm7419.com/article22/pcohjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航搜索引擎優(yōu)化、商城網(wǎng)站外貿(mào)建站、品牌網(wǎng)站制作、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)