Simple, Robust M2M Messaging Via Amazon with Temboo

Simple, Robust M2M Messaging Via Amazon with Temboo © GPL3+

Here we'll show you how to pass short messages and commands to your Arduino Yún via Amazon SQS, a simple cloud-based message queue service.

  • 478 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

Get Set Up

  • Make sure you have a Temboo account. If you don't already have one, you can register for a free account here.
  • You'll also need an Amazon AWS account, which you can create here. Once you've created your account, make sure to set its region to US East as shown in the screenshot below.

Important Note: make sure you stay within the bounds of Amazon's free tier. Otherwise, you can run up an expensive AWS bill.

Message queue

Create a new message queue via the SQS Management Console. Give your queue a name and ignore the rest of the details.

Send a message to your queue

Next, we'll send a message to your queue via our website so that your Yún has something to receive when we run the sketch below.

  • First, go to our Amazon > SQS > SendMessage Choreo. You'll see that it requires a number of inputs.
  • Find your AWS Access Key and Secret Access Key in the Access Keys section of the AWS Security Credentials portal. If you haven't worked with AWS keys before, create a new Access Key to obtain these values. You'll also need your AWS ID, which can be found in the Account Identifiers section.
  • Input the values into the Choreo, along with the name of your message queue and the message you want to send to your Yún.

Note: make sure to remove the dashes from your AWS Account ID. It should be in the format "xxxxyyyyzzzz" as opposed to the format "xxxx-yyyy-zzzz".

  • When you've got all your inputs ready, run the Choreo. You should see an XML output message confirming that everything worked as planned.
  • At this point we strongly recommend that you save your AWS account credentials for future use so that you can easily send more messages to your Yún via SQS. Here's a tutorial that describes how Temboo helps with that.

Now you're all set to get started with the sketch. You'll need the AWS credential info that you gathered earlier, so don't throw it away!

Make sure that your Yún is connected to the Internet.

Write the Sketch

Copy the code below into a new sketch in your Arduino IDE. Replace the placeholder values in the code with your own AWS and message queue details.

Note: Again, remember to remove the dashes from your AWS Account ID. It should be in the format "xxxxyyyyzzzz" as opposed to the format "xxxx-yyyy-zzzz".

/*
  ReceiveAmazonSQSMessage
  Demonstrates reading a message from an Amazon SQS queue using the Temboo Arduino Yun SDK.
  
  This example code is in the public domain.
*/
#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information, as described below
/*** SUBSTITUTE YOUR VALUES BELOW: ***/
// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.
// your Amazon AWS Access Key ID
const String AWS_ACCESS_KEY_ID = "xxxxxxxxxxxxxxxxx";
// your Amazon AWS Secret Key ID
const String AWS_SECRET_KEY_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// your Amazon AWS Account ID
const String AWS_ACCOUNT_ID = "xxxxxxxxxxxx";
// your message queue name
const String MESSAGE_QUEUE_NAME = "xxxxxxxxxxxxx";
// the message visibility timeout sets the duration in seconds that a received message
// is hidden from future requests to retrieve messages.It is an optional input that is 
// specified in seconds.
const String VISIBILITY_TIMEOUT = "43200";
void setup() {
  Serial.begin(9600);
  
  // For debugging, wait until a serial console is connected.
  delay(4000);
  while(!Serial);
  Bridge.begin();
}
void loop()
{   
    TembooChoreo ReceiveMessageChoreo;
    // invoke the Temboo client
    ReceiveMessageChoreo.begin();
    
    // set Temboo account credentials
    ReceiveMessageChoreo.setAccountName(TEMBOO_ACCOUNT);
    ReceiveMessageChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    ReceiveMessageChoreo.setAppKey(TEMBOO_APP_KEY);
    
    
    // set choreo inputs
    ReceiveMessageChoreo.addInput("AWSAccountId", AWS_ACCOUNT_ID);
    ReceiveMessageChoreo.addInput("AWSAccessKeyId", AWS_ACCESS_KEY_ID);
    ReceiveMessageChoreo.addInput("AWSSecretKeyId", AWS_SECRET_KEY_ID);
    ReceiveMessageChoreo.addInput("QueueName", MESSAGE_QUEUE_NAME);
    ReceiveMessageChoreo.addInput("VisibilityTimeout", VISIBILITY_TIMEOUT);   
    
    // identify choreo to run
    ReceiveMessageChoreo.setChoreo("/Library/Amazon/SQS/ReceiveMessage");
    
    // set output filters
    ReceiveMessageChoreo.addOutputFilter("messageID", "/ReceiveMessageResponse/ReceiveMessageResult/Message/MessageId", "Response");
    ReceiveMessageChoreo.addOutputFilter("messageText", "/ReceiveMessageResponse/ReceiveMessageResult/Message/Body", "Response");
    
    
    // run the choreo; when results are available, print them to serial
    ReceiveMessageChoreo.run();
    String messageText; 
    String messageID;
    
    while(ReceiveMessageChoreo.available()) {
       // read the name of the next output item
        String name = ReceiveMessageChoreo.readStringUntil('\x1F');
        name.trim(); // use Ã

Create Your Header File

The sketch above references the TembooAccount.h header file, which contains your Temboo account information.

If you are currently logged in, you'll see your account details in the code snippet below (otherwise you'll see placeholder values). Create a new tab in the Arduino IDE and call it TembooAccount.h. Copy the code below into your new TembooAccount.h tab and then save your sketch.

With both files in place you're ready to upload the sketch and have your Yún retrieve the message from your queue.

When the sketch is running it will be listening out for new messages. One cool thing you can do is send more messages to the the queue and watch the serial monitor update live. To do that, just repeat step 4 as many time as you like.

#define TEMBOO_ACCOUNT "accountName"  // your Temboo account name 
#define TEMBOO_APP_KEY_NAME "myFirstApp"  // your Temboo app key name
#define TEMBOO_APP_KEY  "abc123xxxxxxxxxxxxxx"  // your Temboo app key

What's Next?

When you've got this sketch working, you're in a position to build any number of applications that involve passing messages to and from any kind of device. Amazon SQS acts as a simple, robust middleman - your imagination can do the rest. Check out rest of our 2000+ Choreos and start planning your next project.

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.

Code

Code snippet #1Arduino
/*
  ReceiveAmazonSQSMessage

  Demonstrates reading a message from an Amazon SQS queue using the Temboo Arduino Yun SDK.
  
  This example code is in the public domain.
*/

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information, as described below

/*** SUBSTITUTE YOUR VALUES BELOW: ***/

// Note that for additional security and reusability, you could
// use #define statements to specify these values in a .h file.

// your Amazon AWS Access Key ID
const String AWS_ACCESS_KEY_ID = "xxxxxxxxxxxxxxxxx";

// your Amazon AWS Secret Key ID
const String AWS_SECRET_KEY_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// your Amazon AWS Account ID
const String AWS_ACCOUNT_ID = "xxxxxxxxxxxx";

// your message queue name
const String MESSAGE_QUEUE_NAME = "xxxxxxxxxxxxx";

// the message visibility timeout sets the duration in seconds that a received message
// is hidden from future requests to retrieve messages.It is an optional input that is 
// specified in seconds.
const String VISIBILITY_TIMEOUT = "43200";


void setup() {
  Serial.begin(9600);
  
  // For debugging, wait until a serial console is connected.
  delay(4000);
  while(!Serial);
  Bridge.begin();
}
void loop()
{   
    TembooChoreo ReceiveMessageChoreo;

    // invoke the Temboo client
    ReceiveMessageChoreo.begin();
    
    // set Temboo account credentials
    ReceiveMessageChoreo.setAccountName(TEMBOO_ACCOUNT);
    ReceiveMessageChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    ReceiveMessageChoreo.setAppKey(TEMBOO_APP_KEY);
    
    
    // set choreo inputs
    ReceiveMessageChoreo.addInput("AWSAccountId", AWS_ACCOUNT_ID);
    ReceiveMessageChoreo.addInput("AWSAccessKeyId", AWS_ACCESS_KEY_ID);
    ReceiveMessageChoreo.addInput("AWSSecretKeyId", AWS_SECRET_KEY_ID);
    ReceiveMessageChoreo.addInput("QueueName", MESSAGE_QUEUE_NAME);
    ReceiveMessageChoreo.addInput("VisibilityTimeout", VISIBILITY_TIMEOUT);   
    
    // identify choreo to run
    ReceiveMessageChoreo.setChoreo("/Library/Amazon/SQS/ReceiveMessage");
    
    // set output filters
    ReceiveMessageChoreo.addOutputFilter("messageID", "/ReceiveMessageResponse/ReceiveMessageResult/Message/MessageId", "Response");
    ReceiveMessageChoreo.addOutputFilter("messageText", "/ReceiveMessageResponse/ReceiveMessageResult/Message/Body", "Response");
    
    
    // run the choreo; when results are available, print them to serial
    ReceiveMessageChoreo.run();

    String messageText; 
    String messageID;
    
    while(ReceiveMessageChoreo.available()) {
       // read the name of the next output item
        String name = ReceiveMessageChoreo.readStringUntil('\x1F');
        name.trim(); // use Ã

Comments

Similar projects you might like

Update Your Facebook Status with Temboo

by Temboo_OFFICIAL

  • 584 views
  • 1 comment
  • 0 respects

Read And Update Databases With Temboo

Project tutorial by Temboo_OFFICIAL

  • 2,356 views
  • 2 comments
  • 5 respects

Send an SMS using Twilio with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,452 views
  • 1 comment
  • 1 respect

Send An Email From Your Gmail Account With Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,354 views
  • 0 comments
  • 2 respects

Post Data to a Google Spreadsheet with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,088 views
  • 0 comments
  • 6 respects

Upload a File to your Dropbox Account with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,170 views
  • 0 comments
  • 1 respect
Add projectSign up / Login