如何从Raspberry Pi Zero向PC发送实时传感器数据? [英] How to send real-time sensor data to PC from Raspberry Pi Zero?

查看:69
本文介绍了如何从Raspberry Pi Zero向PC发送实时传感器数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个在 Raspberry Pi Zero W 上运行的 Python3 脚本,该脚本从 IMU 传感器 (MPU9250) 收集数据并创建 3 个不同的角度值;滚动,俯仰,偏航.看起来像这样:

I've written a Python3 script which runs on Raspberry Pi Zero W that collects data from an IMU sensor (MPU9250) and creates 3 different angle values; roll, pitch, yaw. Which looks like this:

def main():
    while True:
        dataAcc = mpu.readAccelerometerMaster()
        dataGyro = mpu.readGyroscopeMaster()
        dataMag = mpu.readMagnetometerMaster()

        [ax, ay, az] = [round(dataAcc[0], 5), round(dataAcc[1], 5), round(dataAcc[2], 5)]
        [gx, gy, gz] = [round(dataGyro[0], 5), round(dataGyro[1], 5), round(dataGyro[2], 5)]
        [mx, my, mz] = [round(dataMag[0], 5), round(dataMag[1], 5), round(dataMag[2], 5)]

        update(gx, gy, gz, ax, ay, az, mx, my, mz)
        roll = getRoll()
        pitch = getPitch()
        yaw = getYaw()

        print(f"Roll: {round(roll, 2)}\tPitch: {round(pitch, 2)}\tYaw: {round(yaw, 2)}")

我想做的是将这 3 个值发送到我的 PC 并读取它们.有什么办法可以发送这些数据.(如果可能,除了serial).

The thing I want to do is send these 3 values to my PC and read them. Is there any way to send this data. (If possible except serial).

推荐答案

有很多方法可以做到这一点,仅举几例:

There are many ways of doing this, to name a few:

  • 从 Raspi 向 PC 发送 UDP 消息
  • 从 Raspi 向 PC 发送 TCP 流
  • 将读数放入 Redis 并允许您网络上的任何人收集它们 - 例如 此处
  • 使用 MQTT 客户端从您的 Raspi 中发布读数,并在您作为服务器的 PC 上订阅该主题
  • 通过蓝牙发送读数
  • send UDP messages from the Raspi to the PC
  • send a TCP stream from the Raspi to the PC
  • throw the readings into Redis and allow anyone on your network to collect them - example here
  • publish the readings from your Raspi with an MQTT client and subscribe to that topic on your PC as server
  • send the readings via Bluetooth

这是上面第一个建议的 UDP 的可能实现.首先,Raspi 端生成 3 个读数 X、Y 和 Z,并通过 UDP 每秒将它们发送到 PC:

Here's a possible implementation of the first suggestion above with UDP. First, the Raspi end generates 3 readings X, Y and Z and sends them to the PC every second via UDP:

#!/usr/bin/env python3

import socket
import sys
from time import sleep
import random
from struct import pack

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

host, port = '192.168.0.8', 65000
server_address = (host, port)

# Generate some random start values
x, y, z = random.random(), random.random(), random.random()

# Send a few messages
for i in range(10):

    # Pack three 32-bit floats into message and send
    message = pack('3f', x, y, z)
    sock.sendto(message, server_address)

    sleep(1)
    x += 1
    y += 1
    z += 1

这是它的 PC 端的匹配代码:

Here's the matching code for the PC end of it:

#!/usr/bin/env python3

import socket
import sys
from struct import unpack

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
host, port = '0.0.0.0', 65000
server_address = (host, port)

print(f'Starting UDP server on {host} port {port}')
sock.bind(server_address)

while True:
    # Wait for message
    message, address = sock.recvfrom(4096)

    print(f'Received {len(message)} bytes:')
    x, y, z = unpack('3f', message)
    print(f'X: {x}, Y: {y}, Z: {z}')


这是 MQTT 建议的可能实现.首先是发布三个值的发布者.请注意,我的桌面上运行了 mosquitto 代理:

#!/usr/bin/env python3

from time import sleep
import random
import paho.mqtt.client as mqtt

broker = '192.168.0.8'
client = mqtt.Client()
client.connect(broker,1883,60)

# Generate some random start values
x, y, z = random.random(), random.random(), random.random()

# Send a few messages
for i in range(10):

    # Publish out three values
    client.publish("topic/XYZ", f'{x},{y},{z}');

    sleep(1)
    x += 1
    y += 1
    z += 1

这里是订阅者,它监听消息并打印它们:

And here is the subscriber, which listens for the messages and prints them:

#!/usr/bin/env python3

import socket
import sys
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
  print("Connected with result code "+str(rc))
  client.subscribe("topic/XYZ")

def on_message(client, userdata, msg):
  message = msg.payload.decode()
  print(f'Message received: {message}')
    
broker = '192.168.0.8'
client = mqtt.Client()
client.connect(broker,1883,60)

client.on_connect = on_connect
client.on_message = on_message

client.loop_forever()

这篇关于如何从Raspberry Pi Zero向PC发送实时传感器数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆