spark.io meets node in a REST pub sub mash up
JavaScript Other CSS
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
duino
libs
public
views
.gitignore
Procfile
README.md
default_core_record.json
index.js
package.json

README.md

The Internet of Wild Things

This project is in support of Moonshot's The Internet of Wild Thing's class. The goal of the class is is to teach students with no electronics or programming experience how to begin combining the internet with the real world. Through the use of a Spark Core and Zapier, students can combine electronics components, configure a web interface, and make the real world and web events work together.

App Flow Home Screen

Firmware Generation

Firmware is generated via the user interface based on pin selection. Pressing Save and Bootload will generate a new file, upload it to the correct spark core, and trigger a core reset. My current method of code generation is a bit sloppy and could probably be improved upon. Check out duino/template.ino to see the prototype file.

Firmware Generation

Setting a pin's state

The generated code has one API end point, setState, which can be accessed via Spark's provided rest service, and is available for every pin explicitly set as an input.

curl https://api.spark.io/v1/devices/{deviceId}/setState \
  -d access_token={accessToken} -d params=D0,1

Setting a Pin's State

Listening for change

The generated code will broadcast events for each pin explicitly set as an output within the namespace iot-update.

Digital pin events are broadcast immediately upon change. After the event is emitted, a one second delay is enforced to prevent emissions which trigger rate limiting from Spark's API

Analog pin events are broadcast when a sufficient change is detected. The default threshold is set at 15 (an incredibly arbitrary number) and is configurable via the interface.

These events can be listened to via Spark's Command Line Interface spark subscribe iot-update.

Pin Listening

Webhook Integration and REST API Endpoints

Each spark core defined within this application has it's own API REST points which mimic those of the Spark API. The reason being is that users should be able to create "if this, then that" scenarios using popular web services like IFTTT and Zapier. By hosting our own variety of the API, we're able to log, debug, and show immediate feedback to the end user even if their wiring schemes are incorrect.

To make things especially easy, all of this app's REST routes are GET.

Set a pin's state:

/core/{coreId}/setPin?pinId={pinId}&pinVal={pinVal}
/core/13456789/setPin?pinId=D0&pinVal=1

Forwards a GET to a POST for a webhook matching the specified core's pinId. One webhook is allowed per pinId.

/core/{coreId}/callHook?pinId={pinId}&pinVal={pinVal}
/core/13456789/callHook?pinId=D0&pinVal=1

Class Materials

Worksheets and Presentation are publicly available via google docs:

Other Tips

  • If a student gets rate limited because of excessive event emission, it's possible that their core will not load new code. To resolve the issue, plug the pin directly into the power source. You should now be able to load new code on the device.
  • Occasionally, someone will send power into an output, drop the core in a toilet, or do something else which will force the core to be factory reset. Hold the mode button, tap reset, and wait 10 seconds until the LED fashes white.
  • The easiest way to connect the core to the internet is via CoolTerm. Connect your device, tap w, to configure the wifi credentials.

Breadboard and Circuit Tool Table

Environment Variables

  • DB_CONNECTOR - A mongo connection string
  • SPARK_ACCESS_TOKEN - An access token for the spark api