Permalink
Find file Copy path
185 lines (105 sloc) 7.81 KB

Shake, Shake for a Tweet -- Retrieve a Twitter message with Azure Functions!

In this tutorial, you learn how to use the motion sensor to trigger an event using Azure Functions. The app retrieves a random tweet with a #hashtag you configure in your Arduino sketch. The tweet displays on the IoT DevKit screen.

About IoT DevKit

The MXChip IoT DevKit (a.k.a. IoT DevKit) is an all-in-one Arduino compatible board with rich peripherals and sensors. You can develop for it using Azure IoT Device Workbench . And it comes with a growing projects catalog to guide you prototype Internet of Things (IoT) solutions that take advantage of Microsoft Azure services.

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 two methods:

Open the project folder

Start VS Code

Open Azure IoT Device Workbench Examples

Use F1 or Ctrl+Shift+P (macOS: Cmd+Shift+P) to open the command palette, type Azure IoT Device Workbench, and then select Open Examples....

IoT Device Workbench: Examples

Select IoT DevKit.

IoT Device Workbench: Examples -> Select board

Then the IoT Device Workbench Example window is shown up.

IoT Device Workbench, Examples window

Find Shake, Shake for a Tweet and click Open Sample button. A new VS Code window with a project folder in it opens.

IoT Device Workbench, select "Shake, Shake for a Tweet" example

Provision Azure services

In the solution window, open the command palette and select Azure IoT Device Workbench: Cloud: Provision Azure Services....

IoT Device Workbench: Cloud -> Provision

Then VS Code guides you through provisioning the required Azure services.

IoT Device Workbench: Cloud -> Provision steps

The whole process includes:

  • Select an existing IoT Hub or create a new IoT Hub.
  • Select an existing IoT Hub device or create a new IoT Hub device. Please take a note of the device id. It will be used in next section.
  • Create a new Azure Functions App.

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 later retrieves a random tweet that includes the hashtag you specify in this step.

Modify code for Azure Functions

Open Function\shakeshake.cs and find the following code:

    // TODO: Input the device name.
    string deviceId = "";

Set deviceId with the device id you provisioned in previous step.

Deploy Azure Functions

Open the command palette and select Azure IoT Device Workbench: Deploy to Azure....

IoT Device Workbench: Cloud -> Deploy

Config IoT Hub Device Connection String

  1. Switch the IoT DevKit into Configuration mode. To do so:

    • Hold down button A.
    • Push and release the Reset button.
  2. The screen displays the DevKit ID and 'Configuration'.

    IoT DevKit Configuration Mode

  3. Open the command palette and select Azure IoT Device Workbench: Config Device Settings....

    IoT Device Workbench: Device -> Settings

  4. Select Config Device Connection String

  5. Select Select IoT Hub Device Connection String.

    IoT Device Workbench: Device -> Connection string

    This sets the connection string that is retrieved from the Provision Azure services step.

  6. The configuration success notification popup bottom right corner once it's done.

    IoT DevKit Connection String OK

Build and upload the device code

  1. Open the command palette and select Azure IoT Device Workbench: Upload Device Code.

    IoT Device Workbench: Device -> Upload

  2. VS Code then starts verifying and uploading the code to your DevKit.

    IoT Device Workbench: Device -> Uploaded

  3. The IoT DevKit reboots and starts running the code.

Test the project

After app initialization, click and release button A, then gently shake the DevKit board. This action retrieves a random tweet, which contains the hashtag you specified earlier. Within a few seconds, a tweet displays on your DevKit screen:

Arduino application initializing... Arduino-application-initializing

Press A to shake... Press-A-to-shake

Ready to shake... Ready-to-shake

Processing... Processing

Press B to read... Press-B-to-read

Display a random tweet... Display-a-random-tweet

  • 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 the Azure IoT Hub. This event triggers the Azure Functions app. Azure Functions app contains the logic to connect to Twitter's API and retrieve a tweet. It then 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

For testing purposes, this sample project uses a pre-configured Twitter bearer token. However, there is a rate limit for every Twitter account. If you want to consider using your own token, follow these steps:

  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. Update the following line in Function\shakeshake.cs with your own token: update-token

    ...
    string authHeader = "Bearer " + "[your own token]";
    ...
  5. Open the command palette and select IoT Device Workbench: Cloud, then select Azure Deploy.

Next Steps

Now you make the IoT DevKit as a Twitter Reader by using Azure Functions.

Check our Projects Catalog for more samples you can build with the IoT DevKit and Azure multiple services.