Pocket Science Lab - Scripts for Sensor Experiments
See the codeThis repo is used for scripts and operators that collect data of specific sensors connected with the PSLab.
PSLab is a tiny pocket science lab that provides an array of equipment for doing science and engineering experiments. It can function like an oscilloscope, waveform generator, frequency counter, programmable voltage and current source and also as a data logger. Our website is at https://pslab.io
For a quick start jump to the initial setup or the basic usage section.
In this project four different experimental setups of the PSLab v5 are created, each incorporating a different sensor: one measuring CO2, one for oxygen, one for temperature, as well as one setup measuring the light intensity in lux. Details for those sensor setups can be found in the docs.
In addition, the following hardware components are connected to the PSLabs:
The following features are implemented on top of that:
/<item_name> <measured_value>, <unit>, <timestamp>. Additionally it is written into the CSV file of this measurement session. The file is structured as <timestamp>, <measured_value>, <unit>, <item_name>.init_pipe.py is called by the systemd service.In order to modify the software, the data on the Raspberry Pi's SD card needs to be accessed. This can either be done by plugging the SD card into a computer, accessing the Raspberry Pi via SSH, or by using the Raspi as a computer itself. In the following, the latter two methods are explained in detail. In any case, the SD card should never be cleared fully, because this would delete the operating system and therefore stop the Raspberry Pi from working.
Here, a monitor needs to be connected to the microHDMI slot of the Raspi. Simultaneously a mouse and a keyboard need to be linked to the inner microUSB slot via an USB hub. Make sure to perform this cabling-up before attaching the device to the electricity source.
An SSH access can be more convenient, as there is no additional cabling needed.
However, to establish this connection, the Raspberry Pi needs to be in the same WiFi network as the connecting laptop. If that is not the case yet, there is no way around connecting the Raspi to a monitor first, in oder to set up this WiFi connection, as the Raspberry Pi Zero W does not have a LAN port. Also, the network specific IP address of the Raspberry Pi needs to be known. It can be retrieved by executing the command ifconfig in the Raspi's terminal while having it connected to a monitor. The IP address can then usually be found in the "wlan0" paragraph and is formatted in a "000.000.000.000" pattern.
To then establish the ssh connection, the following steps need to be executed:
ssh foss@<ip> in the PC's terminal and entering the password foss when asked.Before using the devices in an own network, a one time setup is needed. Once this is done, for every following usage, please directly jump to the basic usage section.
Both, the OSC messages and the CSV file, of the measurement can be retrieved via WiFi.
Currently, the devices are connected to a dummy WiFi Hotspot with the credentials myHotspot as the SSID and password123 as the password.
Thus, for basic testing, one can just open a Hotspot with these details on a Phone/PC, the devices will automatically connect and the measurement data can be obtained.
However, for more extensive usage, it is obviously more convenient to connect the PSLab Sensor Boxes to the buildings usual WiFi. For example a connection to eduroam can be established by logging in with one's university credentials. The network connection can be changed by connecting the Raspberry Pi to a monitor and logging into the new WiFi Hotspot using the graphical user interface. Once connected, it is also possible to set a static private IP address for the Raspberry Pi in this network. This can be very useful, for example, in order to make the Raspi reliably accessible via SSH. Please make sure that the devices can see each other in the network and disable/ reconfigure the VLAN, in case one is set up.
Additionally, the IP address and port of the OSC server, need to be edited according to the actual address of the OSC receiver.
This can be done by adjusting the OSC_RECEIVER_IP and the OSC_RECEIVER_PORT constants on the Raspberry Pis.
The only loophole to this, is to use a OSC server at the pre-configured IP address of 192.168.173.152.
However, that's not always possible, depending on the available namespace of the network.
Once a power source (power bank or cord to electricity outlet) is connected to the PSLab sensor box and the device has fully booted (this may take a minute ot two), a new measurement is automatically started. The measurement results are now simultaneously shared via the Open Sound Control Protocol (OSC) and collected in a CSV file. To retrieve this data, please connect your own PC/Phone to the same public WiFi Hotspot, that the PSLab Sensor Box is connected to (see initial setup).
The CSV measurements of each PSLab sensor box can be retrieved in real time from the file server via WiFi:
Analysis tasks can now be performed on these data sheets, for example by importing them into Jupyter Notebook.
The OSC messages are sent to the specified IP address upon startup. To receive those messages a OSC server like this is necessary:
import argparse
from pythonosc.dispatcher import Dispatcher
from pythonosc import osc_server
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--ip", default="192.168.173.152")
parser.add_argument("--port", type=int, default=5005)
args = parser.parse_args()
dispatcher = Dispatcher()
dispatcher.map("/light", print)
dispatcher.map("/temp", print)
dispatcher.map("/co2", print)
dispatcher.map("/oxygen", print)
server = osc_server.ThreadingOSCUDPServer(
(args.ip, args.port), dispatcher)
server.serve_forever()
This server uses the python-osc package to receive and decode the messages. The package can be installed by executing pip3 install python-osc in the PC's terminal.
Please note that this setup only works if the IP address, specified as OSC_RECEIVER_IP, is identical to the IP address stated in the server scrips (in line 7 here) and corresponds to the laptops own IP address (obtainable by executing "ifconfig" in the terminal). This OSC server for example produces the following output when, at the same time, the Oxygen Sensor Box and the Light Sensor Box are sending OSC data to this IP address at port 5005:
When for example using the PSLab Sensor Box "Lux", changes in the environment's light intensity can be measured. After attaching the box to a power bank and waiting for two minutes, the device starts measuring.
The running OSC server on our laptop, that is connected to the same WiFi Hotspot as the Sensor Box, then receives the following data:
/light 109.5234702837157 lux 21-07-2023 20:44:12
/light 108.62976013160224 lux 21-07-2023 20:44:13
/light 114.54458695414951 lux 21-07-2023 20:44:14
To test the sensor and observe the changes in the environment, the lights are now turned off.
This results in the following OSC messages:
/light 6.80740388396548 lux 21-07-2023 20:46:04
/light 7.05431189748499 lux 21-07-2023 20:46:05
/light 7.52577384043171 lux 21-07-2023 20:46:06
To then test the sensor at a very hight brightness, we turned the lights on again and exposed the photoresistor to a flashlight:
Thus, the OSC server receives these messages:
/light 1721.79974583536023 lux 21-07-2023 20:47:38
/light 1902.29013779831879 lux 21-07-2023 20:47:39
/light 1483.70672770591856 lux 21-07-2023 20:46:40
In an analogue way, the temperature sensor can be tested by warming it up with one's finger tips, while the CO2 and oxygen sensors can by tested by exposing them to exhaled air.
To eventually trigger the shut down process of the Raspberry Pi, please press the attached button for one to two seconds. After waiting 20-30 seconds, the shut down is completed, the green indication light on the Raspberry Pi goes off and the electricity source can be unplugged. Technically, the measurement session can also be terminated by just unplugging the power source directly, however in the long term, this technique can be harmful for the Raspberry Pi's file system.
As this is a hardware project, it is not unlikely that at some point in time, one of the cables or other components will cause problems. To then solve this, it is important to localize the error. Here, different levels of debugging can help, depending on the supposed location of the problem. Basic debugging steps may be:
nmap tool), try having the network open already during the Raspberry Pi's booting process.Some more advanced debugging techniques include the following:
To ensure that the PSLab itself and the sensor are working properly, the following test can be executed. Please note that this routine is the only one that is executed on a normal (Linux) PC, instead of the Raspberry Pi, in order to abstract from any Raspi specific problems.
python3 --version in the terminal. In case there is no item found, install version 3.6 by sudo apt-get install python3.6 and reboot your system afterwards to ensure that everything is fully loaded.pip3 install pslab.sudo git clone https://github.com/fossasia/pslab-scripts.git.cd pslab-scrips.sudo python3 init_pipe.py <type> (for example
sudo python3 init_pipe.py oxygen). This execution can always be stopped by hitting ctrl+c./home/<user name>/data.Often times the script itself actually works fine, but the startup manager might start it
too early, causing resource conflicts and other issues to arise.
One option to debug this, is to first make sure that the script itself works fine by running sudo python3 /home/foss/code/init_pipe.py <type>
on the Raspberry Pi (for example sudo python3 init_pipe.py oxygen) and checking the results in
/home/foss/data. If those results look good, but the data fetching upon startup still does not work, the error logs of the startup script can be examined by executing journalctl -u startup.service --since yesterday in the Raspi's terminal.
Different software and hardware projects can be realized on this combination of PSLab and Raspberry Pi. Some ideas for modifying the existing software are listed below. As python3, the pslab-python library, as well as this pslab-scripts repository, are already installed on the Raspberry Pis, no additional installation is needed here.
Currently a new data point is retrieved from the sensor every second. This can be changed by adjusting the value for theMEASURING_INTERVAL. This constant can be found in the /home/foss/pslab-scripts/measure.py file on the Raspberry Pi.
The data points are flushed into the CSV file after every 10 measurements in the current implementation. To change this, the FLUSHING_THRESHOLD value in /home/foss/pslab-scripts/store_data.py can be adjusted on the Raspi.
As of now, the measurement data is redirected into a CSV file and OSC messages by the pipe function. This can however be modified freely, for example by adding an additional step
to transform and output some data into the data exporting function.
This new process can freely process the measured data points, received via connection.recv().
cd /home/foss/pslab-scripts.git pull in the terminal to fetch the new changes in software.By adjusting the experiment type parameter in the systemd service "startup.service", the supported sensor type of this specific PSLab sensor box can be changed easily:
sudo nano /lib/systemd/system/startup.service.sudo reboot.📦pslab-scripts
┣ 📂docs # Supplementary material
┃ ┣ 📂ao-03_amplifier_circuit_design # KiCad project files of the custom circuit board for the AO-03 sensor
┃ ┃ ┗ 📜...
┃ ┣ 📂casing # Information about the 3d printed casing, as well as the lasercut shield
┃ ┃ ┣ 📜README.md
┃ ┃ ┗ 📜...
┃ ┣ 📂images
┃ ┃ ┗ 📜...
┃ ┣ 📂logos # Sticker designs for the boxes (made with Inkscape)
┃ ┃ ┗ 📜...
┃ ┣ 📜network_connection_manual.md # Manual on how to connect a device to the PSLab's file server
┃ ┣ 📜presentation_may28.pdf
┃ ┣ 📜sensors.md # Detailed descriptions of all four sensor setups
┃ ┗ 📜testing_manual.md # Detailed manual on how to test the devices
┣ 📂sensors # Contains a specific run script for every sensor
┃ ┣ 📜ao03_oxygen.py
┃ ┣ 📜ccs811_co2.py
┃ ┣ 📜driver_ccs811.py # Custom driver for the CCS811 sensor
┃ ┣ 📜gl5528_light.py
┃ ┗ 📜lm35_temp.py
┣ 📜export_data.py # Sharing the measurements via OSC and CSV
┣ 📜init_pipe.py # Main project file
┣ 📜measure.py
┗ 📜shutdown.py # Script for the button logic
The website is hosted on pslab.io.
2,153 followers · starred Nov 2016
136 followers · starred Feb 2018
108 followers · starred Feb 2018
Pocket Science Lab - Scripts for Sensor Experiments
See the codeThis repo is used for scripts and operators that collect data of specific sensors connected with the PSLab.
PSLab is a tiny pocket science lab that provides an array of equipment for doing science and engineering experiments. It can function like an oscilloscope, waveform generator, frequency counter, programmable voltage and current source and also as a data logger. Our website is at https://pslab.io
For a quick start jump to the initial setup or the basic usage section.
In this project four different experimental setups of the PSLab v5 are created, each incorporating a different sensor: one measuring CO2, one for oxygen, one for temperature, as well as one setup measuring the light intensity in lux. Details for those sensor setups can be found in the docs.
In addition, the following hardware components are connected to the PSLabs:
The following features are implemented on top of that:
/<item_name> <measured_value>, <unit>, <timestamp>. Additionally it is written into the CSV file of this measurement session. The file is structured as <timestamp>, <measured_value>, <unit>, <item_name>.init_pipe.py is called by the systemd service.In order to modify the software, the data on the Raspberry Pi's SD card needs to be accessed. This can either be done by plugging the SD card into a computer, accessing the Raspberry Pi via SSH, or by using the Raspi as a computer itself. In the following, the latter two methods are explained in detail. In any case, the SD card should never be cleared fully, because this would delete the operating system and therefore stop the Raspberry Pi from working.
Here, a monitor needs to be connected to the microHDMI slot of the Raspi. Simultaneously a mouse and a keyboard need to be linked to the inner microUSB slot via an USB hub. Make sure to perform this cabling-up before attaching the device to the electricity source.
An SSH access can be more convenient, as there is no additional cabling needed.
However, to establish this connection, the Raspberry Pi needs to be in the same WiFi network as the connecting laptop. If that is not the case yet, there is no way around connecting the Raspi to a monitor first, in oder to set up this WiFi connection, as the Raspberry Pi Zero W does not have a LAN port. Also, the network specific IP address of the Raspberry Pi needs to be known. It can be retrieved by executing the command ifconfig in the Raspi's terminal while having it connected to a monitor. The IP address can then usually be found in the "wlan0" paragraph and is formatted in a "000.000.000.000" pattern.
To then establish the ssh connection, the following steps need to be executed:
ssh foss@<ip> in the PC's terminal and entering the password foss when asked.Before using the devices in an own network, a one time setup is needed. Once this is done, for every following usage, please directly jump to the basic usage section.
Both, the OSC messages and the CSV file, of the measurement can be retrieved via WiFi.
Currently, the devices are connected to a dummy WiFi Hotspot with the credentials myHotspot as the SSID and password123 as the password.
Thus, for basic testing, one can just open a Hotspot with these details on a Phone/PC, the devices will automatically connect and the measurement data can be obtained.
However, for more extensive usage, it is obviously more convenient to connect the PSLab Sensor Boxes to the buildings usual WiFi. For example a connection to eduroam can be established by logging in with one's university credentials. The network connection can be changed by connecting the Raspberry Pi to a monitor and logging into the new WiFi Hotspot using the graphical user interface. Once connected, it is also possible to set a static private IP address for the Raspberry Pi in this network. This can be very useful, for example, in order to make the Raspi reliably accessible via SSH. Please make sure that the devices can see each other in the network and disable/ reconfigure the VLAN, in case one is set up.
Additionally, the IP address and port of the OSC server, need to be edited according to the actual address of the OSC receiver.
This can be done by adjusting the OSC_RECEIVER_IP and the OSC_RECEIVER_PORT constants on the Raspberry Pis.
The only loophole to this, is to use a OSC server at the pre-configured IP address of 192.168.173.152.
However, that's not always possible, depending on the available namespace of the network.
Once a power source (power bank or cord to electricity outlet) is connected to the PSLab sensor box and the device has fully booted (this may take a minute ot two), a new measurement is automatically started. The measurement results are now simultaneously shared via the Open Sound Control Protocol (OSC) and collected in a CSV file. To retrieve this data, please connect your own PC/Phone to the same public WiFi Hotspot, that the PSLab Sensor Box is connected to (see initial setup).
The CSV measurements of each PSLab sensor box can be retrieved in real time from the file server via WiFi:
Analysis tasks can now be performed on these data sheets, for example by importing them into Jupyter Notebook.
The OSC messages are sent to the specified IP address upon startup. To receive those messages a OSC server like this is necessary:
import argparse
from pythonosc.dispatcher import Dispatcher
from pythonosc import osc_server
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--ip", default="192.168.173.152")
parser.add_argument("--port", type=int, default=5005)
args = parser.parse_args()
dispatcher = Dispatcher()
dispatcher.map("/light", print)
dispatcher.map("/temp", print)
dispatcher.map("/co2", print)
dispatcher.map("/oxygen", print)
server = osc_server.ThreadingOSCUDPServer(
(args.ip, args.port), dispatcher)
server.serve_forever()
This server uses the python-osc package to receive and decode the messages. The package can be installed by executing pip3 install python-osc in the PC's terminal.
Please note that this setup only works if the IP address, specified as OSC_RECEIVER_IP, is identical to the IP address stated in the server scrips (in line 7 here) and corresponds to the laptops own IP address (obtainable by executing "ifconfig" in the terminal). This OSC server for example produces the following output when, at the same time, the Oxygen Sensor Box and the Light Sensor Box are sending OSC data to this IP address at port 5005:
When for example using the PSLab Sensor Box "Lux", changes in the environment's light intensity can be measured. After attaching the box to a power bank and waiting for two minutes, the device starts measuring.
The running OSC server on our laptop, that is connected to the same WiFi Hotspot as the Sensor Box, then receives the following data:
/light 109.5234702837157 lux 21-07-2023 20:44:12
/light 108.62976013160224 lux 21-07-2023 20:44:13
/light 114.54458695414951 lux 21-07-2023 20:44:14
To test the sensor and observe the changes in the environment, the lights are now turned off.
This results in the following OSC messages:
/light 6.80740388396548 lux 21-07-2023 20:46:04
/light 7.05431189748499 lux 21-07-2023 20:46:05
/light 7.52577384043171 lux 21-07-2023 20:46:06
To then test the sensor at a very hight brightness, we turned the lights on again and exposed the photoresistor to a flashlight:
Thus, the OSC server receives these messages:
/light 1721.79974583536023 lux 21-07-2023 20:47:38
/light 1902.29013779831879 lux 21-07-2023 20:47:39
/light 1483.70672770591856 lux 21-07-2023 20:46:40
In an analogue way, the temperature sensor can be tested by warming it up with one's finger tips, while the CO2 and oxygen sensors can by tested by exposing them to exhaled air.
To eventually trigger the shut down process of the Raspberry Pi, please press the attached button for one to two seconds. After waiting 20-30 seconds, the shut down is completed, the green indication light on the Raspberry Pi goes off and the electricity source can be unplugged. Technically, the measurement session can also be terminated by just unplugging the power source directly, however in the long term, this technique can be harmful for the Raspberry Pi's file system.
As this is a hardware project, it is not unlikely that at some point in time, one of the cables or other components will cause problems. To then solve this, it is important to localize the error. Here, different levels of debugging can help, depending on the supposed location of the problem. Basic debugging steps may be:
nmap tool), try having the network open already during the Raspberry Pi's booting process.Some more advanced debugging techniques include the following:
To ensure that the PSLab itself and the sensor are working properly, the following test can be executed. Please note that this routine is the only one that is executed on a normal (Linux) PC, instead of the Raspberry Pi, in order to abstract from any Raspi specific problems.
python3 --version in the terminal. In case there is no item found, install version 3.6 by sudo apt-get install python3.6 and reboot your system afterwards to ensure that everything is fully loaded.pip3 install pslab.sudo git clone https://github.com/fossasia/pslab-scripts.git.cd pslab-scrips.sudo python3 init_pipe.py <type> (for example
sudo python3 init_pipe.py oxygen). This execution can always be stopped by hitting ctrl+c./home/<user name>/data.Often times the script itself actually works fine, but the startup manager might start it
too early, causing resource conflicts and other issues to arise.
One option to debug this, is to first make sure that the script itself works fine by running sudo python3 /home/foss/code/init_pipe.py <type>
on the Raspberry Pi (for example sudo python3 init_pipe.py oxygen) and checking the results in
/home/foss/data. If those results look good, but the data fetching upon startup still does not work, the error logs of the startup script can be examined by executing journalctl -u startup.service --since yesterday in the Raspi's terminal.
Different software and hardware projects can be realized on this combination of PSLab and Raspberry Pi. Some ideas for modifying the existing software are listed below. As python3, the pslab-python library, as well as this pslab-scripts repository, are already installed on the Raspberry Pis, no additional installation is needed here.
Currently a new data point is retrieved from the sensor every second. This can be changed by adjusting the value for theMEASURING_INTERVAL. This constant can be found in the /home/foss/pslab-scripts/measure.py file on the Raspberry Pi.
The data points are flushed into the CSV file after every 10 measurements in the current implementation. To change this, the FLUSHING_THRESHOLD value in /home/foss/pslab-scripts/store_data.py can be adjusted on the Raspi.
As of now, the measurement data is redirected into a CSV file and OSC messages by the pipe function. This can however be modified freely, for example by adding an additional step
to transform and output some data into the data exporting function.
This new process can freely process the measured data points, received via connection.recv().
cd /home/foss/pslab-scripts.git pull in the terminal to fetch the new changes in software.By adjusting the experiment type parameter in the systemd service "startup.service", the supported sensor type of this specific PSLab sensor box can be changed easily:
sudo nano /lib/systemd/system/startup.service.sudo reboot.📦pslab-scripts
┣ 📂docs # Supplementary material
┃ ┣ 📂ao-03_amplifier_circuit_design # KiCad project files of the custom circuit board for the AO-03 sensor
┃ ┃ ┗ 📜...
┃ ┣ 📂casing # Information about the 3d printed casing, as well as the lasercut shield
┃ ┃ ┣ 📜README.md
┃ ┃ ┗ 📜...
┃ ┣ 📂images
┃ ┃ ┗ 📜...
┃ ┣ 📂logos # Sticker designs for the boxes (made with Inkscape)
┃ ┃ ┗ 📜...
┃ ┣ 📜network_connection_manual.md # Manual on how to connect a device to the PSLab's file server
┃ ┣ 📜presentation_may28.pdf
┃ ┣ 📜sensors.md # Detailed descriptions of all four sensor setups
┃ ┗ 📜testing_manual.md # Detailed manual on how to test the devices
┣ 📂sensors # Contains a specific run script for every sensor
┃ ┣ 📜ao03_oxygen.py
┃ ┣ 📜ccs811_co2.py
┃ ┣ 📜driver_ccs811.py # Custom driver for the CCS811 sensor
┃ ┣ 📜gl5528_light.py
┃ ┗ 📜lm35_temp.py
┣ 📜export_data.py # Sharing the measurements via OSC and CSV
┣ 📜init_pipe.py # Main project file
┣ 📜measure.py
┗ 📜shutdown.py # Script for the button logic
The website is hosted on pslab.io.
2,153 followers · starred Nov 2016
136 followers · starred Feb 2018
108 followers · starred Feb 2018