這篇文章將為大家詳細(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的庫(kù)單總線溫度計(jì)訪問庫(kù): 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)把它分享出去讓更多的人看到。