some update
This commit is contained in:
parent
cbd4870b0c
commit
e7ba61585b
104
src/main.py
104
src/main.py
@ -1,21 +1,113 @@
|
|||||||
import time
|
import time
|
||||||
|
import json
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
from gps_hat import GPS_DATA
|
from gps_hat import GPS_DATA
|
||||||
from sens_hat import SENS_HAT
|
from sens_hat import SENS_HAT
|
||||||
|
|
||||||
|
def update_hat_data():
|
||||||
|
if gps.get_gps_info():
|
||||||
|
# print(gps)
|
||||||
|
client.publish("MOBKOM/GPS/N", gps.breitengrad)
|
||||||
|
client.publish("MOBKOM/GPS/E", gps.laengengrad)
|
||||||
|
client.publish("MOBKOM/GPS/hoehe", gps.hoehe)
|
||||||
|
client.publish("MOBKOM/GPS/geschwindigkeit", gps.geschwindigkeit_km_h)
|
||||||
|
|
||||||
|
sens.get_sensor_data()
|
||||||
|
client.publish("MOBKOM/SENS/temp", sens.temp)
|
||||||
|
client.publish("MOBKOM/SENS/humidity", sens.humidity)
|
||||||
|
client.publish("MOBKOM/SENS/pressure", sens.pressure)
|
||||||
|
|
||||||
|
client.publish("MOBKOM/IMU/pitch", sens.pitch)
|
||||||
|
client.publish("MOBKOM/IMU/roll", sens.roll)
|
||||||
|
client.publish("MOBKOM/IMU/yaw", sens.yaw)
|
||||||
|
client.publish("MOBKOM/IMU/x", sens.x)
|
||||||
|
client.publish("MOBKOM/IMU/y", sens.y)
|
||||||
|
client.publish("MOBKOM/IMU/z", sens.z)
|
||||||
|
# print(sens.temp)
|
||||||
|
|
||||||
|
def encode_led_set_payload(payload):
|
||||||
|
try:
|
||||||
|
data = json.loads(payload)
|
||||||
|
x = int(data["pos"][0])
|
||||||
|
y = int(data["pos"][1])
|
||||||
|
r = int(data["rgb"][0])
|
||||||
|
b = int(data["rgb"][1])
|
||||||
|
g = int(data["rgb"][2])
|
||||||
|
print(data)
|
||||||
|
sens.led_set_pixel(x, y, r, g, b)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print("Ungültiges JSON formatt:\n" + payload)
|
||||||
|
except KeyError:
|
||||||
|
print("Ungültige Keys:\n" + payload)
|
||||||
|
except ValueError:
|
||||||
|
print("Werte müssen int sein:\n" + payload)
|
||||||
|
|
||||||
|
def encode_led_get_payload(payload):
|
||||||
|
try:
|
||||||
|
data = json.loads(payload)
|
||||||
|
x = int(data["pos"][0])
|
||||||
|
y = int(data["pos"][1])
|
||||||
|
print(data)
|
||||||
|
# sens.led_set_pixel(x, y)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
print("Ungültiges JSON formatt:\n" + payload)
|
||||||
|
except KeyError:
|
||||||
|
print("Ungültige Keys:\n" + payload)
|
||||||
|
except ValueError:
|
||||||
|
print("Werte müssen int sein:\n" + payload)
|
||||||
|
|
||||||
|
|
||||||
|
def on_message(client, userdata, message):
|
||||||
|
payload = message.payload.decode("utf-8").lower()
|
||||||
|
topic = message.topic
|
||||||
|
|
||||||
|
match topic:
|
||||||
|
case "MOBKOM/LED/clear":
|
||||||
|
print("clear LEDs")
|
||||||
|
sens.led_clear()
|
||||||
|
case "MOBKOM/LED/set_pixel":
|
||||||
|
encode_led_set_payload(payload)
|
||||||
|
case "MOBKOM/LED/get_pixel":
|
||||||
|
encode_led_get_payload(payload)
|
||||||
|
case _:
|
||||||
|
print(f"Eingehend auf {topic}: {payload}")
|
||||||
|
|
||||||
|
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
if rc == 0:
|
||||||
|
print(f"Connected")
|
||||||
|
else:
|
||||||
|
print("Failed to connect, return code %d\n", rc)
|
||||||
|
exit(2)
|
||||||
|
client.subscribe("MOBKOM/test")
|
||||||
|
client.subscribe("MOBKOM/LED/set_pixel")
|
||||||
|
client.subscribe("MOBKOM/LED/get_pixel")
|
||||||
|
client.subscribe("MOBKOM/LED/clear")
|
||||||
|
client.on_message = on_message
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# gps = GPS_DATA("/dev/ttyUSB0")
|
# gps = GPS_DATA("/dev/ttyUSB0")
|
||||||
gps = GPS_DATA("/dev/serial0")
|
gps = GPS_DATA("/dev/serial0")
|
||||||
sens = SENS_HAT()
|
sens = SENS_HAT()
|
||||||
|
|
||||||
|
client = mqtt.Client()
|
||||||
|
client.connect("localhost", 1883)
|
||||||
|
|
||||||
|
client.on_connect = on_connect
|
||||||
|
|
||||||
|
update_timer = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
if gps.get_gps_info():
|
if update_timer + 2 < time.time():
|
||||||
print(gps)
|
update_hat_data()
|
||||||
print(sens.temp)
|
update_timer = time.time()
|
||||||
time.sleep(2)
|
|
||||||
|
client.loop()
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
gps.ser.close()
|
gps.ser.close()
|
||||||
|
client.disconnect()
|
||||||
|
|
||||||
|
|||||||
@ -47,3 +47,9 @@ class SENS_HAT:
|
|||||||
return
|
return
|
||||||
|
|
||||||
self.sense.set_pixel(x, y, r, g, b)
|
self.sense.set_pixel(x, y, r, g, b)
|
||||||
|
|
||||||
|
def led_get_pixel(self, x, y):
|
||||||
|
if not (0 <= x <= 7) or not (0 <= y <= 7):
|
||||||
|
print("x und y müssen zwischen 0 und 7 sein!")
|
||||||
|
return
|
||||||
|
return self.led_matrix[x][y]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user