I geeked out recently over the Android task automation app, Automate. The reason I got so excited was that it provided a very elegant solution for creating a custom NFC sensor for a project I’ve been working on. What I also came to find out was that Automate opened up a lot of possibilities beyond this project.
Automate can be easily extended to prototype a ton of IoT projects. This tutorial shows how your Android phone can easily connect your physical world to any cloud service with a mashup of NFC, Automate and IFTTT.
Example Use Case: NFC Enabled Sustainable Clothing Care
The project I’ve been working on is about improving the quality and sustainability of personal clothing care by instrumenting clothes with NFC tags. This way a clothing owner can be more careful about how they wash their clothes and more mindful about how they wear them. I had been looking for a good way to prototype this solution and my first idea was to create unique ID QR codes for all my clothing but that was a bit cumbersome. I had thought to wire up an Arduino but that seemed like a lot of work just for a prototype. I finally realized that the best way to build this prototype was sitting right in front of me.
Android “Automate” App as the Front End
I first started looking at the NFC Tools app which seemed to fit my use case very specifically. But thanks to the Google Play Store’s suggestions, I tested a few other apps and found that Automate seemed to be the most powerful and simple to use.
All I needed to do was read a NFC tag and send the tag’s ID to a database that held more information about the garments. I could then query and visualize the data to complete the feedback loop. That is where IFTTT comes in.
IFTTT as the Middleware
IFTTT had been growing on me for several years but when they introduced the Maker Channel, I was totally sold on it. IFTTT (If This Then That) allows you to trigger actions on one Web service or IoT device with with an event from another. The Maker Channel allows you to broaden the list of services and devices to anything that is capable of sending or receiving a HTTP request. In a prototype, it allows a maker to cut out all the business of constructing and API and hosting a database. See why it’s perfect?
Google Spreadsheets as the Back End
With IFTTT, I could choose from a couple really easy-to-manage data repositories: Google Spreadsheets and Wolfram Datadrop. Google Spreadsheets is simple and can be shaped into almost anything that it needs to be. Wolfram Datadrop is a bit more complex but a lot more powerful in terms of data management, visualization, and analysis. Since its not a ton of data and for a limited time*, both will suit my purpose. IFTTT makes it easy, so I used both with the same trigger. I will show how to use Google Spreadsheets in this example.
*Wolfram Datatdrop offers free “databins” for 30 days but then you will have to pay to store data longer.
Setup: Easy Android NFC Sensor
After spending enough time with an Arduino board to get a light to blink, I found turning my phone into an NFC sensor refreshingly easy. Automate exposes a few Android API’s that make it really easy to get, not only the NFC tag’s ID but the phone’s current location, and current location’s temperature and humidity. Living in Hong Kong, these are all things that would influence the decision to wear a certain garment so I planned to collect this data.
- The Flow is initiated when it the phone reads an NFC tag.
- The NFC ID value is put into a dictionary named payload as value1.
- The phone uses its Location services to retrieve the current location and saves the latitude and longitude as the variables lat and lon.
- The lat and lon variables are then passed to the weather block which uses Weather Underground to get the current location’s weather. The current temperature and humidity are put into the payload dictionary as value2 and value3.
- The payload dictionary is converted into JSON using jsonEncode method and sent via an HTTP POST* to IFTTT
- Set the URL of this HTTP POST with the URL from Step 8 of the IFTTT setup below.
Pretty simple even with a minimal level of programming knowledge.
*The reason that the payload dictionary uses the keys, value1, value2, and value3, is that, whether sent as JSON on a POST request or query string parameters on a GET request, this is the naming convention that IFTTT expects. You can then refer to them by those variable names within IFTTT.
Setup: IFTTT Maker Channel Recipe
This is very simple. All you need to do is get the URL from the whirw Channel and ensure that the data is being sent to IFTTT. Then the Google Spreadsheet will be populated with NFC reading events.
- Setup a new IFTTT recipe here.
- For “if” choose the Maker Channel.
- Select “Receive a Web Request” and name your event.
- For “that” choose Google Drive
- Select “Add a row to a spreadsheet.”
- The default setting for the Google Spreadsheet and the format for the columns is good. Just remember where you will find you data collection spreadsheet when you want to see the results.
- Finish the steps to create the recipe.
- Test your recipe here. (Make sure to set the Maker Channel URL in your Automate Flow)
With that, you can track anything that you can put an NFC tag on using the Android phone you already own.
Prototype All the Things!
This becomes really interesting when you consider that the cost is so low. Instead of buying an Arduino, you can just use a cheap old Android phone that can connect to WiFi and run Automate. Chances are, a phone is more reliable than something you wire together yourself. Additionally, Android provides you with so many other input interfaces, that you can get pretty clever with the context of the activity and connectivity. The opportunities are nearly endless for this. You can make a quick and basic prototype of any crazy IoT idea you have. Happy hacking!