Introduction: Control SparkCore Using Python
The Milwaukee Makerspace was selected for November 2014 Build Night with Spark.io. I decided that this was the perfect tool to finish the lighting controller for the makerspace. Currently, the workshop areas are divided into 7 lighting zones. Power to each each zone is controlled via a Solid State Relay (SSR). The SSR, and the respective lighting zone, is turned on via switches pictured above.
We would like the make the system smarter by controlling the SSR through a SparkCore. The SparkCore will receive commands through a browser interface.
The first step in this process is to control SparkCore I/O pins through Python via Spark Cloud
Step 1: Gather the Parts
I used the following parts for the testing platform:
- Spark Core
- (3) 220 ohm resistors
- (3) LEDs
- Jumper Wires
A computer with a Python installation is needed as well.
Step 2: Electronics
I have supplied a Fritzing diagram of the circuit. SparkCore's D0, D1, and D2 pins are connected to an LED each through 220 ohm resistors. As you can see, this simple circuit comes together pretty quickly - perfect for prototyping.
I followed the instructions on the getting started page to connect the SparkCore to my home WiFi. Using the Tinker app, I could now control individual pins. It's important to note that in this example, the micro controller must be running the default firmware. If this is not the case, you must put it in the factory reset mode, or re-flash with Tinker in the Spark Web IDE.
If the SparkCore is running the Tinker code and you're electronics are hooked up correctly, you're ready to move on to the next step.
Step 3: Software
I used the wonderful Spyrk Python library to communicate with the SparkCore. I used Python 2.7 but this library supports 3.x as well.
First, you must install the library with the following command.
$ pip install spyrk
The rest of the code can be found in the image above.
You must remember to use your own access token. It can be found under Settings in the Spark Web IDE. The settings icon is located in the bottom left of your browser window.
In this example, my device is called light_core_2. You must use the name you gave your device. If you forgot, you can pull it up with spark.devices after you have authenticated.
I have also attached a sample python script for reference.
Now that this simple example works, I will work on controlling the Makerspace lights next. Till then, Happy Making!