Python and sensors on the web


This site uses a version of python compiled to run in your web browser, with some javascript added to allow you to read from sensors on your computer or phone and draw graphs.

The reason for this is so that you can experiment with processing sensor data in python without requiring any specialist hardware. You can access sensors including sound level (on mobile or any PC with a microphone), light level (sort of, on anything with a camera) and mobile phone accelerometers and gyroscopes on Android or iOS.

Browser and System Compatibility of the different sensors

Most things work fine in Chrome, Edge, Firefox and Safari on Windows, Mac or iOS.

Light level sensing will work most accurately on Chrome or Edge for Windows and Android Chrome because other browsers don’t allow manual control of camera exposure, so auto exposure may break things.

Sound level should be cool and dandy everywhere.

There is no accelerometer support on Windows or Mac, so you can only read accelerometer on mobile platforms.

Python Hello World

When you see a python block like the one below, you can run python in it. The examples have python already in them, you can change it if you want to fiddle.

Click the ‘start’ button to run the python, and ‘stop’ to cancel a run.

Getting data from sensors

To get data from sensors, you use the sensors module, like these examples for sound and accelerometer respectively. Note, accelerometer will only work on a phone as computers don’t typically have them in, or don’t expose them to web browsers.

Drawing things on graphs

The graph module in python allows you to draw simple line graphs. It works in any code box that shows a graph below. Use it like this:

Let’s graph a sensor

In the example below, we get the light and sound sensors and show them on a graph.

Note in the set_style call, we use subgraph_y to split the graph into two separate graphs. You can also use subgraph_x to split the graph horizontally, but it is often less useful because things at the same time don’t line up.