electrotwelve

Using RESTful API to work with AWS IoT

5 posts in this topic

I've been tinkering with AWS IoT in conjunction with CC3200 + Energia for a while now with varying degrees of success. The purpose of this post is using the RESTful API to update a "thing" status in the cloud. The code is listed below and I'm seeing some authentication errors. I was hoping someone can correct me if there are any errors in the way I'm using "client.ssConnect". I used a slightly modified WiFiClient.cpp and WiFiClient.h from here. I know its not a library issue since I'm able to connect using MQTT. 

char aws_endpoint[] = "xxxxxxxxxxxxxx.iot.us-east-1.amazonaws.com";
 
if (client.sslConnect(aws_endpoint, 443))
  {
    Serial.println("Connected to server");
 
    String PostData = "{\"value1\" : \"testValue\", \"value2\" : \"Hello\", \"value3\" : \"World!\" }";
 
    request = "POST /things/";
    request += thingname;
    request += "/shadow";
    request += " HTTP/1.1";
    Serial.print("Request:\t"); Serial.println(request);
    Serial.print("Post data:\t"); Serial.println(PostData);
 
    client.println(request);
    client.println("Host: ");
    client.println(aws_endpoint);
    //client.println(":443");
    client.println("User-Agent: Energia/1.1");
    client.println("Connection: close");
    client.println("Content-Type: application/json");
    client.print("Content-Length: "); client.println(PostData.length());
    client.println();
    client.println(PostData);
    client.println();
  }
  else
  {
    Serial.println("Connection failed");
 
  }
 
  Serial.println(); 
 
  // Capture response from the server. (10 second timeout)
  long timeOut = 5000;
  long lastTime = millis();
  
  while((millis()-lastTime) < timeOut)
  { // Wait for incoming response from server
    while (client.available()) 
    { // Characters incoming from the server
      char c = client.read();            // Read characters
      Serial.write(c);
    }
  }

Here is the output that I see:

RootCA found!
Client certificates found!

Device ID is:	3250763216

Attempting to connect to WiFi network .
Connected to WiFi network: ABCDEFGH
Waiting for IP 
IP is: 192.168.1.107
Connected to server
Request:	POST /things/MyCC3200/shadow HTTP/1.1
Post data:	{\"value1\" : \"testValue\", \"value2\" : \"Hello\", \"value3\" : \"World!\" }

HTTP/1.1 403 Forbidden
content-type: application/json
content-length: 91
date: Tue, 26 Jul 2016 11:46:59 GMT
x-amzn-RequestId: 4d5388a9-e3c4-460a-b674-c3f971f3330d
connection: Keep-Alive
x-amzn-ErrorType: ForbiddenException:

{"message":"Missing Authentication Token","traceId":"4d5388a9-e3c4-460a-b674-c3f971f3330d"}

Share this post


Link to post
Share on other sites

Yeah all those sort of REST APIs require an authentication token in the headers.  Look more into AWS documentation for this.  It might be a key you get from their dashboard GUI for your account.

Share this post


Link to post
Share on other sites

Thanks folks. As it turns out I was using the wrong port. Its supposed to be 8443 instead of 443. With that in place, everything worked great! I also had to modify the PostData format to match what AWS needs it to be.

adrianF likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now