Example - copy and change it

A greenhouse on your dashboard in 40 lines

Source:
examples/greenhouse.py
Widgets:
Gauge, Button
Hardware:
None to run it; a temperature and humidity sensor to make it real
Status:
Tested with a fake connection. Run against a real server as far as the link-code prompt; not yet seen appearing on a dashboard.

A greenhouse on your dashboard in 40 lines

Most Raspberry Pi projects start the same way: a sensor, a number, and the wish to see that number from somewhere else. This example is the shortest complete version of that wish, and it shows the three things you will use in almost every program you write.

The gauges

temperature = greenhouse.output(id="temperature", type="Gauge")
humidity = greenhouse.output(id="humidity", type="Gauge")
...
temperature.set(24.0)
humidity.set(38.0)

An output is something your program shows. You declare it once, give it a type, and then call .set() whenever you have a new reading. The dashboard draws the gauge and keeps it up to date. The numbers here are fixed; swap them for a real sensor reading and you have a working greenhouse monitor.

The button

water_plants = greenhouse.command(id="waterPlants", type="Button")

@water_plants.handler
def water(command):
    print("Water plants pressed")  # shows in the terminal
    greenhouse.info("Plants watered")  # and in the dashboard's Notification history

A command goes the other way: something a person does on the dashboard that your program answers. Press the button in the browser and your function runs on the Pi. Open a valve, switch a pump, whatever you need, then tell the dashboard what happened.

The warning

greenhouse.warning("Soil moisture low")

info() and warning() send a notification. A warning appears as a toast on the dashboard and stays there until someone acknowledges it, so an alert cannot be missed just because nobody was looking at the moment it fired.

Running it

Copy greenhouse.py anywhere and run it:

python greenhouse.py

If this is the first time you have connected from this device, it prints a link and a code. Open the link while you are logged in and approve it, then answer y when the terminal asks you to confirm the account: the device is yours for good, and the platform gives it its id. You never have to pick or type one. Every run after that connects straight away. Press Ctrl+C to stop it.

Make it yours

Replace the two fixed readings with a sensor, decide what the button should do, and choose what deserves a warning. That is the whole shape of a device program.

← Back to Projects