怎么用树莓派搭建传感器物联网应用
这篇文章将为大家详细讲解有关怎么用树莓派搭建传感器物联网应用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联公司专注于正定企业网站建设,响应式网站开发,电子商务商城网站建设。正定网站建设公司,为正定等地区提供建站服务。全流程按需定制网站,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
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 = '乐为物联的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. 如果温度读取成功且上传成功会打印相关消息.
关于“怎么用树莓派搭建传感器物联网应用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
网站名称:怎么用树莓派搭建传感器物联网应用
文章转载:http://scyingshan.cn/article/pcohjc.html