From fa7baccffd85c46670a1293c7e7e62c530dc2a1c Mon Sep 17 00:00:00 2001 From: Bram Barbieri Date: Thu, 4 Apr 2024 17:18:39 +0200 Subject: [PATCH] database documentation update --- docs/rpi-documentation/Databaseconnection.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/rpi-documentation/Databaseconnection.md b/docs/rpi-documentation/Databaseconnection.md index 1a8f2b2..536c913 100644 --- a/docs/rpi-documentation/Databaseconnection.md +++ b/docs/rpi-documentation/Databaseconnection.md @@ -13,13 +13,18 @@ In the given Raspberry Pi, a file named "data.py" was created, from which this s At the beginning of the file, the code starts with importing the different required libraries. ```py +#this library makes functions run simultaneously import asyncio +#This library makes the connection to the websocket possible. import websockets +#This library makes a connection with the database import mysql.connector +#This library makes json data readable. import json ``` Afterward, a function would be called related to the "mysql.connector" library, where you would provide the login credentials of the database you have set up to establish a solid connection. This connection will be utilized multiple times later in the code. ```py +#async is making use of the asyncio library. async def process_data(data): try: mydb = mysql.connector.connect( @@ -44,16 +49,27 @@ Afterward, a query is made for a different part of the code and acts as a "mold" #make a mold for the data to get to the DB query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)" ``` +In the next part of the code, the data collected from the node (which is done later on) is processed here and made readable for the Python code. +This processed data is then split up into five different types: the temperature, the humidity, the eCO2 and TVOC values, and the MAC address from which this information was sent. + +The MAC address is then taken and turned into a tuple. This is done because the database expects a tuple to be inserted instead of a string. + +(for more information on tuples I suggest visiting https://www.w3schools.com/python/python_tuples.asp) ```py + #load the json data and make it readable. processedData = json.loads(data) + #divide the data into types processedTemp = (processedData['Temp']) processedHumi = (processedData['Humi']) processedeCO2 = (processedData['eCO2']) processedTvoc = (processedData['TVOC']) processedMAC = (processedData['node']) + #make a tuple of the MAC by placing a comma. MACTuple = (processedMAC,) - +``` +Coming back to previous lines of code, the data which was first aked for is now gatherd and gets put in an array. +```py MACDataFetching = MACDataReading.fetchall() MACArray = list(MACDataFetching)