database documentation update
This commit is contained in:
@@ -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.
|
At the beginning of the file, the code starts with importing the different required libraries.
|
||||||
```py
|
```py
|
||||||
|
#this library makes functions run simultaneously
|
||||||
import asyncio
|
import asyncio
|
||||||
|
#This library makes the connection to the websocket possible.
|
||||||
import websockets
|
import websockets
|
||||||
|
#This library makes a connection with the database
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
#This library makes json data readable.
|
||||||
import json
|
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.
|
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
|
```py
|
||||||
|
#async is making use of the asyncio library.
|
||||||
async def process_data(data):
|
async def process_data(data):
|
||||||
try:
|
try:
|
||||||
mydb = mysql.connector.connect(
|
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
|
#make a mold for the data to get to the DB
|
||||||
query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
|
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
|
```py
|
||||||
|
#load the json data and make it readable.
|
||||||
processedData = json.loads(data)
|
processedData = json.loads(data)
|
||||||
|
#divide the data into types
|
||||||
processedTemp = (processedData['Temp'])
|
processedTemp = (processedData['Temp'])
|
||||||
processedHumi = (processedData['Humi'])
|
processedHumi = (processedData['Humi'])
|
||||||
processedeCO2 = (processedData['eCO2'])
|
processedeCO2 = (processedData['eCO2'])
|
||||||
processedTvoc = (processedData['TVOC'])
|
processedTvoc = (processedData['TVOC'])
|
||||||
processedMAC = (processedData['node'])
|
processedMAC = (processedData['node'])
|
||||||
|
#make a tuple of the MAC by placing a comma.
|
||||||
MACTuple = (processedMAC,)
|
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()
|
MACDataFetching = MACDataReading.fetchall()
|
||||||
MACArray = list(MACDataFetching)
|
MACArray = list(MACDataFetching)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user