How to communicate with Sparkfun’s free cloud data service

sparkfun_logo

Last week Sparkfun Electronics announced a new, open source cloud server application called Phant. Phant acts as a logging tool which allows any device to read and write data to and from the service. Additionally, Sparkfun also created data.sparkfun.com, which is a free service running phant. This service allows anyone to save up to 50MB worth of data to the cloud, allowing 100 pushes over 15 minutes, which averages out to a push every 10 seconds or so.

To send data to and from the service, you just need to register a new stream. Registering gives you a private key and public key for accessing the data. A private key is required to update that stream, while a public key grants access to any other stream on the service.

Communication with Phant

All communication with Phant is carried out over HTTP. The simplest way to log data is through your web browser. Type in a URL like the following in your favorite browser:

http://data.sparkfun.com/input/<public_key>?private_key=<private_key>&<variable_name>=<variable_value>

This URL would update the specified variable with the specified value. Typing a URL in the browser is great to confirm that the service is working, but what do you do when you want to send data without a user interface?

To answer this, let’s briefly go back to the basics. When you type a URL in to the browser, the following is performed over TCP.

  1. Open a TCP socket to a server on port 80
  2. Send an HTML GET request
  3. Read any response that may come back from the server
  4. Close the socket

All of these commands can be boiled down to a few lines of code. The following code has been testing with the NetBurner “IoT cloud kit”, with our MOD54415 module. This code however could run on any NetBurner network module.

Open a TCP socket to a server on port 80

int httpSocket = connect(serverIP, 0, 80, 5 * TICKS_PER_SECOND);
if (httpSocket < 0) {
    iprintf("Unable to connect to: ");
    ShowIP(serverIP);
    iprintf("rn");
}

This line of code opens up a TCP socket to the IP address specified at port 80. A timeout is included so in case the connection fails. After verifying that the socket was opened, continue.

Send an HTML GET request

char buffer[255];
sprintf(buffer,
        "GET /input/%s?private_key=%s&%s=%lu HTTP1.0rnrn", public_key, private_key, "uptime", Secs);
int n = write(httpSocket, buffer, strlen(buffer));

When you type a URL in to your browser, you are usually sending a GET request. You are getting information from the server at the specified URI. In this case, everything the server needs to know with regards to logging is send in the GET request. The full URI indicates that I want to input data, includes a private key and public key, specifies are variable name to be updated, and finally specifies the new value.

Read any response that may come back from the server

char rxbuffer[1500];
do {
    bytes = ReadWithTimeout(httpSocket, rxbuffer, 1500, 5 * TICKS_PER_SECOND);
    rxbuffer[bytes] = 0;
    iprintf("%s",rxbuffer);
} while (bytes > 0);

In this case, the only message sent back from Sparkfun is a confirmation of the submission.

Close the socket

close(httpSocket);

Finally, close the socket that is currently open to Sparkfun. This will resolve the communication. If they have not already, the Sparkfun service will reciprocate by closing the socket as well.

With that, the application is now sending data to the cloud. To view the data on the service, visit data.sparkfun.com and view the stream. Alternatively, the application can retrieve the stream in a similar manner as sending data to the service. The difference is the URI that the applications needs to send to the service. To retrieve data, try the following code:

char buffer[255];
sprintf(buffer, "GET /output/%s.json?page=1 HTTP/1.0rnrn",public_key);
int n = write(httpSocket, buffer, strlen(buffer));

This code would retrieve the first 50Kb of data from the stream associated with the provided public key.

Sending data out to the cloud offers an easy way to store your data. To learn more about Phant, see the Phant Github Repository.

Share this post

Subscribe to our Newsletter

Get monthly updates from our Learn Blog with the latest in IoT and Embedded technology news, trends, tutorial and best practices. Or just opt in for product change notifications.

Leave a Reply
Click to access the login or register cheese