From 4b3d6ea7ccdc97264f798bbbe3b7b82c2d5cef4e Mon Sep 17 00:00:00 2001 From: Atilla Date: Wed, 13 Mar 2024 09:19:15 +0100 Subject: [PATCH] Kleine veranderingen mqtt --- ReadMe.md | 2 +- mqtt/publisher.py | 9 +- script/devices.py | 12 ++ script/samenvoegen_databases.py | 202 +------------------------------- 4 files changed, 20 insertions(+), 205 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 01497b6..f95aff9 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -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 diff --git a/mqtt/publisher.py b/mqtt/publisher.py index a324c5e..b6ff42a 100644 --- a/mqtt/publisher.py +++ b/mqtt/publisher.py @@ -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() diff --git a/script/devices.py b/script/devices.py index b26fe50..b0cd8df 100644 --- a/script/devices.py +++ b/script/devices.py @@ -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__": diff --git a/script/samenvoegen_databases.py b/script/samenvoegen_databases.py index 0a3d195..b604664 100644 --- a/script/samenvoegen_databases.py +++ b/script/samenvoegen_databases.py @@ -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,46 +69,12 @@ 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 ######################## # # # # # # # # ##########################* 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() @@ -191,47 +106,12 @@ 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 ######################## # # # # # # # # ##########################* 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() @@ -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,49 +180,12 @@ 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 ######################## # # # # # # # # ##########################* 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() @@ -407,12 +215,4 @@ def load_data(data): mycursor.close() 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) \ No newline at end of file + print("Data inserted into the database.") \ No newline at end of file