Clean versie mqtt
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,3 @@
|
||||
# mqtt_client.py
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
def create_client(client_id, on_connect, on_message, broker="localhost", port=1883):
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import requests
|
||||
import time
|
||||
from mqtt_client import create_client, start_loop # Importeer de aangepaste MQTT client module
|
||||
|
||||
from mqtt_client import create_client, start_loop
|
||||
|
||||
publish_interval = 300 # Secondes om een aanvraag te doen
|
||||
|
||||
@@ -9,12 +10,11 @@ api_endpoints = [
|
||||
{"url": "https://garden.inajar.nl/api/relative_humidity_events/", "topic": "goodgarden/relative_humidity"},
|
||||
{"url": "https://garden.inajar.nl/api/battery_voltage_events/", "topic": "goodgarden/battery_voltage"},
|
||||
{"url": "https://garden.inajar.nl/api/soil_electric_conductivity_events/", "topic": "goodgarden/soil_electric_conductivity"},
|
||||
{"url": "https://garden.inajar.nl/api/soil_relative_permittivity_events/", "topic": "goodgarden/soil_electric_permittivity"},
|
||||
{"url": "https://garden.inajar.nl/api/soil_relative_permittivity_events/", "topic": "goodgarden/soil_relative_permittivity"},
|
||||
{"url": "https://garden.inajar.nl/api/soil_temperature_events/", "topic": "goodgarden/soil_temperature"},
|
||||
{"url": "https://garden.inajar.nl/api/par_events/", "topic": "goodgarden/par_events"}
|
||||
]
|
||||
|
||||
# Pas de on_connect en on_message functies aan indien nodig voor de publisher
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
print("Connected with result code "+str(rc))
|
||||
|
||||
@@ -37,7 +37,8 @@ def fetch_and_publish_data():
|
||||
for endpoint in api_endpoints:
|
||||
url = endpoint["url"]
|
||||
mqtt_topic = endpoint["topic"]
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang door je echte toegangstoken
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
|
||||
|
||||
try:
|
||||
headers = {"Authorization": f"Token {access_token}"}
|
||||
response = requests.get(url, headers=headers)
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
from mqtt_client import create_client, start_loop
|
||||
|
||||
# Lijst waarop je je wil subscriben
|
||||
mqtt_topics = [
|
||||
"goodgarden/devices",
|
||||
"goodgarden/relative_humidity"
|
||||
]
|
||||
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
print("Connected with result code " + str(rc))
|
||||
# Abonneer op alle topics in de mqtt_topics lijst
|
||||
for topic in mqtt_topics:
|
||||
client.subscribe(topic)
|
||||
print(f"Subscribed to {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__":
|
||||
client = create_client("subscriber1", on_connect, on_message) # Zorg voor een unieke client ID
|
||||
start_loop(client)
|
||||
Reference in New Issue
Block a user