Сообщение
getro_ded » Вт май 30, 2017 11:46 pm
Я вопрос решил через MQTT и Python
Раз в минуту опрашиваются все датчики и для каждого со своим топиком отправляется информация.
поставил в автозапуск и нет проблем.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#The header is important make sure you use pyhton3 or paho will not work
# Import the Modules
import sys
import os
import time
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client = mqtt.Client()
#Set username and pw here
client.username_pw_set("username",password="password")
client.on_connect = on_connect
#add the url or ip to your mqtt broker port 1883 is the default non encrypted port
client.connect("localhost", 1883, 60)
# read 1-Wire Slave-list
#file = open('/sys/devices/w1_bus_master1/w1_master_slaves')
#w1_slaves = file.readlines()
#file.close()
while True:
# read 1-Wire Slave-list
file = open('/sys/devices/w1_bus_master1/w1_master_slaves')
w1_slaves = file.readlines()
file.close()
# print temperature for each 1-Wire Slave
for line in w1_slaves:
# extract 1-wire Slave
w1_slave = line.split("\n")[0]
# 1-wire Slave read file
file = open('/sys/bus/w1/devices/' + str(w1_slave) + '/w1_slave')
filecontent = file.read()
file.close()
# Read values and convert it to a floating point number in celsius
stringvalue = filecontent.split("\n")[1].split(" ")[9]
temperature = float(stringvalue[2:]) / 1000
#publish the values to mqtt broker
client.publish("/1warelocalhost/DS1820/%s" % w1_slave, "%s" % temperature)
# print temperature
# print(str(w1_slave) + ': %s °C' % temperature)
time.sleep(60) # delay for a second
sys.exit(0)
p.s. а как тут отступы указать?
Raspberry PI B+ 1, 2, 3; esp8266; Python