How to use raspberry pie to build sensor Internet of things application
This article will explain in detail how to use raspberry pie to build sensor Internet of things applications. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. Mount DS18B20
Sudo modprobe w1-gpiosudo modprobe w1-therm
two。 Install Python library 1-wire thermometer access library: w1thermsensor
Pip install w1thermsensor
3. Create a new iot.py file and type in the following code
#! / usr/bin/env python#coding=utf8import httplib, urllibfrom w1thermsensor import W1ThermSensorimport timeimport socket, serial Timeimport httplibimport jsonHOST = "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", 'Umura print params httpClient:' api key' of Yeelink} print params httpClient = httplib.HTTPConnection ('api.yeelink.net', 80, timeout=30) httpClient.request ("POST", "/ v1.1/device/ device id/sensor/ sensor id/datapoints", params Headers) response = httpClient.getresponse () print response.status print response.reason print response.read () print response.getheaders () # get header information except Exception E: print e finally: if httpClient: httpClient.close () def send_data (temp): httpClient = None try: params = json.dumps ([{'Name':' sensor name' ) 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 () # get header information 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
4. Run the python script
Python iot.py
5. If the temperature is read successfully and uploaded successfully, the relevant message will be printed.
This is the end of the article on "how to use raspberry pie to build sensor Internet of things applications". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.