In this project, you will learn how to use the motion sensor to trigger an event using Azure Functions. The app will retrieve a random tweet with a #hashtag you have configured in your Arduino sketch. The tweet will display on the DevKit screen.

What you need

Finish the Getting Started Guide to:

  • Have your DevKit connected to Wi-Fi
  • Prepare the development environment

An active Azure subscription. If you do not have one, you can register via one of the methods:

Step 1. Open the project folder

A. Start VS Code

Make sure your DevKit is not connected. Start VS Code first and connect the DevKit to your computer. VS Code will automatically find it and pops up introduction page:

mini-solution-vscode

Notice: Occasionally, when you launch VS Code, you will be prompted with an error that cannot find Arduino IDE or related board package. Close VS Code, launch Arduino IDE once and VS Code should locate Arduino IDE path correctly.

B. Open Arduino Examples folder

Expand left side ARDUINO EXAMPLES section, browse to Examples for MXCHIP AZ3166 > AzureIoT, and select ShakeShake. This will open a new VS Code window with project folder in it.

mini-solution-examples

If you happen to close the pane, you can reopen it. Use Ctrl+Shift+P (macOS: Cmd+Shift+P) to open the command palette, type Arduino, and then find and select Arduino: Examples.

Step 2. Provision Azure services

In the solution window, run your task through Ctrl+P (macOS: Cmd+P) by entering task cloud-provision:

In the VS Code terminal, an interactive command line will guide you through provisioning the required Azure services:

cloud-provision

Notice: If the page hangs in the loading status when trying to sign in to Azure, plese check this FAQ steps to solve it.

Step 3. Modify the #hashtag

Open ShakeShake.ino and look for the line of code:

static const char* iot_event = "{\"topic\":\"iot\"}";

Replace the string iot in the curly brace with your preferred hashtag. And DevKit will later retrieve a random tweet with the hashtag you set here.

Step 4. Deploy Azure Functions

Use Ctrl+P (macOS: Cmd+P) to run task cloud-deploy. It will start deploying the Azure Functions code:

cloud-deploy

Notice: Occasionally, if the Azure Function can’t work properly please check this FAQ steps to solve it.

Step 5. Build and upload the device code

Windows

  1. Use Ctrl+P to run task device-upload.
  2. The terminal prompts you to enter configuration mode. To do so, hold down button A, then push and release the reset button. The screen displays the DevKit id and ‘Configuration’.

This is to set the connection string that retrieves from task cloud-provision step.

Then VS Code starts verifying and uploading the Arduino sketch:

device-upload

The DevKit reboots and starts running the code.

Notice: Occasionally, you get error “Error: AZ3166: Unknown package”. This is due to the board package index is not refreshed. Check this FAQ steps to solve it.

macOS

  1. Put DevKit into configuration mode: Hold down button A, then push and release the reset button. The screen displays ‘Configuration’.
  2. Use Cmd+P to run task device-upload.

This is to set the connection string that retrieves from task cloud-provision step.

Then VS Code starts verifying and uploading the Arduino sketch:

device-upload

The DevKit reboots and starts running the code.

Notice: Occasionally, you get error “Error: AZ3166: Unknown package”. This is due to the board package index is not refreshed. Check this FAQ steps to solve it.

Test the project

After app initialization, click button A and mildly shake the board to retrieve a random tweet with your hashtag (e.g. #iot). A tweet will display on your screen in a few seconds:

Arduino application initializing Press A to shake Ready to shake Processing... Press B to read Display a random tweet
Shake, shake for a random tweet with #hashtag you set in the code.
  • Press button A again, then shake for a new tweet.
  • Press button B to scroll through the rest of the tweet.

How it works

diagram

The Arduino sketch sends an event to Azure IoT Hub which triggers the Azure Functions app. Azure Functions contains the logic to connect to Twitter’s API and retrieve a tweet. It wraps the tweet text into a C2D (Cloud-to-device) message and sends it back to the device.

Optional: Use your own Twitter bearer token

This sample project uses a pre-configured Twitter bearer token for testing purpose. But there is a rate limit for every Twitter account. You might want to consider using your own token. Here are simple instructions to do so:

  1. Go to Twitter Developer portal to register a new Twitter app.

  2. Get Consumer Key and Consumer Secrets of your app.

  3. Use some utility to generate Twitter bearer token from these two keys.

  4. In the Azure portal, get into the Resource Group and find the Azure Function (Type: App Service) for your “Shake, Shake” project. The name always contains ‘shake…’ string. azure-function

  5. Update the code for run.csx within Functions > shakeshake-cs with your own token:
      ...
      string authHeader = "Bearer " + "[your own token]";
      ...
    

    twitter-token

  6. Save the file and click Run.

Problems and feedback

Problems

The screen displays ‘No Tweets’ while every step has run successfully

It normally happens for the first time you deploy and run the sample. This is because the function app will require a couple of seconds up to one minute to cold start the app. Or there are some blips when running the code that will cause restarting of the app. Then the device app can get timeout for fetching the tweet. In this case, you may try one or both methods to solve this issue:

  1. Click reset button on the DevKit to run the device app again.

  2. In the Azure portal, find the Azure Functions app you created and restart it: azure-function-restart

Feedback

If you encounter problems, you can find FAQs if you encounter other problems or reach out to us from the channels below.

Updated: