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