溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2021-11-20 09:44:46 來源:億速云 閱讀:132 作者:小新 欄目:互聯(lián)網(wǎng)科技

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

1. 掛載DS18B20

sudo modprobe w1-gpio
sudo modprobe w1-therm

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

pip install w1thermsensor

3. 新建一個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/設備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. 運行python腳本

python iot.py

5. 如果溫度讀取成功且上傳成功會打印相關消息.

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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI