Main Content

Queue Commands Using the TalkBack App From MATLAB

TalkBack enables any device to act upon queued commands. This example shows how to queue commands using the TalkBack app from MATLAB® with built-in MATLAB functions such as webread and webwrite. For example, if you have a door with a wireless network and a motion sensor, you can queue up commands to open and close the door. When the door senses someone nearby, open the door. After a specified time, close the door. In ThingSpeak™, use the MATLAB Analysis app to write the MATLAB code.

Use webread with TalkBack

GET a TalkBack Command

You can use the webread to send an HTTP GET request to fetch a TalkBack command.

Specify the IDs of your TalkBack app and the TalkBack command. You can find the TalkBack app ID, the command ID, and the TalkBack API key in the TalkBack app page.

% TalkBack app ID
TalkBack_ID = '7991';
% TalkBack command ID
Command_ID = '1322093';
% TalkBack app API key
TalkBack_apikey = 'I586FE13SXIN9ZET';

Specify the api_key query parameter. webread appends web service query parameter names and values to the url.

url = strcat('https://api.thingspeak.com/talkbacks/',TalkBack_ID,'/commands/',Command_ID,'.json');
data = webread(url,'api_key',TalkBack_apikey)

Click Save and Run to run the MATLAB code in the MATLAB Analysis app.

webread returns the JSON object for the search result page as a structure array.

data = 
                id: 1322093
    command_string: 'OPENDOOR'
          position: 2
       executed_at: []
        created_at: '2016-04-13T20:08:58Z'

The command 'OPENDOOR' now opens the Wi-Fi enabled door.

POST a TalkBack Command

You can use the webread to send an HTTP POST request using weboptions to add a TalkBack command via TalkBack app.

url =  strcat('https://api.thingspeak.com/talkbacks/',TALKBACK_ID,'/commands.json');
options = weboptions('RequestMethod','post');
data = webread(url,'api_key','I586FE13SXIN9ZET','command_string','OPENDOOR',options)

Click Save and Run to run the MATLAB code in the MATLAB Analysis app. The following output is displayed on the Output section:

data = 
                id: 1328190
    command_string: 'OPENDOOR'
          position: 7
       executed_at: []
        created_at: '2016-04-14T18:26:45Z'

The command 'OPENDOOR' now opens the Wi-Fi enabled door.

Use webwrite with TalkBack

Write a TalkBack Command

Use the TalkBack app to create a TalkBack command. Open a new MATLAB Analysis app to write a command to the TalkBack app using webwrite.

Call webwrite using your TalkBack ID, Command ID, and TalkBack API key. You can find the TalkBack app ID, the command ID, and the TalkBack API key in the TalkBack app page.

% TalkBack app ID
TalkBack_ID = '7991'; 
% TalkBack command ID
Command_ID = '1322093'; 
% TalkBack app API key
TalkBack_apikey = 'I586FE13SXIN9ZET'; 
url =  strcat('https://api.thingspeak.com/talkbacks/',TALKBACK_ID,'/commands.json');
response = webwrite(url,'api_key',TALKBACK_APIKEY,'command_string','CLOSEDOOR')

Click Save and Run to run the MATLAB code in the MATLAB Analysis app. The following output is displayed on the Output section:

response =
                id: 1333480
    command_string: 'CLOSEDOOR'
          position: 10
       executed_at: []
        created_at: '2016-04-15T13:30:42Z'

The command 'CLOSEDOOR' now closes the Wi-Fi enabled door.

Write a TalkBack App Command as a Form-Encoded Character Array

Alternatively, you can encode your TalkBack API key and command as a form-encoded character array.

data = ['api_key=',TalkBack_apikey,'&command_string=','CLOSEDOOR'];
response = webwrite(url,data)

Click Save and Run to run the MATLAB code in the MATLAB Analysis app. The following output is displayed on the Output section:

response =
                id: 1333835
    command_string: 'CLOSEDOOR'
          position: 11
       executed_at: []
        created_at: '2016-04-15T14:50:53Z'

The command 'CLOSEDOOR' now closes the Wi-Fi enabled door.

Write a TalkBack App Command as a JSON Object

Alternatively, you can write a command to the TalkBack app as a JSON object. Create a structure where the fields are your TalkBack Key and the command. Specify the media type using weboptions as 'application/json'.

data = struct('api_key',TalkBack_apikey,'command_string','CLOSEDOOR');
options = weboptions('MediaType','application/json');
response = webwrite(url,data,options)

Click Save and Run to run the MATLAB code in the MATLAB Analysis app. The following output is displayed on the Output section:

response = 
                id: 1334587
    command_string: 'CLOSEDOOR'
          position: 12
       executed_at: []
        created_at: '2016-04-15T17:45:31Z'

The command 'CLOSEDOOR' now closes the wireless network enabled door.

See Also

(MATLAB) | (MATLAB) | (MATLAB)

Related Topics