JSON MQTT receive and Sensor-Class added
This commit is contained in:
parent
d27076ba54
commit
a3e3da1297
@ -8,4 +8,4 @@ Jinja2==3.1.2
|
||||
MarkupSafe==2.1.2
|
||||
requests==2.28.2
|
||||
urllib3==1.26.14
|
||||
paho-mqtt
|
||||
paho-mqtt==1.6.1
|
35
software/roboter/raspy/mainProg.py
Normal file
35
software/roboter/raspy/mainProg.py
Normal file
@ -0,0 +1,35 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
from raspySensors import RaspySensors
|
||||
|
||||
#region global Varaibles
|
||||
sensors = RaspySensors()
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region MQTT callbacks
|
||||
def send_data_json(client, userdata, message):
|
||||
strIn = str(message.payload.decode("UTF-8"))
|
||||
dataDict = json.loads(strIn)
|
||||
print("Received data: ", json.dumps(dataDict))
|
||||
|
||||
def drive_plant_1(clients, userdata, message):
|
||||
print("Driving to plant 1")
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
mqttBroker = "mqtt.eclipseprojects.io"
|
||||
client = mqtt.Client("Smartphone_temp")
|
||||
|
||||
dataDict = {}
|
||||
|
||||
client.message_callback_add("Robot/Data", send_data_json)
|
||||
client.message_callback_add("Robot/Plant/1", drive_plant_1)
|
||||
|
||||
client.connect(mqttBroker)
|
||||
client.subscribe("Robot/Data")
|
||||
client.subscribe("Robot/Plant/1")
|
||||
|
||||
client.loop_forever()
|
44
software/roboter/raspy/raspySensors.py
Normal file
44
software/roboter/raspy/raspySensors.py
Normal file
@ -0,0 +1,44 @@
|
||||
from picamera import PiCamera
|
||||
|
||||
class RaspySensors:
|
||||
'''Class to handle all sensors'''
|
||||
#Air Temp&Humidity
|
||||
dht22 = 0
|
||||
|
||||
#Earth Humidity
|
||||
|
||||
|
||||
#Brightness Sensor
|
||||
|
||||
|
||||
#PiCamera
|
||||
camera = PiCamera()
|
||||
|
||||
#GPS
|
||||
|
||||
#Varaibles
|
||||
sensorData ={
|
||||
"Air Temperature" : 0,
|
||||
"Air Humidity" : 0,
|
||||
"Earth Humidity" : 0,
|
||||
"Brightness" : 0
|
||||
}
|
||||
image = 0
|
||||
position = 0
|
||||
|
||||
def __init__(self) -> None:
|
||||
'''Init all Sensors
|
||||
#Message if Error'''
|
||||
dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
|
||||
|
||||
def readSensors(self):
|
||||
'''Read all Sensors and return Dictionary with data'''
|
||||
return self.sensorData
|
||||
|
||||
def takePicture(self):
|
||||
'''Take picture and return image'''
|
||||
return self.image
|
||||
|
||||
def readPosition(self):
|
||||
'''Read and return Position'''
|
||||
return self.position
|
20
software/roboter/raspy/receive_json.py
Normal file
20
software/roboter/raspy/receive_json.py
Normal file
@ -0,0 +1,20 @@
|
||||
import paho.mqtt.client as mqtt
|
||||
import json
|
||||
|
||||
def on_message_json(client, userdata, message):
|
||||
strIn = str(message.payload.decode("UTF-8"))
|
||||
dataDict = json.loads(strIn)
|
||||
print("Received data: ", json.dumps(dataDict))
|
||||
|
||||
|
||||
mqttBroker = "mqtt.eclipseprojects.io"
|
||||
client = mqtt.Client("Smartphone_temp")
|
||||
|
||||
dataDict = {}
|
||||
|
||||
client.message_callback_add("Robot/Data", on_message_json)
|
||||
|
||||
client.connect(mqttBroker)
|
||||
client.subscribe("Robot/Data")
|
||||
|
||||
client.loop_forever()
|
Loading…
x
Reference in New Issue
Block a user