The following instructions and details describe setting up a Wi-Fi microcontroller board to detect when a magnet switch separates/connects (i.e., a door opening/closing) and to send an xAPI statement to an LRS (Learning Record Store).
This project was used in a half-day workshop during the Spring 2019 xAPI Party, a small mini-conference to meet with participants after the culmination of the Spring 2019 xAPI Cohort, both hosted by TorranceLearning.
- NodeMCU ESP8266 12E Wi-Fi Microcontroller
- USB cable
- Wall Charger
- Magnet Switch
- LEDs
- Prototype breadboard
- Wiring
You can download the Arduino IDE for Window, Mac and Linux to edit and run your projects:
https://www.arduino.cc/en/Main/Software
(Currently, the Arduino Web Editor can't be used for this project, since the ESP8266 board we're using can't be loaded there. If you're using another supported board, feel free to try the Web Editor instead.)
First, load the ESP8266 Board Manager manually by adding the below link to your Preferences.
File > Preferences > Settings
http://arduino.esp8266.com/stable/package_esp8266com_index.json
These libraries are included when you load the ESP8266 Arduino Core from the above Board Manager.
ESP8266 Arduino Core Documentation
- ESP8266WiFi
- ESP8266WebServer
- DNSServer
- WiFiClientSecure
- Ticker
The WiFiManager can be installed separately in the Arduino IDE, from the included library list:
Tools > Manage Libraries...
This file (in the generic-magnet-switch folder) is the entry point that can be loaded into the Arduino IDE. It should automatically bring in the other support files that are in that folder.
Near the top of the file are the variables that can be changed and hard-coded (or see below for Zapier options) to set up your unique board configuration.
This file contains code to send real-time signals (via WebSockets) to a free service called dweet.io. This could then be used in projects to create visualizations, save activity, or trigger other activity using their client libraries (Node.js, JavaScript, Python or Ruby). You can also view device activity in their web interface.
This is where the relevant data is sent to Zapier, where it is then processed to send an xAPI statement (and other actions, if desired).
This file controls the basic green/yellow/red LED activity.
This is a helper file to encode URL components for HTTP requests (Zapier).
Initially, the code is set up to send to TorranceLearning's Zapier account.
To send to your own Zapier webhook:
- Create a new Zap
- Add a Webhooks by Zapier Trigger step ("Catch Hook")
- Copy/paste the relevant parts of the new Webhook address into the
generic-magnet-switch.inofile (zapierHookvariable) - Run your Arduino code from the board, and trigger the magnet switch
- Test the "Webhook" step to get sample data from your trigger
- NOTE: You can add any intermediate steps here if you have a Zapier plan with multi-steps: send a Slack message, update a Google Sheet, send an email, etc.
- Copy/paste the relevant parts of the new Webhook address into the
- Add a Code by Zapier Action step ("Run Javascript")
- In "Edit Template" (using the sample data from the "Webhook" step), add the "Input Data" field names and their counterpart sample data values:
- status
- statusNum
- endpoint
- auth
- actorMbox
- actorName
- activityName
- NOTE: If you do not wish to use hard-coded values in your Arduino code, you can override them with your own values in Zapier here instead. This is the preferred method generally, since it makes remote updates a lot easier without having write new code to the board.
- Copy/paste the
generic-magnet-switch.jsJavaScript file contents into the "Code" text area on this step
- In "Edit Template" (using the sample data from the "Webhook" step), add the "Input Data" field names and their counterpart sample data values:
- Test this final step to check for errors
- If there are errors, make sure there are no typos and that your
endpointandauthvalues are correct - If there are no errors, you should see the test statement in your LRS
- If there are errors, make sure there are no typos and that your
- Make sure the Zap is enabled at the end, and it should now fire the Zap when the magnet switch status changes
- You can always check the "Task History" in Zapier to check for errors/successes
