database documentation presentable
This commit is contained in:
@@ -13,6 +13,7 @@ import json
|
|||||||
```
|
```
|
||||||
Then we began the process of connecting with both the websocket and the database.
|
Then we began the process of connecting with both the websocket and the database.
|
||||||
|
|
||||||
|
First-off, we began making a connection to the database by using a mysql library in wich we gave the log in in order to connect to our ow database.
|
||||||
```js
|
```js
|
||||||
//the data that has to be pushed needs to be
|
//the data that has to be pushed needs to be
|
||||||
async def process_data(data):
|
async def process_data(data):
|
||||||
@@ -23,6 +24,10 @@ async def process_data(data):
|
|||||||
password="*********",
|
password="*********",
|
||||||
database="*******"
|
database="*******"
|
||||||
)
|
)
|
||||||
|
```
|
||||||
|
Then after this we code in the infromation we want to put inside of the database.
|
||||||
|
The data collected from the websocket is json data, so this has to be changed.
|
||||||
|
```js
|
||||||
cursor = mydb.cursor()
|
cursor = mydb.cursor()
|
||||||
// the segments in wich the infromation will be sent.
|
// the segments in wich the infromation will be sent.
|
||||||
query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
|
query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
|
||||||
@@ -48,16 +53,21 @@ async def process_data(data):
|
|||||||
finally:
|
finally:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
mydb.close()
|
mydb.close()
|
||||||
|
```
|
||||||
|
After fully connecting t othe database, making statements of what to put there and telling the code what to do, we ofcourse need to write the code to connect to the weebsocket.
|
||||||
|
We begin by telling our websocket id and what type of port we are using.
|
||||||
|
Then we will collect live data from the conected websocket, store it in a variable, and then in the previous code
|
||||||
|
```js
|
||||||
//here the connection to the websocked is made
|
//here the connection to the websocked is made
|
||||||
async def receive_data():
|
async def receive_data():
|
||||||
uri = "ws://145.92.8.114/ws"
|
uri = "****************"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with websockets.connect(uri) as websocket:
|
async with websockets.connect(uri) as websocket:
|
||||||
while True:
|
while True:
|
||||||
// the data collected from the websocket is.
|
// the data collected from the websocket is.
|
||||||
data = await websocket.recv()
|
data = await websocket.recv()
|
||||||
// dara recieved conformation.
|
// data recieved: conformation.
|
||||||
print(f"Received data: {data}")
|
print(f"Received data: {data}")
|
||||||
await process_data(data)
|
await process_data(data)
|
||||||
// error sowing.
|
// error sowing.
|
||||||
|
Reference in New Issue
Block a user