Kleine veranderingen mqtt
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# GoodGarden
|
||||
|
||||
Welkom bij ons project genaamd "GoodGarden". Wij hebben besloten om er een semi-monolitische project van te maken. Alles is te vinden binnen deze repository, inclusief de "mqtt".
|
||||
Welkom bij ons project genaamd "GoodGarden". Wij hebben besloten om er een semi-monolithic project van te maken. Alles is te vinden binnen deze repository, inclusief de "mqtt" publisher.
|
||||
|
||||
### Vereisten
|
||||
|
||||
|
||||
@@ -28,7 +28,8 @@ def publish_to_mqtt(topic, 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}.")
|
||||
print(f"\033[92mData published to MQTT topic {topic}.\033[0m")
|
||||
|
||||
|
||||
def fetch_and_publish_data():
|
||||
|
||||
@@ -52,6 +53,8 @@ if __name__ == "__main__":
|
||||
client.loop_start() # Start de niet-blokkerende loop
|
||||
while True:
|
||||
fetch_and_publish_data()
|
||||
print("Wachten, wachten en nog eens wachten...")
|
||||
print(f"\033[91mWachten, wachten en nog eens wachten... {publish_interval} secondes!\033[0m\n")
|
||||
|
||||
|
||||
time.sleep(publish_interval)
|
||||
client.loop_stop()
|
||||
client.loop_stop()
|
||||
|
||||
@@ -6,6 +6,18 @@ def on_message(client, userdata, message):
|
||||
payload_str = message.payload.decode("utf-8")
|
||||
data = json.loads(payload_str)
|
||||
|
||||
device_256 = 0
|
||||
device_322 = 0
|
||||
|
||||
for key in data["results"]:
|
||||
if int(key["id"]) == 256:
|
||||
device_256 = int(key["id"])
|
||||
print(f"{device_256}")
|
||||
|
||||
elif int(key["id"]) == 322:
|
||||
device_322 = int(key["id"])
|
||||
print(f"{device_322}")
|
||||
|
||||
print(f"Message received on topic {message.topic}: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -5,24 +5,6 @@ from db_connect import database_connect
|
||||
|
||||
##########################* DEVICES #######################
|
||||
|
||||
def fetch_and_display_all(url, access_token):
|
||||
|
||||
try:
|
||||
headers = {
|
||||
"Authorization": f"Token {access_token}"
|
||||
}
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
print(f"Data from {url}:")
|
||||
print(data)
|
||||
load_data(data)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching data from {url}: {e}")
|
||||
print("Waiting for the next retrieval action...")
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
def load_data(data):
|
||||
mydb = database_connect()
|
||||
if mydb.is_connected():
|
||||
@@ -50,12 +32,6 @@ def load_data(data):
|
||||
|
||||
print("Data inserted into the database.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
url = "https://garden.inajar.nl/api/devices/?format=json"
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
|
||||
|
||||
fetch_and_display_all(url, access_token)
|
||||
|
||||
############################### EINDE ########################
|
||||
# #
|
||||
# #
|
||||
@@ -63,33 +39,6 @@ if __name__ == "__main__":
|
||||
# #
|
||||
##########################* PAR_EVENTS #######################
|
||||
|
||||
import requests
|
||||
import time
|
||||
|
||||
from db_connect import database_connect
|
||||
|
||||
def fetch_and_display_all(url, access_token, repeat_count=5):
|
||||
for _ in range(repeat_count):
|
||||
try:
|
||||
headers = {
|
||||
"Authorization": f"Token {access_token}"
|
||||
}
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
print(f"Data from {url}:")
|
||||
print(data)
|
||||
load_data(data)
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching data from {url}: {e}")
|
||||
|
||||
print("Waiting for the next retrieval action...")
|
||||
|
||||
time.sleep(1) # Time here is in seconds.
|
||||
|
||||
|
||||
def load_data(data):
|
||||
mydb = database_connect()
|
||||
if mydb.is_connected():
|
||||
@@ -120,14 +69,6 @@ def load_data(data):
|
||||
|
||||
print("Data inserted into the database.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
url = "https://garden.inajar.nl/api/par_events/?format=json"
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
|
||||
# You can change the repeat_count to control how many times you want to repeat the process
|
||||
repeat_count = 10
|
||||
|
||||
fetch_and_display_all(url, access_token, repeat_count)
|
||||
|
||||
############################### EINDE ########################
|
||||
# #
|
||||
# #
|
||||
@@ -135,32 +76,6 @@ if __name__ == "__main__":
|
||||
# #
|
||||
##########################* RELATIVE_HUMIDITY_EVENTS #######################
|
||||
|
||||
import requests
|
||||
import time
|
||||
|
||||
from db_connect import database_connect
|
||||
|
||||
def fetch_and_display_all(url, access_token, repeat_count=5):
|
||||
for _ in range(repeat_count):
|
||||
try:
|
||||
headers = {
|
||||
"Authorization": f"Token {access_token}"
|
||||
}
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
print(f"Data from {url}:")
|
||||
print(data)
|
||||
load_data(data)
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching data from {url}: {e}")
|
||||
|
||||
print("Waiting for the next retrieval action...")
|
||||
|
||||
time.sleep(1) # Time here is in seconds.
|
||||
|
||||
def load_data(data):
|
||||
mydb = database_connect()
|
||||
if mydb.is_connected():
|
||||
@@ -191,15 +106,6 @@ def load_data(data):
|
||||
|
||||
print("Data inserted into the database.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
url = "https://garden.inajar.nl/api/relative_humidity_events/?format=json"
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
|
||||
|
||||
# You can change the repeat_count to control how many times you want to repeat the process
|
||||
repeat_count = 10
|
||||
|
||||
fetch_and_display_all(url, access_token, repeat_count)
|
||||
|
||||
############################### EINDE ########################
|
||||
# #
|
||||
# #
|
||||
@@ -207,32 +113,6 @@ if __name__ == "__main__":
|
||||
# #
|
||||
##########################* SOIL_ELECTRIC_CONDUCTIVITY_EVENTS #######################
|
||||
|
||||
import requests
|
||||
import time
|
||||
|
||||
from db_connect import database_connect
|
||||
|
||||
def fetch_and_display_all(url, access_token, repeat_count=5):
|
||||
for _ in range(repeat_count):
|
||||
try:
|
||||
headers = {
|
||||
"Authorization": f"Token {access_token}"
|
||||
}
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
print(f"Data from {url}:")
|
||||
print(data)
|
||||
load_data(data)
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching data from {url}: {e}")
|
||||
|
||||
print("Waiting for the next retrieval action...")
|
||||
|
||||
time.sleep(1) # Time here is in seconds.
|
||||
|
||||
def load_data(data):
|
||||
mydb = database_connect()
|
||||
if mydb.is_connected():
|
||||
@@ -262,14 +142,6 @@ def load_data(data):
|
||||
mydb.close()
|
||||
|
||||
print("Data inserted into the database.")
|
||||
if __name__ == "__main__":
|
||||
url = "https://garden.inajar.nl/api/soil_electric_conductivity_events/?format=json"
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Replace this with your actual access token
|
||||
|
||||
# You can change the repeat_count to control how many times you want to repeat the process
|
||||
repeat_count = 10
|
||||
|
||||
fetch_and_display_all(url, access_token, repeat_count)
|
||||
|
||||
############################### EINDE ########################
|
||||
# #
|
||||
@@ -278,33 +150,6 @@ if __name__ == "__main__":
|
||||
# #
|
||||
##########################* SOIL_TEMPERATURE_EVENTS #######################
|
||||
|
||||
import requests
|
||||
import time
|
||||
|
||||
from db_connect import database_connect
|
||||
|
||||
def fetch_and_display_all(url, access_token, repeat_count=5):
|
||||
for _ in range(repeat_count):
|
||||
try:
|
||||
headers = {
|
||||
"Authorization": f"Token {access_token}"
|
||||
}
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
print(f"Data from {url}:")
|
||||
print(data)
|
||||
load_data(data)
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching data from {url}: {e}")
|
||||
|
||||
print("Waiting for the next retrieval action...")
|
||||
|
||||
time.sleep(1) # Time here is in seconds.
|
||||
|
||||
|
||||
def load_data(data):
|
||||
mydb = database_connect()
|
||||
if mydb.is_connected():
|
||||
@@ -335,16 +180,6 @@ def load_data(data):
|
||||
|
||||
print("Data inserted into the database.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
url = "https://garden.inajar.nl/api/soil_relative_permittivity_events/?format=json"
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
|
||||
# You can change the repeat_count to control how many times you want to repeat the process
|
||||
repeat_count = 10
|
||||
|
||||
# fetch_and_display_all(urls, access_token)
|
||||
|
||||
fetch_and_display_all(url, access_token, repeat_count)
|
||||
|
||||
############################### EINDE ########################
|
||||
# #
|
||||
# #
|
||||
@@ -352,33 +187,6 @@ if __name__ == "__main__":
|
||||
# #
|
||||
##########################* SOIL_TEMPERATURE_EVENTS #######################
|
||||
|
||||
import requests
|
||||
import time
|
||||
|
||||
from db_connect import database_connect
|
||||
|
||||
def fetch_and_display_all(url, access_token, repeat_count=5):
|
||||
for _ in range(repeat_count):
|
||||
|
||||
try:
|
||||
headers = {
|
||||
"Authorization": f"Token {access_token}"
|
||||
}
|
||||
response = requests.get(url, headers=headers)
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
print(f"Data from {url}:")
|
||||
print(data)
|
||||
load_data(data)
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching data from {url}: {e}")
|
||||
|
||||
print("Waiting for the next retrieval action...")
|
||||
|
||||
time.sleep(1) # Time here is in seconds.
|
||||
|
||||
def load_data(data):
|
||||
mydb = database_connect()
|
||||
if mydb.is_connected():
|
||||
@@ -408,11 +216,3 @@ def load_data(data):
|
||||
mydb.close()
|
||||
|
||||
print("Data inserted into the database.")
|
||||
if __name__ == "__main__":
|
||||
url = "https://garden.inajar.nl/api/soil_temperature_events/?format=json"
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Replace this with your actual access token
|
||||
|
||||
# You can change the repeat_count to control how many times you want to repeat the process
|
||||
repeat_count = 10
|
||||
|
||||
fetch_and_display_all(url, access_token, repeat_count)
|
||||
Reference in New Issue
Block a user