Clean versie mqtt

This commit is contained in:
Atilla
2024-03-12 19:56:49 +01:00
parent c30cedc9e0
commit 261427519f
17 changed files with 513 additions and 584 deletions

View File

@@ -1,16 +1,17 @@
# GoodGarden # 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 ### Vereisten
* Python * Python
* Node.Js * Node.Js
* XAMPP (of andere naar keuze) * XAMPP (of andere naar keuze)
* MQTT Geinstalleerd en toegevoegd aan je PATH variable
### Installeren ### 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 electron
- npm install express - npm install express

View File

@@ -1,4 +1,3 @@
# mqtt_client.py
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
def create_client(client_id, on_connect, on_message, broker="localhost", port=1883): def create_client(client_id, on_connect, on_message, broker="localhost", port=1883):

View File

@@ -1,6 +1,7 @@
import requests import requests
import time 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 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/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/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_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/soil_temperature_events/", "topic": "goodgarden/soil_temperature"},
{"url": "https://garden.inajar.nl/api/par_events/", "topic": "goodgarden/par_events"} {"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): def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc)) print("Connected with result code "+str(rc))
@@ -37,7 +37,8 @@ def fetch_and_publish_data():
for endpoint in api_endpoints: for endpoint in api_endpoints:
url = endpoint["url"] url = endpoint["url"]
mqtt_topic = endpoint["topic"] mqtt_topic = endpoint["topic"]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang door je echte toegangstoken access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
try: try:
headers = {"Authorization": f"Token {access_token}"} headers = {"Authorization": f"Token {access_token}"}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)

View File

@@ -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)

View File

@@ -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
}
]
}

View File

@@ -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 sys
import uuid
from os.path import dirname, abspath, join from os.path import dirname, abspath, join
# Voeg het pad naar de 'root' directory toe aan sys.path # Voeg het pad naar de 'root' directory toe aan sys.path
root_dir = dirname(dirname(abspath(__file__))) root_dir = dirname(dirname(abspath(__file__)))
sys.path.append(root_dir) sys.path.append(root_dir)
# Nu kan je de mqtt_client importeren
from mqtt.mqtt_client import create_client, start_loop 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 # Lijst waarop je je wil subscriben
mqtt_topics = [ mqtt_topic = "goodgarden/devices"
"goodgarden/devices"
]
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc)) client.subscribe(mqtt_topic)
# Abonneer op alle topics in de mqtt_topics lijst print(f"Subscribed to {mqtt_topic}")
for topic in mqtt_topics:
client.subscribe(topic)
print(f"Subscribed to {topic}")
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
# Decodeer de payload van bytes naar string # 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 # Hier kun je code toevoegen om iets te doen met het ontvangen bericht
if __name__ == "__main__": 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) start_loop(client)

View File

@@ -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

View File

@@ -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()

View File

@@ -1,64 +1,24 @@
import requests import sys
import time 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): root_dir = dirname(dirname(abspath(__file__)))
for _ in range(repeat_count): sys.path.append(root_dir)
try:
headers = {
"Authorization": f"Token {access_token}"
}
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json() from mqtt.mqtt_client import create_client, start_loop
print(f"Data from {url}:")
print(data)
load_data(data)
except requests.exceptions.RequestException as e: mqtt_topic = "goodgarden/par_events"
print(f"Error fetching data from {url}: {e}")
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.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__": if __name__ == "__main__":
url = "https://garden.inajar.nl/api/par_events/?format=json" unique_client_id = f"subscriber_{uuid.uuid4()}"
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" client = create_client(unique_client_id, on_connect, on_message)
# You can change the repeat_count to control how many times you want to repeat the process start_loop(client)
repeat_count = 10
fetch_and_display_all(url, access_token, repeat_count)

View File

@@ -1,64 +1,24 @@
import requests import sys
import time 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): root_dir = dirname(dirname(abspath(__file__)))
for _ in range(repeat_count): sys.path.append(root_dir)
try:
headers = {
"Authorization": f"Token {access_token}"
}
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json() from mqtt.mqtt_client import create_client, start_loop
print(f"Data from {url}:")
print(data)
load_data(data)
except requests.exceptions.RequestException as e: mqtt_topic = "goodgarden/relative_humidity"
print(f"Error fetching data from {url}: {e}")
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()
def load_data(data): print(f"Message received on topic {msg.topic}: {message}")
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__": if __name__ == "__main__":
url = "https://garden.inajar.nl/api/relative_humidity_events/?format=json" unique_client_id = f"subscriber_{uuid.uuid4()}"
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" client = create_client(unique_client_id, on_connect, on_message)
start_loop(client)
# 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)

View File

@@ -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)

View File

@@ -1,63 +1,24 @@
import requests import sys
import time 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): root_dir = dirname(dirname(abspath(__file__)))
for _ in range(repeat_count): sys.path.append(root_dir)
try:
headers = {
"Authorization": f"Token {access_token}"
}
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json() from mqtt.mqtt_client import create_client, start_loop
print(f"Data from {url}:")
print(data)
load_data(data)
except requests.exceptions.RequestException as e: mqtt_topic = "goodgarden/soil_electric_conductivity"
print(f"Error fetching data from {url}: {e}")
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__": if __name__ == "__main__":
url = "https://garden.inajar.nl/api/soil_electric_conductivity_events/?format=json" unique_client_id = f"subscriber_{uuid.uuid4()}"
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Replace this with your actual access token client = create_client(unique_client_id, on_connect, on_message)
start_loop(client)
# 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)

View File

@@ -1,66 +1,24 @@
import requests import sys
import time 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): root_dir = dirname(dirname(abspath(__file__)))
for _ in range(repeat_count): sys.path.append(root_dir)
try:
headers = {
"Authorization": f"Token {access_token}"
}
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json() from mqtt.mqtt_client import create_client, start_loop
print(f"Data from {url}:")
print(data)
load_data(data)
except requests.exceptions.RequestException as e: mqtt_topic = "goodgarden/soil_relative_permittivity"
print(f"Error fetching data from {url}: {e}")
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_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__": if __name__ == "__main__":
url = "https://garden.inajar.nl/api/soil_relative_permittivity_events/?format=json" unique_client_id = f"subscriber_{uuid.uuid4()}"
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" client = create_client(unique_client_id, on_connect, on_message)
# You can change the repeat_count to control how many times you want to repeat the process start_loop(client)
repeat_count = 10
# fetch_and_display_all(urls, access_token)
fetch_and_display_all(url, access_token, repeat_count)

View File

@@ -1,64 +1,24 @@
import requests import sys
import time 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): root_dir = dirname(dirname(abspath(__file__)))
for _ in range(repeat_count): sys.path.append(root_dir)
try: from mqtt.mqtt_client import create_client, start_loop
headers = {
"Authorization": f"Token {access_token}"
}
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json() mqtt_topic = "goodgarden/soil_temperature"
print(f"Data from {url}:")
print(data)
load_data(data)
except requests.exceptions.RequestException as e: def on_connect(client, userdata, flags, rc):
print(f"Error fetching data from {url}: {e}") 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__": if __name__ == "__main__":
url = "https://garden.inajar.nl/api/soil_temperature_events/?format=json" unique_client_id = f"subscriber_{uuid.uuid4()}"
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Replace this with your actual access token client = create_client(unique_client_id, on_connect, on_message)
start_loop(client)
# 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)