import asyncio
import websockets
import matplotlib.pyplot as plt
import numpy as np
# Create lists to store the received data
time_values = []
acceleration_values = []
#向服务器发送
async def clientRecv(websocket):
while True:
recv_text = await websocket.recv()
print(recv_text)
await websocket.send("OK!")
# Assuming recv_text contains acceleration data, parse and store it
acceleration = float(recv_text) # Assuming recv_text contains a float value
acceleration_values.append(acceleration)
# Update the time values list with the new data
time = len(acceleration_values) * 0.02 # Assuming data is received at 50 Hz
time_values.append(time)
# Update the plot with the new data
plt.clf() # Clear the previous plot
plt.plot(time_values, acceleration_values)
plt.xlabel('Time (s)')
plt.ylabel('Acceleration (m/s$^2$)')
plt.pause(0.01) # Add a small pause to update the plot
plt.show()
# 进行websocket连接
async def clientRun():
async with websockets.connect("ws://192.168.4.1/webcontrol") as websocket:
await clientRecv(websocket)
if __name__ == '__main__':
plt.ion() # Enable interactive mode for real-time plotting
asyncio.get_event_loop().run_until_complete(clientRun())
无线数据传输及可视化
于 2024-07-04 14:44:58 首次发布