IoT on Your Raspberry Pi

APScheduler

The Advanced Python Scheduler (APScheduler) is a light but powerful in-process task scheduler that lets you schedule functions (or any other Python callables) to execute at times of your choosing.

I have used the APScheduler software for a number of projects in the past, such as Project Curacao [9], SunRover [10], the upcoming Project Curacao 2 project (I revisit wind power in the Caribbean with a second generation Project Curacao), and other SwitchDoc Labs projects for customers. It's a great package.

You can install it with the following two commands on your Raspberry Pi:

sudo pip install setuptools --upgrade
sudo pip install apscheduler

A reading of the SunIOT software and examples show how it is used.

Installing the SunIOT Software

The source code for this project is on GitHub [11], and you can download your own copy by running:

git clone https://github.com/switchdoclabs/SunIOT.git

The SunIOT software for this column is pretty straightforward. The first part is shown in Listing 2. The first few lines import all the necessary libraries; then, the LED is mapped to D4 (GPIO 4) on the Pi2Grover board in lines 16-17, and lines 22-23 start the SI1145 I2C sensor.

The next section of code (Listing 3) shows the setup for three tasks to be scheduled. The first task just prints the time to the console (lines 3-4), the second task blinks the LED to show that certain events are happening (lines 11-16). By changing the timing on blinks and the number of blinks, you can indicate that a number of different events are happening. The third task is the real guts of the IoT device, where I read the SI1145 light sensors (19-21) and return the values to the mainline program (lines 30-34). I also convert the UV readings into the UV Index values here (line 22).

Listing 4 shows the main program, which has the main loop and is where I set up APScheduler to execute the tasks (line 25). The three tasks are the tick process, which prints the time to the console every 60 seconds (line 17), and the blinkLED and IoT readSunLight processes, which give a heartbeat every five seconds (line 19) and 10 seconds (line 22), respectively.