In this project, you 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 these methods:

Step 1. Open the project folder

A. Start VS Code

  • Make sure your DevKit is not connected to your computer.
  • Start VS Code.
  • Connect the DevKit to your computer.

VS Code will automatically find your DevKit and display an introduction page:

mini-solution-vscode

Notice: Occasionally, when you launch VS Code, you may be prompted with an error message saying that the Arduino IDE or related board package cannot be found. If this should occur, close VS Code, launch the Arduino IDE again 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 opens a new VS Code window with a 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, please refer to this FAQ steps to resolve the issue.

Step 3. Modify the #hashtag

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

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

Replace the string iot within the curly braces with your preferred hashtag. DevKit will later retrieve a random tweet containing the hashtag you specified here.

Step 4. Deploy Azure Functions

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

cloud-deploy

Notice: Occasionally, the Azure Function may not work properly. If this occurs, please check this FAQ steps to resolve this issue.

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 sets the connection string that is retrieved from the task cloud-provision step.

VS Code then starts verifying and uploading the Arduino sketch to your DevKit:

device-upload

The DevKit reboots and starts running the code.

Notice: Occasionally, you may get an “Error: AZ3166: Unknown package” error message. This occurs when the board package index is not refreshed correctly. Check this FAQ steps to resolve this issue.

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 sets the connection string that is retrieved from the task cloud-provision step.

VS Code then starts verifying and uploading the Arduino sketch to your DevKit:

device-upload

The DevKit reboots and starts running the code.

Notice: Occasionally, you may get an “Error: AZ3166: Unknown package” error message. This occurs when the board package index is not refreshed correctly. Check this FAQ steps to resolve this issue.

Test the project

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

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 containing the #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 purposes. 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 a 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

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

  1. Click the 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 refer to FAQs or reach out to us from the channels below.

Updated: