While the temperature and humidity of your workplace probably aren’t your most important business KPIs, at Geckoboard HQ we’ve found displaying temperature useful for resolving differing opinions of whether the air conditioning should be on or off!
Step 1 - Get your hardware
Sensors
There’s a huge choice of sensors on the market. Temperature’s the most useful, but you could go all fancy and get pressure and humidity too.
For this walkthrough we’re using a Pimoroini BME680 breakout board. It’s small, cheap, well supported and comes with temperature, humidity, pressure and air quality sensors. You'll need to solder on the header board but that’s part of the fun. Don’t be daunted, you can do it as a complete beginner – just keep your appendages away from the hot bit!
Microcontroller/computer with power supply
Recommended Microcontroller/computer
We’re big fans of Raspberry Pis – the Zero W is perfect for this – but there are lots of alternatives like the Particle Photon.
For the brains of the operation we went for a Raspberry Pi. We went for a Model 3, as it’s what we had lying around, but a Zero W would have been a cheaper and more compact option. Any Pi should do, but it’s easier if it’s one of the models with Wifi built in. If you’re starting out, buying a kit complete with a power supply and HDMI/USB dongles is usually a good idea.
Wiring
You'll need a way of connecting your sensor to your microcontroller/computer of choice.
The BME680 has been designed so you can plug it directly into the Pi without any connecting wires, but if you want a bit more flexibility you’ll need a few jumper wires. Make sure to buy the right gender, this will depend on what header you put on the Pi and break out board.
Cases (optional)
You might also want a case for the Pi. We like Pimoroni’s Pibow cases, but there are some really fancy cases out there like the Anidees aluminium case if you want to go all out.
Step 2 - Set up your Pi
If this is your first time using a Raspberry Pi follow these instructions for getting set up.
Tips
For this initial set up life’s a lot easier if you have a keyboard, mouse and screen to plug directly into the Pi.
Also, if you’re picking up Pi that’s been hanging around for a while, make sure you update it. It makes a lot of problems just go away!
sudo apt-get update sudo apt-get upgrade
Once you’ve got the operating on system on the Pi installed connect it your WiFi. It’s also a good idea to change the Pi’s password.
At this point you can enable SSH if you like and program the Pi remotely from your usual computer. This means you can unplug the screen and keyboard attached to the Pi.
Step 3 – Get your sensor working
Now the Pi’s all set up it’s time to get your sensor working.
For the Pimoroni BME680 follow this getting started tutorial.
Once you’ve reached the Running the built-in example and burning in the sensor step, you’ll be able to see the sensor working - hooray!
cd /home/pi/bme680/examples python read-all.py
You’re now ready to move on to sending the data to your Geckoboard dashboard.
Get help
If you encounter any issues, make sure you've:
Wired it up correctly. It’s easy to get things out by one pin or in the wrong orientation
Enabled I2C. You can use this to check if the Pi’s seeing the sensor.
Step 4 – Push your data
Given it’s what we’re using to read the sensor values, we'll use Python for sending the data to Geckoboard.
First you’ll need to head to the terminal and install the Geckoboard Python library.
pip install geckoboard.py
Important note
The
.py
is important. Without it you’ll end up with the wrong library.Next you’ll need to find your Geckoboard API key.
If you’ve not yet got a Geckoboard account, now’s a good time to sign up.Now you can cheat and create a new file on your Pi called
gecko-weather.py
and just paste in the following script:import geckoboard, datetime, bme680# Set up Pimroni BME680 sensors # https://github.com/pimoroni/bme680-python/blob/master/examples/read-all.pysensor = bme680.BME680()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)sensor.set_gas_status(bme680.ENABLE_GAS_MEAS) sensor.set_gas_heater_temperature(320) sensor.set_gas_heater_duration(150) sensor.select_gas_heater_profile(0)# Set up Geckoboard connection (This is where you need to put your API key).client = geckoboard.client("YOUR-API-KEY-GOES-HERE")try: client.ping() print "Authentication successful"except: print "Incorrect API Key"# Look for a dataset with the following name and if it doesn't exist create it. # Here we tell Geckoboard what schema to expect. dataset = client.datasets.find_or_create( 'environmentreadings', { 'temperature' : {'type':'number','name':'Temperature','optional':False}, 'pressure' : {'type':'number','name':'Pressure','optional':False}, 'humidity' : {'type':'number','name':'Humidity','optional':False}, 'air-quality' : {'type':'number','name':'Air Quality','optional':True}, 'timestamp': { 'type': 'datetime', 'name': 'Time' }}, ['timestamp']) # This sets timestamp as the primary keyprint "Dataset successfully created"# Get the current time reading_time = datetime.datetime.now().isoformat()# Read the sensor values and post them to Geckoboarddataset.post([ { 'timestamp': reading_time, 'temperature': sensor.data.temperature, 'pressure': sensor.data.pressure, 'humidity': sensor.data.humidity, 'air-quality': sensor.data.gas_resistance }], 'timestamp')# Print the values in the consoleoutput = "{0:.2f} C,{1:.2f} hPa,{2:.2f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity)print "Data successfully appended" print output
Ensure that you replace
YOUR-API-KEY-GOES-HERE
with your API key.Get help
If you need help creating and editing files on the Pi, this article walks you through the process:
You’re now ready to run the file and send your first data to Geckoboard! To do this just type the following line in the terminal:
python gecko-weather.py
Note
Look for the comments to the code explaining what the various parts do. Datasets requires you to define a schema - what data of what type to expect. You also need to tell it what the primary key is.
Head over to Geckoboard and start to add a new Dataset widget. You should see the
environmentreadings
dataset with one reading.Add a line graph or number widget to your dashboard.
Run the script again on your pi with
python gecko-weather.py
and you should see your dashboard update almost instantly – Success!
Step 5 – Automate
Obviously you don’t want to have to run the script manually every time to log a new reading. We could put our script in a loop with a delay but instead to get it to update automatically we’re going to use Cron.
Cron’s a way of running a script at a set interval. If your Pi reboots the script will start running again automatically. To set it up, follow these steps:
Open crontab by typing in the terminal:
crontab -e
Next add the following line, making sure the last part matches the location of your script:
* * * * * /usr/bin/python /home/pi/Documents/gecko-weather.py
Tip: The
pwd
command is useful for finding the full path of a file.
If your Pi’s set up differently, you may also need to change the python path from
/usr/bin/python
. You can check your Python’s location by runningwhich python
in the terminal.If you want to change the frequency your script runs you’ll need to change the
* * * * *
. Currently it updates every minute. Read more about configuring cron jobs using thecron
command.Once you’ve saved the Cron file the script should run. Watch the data roll in!
And that’s it! Sit back and enjoy your up to the minute environment data. Why not now experiment with different visualizations?