De echte 'clean' versie!!!!
This commit is contained in:
Binary file not shown.
@@ -1,9 +1,10 @@
|
||||
import requests
|
||||
import time
|
||||
import json
|
||||
|
||||
from mqtt_client import create_client, start_loop
|
||||
|
||||
publish_interval = 300 # Secondes om een aanvraag te doen
|
||||
publish_interval = 30 # Secondes om een aanvraag te doen - MOET ~300 ZIJN!!!!!!!!!
|
||||
|
||||
api_endpoints = [
|
||||
{"url": "https://garden.inajar.nl/api/devices/", "topic": "goodgarden/devices"},
|
||||
@@ -24,16 +25,13 @@ def on_message(client, userdata, msg):
|
||||
client = create_client("publisher1", on_connect, on_message) # Gebruik een unieke client ID
|
||||
|
||||
def publish_to_mqtt(topic, data):
|
||||
"""
|
||||
Publiceer de opgehaalde data naar een MQTT-topic.
|
||||
"""
|
||||
client.publish(topic, str(data))
|
||||
|
||||
json_data = json.dumps(data) # Serialiseer de data naar een JSON-string
|
||||
client.publish(topic, json_data)
|
||||
print(f"Data published to MQTT topic {topic}.")
|
||||
|
||||
def fetch_and_publish_data():
|
||||
"""
|
||||
Haal data op van alle endpoints en publiceer naar MQTT.
|
||||
"""
|
||||
|
||||
for endpoint in api_endpoints:
|
||||
url = endpoint["url"]
|
||||
mqtt_topic = endpoint["topic"]
|
||||
@@ -54,6 +52,6 @@ if __name__ == "__main__":
|
||||
client.loop_start() # Start de niet-blokkerende loop
|
||||
while True:
|
||||
fetch_and_publish_data()
|
||||
print("Waiting for the next retrieval action...")
|
||||
print("Wachten, wachten en nog eens wachten...")
|
||||
time.sleep(publish_interval)
|
||||
client.loop_stop()
|
||||
|
||||
@@ -1,28 +1,13 @@
|
||||
import sys
|
||||
import uuid
|
||||
import json
|
||||
|
||||
from os.path import dirname, abspath, join
|
||||
from paho.mqtt import subscribe
|
||||
|
||||
# Voeg het pad naar de 'root' directory toe aan sys.path
|
||||
root_dir = dirname(dirname(abspath(__file__)))
|
||||
sys.path.append(root_dir)
|
||||
def on_message(client, userdata, message):
|
||||
payload_str = message.payload.decode("utf-8")
|
||||
data = json.loads(payload_str)
|
||||
|
||||
from mqtt.mqtt_client import create_client, start_loop
|
||||
|
||||
# Lijst waarop je je wil subscriben
|
||||
mqtt_topic = "goodgarden/devices"
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(mqtt_topic)
|
||||
print(f"Subscribed to {mqtt_topic}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
# Decodeer de payload van bytes naar string
|
||||
message = msg.payload.decode()
|
||||
print(f"Message received on topic {msg.topic}: {message}")
|
||||
# Hier kun je code toevoegen om iets te doen met het ontvangen bericht
|
||||
print(f"Message received on topic {message.topic}: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unique_client_id = f"subscriber_{uuid.uuid4()}" # Zorg voor een unieke client ID, zodat meerdere subscribers kunnen runnen
|
||||
client = create_client(unique_client_id, on_connect, on_message)
|
||||
start_loop(client)
|
||||
topic = "goodgarden/devices"
|
||||
subscribe.callback(on_message, topic)
|
||||
@@ -1,24 +1,25 @@
|
||||
import sys
|
||||
import uuid
|
||||
import json
|
||||
|
||||
from os.path import dirname, abspath, join
|
||||
from paho.mqtt import subscribe
|
||||
|
||||
root_dir = dirname(dirname(abspath(__file__)))
|
||||
sys.path.append(root_dir)
|
||||
def on_message(client, userdata, message):
|
||||
payload_str = message.payload.decode("utf-8")
|
||||
data = json.loads(payload_str)
|
||||
|
||||
from mqtt.mqtt_client import create_client, start_loop
|
||||
device_322_value = None
|
||||
device_256_value = None
|
||||
|
||||
mqtt_topic = "goodgarden/par_events"
|
||||
for key in data["results"]:
|
||||
if key["device"] == 322:
|
||||
device_322_value = key["value"]
|
||||
elif key["device"] == 256:
|
||||
device_256_value = key["value"]
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(mqtt_topic)
|
||||
print(f"Subscribed to {mqtt_topic}")
|
||||
print(f"Device 322 value: {device_322_value}")
|
||||
print(f"Device 256 value: {device_256_value}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
message = msg.payload.decode()
|
||||
print(f"Message received on topic {msg.topic}: {message}")
|
||||
print(f"Message received on topic {message.topic}: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unique_client_id = f"subscriber_{uuid.uuid4()}"
|
||||
client = create_client(unique_client_id, on_connect, on_message)
|
||||
start_loop(client)
|
||||
topic = "goodgarden/par_events"
|
||||
subscribe.callback(on_message, topic)
|
||||
@@ -1,24 +1,13 @@
|
||||
import sys
|
||||
import uuid
|
||||
import json
|
||||
|
||||
from os.path import dirname, abspath, join
|
||||
from paho.mqtt import subscribe
|
||||
|
||||
root_dir = dirname(dirname(abspath(__file__)))
|
||||
sys.path.append(root_dir)
|
||||
def on_message(client, userdata, message):
|
||||
payload_str = message.payload.decode("utf-8")
|
||||
data = json.loads(payload_str)
|
||||
|
||||
from mqtt.mqtt_client import create_client, start_loop
|
||||
|
||||
mqtt_topic = "goodgarden/relative_humidity"
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(mqtt_topic)
|
||||
print(f"Subscribed to {mqtt_topic}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
message = msg.payload.decode()
|
||||
print(f"Message received on topic {msg.topic}: {message}")
|
||||
print(f"Message received on topic {message.topic}: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unique_client_id = f"subscriber_{uuid.uuid4()}"
|
||||
client = create_client(unique_client_id, on_connect, on_message)
|
||||
start_loop(client)
|
||||
topic = "goodgarden/relative_humidity"
|
||||
subscribe.callback(on_message, topic)
|
||||
@@ -1,24 +1,13 @@
|
||||
import sys
|
||||
import uuid
|
||||
import json
|
||||
|
||||
from os.path import dirname, abspath, join
|
||||
from paho.mqtt import subscribe
|
||||
|
||||
root_dir = dirname(dirname(abspath(__file__)))
|
||||
sys.path.append(root_dir)
|
||||
def on_message(client, userdata, message):
|
||||
payload_str = message.payload.decode("utf-8")
|
||||
data = json.loads(payload_str)
|
||||
|
||||
from mqtt.mqtt_client import create_client, start_loop
|
||||
|
||||
mqtt_topic = "goodgarden/soil_electric_conductivity"
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(mqtt_topic)
|
||||
print(f"Subscribed to {mqtt_topic}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
message = msg.payload.decode()
|
||||
print(f"Message received on topic {msg.topic}: {message}")
|
||||
print(f"Message received on topic {message.topic}: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unique_client_id = f"subscriber_{uuid.uuid4()}"
|
||||
client = create_client(unique_client_id, on_connect, on_message)
|
||||
start_loop(client)
|
||||
topic = "goodgarden/soil_electric_conductivity"
|
||||
subscribe.callback(on_message, topic)
|
||||
@@ -1,24 +1,13 @@
|
||||
import sys
|
||||
import uuid
|
||||
import json
|
||||
|
||||
from os.path import dirname, abspath, join
|
||||
from paho.mqtt import subscribe
|
||||
|
||||
root_dir = dirname(dirname(abspath(__file__)))
|
||||
sys.path.append(root_dir)
|
||||
def on_message(client, userdata, message):
|
||||
payload_str = message.payload.decode("utf-8")
|
||||
data = json.loads(payload_str)
|
||||
|
||||
from mqtt.mqtt_client import create_client, start_loop
|
||||
|
||||
mqtt_topic = "goodgarden/soil_relative_permittivity"
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(mqtt_topic)
|
||||
print(f"Subscribed to {mqtt_topic}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
message = msg.payload.decode()
|
||||
print(f"Message received on topic {msg.topic}: {message}")
|
||||
print(f"Message received on topic {message.topic}: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unique_client_id = f"subscriber_{uuid.uuid4()}"
|
||||
client = create_client(unique_client_id, on_connect, on_message)
|
||||
start_loop(client)
|
||||
topic = "goodgarden/soil_relative_permittivity"
|
||||
subscribe.callback(on_message, topic)
|
||||
@@ -1,24 +1,13 @@
|
||||
import sys
|
||||
import uuid
|
||||
import json
|
||||
|
||||
from os.path import dirname, abspath, join
|
||||
from paho.mqtt import subscribe
|
||||
|
||||
root_dir = dirname(dirname(abspath(__file__)))
|
||||
sys.path.append(root_dir)
|
||||
def on_message(client, userdata, message):
|
||||
payload_str = message.payload.decode("utf-8")
|
||||
data = json.loads(payload_str)
|
||||
|
||||
from mqtt.mqtt_client import create_client, start_loop
|
||||
|
||||
mqtt_topic = "goodgarden/soil_temperature"
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
client.subscribe(mqtt_topic)
|
||||
print(f"Subscribed to {mqtt_topic}")
|
||||
|
||||
def on_message(client, userdata, msg):
|
||||
message = msg.payload.decode()
|
||||
print(f"Message received on topic {msg.topic}: {message}")
|
||||
print(f"Message received on topic {message.topic}: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unique_client_id = f"subscriber_{uuid.uuid4()}"
|
||||
client = create_client(unique_client_id, on_connect, on_message)
|
||||
start_loop(client)
|
||||
topic = "goodgarden/soil_temperature"
|
||||
subscribe.callback(on_message, topic)
|
||||
Reference in New Issue
Block a user