无线数据传输及可视化

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())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值