Having just hit and worked round an issue accessing a Pimoroni BME688 [BME680 family] Atmosphere Sensor card, I thought it might be helpful to document the solution I got to work. I first installed a 680 in early 2023 - and it's worked flawlessly ever since - but I want to migrate to a new host [part of an upgrade from Bookworm to Trixie] - and it made sense to reduce the risk by setting up the new machine with a spare BME688 that I bought at the same time as the first.
There have been changes to the Pimoroni Python libraries in the intervening 3 years, chief of which is the switch to Python virtual environments - and as a result of this there is a requirement to "activate" the libraries with a shell command similar to the following:-
source ~/.virtualenvs/pimoroni/bin/activate
If you try this, you'll quickly find that this works perfectly if you want to interact with your sensor in real time, but that it simply won't work if you want to automate data capture through e.g. a simple Python script, invoked by cron.
Here's a solution that you can get to work.
Start by using the Pimoroni installer for Python libraries, which you can find linked from their product page and hosted at github. Next, check and confirm that the installer has created a folder structure beginning with the ".virtualenvs" folder directly below your user's "home" folder on your Pi.
1. Copy the Pimoroni code to a more sensible, system-wide location:-
sudo mkdir /opt/pimoroni
cd
sudo cp -pr .virtualenvs /opt/pimoroni/
You can choose somewhere other than "/opt" ... and if you use the "-p" parameter, the copy will preserve ownership and timestamps... [which you might elect to change for system-wide code].
2. Confirm the location of the BME688 libraries in the new folder structure...
You should hopefully find that the pimoroni folder structure will include:-
/opt/pimoroni/lib/python3.13/site-packages/
The python version should match whatever happens to be your default on your host, so may be different than this example.
3. Write a cron trigger script
In my case, I want my Pimoroni sensor to write data held in a CSV file hosted on a NAS. Because nfs connections can lapse in to an idle state and because I want my data capture to run 24x7, I extended my bash script to prod my NAS awake if it's been snoozing...
#!/bin/.bash
mount -a
sleep 10
/usr/bin/python3 /opt/scripts/atmosphereSensorCapture.py
In the above script, the "mount -a" and the "sleep 10" have proven 100% reliable at getting my NAS to wake up and be ready to work...
4. Write a Python Script to Record your Sensor Data
What follows is going to be significantly truncated, because I do a little bit of manipulation of date and time variables that I then use to name my output file... But the basics are:-
sys.path.insert(0, 'opt/pimoroni/lib/python3.13/site-packages/')
import bme680
pathName = '/your/intended/destination/folder/'
outputFile = pathName + time.ctime()[-4:]
try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except (RuntimeError, IOError):
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
outputDayName = time.ctime()[0:3]
outputMonthName = time.ctime()[4:7]
outputDayOfMonth = time.ctime()[8:10]
outputHours = time.ctime()[11:13]
outputMinutes = time.ctime()[14:16]
outputSeconds = time.ctime()[17:19]
if sensor.get_sensor_data():
output = outputDayName + ","
output += outputMonthName + ","
output += outputDayOfMonth + ","
output += outputHours + ","
output += outputMinutes + ","
output += outputSeconds + ","
output += '{0:.2f},{1:.2f},{2:.3f}'.format(
sensor.data.temperature,
sensor.data.pressure,
sensor.data.humidity)
outputFile = pathName + time.ctime()[-4:]
outputFile = outputFile + "/" + time.ctime()[4:7] + '.csv'
with open(outputFile, 'a') as f:
f.write(output + '\n')
5. Test Manually
You can validate the effectiveness of your work by invoking from the command line, with:-
sudo /opt/scripts/aSCtrigger./sh
You should see a file named e.g. "Feb.csv" or "Mar.csv" or "May.csv" being created in your output destination folder, with the various sensor readings in discrete columns. As a consequence of using the system date and time to name the output file, I have no need here for using any form of log rotation to keep files to an easily-managed size - however, an equally simple approach would be to settle on a single, simple output file name and then write a configuration file for "logrotate" and let that system utility handle your output file rotations...
6. Automate with Cron
You can access your system-level crontab settings with
sudo crontab -e
If this is your first access of crontab, you'll be prompted to select your editor of choice. Option 1, nano, is the easiest to use. In the file, you just need to enter a single line to tell cron when to activate your script. For example, the following triggers the script every 15 minutes, at 00, 15, 30 and 45 minutes past each hour:-
*/15 * * * * /opt/scripts/aSCtrigger.sh
If 15-minute intervals isn't to your liking, then simply go to Crontab Guru, https://crontab.guru/, and plug in the details that you want... Copy the output from the page and edit it in to your crontab file.
That's it...
Hope this proves helpful to other Pimoroni users out there...
There have been changes to the Pimoroni Python libraries in the intervening 3 years, chief of which is the switch to Python virtual environments - and as a result of this there is a requirement to "activate" the libraries with a shell command similar to the following:-
source ~/.virtualenvs/pimoroni/bin/activate
If you try this, you'll quickly find that this works perfectly if you want to interact with your sensor in real time, but that it simply won't work if you want to automate data capture through e.g. a simple Python script, invoked by cron.
Here's a solution that you can get to work.
Start by using the Pimoroni installer for Python libraries, which you can find linked from their product page and hosted at github. Next, check and confirm that the installer has created a folder structure beginning with the ".virtualenvs" folder directly below your user's "home" folder on your Pi.
1. Copy the Pimoroni code to a more sensible, system-wide location:-
sudo mkdir /opt/pimoroni
cd
sudo cp -pr .virtualenvs /opt/pimoroni/
You can choose somewhere other than "/opt" ... and if you use the "-p" parameter, the copy will preserve ownership and timestamps... [which you might elect to change for system-wide code].
2. Confirm the location of the BME688 libraries in the new folder structure...
You should hopefully find that the pimoroni folder structure will include:-
/opt/pimoroni/lib/python3.13/site-packages/
The python version should match whatever happens to be your default on your host, so may be different than this example.
3. Write a cron trigger script
In my case, I want my Pimoroni sensor to write data held in a CSV file hosted on a NAS. Because nfs connections can lapse in to an idle state and because I want my data capture to run 24x7, I extended my bash script to prod my NAS awake if it's been snoozing...
#!/bin/.bash
mount -a
sleep 10
/usr/bin/python3 /opt/scripts/atmosphereSensorCapture.py
In the above script, the "mount -a" and the "sleep 10" have proven 100% reliable at getting my NAS to wake up and be ready to work...
4. Write a Python Script to Record your Sensor Data
What follows is going to be significantly truncated, because I do a little bit of manipulation of date and time variables that I then use to name my output file... But the basics are:-
sys.path.insert(0, 'opt/pimoroni/lib/python3.13/site-packages/')
import bme680
pathName = '/your/intended/destination/folder/'
outputFile = pathName + time.ctime()[-4:]
try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except (RuntimeError, IOError):
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
outputDayName = time.ctime()[0:3]
outputMonthName = time.ctime()[4:7]
outputDayOfMonth = time.ctime()[8:10]
outputHours = time.ctime()[11:13]
outputMinutes = time.ctime()[14:16]
outputSeconds = time.ctime()[17:19]
if sensor.get_sensor_data():
output = outputDayName + ","
output += outputMonthName + ","
output += outputDayOfMonth + ","
output += outputHours + ","
output += outputMinutes + ","
output += outputSeconds + ","
output += '{0:.2f},{1:.2f},{2:.3f}'.format(
sensor.data.temperature,
sensor.data.pressure,
sensor.data.humidity)
outputFile = pathName + time.ctime()[-4:]
outputFile = outputFile + "/" + time.ctime()[4:7] + '.csv'
with open(outputFile, 'a') as f:
f.write(output + '\n')
5. Test Manually
You can validate the effectiveness of your work by invoking from the command line, with:-
sudo /opt/scripts/aSCtrigger./sh
You should see a file named e.g. "Feb.csv" or "Mar.csv" or "May.csv" being created in your output destination folder, with the various sensor readings in discrete columns. As a consequence of using the system date and time to name the output file, I have no need here for using any form of log rotation to keep files to an easily-managed size - however, an equally simple approach would be to settle on a single, simple output file name and then write a configuration file for "logrotate" and let that system utility handle your output file rotations...
6. Automate with Cron
You can access your system-level crontab settings with
sudo crontab -e
If this is your first access of crontab, you'll be prompted to select your editor of choice. Option 1, nano, is the easiest to use. In the file, you just need to enter a single line to tell cron when to activate your script. For example, the following triggers the script every 15 minutes, at 00, 15, 30 and 45 minutes past each hour:-
*/15 * * * * /opt/scripts/aSCtrigger.sh
If 15-minute intervals isn't to your liking, then simply go to Crontab Guru, https://crontab.guru/, and plug in the details that you want... Copy the output from the page and edit it in to your crontab file.
That's it...
Hope this proves helpful to other Pimoroni users out there...
Statistics: Posted by sproggit — Sat Feb 28, 2026 5:08 pm — Replies 1 — Views 47