How to send real-time sensor data to PC from Raspberry Pi Zero?

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

  • 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
  • run a Python Multiprocessing Manager on one machine and connect from the other – see “Using a Remote Manager” here
  • send the readings via Bluetooth

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

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}')

Here’s a possible implementation of the MQTT suggestion. First, the Publisher which is publishing the three values. Note that I have the mosquitto broker running on my desktop:

#!/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()

There’s an example of Bluetooth communication here.

Leave a Comment