Repository navigation
Expand file tree
/
Copy pathreceive_IMU_GPS.py
More file actions
57 lines (49 loc) · 1.85 KB
/
Copy pathreceive_IMU_GPS.py
File metadata and controls
57 lines (49 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import asyncio
import websockets
import sqlite3
# Establish connection to the database
conn = sqlite3.connect('rover_data.db')
cursor = conn.cursor()
# Define a function to create the table if it doesn't exist
def create_table():
cursor.execute("""
CREATE TABLE IF NOT EXISTS SensorData (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
pitch REAL,
roll REAL,
yaw REAL,
longitude REAL,
latitude REAL,
speed REAL
)
""")
conn.commit()
create_table()
async def insert_data(pitch, roll, yaw, longitude, latitude, speed):
cursor.execute("""
INSERT INTO SensorData (pitch, roll, yaw, longitude, latitude, speed)
VALUES (?, ?, ?, ?, ?, ?)
""", (pitch, roll, yaw, longitude, latitude, speed))
conn.commit()
async def handler(websocket, path):
async for message in websocket:
print(f"Received message: {message}")
# Assuming the data format is: pitch,roll,yaw,longitude,latitude,speed
if message:
try:
data = message.split(',')
pitch = float(data[0])
roll = float(data[1])
yaw = float(data[2])
longitude = float(data[3])
latitude = float(data[4])
speed = float(data[5])
# Insert data into the database
await insert_data(pitch, roll, yaw, longitude, latitude, speed)
except Exception as e:
print(f"Error processing message: {e}")
start_server = websockets.serve(handler, "0.0.0.0", 5678)
print("Websocket server started")
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()