Remote testing a Raspberry with HMC5883L

I wanted to get a taste of how precise and instantly the magnetometer worked, so my first program will read ‘the values’ and ‘show it’ on the screen.

Useful resources on the internet are:

    1. Using a HMC5883L magnetometer/compass with the #RaspberryPi
    2. Using the HMC5883L magnetometer/compass on the Raspberry Pi
    3. I2C Python Library – 3-Axis Digital Compass (HMC5883L) with the Raspberry Pi
    4. OpenGL with PyOpenGL introduction and creation of Rotating Cube
    5. Remote Programming of RaspberryPi using PyCharm – CodeProject
    6. PyCharm 2016.1 Help :: Remote Debugging – JetBrains

To get started, 1 and 2 show an example of how to use the library from 3, that I needed to access through, to get data from the compass. The compass readout code needs to run on the Pi, but my Pi is not backed up and I like the PyCharm IDE, so I also spend some time setting up remote development and debugging using PyCharm using 5 and 6. Finally, the raw data library 3 is presented as three coordinates x, y and z. Instead of printing values on the console, I wanted to visualize it, so I modified the example program from 4 to include an line (edge) from the origin to (x,y,z).

Pi compass programm

The Pi program sets up the i2c connection to the compass and then listens for HTTP requests. On a request of /, the current values are output as a json blob.

from i2clibraries import i2c_hmc5883l
from flask import Flask, Response
from json import dumps
import math

con = i2c_hmc5883l.i2c_hmc5883l(1)

app = Flask(__name__)

def index():
    (x, y, z) = con.getAxes()
    payload = dumps({'x': x, 'y': y, 'z': z})
    return Response(payload, mimetype="application/json")

if __name__ == '__main__':, host="")

Host glplot program

The host program is in a game loop. In each loop, the program will read from the Pi with http the latest axes values, plot the cube and the compass edge. It also averages the latest MAX retrieved values to smooth the direction.

import pygame
import numpy as np
import requests
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *


verticies = [
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1),

edges = (

MAX = 30
xa = np.zeros(MAX,)
ya = np.zeros(MAX,)
za = np.zeros(MAX,)
array_index = 0

def arrayMean(a):
    return np.mean(a)

def arrayAdd(t):
    global array_index, MAX

    xa[array_index] = t[0]
    ya[array_index] = t[1]
    za[array_index] = t[2]
    array_index = array_index + 1
    if array_index >= MAX:
        array_index = 0

def getAxes():
    response = requests.get(PIURL)
    data = response.json()
    t = (data.get('x'), data.get('y'), data.get('z'))
    # return t
    return (arrayMean(xa), arrayMean(ya), arrayMean(za))

def arrow():
    verticies[9] = getAxes()

    for edge in edges:
        for vertex in edge:


def main():
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 500.0)

    glTranslatef(0.0,0.0, -5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:

        # glRotatef(1, 3, 1, 1)

if __name__ == '__main__':




Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen. logo

Je reageert onder je account. Log uit /  Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit /  Bijwerken )


Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s