electronut Programming & Embedded Systems - by Mahesh Venkitachalam

A Python script to talk to the Spark Core

I have been playing around with the Spark Core and am very impressed with the built-in software support from Spark. This is a quick project that uses Python to talk to the Spark Core using the Spark cloud API. I also wanted the Spark Core to sent notifications to my program, and for this I am using Spark.publish() and Server Sent Events (SSE).

Here is a sample run of the program. (You can get the access token from Spark Build IDE.)

$ python talk_spark.py --at XXXX_ACCESS_TOKEN --list
[{u'connected': True, u'last_heard': u'2015-02-26T02:17:22.450Z', u'last_app': None, u'id': u'xxxxxxx', u'name': u'yyyy'}]

$ python talk_spark.py --at XXXX_ACCESS_TOKEN --listen
Notification: Yo 4 at 2015-02-26T01:59:02.272Z
Notification: Yo 5 at 2015-02-26T01:59:07.278Z
Notification: Yo 6 at 2015-02-26T01:59:12.281Z
^Cexiting.

You can find the source files for the project here, which has the Python code, as well as a simple Spark Core program for testing notifications. I look forward to playing with the Spark Photon next.

If you liked this article, please consider supporting my efforts by purchasing my book.

Python Playground, published by No Starch Press, USA, is a collection of imaginative programming projects that will inspire you to use Python to make art and music, build simulations of real-world phenomena, and interact with hardware like the Arduino and Raspberry Pi. Do check it out!