Jose Marco de la Rosa
Published © GPL3+

Android Sensors as IMU for Raspberry Pi 2 via USB and UDP

Use your smartphone as an IMU for your Raspberry Pi project!

IntermediateFull instructions provided3 hours4,981
Android Sensors as IMU for Raspberry Pi 2 via USB and UDP

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Android device
Android device
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Sensor UDP
Fwd: the port forwarding app
Raspbian
Raspberry Pi Raspbian

Story

Read more

Schematics

Smartphone - Raspberry Pi connection

Just connect your smartphone with your Raspberry Pi!

Code

sensors

Python
Small python script to show the sensor values received in the Raspi
import socket
from struct import *

# Prepare the UDP connection
UDP_IP = "192.168.42.100"
print "Receiver IP: ", UDP_IP
UDP_PORT = 50000
print "Port: ", UDP_PORT
sock = socket.socket(socket.AF_INET, # Internet
                    socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

# Continuously read from the UDP socket
while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    # The next line just chops a lot of floats from the received data chunk
    # Just activate and deactivate sensors in the Sensor UDP app to locate
    # the values you want
    print "received message: ",\
        "%1.4f" %unpack_from ('!f', data, 0),\
        "%1.4f" %unpack_from ('!f', data, 4),\
        "%1.4f" %unpack_from ('!f', data, 8),\
        "%1.4f" %unpack_from ('!f', data, 12),\
        "%1.4f" %unpack_from ('!f', data, 16),\
        "%1.4f" %unpack_from ('!f', data, 20),\
        "%1.4f" %unpack_from ('!f', data, 24),\
        "%1.4f" %unpack_from ('!f', data, 28),\
        "%1.4f" %unpack_from ('!f', data, 32),\
        "%1.4f" %unpack_from ('!f', data, 36),\
        "%1.4f" %unpack_from ('!f', data, 40),\
        "%1.4f" %unpack_from ('!f', data, 44),\
        "%1.4f" %unpack_from ('!f', data, 48),\
        "%1.4f" %unpack_from ('!f', data, 52),\
        "%1.4f" %unpack_from ('!f', data, 56),\
        "%1.4f" %unpack_from ('!f', data, 60),\
        "%1.4f" %unpack_from ('!f', data, 64),\
        "%1.4f" %unpack_from ('!f', data, 68),\
        "%1.4f" %unpack_from ('!f', data, 72),\
        "%1.4f" %unpack_from ('!f', data, 76),\
        "%1.4f" %unpack_from ('!f', data, 80),\
        "%1.4f" %unpack_from ('!f', data, 84),\
        "%1.4f" %unpack_from ('!f', data, 88),\
        "%1.4f" %unpack_from ('!f', data, 92)

Credits

Jose Marco de la Rosa

Jose Marco de la Rosa

2 projects • 3 followers
DIY!

Comments