@__deep__ Galileo von EATON gibt es auch als open-version für alle möglichen CPU's, allerdings nur die wirklich Leistungsfähigen. Codesys hat eine Socketverwaltung und auch noch extrem praktischeund stabile UDP Netzvariablen.
Ich habe jetzt einfach ein zweites Programm gemacht um die Werte vom MQTT auszulesen und über tkinter auf einer Seite darzustellen.
Ausser viel Schreibarbeit war es relativ einfach die bidirektionale Kommunikation hinzubekommen. Läuft auch trotz der vielen Programmzeilen stabil. Sind halt meine ersten Schritte mit tkinter :/
Vielleicht habt ihr eine Idee wie man das Ganze kürzen kann.
Es werden ximmer fast gleiche json Strings eingelesen, die als Kenner immer eine Sensornr haben und dann, je nacj angeschlossenen Sensoren, ab und zu unterschiedliche, aber bekannte Key's haben, für die einzelnen Werte. Das müsste ja irgendwie über eine Schleife machbar sein, wenn es schon zu Fuss fast nur aus copy and paste besteht. Meine uralte programmierbare Alphakey Tastatur hatte mächtig damit zu tun die Blöcke zu generieren
Beim Import habe ich tkinter ganz importiert, weil sonst sticky nicht funktioniert hat.
Gibt es die Möglichkeit ein Template anzulegen, wenn man immer gleiche Label hat ?
Kann man dem Grid Manager beibringen, Nachkommastellen immer mit 2 digits anzuzeigen ?
Code: Alles auswählen
#!/usr/bin/python3
import paho.mqtt.client as mqtt
from tkinter import * # wegen sticky usw.
import time
import json
import sys
def on_connect(mqttsub, obj, flags, rc):
print("on Connect", "rc: " + str(rc))
pass
def on_message(mqttsub, obj, msg):
print("message", msg.topic + " " + str(msg.qos) + " " + msg.payload.decode('UTF-8'))
json_payload = json.loads(str(msg.payload.decode('UTF-8')))
# print("payload", payload) # {"Sensor":2,"Temperature":20.40,"Humidity":37.40,"Moisture":28.13,"Tmp_DS18B20":19.50}
sensor = int(json_payload['Sensor'])
title0.config(text="Plant Sensors Group 1 : ")
topic0.config(text= "")
# Sensor Titles in column 1
title1_01.config(text="NodeMcu Sensoren : ")
title1_02.config(text="Temperature : ")
title1_03.config(text="Humidity : ")
title1_04.config(text="Moisture : ")
title1_05.config(text="Soil_Temp : ")
title1_06.config(text="Weight : ")
title1_07.config(text="CO₂ ppm : ")
title1_08.config(text="Conductivity: ")
title1_09.config(text="pH Value : ")
title1_10.config(text="Air Temperature :")
title1_11.config(text="Air Humidity :")
title1_12.config(text="Air Pressure :")
title1_13.config(text="Sunlight Intensity :")
title1_14.config(text="UV Intensity :")
title1_15.config(text="Particle 10.0 µg/m³:")
title1_16.config(text="Particle 2.5 µg/m³:")
title1_17.config(text="Windspeed :")
title1_18.config(text="------------------ :")
title1_19.config(text="Rain Yesterday :")
title1_20.config(text="Ŕain Today :")
title1_21.config(text="Add. Water Today :")
title1_22.config(text="Ionisation :")
# Sensor values from ModeMCU's in columns 2-16
if sensor == 1 :
topic1_1.config(text= json_payload['Sensor'])
topic1_2.config(text= json_payload['Temperature'])
topic1_3.config(text= json_payload['Humidity'])
topic1_4.config(text= json_payload['Moisture'])
topic1_5.config(text= json_payload['Tmp_DS18B20'])
topic1_6.config(text= "") # empty for further use
elif sensor == 2 :
topic2_1.config(text= json_payload['Sensor'])
topic2_2.config(text= json_payload['Temperature'])
topic2_3.config(text= json_payload['Humidity'])
topic2_4.config(text= json_payload['Moisture'])
topic2_5.config(text= json_payload['Tmp_DS18B20'])
topic2_6.config(text= "")
elif sensor == 3 :
topic3_1.config(text= json_payload['Sensor'])
topic3_2.config(text= json_payload['Temperature'])
topic3_3.config(text= json_payload['Humidity'])
topic3_4.config(text= "") #json_payload['Moisture']) not installed
topic3_5.config(text= "") #json_payload['Tmp_DS18B20']) not installed
topic3_6.config(text= json_payload['CO2ppm']) # CO2 Sensor
elif sensor == 4 :
topic4_1.config(text= json_payload['Sensor'])
topic4_2.config(text= json_payload['Temperature'])
topic4_3.config(text= json_payload['Humidity'])
topic4_4.config(text= "") # json_payload['Moisture']) not installed
topic4_5.config(text= "") #json_payload['Tmp_DS18B20']) not installed
topic4_6.config(text= json_payload['Weight']) # scale sensor
elif sensor == 5 :
topic5_1.config(text= json_payload['Sensor'])
topic5_2.config(text= json_payload['Temperature'])
topic5_3.config(text= json_payload['Humidity'])
topic5_4.config(text= json_payload['Moisture'])
topic5_5.config(text= json_payload['Tmp_DS18B20'])
topic5_6.config(text= "")
elif sensor == 6 :
topic6_1.config(text= json_payload['Sensor'])
topic6_2.config(text= json_payload['Temperature'])
topic6_3.config(text= json_payload['Humidity'])
topic6_4.config(text= json_payload['Moisture'])
topic6_5.config(text= json_payload['Tmp_DS18B20'])
topic6_6.config(text= "")
elif sensor == 7 :
topic7_1.config(text= json_payload['Sensor'])
topic7_2.config(text= json_payload['Temperature'])
topic7_3.config(text= json_payload['Humidity'])
topic7_4.config(text= json_payload['Moisture'])
topic7_5.config(text= json_payload['Tmp_DS18B20'])
topic7_6.config(text= "")
#additional Sensorvalues from Raspberry and Homematic
#json string via mqtt
#topic8_1 - topic22_6
def on_publish(mqttsub, obj, mid):
pass
def on_subscribe(mqttsub, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
pass
def publish_message_water_on():
out_topic = 'ESP_01/nodemcu6' # waterpump connected on client 6
message = ('{"DHT-TEMP":0,"DHT-HUM":0, "DS18B20":0,"ANALOG":0,"TIME":10000,"SWITCH":1}')
mqttsub.publish(out_topic, message)
print(message, " to Client 6 sent" )
def publish_message_water_off():
out_topic = 'ESP_01/nodemcu6'
message = ('{"DHT-TEMP":0,"DHT-HUM":0, "DS18B20":0,"ANALOG":0,"TIME":0,"SWITCH":0}') #
mqttsub.publish(out_topic, message)
print(message, " to Client 6 sent" )
#----------------------------------------------------------
# Main prog
mqttsub = mqtt.Client()
mqttsub.on_message = on_message
mqttsub.on_connect = on_connect
mqttsub.on_publish = on_publish
mqttsub.on_subscribe = on_subscribe
mqttsub.connect("192.168.10.125", 1883, 60)
mqttsub.subscribe("ESP_01", 0)
rootWindow = Tk()
rootWindow.title('MQTT monitor')
rootWindow.geometry("1600x800+200+100")
title0 = Label(rootWindow, font = ('fixed', 11),) # show group
title0.grid(sticky = W, row = 1, column = 1, padx = 3, pady = 3)
topic0 = Label(rootWindow, font = ('fixed', 11),)
topic0.grid(sticky = W, row = 1, column = 2, padx = 3, pady = 3)
# sensor names in column 1
title1_01 = Label(rootWindow, font = ('fixed', 11),)
title1_01.grid(sticky = W, row = 2, column = 1, padx = 3, pady = 3)
title1_02 = Label(rootWindow, font = ('fixed', 11),)
title1_02.grid(sticky = W, row = 3, column = 1, padx = 3, pady = 3)
title1_03 = Label(rootWindow, font = ('fixed', 11),)
title1_03.grid(sticky = W, row = 4, column = 1, padx = 3, pady = 3)
title1_04 = Label(rootWindow, font = ('fixed', 11),)
title1_04.grid(sticky = W, row = 5, column = 1, padx = 3, pady = 3)
title1_05 = Label(rootWindow, font = ('fixed', 11),)
title1_05.grid(sticky = W, row = 6, column = 1, padx = 3, pady = 3)
title1_06 = Label(rootWindow, font = ('fixed', 11),)
title1_06.grid(sticky = W, row = 7, column = 1, padx = 3, pady = 3)
title1_07 = Label(rootWindow, font = ('fixed', 11),)
title1_07.grid(sticky = W, row = 8, column = 1, padx = 3, pady = 3)
title1_08 = Label(rootWindow, font = ('fixed', 11),)
title1_08.grid(sticky = W, row = 9, column = 1, padx = 3, pady = 3)
title1_09 = Label(rootWindow, font = ('fixed', 11),)
title1_09.grid(sticky = W, row = 10, column = 1, padx = 3, pady = 3)
title1_10 = Label(rootWindow, font = ('fixed', 11),)
title1_10.grid(sticky = W, row = 11, column = 1, padx = 3, pady = 3)
title1_11 = Label(rootWindow, font = ('fixed', 11),)
title1_11.grid(sticky = W, row = 12, column = 1, padx = 3, pady = 3)
title1_12 = Label(rootWindow, font = ('fixed', 11),)
title1_12.grid(sticky = W, row = 13, column = 1, padx = 3, pady = 3)
title1_13 = Label(rootWindow, font = ('fixed', 11),)
title1_13.grid(sticky = W, row = 14, column = 1, padx = 3, pady = 3)
title1_14 = Label(rootWindow, font = ('fixed', 11),)
title1_14.grid(sticky = W, row = 15, column = 1, padx = 3, pady = 3)
title1_15 = Label(rootWindow, font = ('fixed', 11),)
title1_15.grid(sticky = W, row = 16, column = 1, padx = 3, pady = 3)
title1_16 = Label(rootWindow, font = ('fixed', 11),)
title1_16.grid(sticky = W, row = 17, column = 1, padx = 3, pady = 3)
title1_17 = Label(rootWindow, font = ('fixed', 11),)
title1_17.grid(sticky = W, row = 18, column = 1, padx = 3, pady = 3)
title1_18 = Label(rootWindow, font = ('fixed', 11),)
title1_18.grid(sticky = W, row = 19, column = 1, padx = 3, pady = 3)
title1_19 = Label(rootWindow, font = ('fixed', 11),)
title1_19.grid(sticky = W, row = 20, column = 1, padx = 3, pady = 3)
title1_20 = Label(rootWindow, font = ('fixed', 11),)
title1_20.grid(sticky = W, row = 21, column = 1, padx = 3, pady = 3)
title1_21 = Label(rootWindow, font = ('fixed', 11),)
title1_21.grid(sticky = W, row = 22, column = 1, padx = 3, pady = 3)
title1_22 = Label(rootWindow, font = ('fixed', 11),)
title1_22.grid(sticky = W, row = 23, column = 1, padx = 3, pady = 3)
# sensor values in columns 2-6
topic1_1 = Label(rootWindow, font = ('fixed', 12),) #sensor nr bigger and in middle
topic1_1.grid(sticky = N, row = 2, column = 2, padx = 3, pady = 3)
topic1_2 = Label(rootWindow, font = ('fixed', 11),)
topic1_2.grid(sticky = W, row = 3, column = 2, padx = 3, pady = 3)
topic1_3 = Label(rootWindow, font = ('fixed', 11),)
topic1_3.grid(sticky = W, row = 4, column = 2, padx = 3, pady = 3)
topic1_4 = Label(rootWindow, font = ('fixed', 11),)
topic1_4.grid(sticky = W, row = 5, column = 2, padx = 3, pady = 3)
topic1_5 = Label(rootWindow, font = ('fixed', 11),)
topic1_5.grid(sticky = W, row = 6, column = 2, padx = 3, pady = 3)
topic1_6 = Label(rootWindow, font = ('fixed', 11),)
topic1_6.grid(sticky = W, row = 7, column = 2, padx = 3, pady = 3)
topic2_1 = Label(rootWindow, font = ('fixed', 12),)
topic2_1.grid(sticky = N, row = 2, column = 3, padx = 3, pady = 3)
topic2_2 = Label(rootWindow, font = ('fixed', 11),)
topic2_2.grid(sticky = W, row = 3, column = 3, padx = 3, pady = 3)
topic2_3 = Label(rootWindow, font = ('fixed', 11),)
topic2_3.grid(sticky = W, row = 4, column = 3, padx = 3, pady = 3)
topic2_4 = Label(rootWindow, font = ('fixed', 11),)
topic2_4.grid(sticky = W, row = 5, column = 3, padx = 3, pady = 3)
topic2_5 = Label(rootWindow, font = ('fixed', 11),)
topic2_5.grid(sticky = W, row = 6, column = 3, padx = 3, pady = 3)
topic2_6 = Label(rootWindow, font = ('fixed', 11),)
topic2_6.grid(sticky = W, row = 7, column = 3, padx = 3, pady = 3)
topic3_1 = Label(rootWindow, font = ('fixed', 12),)
topic3_1.grid(sticky = N, row = 2, column = 4, padx = 3, pady = 3)
topic3_2 = Label(rootWindow, font = ('fixed', 11),)
topic3_2.grid(sticky = W, row = 3, column = 4, padx = 3, pady = 3)
topic3_3 = Label(rootWindow, font = ('fixed', 11),)
topic3_3.grid(sticky = W, row = 4, column = 4, padx = 3, pady = 3)
topic3_4 = Label(rootWindow, font = ('fixed', 11),)
topic3_4.grid(sticky = W, row = 5, column = 4, padx = 3, pady = 3)
topic3_5 = Label(rootWindow, font = ('fixed', 11),)
topic3_5.grid(sticky = W, row = 6, column = 4, padx = 3, pady = 3)
topic3_6 = Label(rootWindow, font = ('fixed', 11),)
topic3_6.grid(sticky = W, row = 8, column = 4, padx = 3, pady = 3) # co2ppm
topic4_1 = Label(rootWindow, font = ('fixed', 12),)
topic4_1.grid(sticky = N, row = 2, column = 5, padx = 3, pady = 3)
topic4_2 = Label(rootWindow, font = ('fixed', 11),)
topic4_2.grid(sticky = W, row = 3, column = 5, padx = 3, pady = 3)
topic4_3 = Label(rootWindow, font = ('fixed', 11),)
topic4_3.grid(sticky = W, row = 4, column = 5, padx = 3, pady = 3)
topic4_4 = Label(rootWindow, font = ('fixed', 11),)
topic4_4.grid(sticky = W, row = 5, column = 5, padx = 3, pady = 3)
topic4_5 = Label(rootWindow, font = ('fixed', 11),)
topic4_5.grid(sticky = W, row = 6, column = 5, padx = 3, pady = 3)
topic4_6 = Label(rootWindow, font = ('fixed', 11),)
topic4_6.grid(sticky = W, row = 7, column = 5, padx = 3, pady = 3) # scale
topic5_1 = Label(rootWindow, font = ('fixed', 12),)
topic5_1.grid(sticky = N, row = 2, column = 6, padx = 3, pady = 3)
topic5_2 = Label(rootWindow, font = ('fixed', 11),)
topic5_2.grid(sticky = W, row = 3, column = 6, padx = 3, pady = 3)
topic5_3 = Label(rootWindow, font = ('fixed', 11),)
topic5_3.grid(sticky = W, row = 4, column = 6, padx = 3, pady = 3)
topic5_4 = Label(rootWindow, font = ('fixed', 11),)
topic5_4.grid(sticky = W, row = 5, column = 6, padx = 3, pady = 3)
topic5_5 = Label(rootWindow, font = ('fixed', 11),)
topic5_5.grid(sticky = W, row = 6, column = 6, padx = 3, pady = 3)
topic5_6 = Label(rootWindow, font = ('fixed', 11),)
topic5_6.grid(sticky = W, row = 7, column = 6, padx = 3, pady = 3)
topic6_1 = Label(rootWindow, font = ('fixed', 12),)
topic6_1.grid(sticky = N, row = 2, column = 7, padx = 3, pady = 3)
topic6_2 = Label(rootWindow, font = ('fixed', 11),)
topic6_2.grid(sticky = W, row = 3, column = 7, padx = 3, pady = 3)
topic6_3 = Label(rootWindow, font = ('fixed', 11),)
topic6_3.grid(sticky = W, row = 4, column = 7, padx = 3, pady = 3)
topic6_4 = Label(rootWindow, font = ('fixed', 11),)
topic6_4.grid(sticky = W, row = 5, column = 7, padx = 3, pady = 3)
topic6_5 = Label(rootWindow, font = ('fixed', 11),)
topic6_5.grid(sticky = W, row = 6, column = 7, padx = 3, pady = 3)
topic6_6 = Label(rootWindow, font = ('fixed', 11),)
topic6_6.grid(sticky = W, row = 7, column = 7, padx = 3, pady = 3)
topic7_1 = Label(rootWindow, font = ('fixed', 12),)
topic7_1.grid(sticky = N, row = 2, column = 8, padx = 3, pady = 3)
topic7_2 = Label(rootWindow, font = ('fixed', 11),)
topic7_2.grid(sticky = W, row = 3, column = 8, padx = 3, pady = 3)
topic7_3 = Label(rootWindow, font = ('fixed', 11),)
topic7_3.grid(sticky = W, row = 4, column = 8, padx = 3, pady = 3)
topic7_4 = Label(rootWindow, font = ('fixed', 11),)
topic7_4.grid(sticky = W, row = 5, column = 8, padx = 3, pady = 3)
topic7_5 = Label(rootWindow, font = ('fixed', 11),)
topic7_5.grid(sticky = W, row = 6, column = 8, padx = 3, pady = 3)
topic7_6 = Label(rootWindow, font = ('fixed', 11),)
topic7_6.grid(sticky = W, row = 7, column = 8, padx = 3, pady = 3)
#additional Sensorvalues from Raspberry and Homematic
#topic8_1 - topic22_6
#Buttons
button01 = Button(rootWindow, text="Close Window", width=25, command= rootWindow.destroy).grid(row=30, column=10)
button02 = Button(rootWindow, text="Stop Programm", width=25, command = sys.exit).grid(row=31, column=10)
button03 = Button(rootWindow, text="Run Water for 10 secs", width=25, command = publish_message_water_on).grid(row=32, column=10)
button03 = Button(rootWindow, text="Stop Water", width=25, command = publish_message_water_off).grid(row=33, column=10)
mqttsub.loop_start()
rootWindow.mainloop()
# end of prog