diff --git a/ReadMe.md b/ReadMe.md index 6e182a4..01497b6 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,16 +1,17 @@ # GoodGarden -Welkom bij ons project genaamd "GoodGarden". Wij hebben besloten om er een monolitische project van te maken. Alles is te vinden binnen deze repository. +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". ### Vereisten * Python * Node.Js * XAMPP (of andere naar keuze) + * MQTT Geinstalleerd en toegevoegd aan je PATH variable ### Installeren -Zorg dat je in de hoofdmap "GoodGarden" zit. Kijk in je path: "/GoodGarden". Als je in de correcte map zit moet je de volgende items installeren: +Zorg dat je in de hoofdmap "GoodGarden" zit. Kijk in je path: "../GoodGarden". Als je in de correcte map zit moet je de volgende items installeren: - npm install electron - npm install express diff --git a/mqtt/__pycache__/__init__.cpython-311.pyc b/mqtt/__pycache__/__init__.cpython-311.pyc index 46f90e1..96c729c 100644 Binary files a/mqtt/__pycache__/__init__.cpython-311.pyc and b/mqtt/__pycache__/__init__.cpython-311.pyc differ diff --git a/mqtt/__pycache__/mqtt_client.cpython-311.pyc b/mqtt/__pycache__/mqtt_client.cpython-311.pyc index 8af0f8e..73f1b19 100644 Binary files a/mqtt/__pycache__/mqtt_client.cpython-311.pyc and b/mqtt/__pycache__/mqtt_client.cpython-311.pyc differ diff --git a/mqtt/mqtt_client.py b/mqtt/mqtt_client.py index b14b472..35f8402 100644 --- a/mqtt/mqtt_client.py +++ b/mqtt/mqtt_client.py @@ -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): diff --git a/mqtt/publisher.py b/mqtt/publisher.py index d5c48c7..282b6ba 100644 --- a/mqtt/publisher.py +++ b/mqtt/publisher.py @@ -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) diff --git a/mqtt/subscribe.py b/mqtt/subscribe.py deleted file mode 100644 index 4599c01..0000000 --- a/mqtt/subscribe.py +++ /dev/null @@ -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) \ No newline at end of file diff --git a/script/DUMMY.json b/script/DUMMY.json deleted file mode 100644 index 98acdaf..0000000 --- a/script/DUMMY.json +++ /dev/null @@ -1,123 +0,0 @@ - - // Deze spanningswaarden zijn relatief hoog, wat suggereert dat de batterijen van de sensoren goed opgeladen zijn. De meeste sensorapparaten gebruiken batterijen die een nominale spanning van ongeveer 3,7 volt hebben (typisch voor lithium-ion batterijen) en kunnen worden beschouwd als "volledig opgeladen" wanneer ze een spanning dicht bij of iets boven 4,1 volt bereiken - "battery_voltage_events": [ - { - "timestamp": 1707825721, - "gateway_receive_time": "2024-02-13T12:02:01Z", - "device": 256, - "value": 4.098901271820068 - // Volledig opgeladen: Rond 4.2 volt. - // Nominale spanning: Ongeveer 3.7 volt. - // Kritiek laag: Minder dan 3.0 volt. - }, - { - "timestamp": 1707837460, - "gateway_receive_time": "2024-02-13T15:17:40Z", - "device": 322, - "value": 4.105006217956543 - } - ], - - - - - "devices": [ - { - "id": 256, - "serial_number": "0033889B1BAB1169", - "name": "firefly2_0051", - "label": "The Field", - "last_seen": 1707765066, - "last_battery_voltage": 4.09768009185791 - }, - { - "id": 322, - "serial_number": "006FE1FC316ED7D8", - "name": "firefly2_0111", - "label": "The Field", - "last_seen": 1707764966, - "last_battery_voltage": 4.107448101043701 - } - ], - - - - - - // Fotosyntese??? (zonlicht) - "par_events": [ - { - "timestamp": 1707844638, - "gateway_receive_time": "2024-02-13T17:17:18Z", - "device": 322, - "value": 0.0 - }, - { - "timestamp": 1707851099, - "gateway_receive_time": "2024-02-13T19:04:59Z", - "device": 256, - "value": 0.0 - } - ], - - - - - // Luchtvochtigheid - "relative_humidity_events": [ - { - "timestamp": 1707844638, - "gateway_receive_time": "2024-02-13T17:17:18Z", - "device": 322, - "value": 71.08984375 - }, - { - "timestamp": 1707851099, - "gateway_receive_time": "2024-02-13T19:04:59Z", - "device": 256, - "value": 66.7294921875 - } - ], - - - - - - // De "soil_electric_conductivity_events" gegevens wijzen op metingen van de elektrische geleidbaarheid (EC) van de bodem, een indicator van de hoeveelheid oplosbare zouten of voedingsstoffen in de bodem. EC wordt gemeten in Siemens per meter (S/m) of, voor bodemmetingen, vaak in milliSiemens per centimeter (mS/cm). Een hogere EC-waarde duidt op een hogere concentratie van opgeloste zouten, wat belangrijk is voor het bepalen van de voedingsstatus van de bodem en het beheren van de bemesting voor optimale plantengroei. - "soil_electric_conductivity_events": [ - { - "timestamp": 1707851215, - "gateway_receive_time": "2024-02-13T19:06:55Z", - "device": 322, - "value": 0.0 - } - ], - - - - - - // In de context van bodemmetingen, geeft deze waarde inzicht in het vochtgehalte van de bodem, omdat water een relatief hoge diëlektrische constante heeft in vergelijking met droge bodem of lucht. - "soil_relative_permittivity_events": [ - { - "timestamp": 1707851215, - "gateway_receive_time": "2024-02-13T19:06:55Z", - "device": 322, - "value": 1.52 - // Een waarde van 1.52 is relatief laag en suggereert dat de bodem droog kan zijn op het moment van meting. Ter referentie, de diëlektrische constante van lucht is ongeveer 1, terwijl water een diëlektrische constante heeft van ongeveer 80 bij kamertemperatuur. - } - ], - - - - //De "soil_temperature_events" gegevens bevatten informatie over de temperatuur van de bodem op een specifiek tijdstip. De waarde geeft de temperatuur van de bodem aan in graden Celsius (°C). - "soil_temperature_events": [ - { - "timestamp": 1707851215, - "gateway_receive_time": "2024-02-13T19:06:55Z", - "device": 322, - "value": 12.06 - } - ] - } - \ No newline at end of file diff --git a/script/__pycache__/db_connect.cpython-311.pyc b/script/__pycache__/db_connect.cpython-311.pyc index d9b2127..2a248d0 100644 Binary files a/script/__pycache__/db_connect.cpython-311.pyc and b/script/__pycache__/db_connect.cpython-311.pyc differ diff --git a/script/devices.py b/script/devices.py index 04875c9..b00096c 100644 --- a/script/devices.py +++ b/script/devices.py @@ -1,94 +1,20 @@ -# import requests -# import time - -# from db_connect import database_connect - -# def fetch_and_display_all(url, access_token): -# # 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(300) # Time here is in seconds. -# time.sleep(1) # Time here is in seconds. - -# def load_data(data): -# mydb = database_connect() -# if mydb.is_connected(): -# mycursor = mydb.cursor() - -# # Here you need to adjust the correct column names and data formats based on the API response -# insert_query = """ -# INSERT INTO goodgarden.devices (serial_number, name, label, last_seen, last_battery_voltage) -# VALUES (%s, %s, %s, %s, %s ) -# """ -# for record in data['results']: -# serial_number = record.get('serial_number', '') -# name = record.get('name', '') -# label = record.get('label', '') -# last_seen = record.get('last_seen', '') -# last_battery_voltage = record.get('last_battery_voltage', '') - -# print(f"Inserting data: serial_number={serial_number}, name={name}, label={label}, last_seen={last_seen}, last_battery_voltage={last_battery_voltage}") - -# # Execute the query -# mycursor.execute(insert_query, (serial_number, name, label, last_seen, last_battery_voltage)) - -# # Commit the changes -# mydb.commit() - -# # Close cursor and connection -# mycursor.close() -# mydb.close() - -# print("Data inserted into the database.") - -# if __name__ == "__main__": -# url = "https://garden.inajar.nl/api/devices/?format=json" -# access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Replace this with your actual access token - - -# # 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) - - import sys +import uuid + from os.path import dirname, abspath, join # Voeg het pad naar de 'root' directory toe aan sys.path root_dir = dirname(dirname(abspath(__file__))) sys.path.append(root_dir) -# Nu kan je de mqtt_client importeren from mqtt.mqtt_client import create_client, start_loop -# Je kunt nu de create_client en start_loop functies gebruiken - # Lijst waarop je je wil subscriben -mqtt_topics = [ - "goodgarden/devices" -] +mqtt_topic = "goodgarden/devices" 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}") + client.subscribe(mqtt_topic) + print(f"Subscribed to {mqtt_topic}") def on_message(client, userdata, msg): # Decodeer de payload van bytes naar string @@ -97,5 +23,6 @@ def on_message(client, userdata, msg): # 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 + 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) \ No newline at end of file diff --git a/script/fetch.py b/script/fetch.py deleted file mode 100644 index b83a252..0000000 --- a/script/fetch.py +++ /dev/null @@ -1,12 +0,0 @@ -import paho.mqtt.client as mqtt - -def on_message(client, userdata, msg): - topic = msg.topic - payload = str(msg.payload.decode("utf-8")) - print(f"Message received on topic {topic}: {payload}") - if topic == "goodgarden/temperature": - # Verwerk temperatuurdata - # elif topic == "goodgarden/humidity": - print(f"Message received on topic {topic}: {payload}") - # Verwerk vochtigheidsdata - # Voeg meer condities toe voor andere subtopics \ No newline at end of file diff --git a/script/fetch_data.py b/script/fetch_data.py deleted file mode 100644 index 72964ce..0000000 --- a/script/fetch_data.py +++ /dev/null @@ -1,57 +0,0 @@ -import requests -import time - -from db_connect import database_connect - -def fetch_battery_voltage_events(): - url = "https://garden.inajar.nl/api/battery_voltage_events/?format=json" - headers = { - "Authorization": "Token 33bb3b42452306c58ecedc3c86cfae28ba22329c" - } - - while True: - try: - response = requests.get(url, headers=headers) - response.raise_for_status() - - data = response.json() - load_data(data) - - print("Wachten voor de volgende ophaalactie...") - time.sleep(300) # De tijd hier is in seconden. - - except requests.exceptions.RequestException as e: - print(f"Error fetching data: {e}") - # Wacht ook hier bij een fout, om niet in een snelle foutloop te komen - time.sleep(300) - -def load_data(data): - mydb = database_connect() - if mydb.is_connected(): - mycursor = mydb.cursor() - - # Hier moet je de juiste kolomnamen en dataformaten aanpassen op basis van de API-respons - insert_query = """ - INSERT INTO goodgarden.battery_voltage_events (timestamp, gateway_receive_time, device, value) - VALUES (%s, %s, %s, %s) - """ - for record in data['results']: - timestamp = record['timestamp'] - gateway_receive_time = record['gateway_receive_time'] - device = record['device'] - value = record['value'] - - # print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") - mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) - - # Commit de wijzigingen - mydb.commit() - - # Sluit cursor en verbinding - mycursor.close() - mydb.close() - - print("Data ingevoegd in de database.") - -if __name__ == "__main__": - fetch_battery_voltage_events() \ No newline at end of file diff --git a/script/par_events.py b/script/par_events.py index 8246737..f754603 100644 --- a/script/par_events.py +++ b/script/par_events.py @@ -1,64 +1,24 @@ -import requests -import time +import sys +import uuid -from db_connect import database_connect +from os.path import dirname, abspath, join -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() +root_dir = dirname(dirname(abspath(__file__))) +sys.path.append(root_dir) - data = response.json() - print(f"Data from {url}:") - print(data) - load_data(data) +from mqtt.mqtt_client import create_client, start_loop - except requests.exceptions.RequestException as e: - print(f"Error fetching data from {url}: {e}") +mqtt_topic = "goodgarden/par_events" - print("Waiting for the next retrieval action...") +def on_connect(client, userdata, flags, rc): + client.subscribe(mqtt_topic) + print(f"Subscribed to {mqtt_topic}") - time.sleep(1) # Time here is in seconds. - - -def load_data(data): - mydb = database_connect() - if mydb.is_connected(): - mycursor = mydb.cursor() - - # Here you need to adjust the correct column names and data formats based on the API response - insert_query = """ - INSERT INTO goodgarden.par_events (timestamp, gateway_receive_time, device, value) - VALUES (%s, %s, %s, %s) - """ - for record in data['results']: - timestamp = record.get('timestamp', '') - gateway_receive_time = record.get('gateway_receive_time', '') - device = record.get('device', '') - value = record.get('value', '') - - print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") - - # Execute the query - mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) - - # Commit the changes - mydb.commit() - - # Close cursor and connection - mycursor.close() - mydb.close() - - print("Data inserted into the database.") +def on_message(client, userdata, msg): + message = msg.payload.decode() + print(f"Message received on topic {msg.topic}: {message}") 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) \ No newline at end of file + unique_client_id = f"subscriber_{uuid.uuid4()}" + client = create_client(unique_client_id, on_connect, on_message) + start_loop(client) \ No newline at end of file diff --git a/script/relative_humidity_events.py b/script/relative_humidity_events.py index 6961094..fb53109 100644 --- a/script/relative_humidity_events.py +++ b/script/relative_humidity_events.py @@ -1,64 +1,24 @@ -import requests -import time +import sys +import uuid -from db_connect import database_connect +from os.path import dirname, abspath, join -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() +root_dir = dirname(dirname(abspath(__file__))) +sys.path.append(root_dir) - data = response.json() - print(f"Data from {url}:") - print(data) - load_data(data) +from mqtt.mqtt_client import create_client, start_loop - except requests.exceptions.RequestException as e: - print(f"Error fetching data from {url}: {e}") +mqtt_topic = "goodgarden/relative_humidity" - print("Waiting for the next retrieval action...") +def on_connect(client, userdata, flags, rc): + client.subscribe(mqtt_topic) + print(f"Subscribed to {mqtt_topic}") - time.sleep(1) # Time here is in seconds. - -def load_data(data): - mydb = database_connect() - if mydb.is_connected(): - mycursor = mydb.cursor() - - # Here you need to adjust the correct column names and data formats based on the API response - insert_query = """ - INSERT INTO goodgarden.relative_humidity_events (timestamp, gateway_receive_time, device, value) - VALUES (%s, %s, %s, %s) - """ - for record in data['results']: - timestamp = record.get('timestamp', '') - gateway_receive_time = record.get('gateway_receive_time', '') - device = record.get('device', '') - value = record.get('value', '') - - print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") - - # Execute the query - mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) - - # Commit the changes - mydb.commit() - - # Close cursor and connection - mycursor.close() - mydb.close() - - print("Data inserted into the database.") +def on_message(client, userdata, msg): + message = msg.payload.decode() + print(f"Message received on topic {msg.topic}: {message}") 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) \ No newline at end of file + unique_client_id = f"subscriber_{uuid.uuid4()}" + client = create_client(unique_client_id, on_connect, on_message) + start_loop(client) \ No newline at end of file diff --git a/script/samenvoegen_databases.py b/script/samenvoegen_databases.py new file mode 100644 index 0000000..0a3d195 --- /dev/null +++ b/script/samenvoegen_databases.py @@ -0,0 +1,418 @@ +import requests +import time + +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(): + mycursor = mydb.cursor() + + insert_query = """ + INSERT INTO goodgarden.devices (serial_number, name, label, last_seen, last_battery_voltage) + VALUES (%s, %s, %s, %s, %s ) + """ + for record in data['results']: + serial_number = record.get('serial_number', '') + name = record.get('name', '') + label = record.get('label', '') + last_seen = record.get('last_seen', '') + last_battery_voltage = record.get('last_battery_voltage', '') + + print(f"Inserting data: serial_number={serial_number}, name={name}, label={label}, last_seen={last_seen}, last_battery_voltage={last_battery_voltage}") + + mycursor.execute(insert_query, (serial_number, name, label, last_seen, last_battery_voltage)) + + mydb.commit() + + mycursor.close() + mydb.close() + + 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 ######################## + # # + # # + # # + # # +##########################* 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(): + mycursor = mydb.cursor() + + # Here you need to adjust the correct column names and data formats based on the API response + insert_query = """ + INSERT INTO goodgarden.par_events (timestamp, gateway_receive_time, device, value) + VALUES (%s, %s, %s, %s) + """ + for record in data['results']: + timestamp = record.get('timestamp', '') + gateway_receive_time = record.get('gateway_receive_time', '') + device = record.get('device', '') + value = record.get('value', '') + + print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") + + # Execute the query + mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) + + # Commit the changes + mydb.commit() + + # Close cursor and connection + mycursor.close() + mydb.close() + + 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() + if mydb.is_connected(): + mycursor = mydb.cursor() + + # Here you need to adjust the correct column names and data formats based on the API response + insert_query = """ + INSERT INTO goodgarden.relative_humidity_events (timestamp, gateway_receive_time, device, value) + VALUES (%s, %s, %s, %s) + """ + for record in data['results']: + timestamp = record.get('timestamp', '') + gateway_receive_time = record.get('gateway_receive_time', '') + device = record.get('device', '') + value = record.get('value', '') + + print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") + + # Execute the query + mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) + + # Commit the changes + mydb.commit() + + # Close cursor and connection + mycursor.close() + mydb.close() + + 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() + if mydb.is_connected(): + mycursor = mydb.cursor() + + # Here you need to adjust the correct column names and data formats based on the API response + insert_query = """ + INSERT INTO goodgarden.soil_electric_conductivity_events (timestamp, gateway_receive_time, device, value) + VALUES (%s, %s, %s, %s) + """ + for record in data['results']: + timestamp = record.get('timestamp', '') + gateway_receive_time = record.get('gateway_receive_time', '') + device = record.get('device', '') + value = record.get('value', '') + + print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") + + # Execute the query + mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) + + # Commit the changes + mydb.commit() + + # Close cursor and connection + mycursor.close() + 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 ######################## + # # + # # + # # + # # +##########################* 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(): + mycursor = mydb.cursor() + + # Here you need to adjust the correct column names and data formats based on the API response + insert_query = """ + INSERT INTO goodgarden.soil_temperature_events (timestamp, gateway_receive_time, device, value) + VALUES (%s, %s, %s, %s) + """ + for record in data['results']: + timestamp = record.get('timestamp', '') + gateway_receive_time = record.get('gateway_receive_time', '') + device = record.get('device', '') + value = record.get('value', '') + + print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") + + # Execute the query + mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) + + # Commit the changes + mydb.commit() + + # Close cursor and connection + mycursor.close() + mydb.close() + + 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() + if mydb.is_connected(): + mycursor = mydb.cursor() + + # Here you need to adjust the correct column names and data formats based on the API response + insert_query = """ + INSERT INTO goodgarden.soil_temperature_events (timestamp, gateway_receive_time, device, value) + VALUES (%s, %s, %s, %s) + """ + for record in data['results']: + timestamp = record.get('timestamp', '') + gateway_receive_time = record.get('gateway_receive_time', '') + device = record.get('device', '') + value = record.get('value', '') + + print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") + + # Execute the query + mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) + + # Commit the changes + mydb.commit() + + # Close cursor and connection + 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 diff --git a/script/soil_electric_conductivity_events.py b/script/soil_electric_conductivity_events.py index d9a598a..f3a79f2 100644 --- a/script/soil_electric_conductivity_events.py +++ b/script/soil_electric_conductivity_events.py @@ -1,63 +1,24 @@ -import requests -import time +import sys +import uuid -from db_connect import database_connect +from os.path import dirname, abspath, join -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() +root_dir = dirname(dirname(abspath(__file__))) +sys.path.append(root_dir) - data = response.json() - print(f"Data from {url}:") - print(data) - load_data(data) +from mqtt.mqtt_client import create_client, start_loop - except requests.exceptions.RequestException as e: - print(f"Error fetching data from {url}: {e}") +mqtt_topic = "goodgarden/soil_electric_conductivity" - print("Waiting for the next retrieval action...") +def on_connect(client, userdata, flags, rc): + client.subscribe(mqtt_topic) + print(f"Subscribed to {mqtt_topic}") - time.sleep(1) # Time here is in seconds. +def on_message(client, userdata, msg): + message = msg.payload.decode() + print(f"Message received on topic {msg.topic}: {message}") -def load_data(data): - mydb = database_connect() - if mydb.is_connected(): - mycursor = mydb.cursor() - - # Here you need to adjust the correct column names and data formats based on the API response - insert_query = """ - INSERT INTO goodgarden.soil_electric_conductivity_events (timestamp, gateway_receive_time, device, value) - VALUES (%s, %s, %s, %s) - """ - for record in data['results']: - timestamp = record.get('timestamp', '') - gateway_receive_time = record.get('gateway_receive_time', '') - device = record.get('device', '') - value = record.get('value', '') - - print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") - - # Execute the query - mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) - - # Commit the changes - mydb.commit() - - # Close cursor and connection - mycursor.close() - 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) \ No newline at end of file + unique_client_id = f"subscriber_{uuid.uuid4()}" + client = create_client(unique_client_id, on_connect, on_message) + start_loop(client) \ No newline at end of file diff --git a/script/soil_relative_permittivity_events.py b/script/soil_relative_permittivity_events.py index 62b3483..0c3e584 100644 --- a/script/soil_relative_permittivity_events.py +++ b/script/soil_relative_permittivity_events.py @@ -1,66 +1,24 @@ -import requests -import time +import sys +import uuid -from db_connect import database_connect +from os.path import dirname, abspath, join -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() +root_dir = dirname(dirname(abspath(__file__))) +sys.path.append(root_dir) - data = response.json() - print(f"Data from {url}:") - print(data) - load_data(data) +from mqtt.mqtt_client import create_client, start_loop - except requests.exceptions.RequestException as e: - print(f"Error fetching data from {url}: {e}") +mqtt_topic = "goodgarden/soil_relative_permittivity" - print("Waiting for the next retrieval action...") +def on_connect(client, userdata, flags, rc): + client.subscribe(mqtt_topic) + print(f"Subscribed to {mqtt_topic}") - time.sleep(1) # Time here is in seconds. - - -def load_data(data): - mydb = database_connect() - if mydb.is_connected(): - mycursor = mydb.cursor() - - # Here you need to adjust the correct column names and data formats based on the API response - insert_query = """ - INSERT INTO goodgarden.soil_temperature_events (timestamp, gateway_receive_time, device, value) - VALUES (%s, %s, %s, %s) - """ - for record in data['results']: - timestamp = record.get('timestamp', '') - gateway_receive_time = record.get('gateway_receive_time', '') - device = record.get('device', '') - value = record.get('value', '') - - print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") - - # Execute the query - mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) - - # Commit the changes - mydb.commit() - - # Close cursor and connection - mycursor.close() - mydb.close() - - print("Data inserted into the database.") +def on_message(client, userdata, msg): + message = msg.payload.decode() + print(f"Message received on topic {msg.topic}: {message}") 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) \ No newline at end of file + unique_client_id = f"subscriber_{uuid.uuid4()}" + client = create_client(unique_client_id, on_connect, on_message) + start_loop(client) \ No newline at end of file diff --git a/script/soil_temperature_events.py b/script/soil_temperature_events.py index 4660862..d28a2ff 100644 --- a/script/soil_temperature_events.py +++ b/script/soil_temperature_events.py @@ -1,64 +1,24 @@ -import requests -import time +import sys +import uuid -from db_connect import database_connect +from os.path import dirname, abspath, join -def fetch_and_display_all(url, access_token, repeat_count=5): - for _ in range(repeat_count): +root_dir = dirname(dirname(abspath(__file__))) +sys.path.append(root_dir) - try: - headers = { - "Authorization": f"Token {access_token}" - } - response = requests.get(url, headers=headers) - response.raise_for_status() +from mqtt.mqtt_client import create_client, start_loop - data = response.json() - print(f"Data from {url}:") - print(data) - load_data(data) +mqtt_topic = "goodgarden/soil_temperature" - except requests.exceptions.RequestException as e: - print(f"Error fetching data from {url}: {e}") +def on_connect(client, userdata, flags, rc): + client.subscribe(mqtt_topic) + print(f"Subscribed to {mqtt_topic}") - print("Waiting for the next retrieval action...") +def on_message(client, userdata, msg): + message = msg.payload.decode() + print(f"Message received on topic {msg.topic}: {message}") - time.sleep(1) # Time here is in seconds. - -def load_data(data): - mydb = database_connect() - if mydb.is_connected(): - mycursor = mydb.cursor() - - # Here you need to adjust the correct column names and data formats based on the API response - insert_query = """ - INSERT INTO goodgarden.soil_temperature_events (timestamp, gateway_receive_time, device, value) - VALUES (%s, %s, %s, %s) - """ - for record in data['results']: - timestamp = record.get('timestamp', '') - gateway_receive_time = record.get('gateway_receive_time', '') - device = record.get('device', '') - value = record.get('value', '') - - print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") - - # Execute the query - mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value)) - - # Commit the changes - mydb.commit() - - # Close cursor and connection - 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 + unique_client_id = f"subscriber_{uuid.uuid4()}" + client = create_client(unique_client_id, on_connect, on_message) + start_loop(client) \ No newline at end of file