Trying to connect my DS18B20 temperature sensor to my smart greenhouse , I found that the Internet does not have full instructions for connecting this sensor using the Python programming language. I use it as I work with the Raspberry Pi platform. I decided to fix this problem. It turns out to work with TCP is not so difficult, but you need to understand what we are doing and why. A two-hour dance with a tambourine obviously pissed me off. So here, in addition to the program part, I want to tell the whole algorithm from beginning to end. I think that other sensors work similarly, so a big article will be the same for everyone. I hope that if you want to connect your sensor, you will not need a tambourine already :) And so, let's get started, I ask you under Habrokat.
For we need to connect the sensor itself. I will work with the DS18B20 temperature sensor. There are lots of articles on this topic, we will not duplicate them. You can read about the connection here . Then we need to get data from the sensor. We will do the same as stated in the article above. There is a great example in Python, of which I am a fan.import os tfile=open("/sys/bus/w1/devices/28-000000d7970b/w1_slave") ttext=tfile.read() tfile.close() temp=ttext.split("\n")[1].split(" ")[9] temperature=float(temp[2:])/1000 print temperature #MAC[#NAME][#LAT][#LNG][#ELE]\n #mac1#value1[#time1][#name1]\n ... #macN#valueN[#timeN][#nameN]\n ## #MAC\n #mac1#value1\n #macN#valueN\n ## #!/usr/bin/env python2 # -*- coding: utf-8 -*- # by Roman Vishnevsky aka.x0x01 @ gmail.com import socket # MAC . ! DEVICE_MAC = '0123456789012' # , 01 (02) mac SENSOR_ID_1 = DEVICE_MAC + '01' SENSOR_ID_2 = DEVICE_MAC + '02' # , float/integer sensor_value_1 = 20 sensor_value_2 = -20.25 # sock = socket.socket() # try: # sock.connect(('narodmon.ru', 8283)) # sock.send("#{}\n#{}#{}\n##".format(DEVICE_MAC, SENSOR_ID_1, sensor_value_1)) # # sock.send("#{}\n#{}#{}\n#{}#{}\n##".format(DEVICE_MAC, SENSOR_ID_1, sensor_value_1, SENSOR_ID_2, sensor_value_2)) # data = sock.recv(1024) sock.close() print data except socket.error, e: print('ERROR! Exception {}'.format(e)) #!/usr/bin/env python2 # -*- coding: utf-8 -*- import socket import os import fnmatch # MAC . ! DEVICE_MAC = 'FF:FF:FF:FF:FF:FF' # SENSOR_ID_1 = 'T1' SENSOR_ID_2 = 'T2' # temperature = [] IDs = [] for filename in os.listdir("/sys/bus/w1/devices"): if fnmatch.fnmatch(filename, '28-031652ddbdff'): with open("/sys/bus/w1/devices/" + filename + "/w1_slave") as fileobj: lines = fileobj.readlines() if lines[0].find("YES"): pok = lines[1].find('=') temperature.append(float(lines[1][pok+1:pok+7])/1000) IDs.append(filename) else: logger.error("Error reading sensor with ID: %s" % (filename)) temperature2 = [] for filename in os.listdir("/sys/bus/w1/devices"): if fnmatch.fnmatch(filename, '28-011563e8d2ff'): with open("/sys/bus/w1/devices/" + filename + "/w1_slave") as fileobj: lines = fileobj.readlines() if lines[0].find("YES"): pok = lines[1].find('=') temperature2.append(float(lines[1][pok+1:pok+7])/1000) IDs.append(filename) else: logger.error("Error reading sensor with ID: %s" % (filename)) sock = socket.socket() # try: sock.connect(('narodmon.ru', 8283)) # , sock.send("#{}\n#{}#{}\n#{}#{}\n##".format(DEVICE_MAC, SENSOR_ID_1, str(temperature)[1:-1], SENSOR_ID_2, str(temperature2)[1:-1])) # data=sock.recv(1024) sock.close() print data except socket.error, e: print('ERROR! Exception {}'.format(e)) print str(temperature)[1:-1] print str(temperature2)[1:-1] crontab -e and add this line to it: */10 * * * * sudo python /home/pi/narod.py Source: https://habr.com/ru/post/401313/
All Articles