Main Content

Read and Write to ThingSpeak Channel Using Android HTTP Client Block

This example shows how to use the HTTP Client block from the Simulink® Support Package for Android® Devices to read and write data to a field in a ThingSpeak™ channel.

In this example, a linear acceleration data from the Android device accelerometer is read and written to the fields of a ThingSpeak channel.

Prerequisites

For more information on how to use the Simulink Support Package for Android Devices to run a Simulink model on your Android device, see Getting Started with Android Devices.

Required Hardware

Android device, such as a phone or tablet

Create ThingSpeak Channel

For more details on how to create a new ThingSpeak channel, see Collect Data in a New Channel (ThingSpeak). Update the name and fields of the ThingSpeak channel for the data you are publishing and retrieving. To find your Channel ID and Write API Key, refer to Channel Info on the My Channels tab. You can use the Write API key to update the channel. Similarly, you can use the Read API key to read data from the API. To get the Write or Read API key, click Channels > My Channels. Select the API channel and then select the API Keys tab. For more information, see Channel Data Control (ThingSpeak).

Configure Simulink Model and Calibrate Parameters

Open the androidHttpClient Simulink model.

This example uses a preconfigured Simulink model that uses two HTTP Client blocks, one to write the acceleration data to the ThingSpeak channel while, the other to read the acceleration data from the ThingSpeak channel and display it on your Android device.

In the Read from a channel field area, update the Read API key and channel number.

Deploy Simulink Model on Android Device

Read from a Channel

In the Simulink model, comment out the Read from a channel field area. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start. On your ThingSpeak channel, click the Private View tab. The view displays the X-, Y, and Z-axis accelerometer data of your Android device that is published to ThingSpeak.

Write to a Channel

On the Simulink model, uncomment the Read from a channel field area and comment out the Write to a channel field area. Deploy the Simulink model once again. The androidHttpClient application launches automatically. Observe the field 2 data displayed on the application.

See Also