Using AT&T's M2X With the CC3000

This Tutorial is Retired!

This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.

Pages
Contributors: Shawn Hymel
Favorited Favorite 5

Post Temperature Data

The first logical step in using the M2X service is to create a data stream and post information to it. In this tutorial, we connect a temperature sensor to an Arduino equipped with a CC3000 shield and send temperature readings to our M2X stream. This stream will automatically display the data in an easy-to-read chart for us to look at.

Posting temperature and humidity data to M2X

Required Materials

The Circuit

Connect the CC3000 shield to the Arduino, and connect the level shifter and temperature sensor as shown.

Fritzing of CC3000 with HTU21D

Example Code

Open up the Arduino IDE and paste in the following code:

language:c
/****************************************************************
M2X_CC3000_Post.ino
Post temperature data to AT&T's M2X
Shawn Hymel @ SparkFun Electronics
August 19, 2014

Manually connects to a WiFi network and an M2X stream. Reads 
temperature data from an HTU21D temperature and posts it to the
M2X stream.

Change AP_SSID, AP_PASSWORD, AP_SECURITY, FEED_ID, STREAM_NAME,
and M2X_KEY to match your WiFi and M2X parameters.

The security mode is defined by one of the following:
WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA, WLAN_SEC_WPA2

Resources:
Include SPI.h, SFE_CC3000.h, SFE_CC3000_Client.h, jsonlite.h,
M2XStreamClient.h, Wire.h, HTU21D.h

Development environment specifics:
Written in Arduino 1.0.5
Tested with Arduino UNO R3

This code is beerware; if you see me (or any other SparkFun 
employee) at the local, and you've found our code helpful, please
buy us a round!

Distributed as-is; no warranty is given.
****************************************************************/

#include <SPI.h>
#include <SFE_CC3000.h>
#include <SFE_CC3000_Client.h>
#include <jsonlite.h>
#include <M2XStreamClient.h>
#include <Wire.h>
#include <HTU21D.h>

// Parameters
#define POST_DELAY_MS   10000 // Post to stream every 10 seconds
#define DEGREE_SYMBOL   176   // Degree symbol for Serial.write

// Pins
#define CC3000_INT      2   // Needs to be an interrupt pin (D2/D3)
#define CC3000_EN       7   // Can be any digital pin
#define CC3000_CS       10  // Preferred is pin 10 on Uno

// Connection info data lengths
#define IP_ADDR_LEN     4   // Length of IP address in bytes

// WiFi Constants
#define AP_SSID         "<SSID>"      // SSID of network
#define AP_PASSWORD     "<PASSWORD>"  // Password of network
#define AP_SECURITY     WLAN_SEC_WPA2 // Security of network
#define TIMEOUT         30000         // Milliseconds

// M2X Constants
#define FEED_ID         "<FFED ID>"
#define STREAM_NAME     "<STREAM NAME>"
#define M2X_KEY         "<M2X MASTER KEY>"

// Global Variables
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);
SFE_CC3000_Client client = SFE_CC3000_Client(wifi);
M2XStreamClient m2x_client(&client, M2X_KEY);
HTU21D sensor;
float temp;
int g_response;

// Setup. Configure HTU21D, WiFi, and M2X.
void setup() {

  g_response = 0;

  // Initialize UART for debugging
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("SparkFun CC3000 - M2X Post"));

  // Initialize HTU21D
  sensor.begin();

  // Initialize CC3000 (configure SPI communications)
  if ( wifi.init() ) {
    Serial.println(F("CC3000 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during CC3000 init!"));
  }

  // Connect using DHCP
  Serial.print(F("Connecting to SSID: "));
  Serial.println(AP_SSID);
  if(wifi.connect(AP_SSID, AP_SECURITY, AP_PASSWORD, TIMEOUT)) {
    Serial.println(F("Connected!"));
  } else {
    Serial.println(F("Error: Could not connect to AP"));
  }
}

// Main loop. Post sensor readings at regular intervals.
void loop() {

  // Read sensor
  temp = sensor.readTemperature();

  // Print reading to console with degree symbol and 'C'
  Serial.print(F("Temperature: "));
  Serial.print(temp, 2);
  Serial.write(176);
  Serial.println("C");

  // Post data to your stream
  g_response = m2x_client.post(FEED_ID, STREAM_NAME, temp);

  // If we fail to receive a response, stop running
  Serial.print(F("Post response: "));
  Serial.println(g_response);
  if ( g_response == -1 ) {
    while(1);
  }

  // Wait to post to stream again
  delay(POST_DELAY_MS);
}

Before you can run the code, you will need to update a few variables:

  • \<SSID> should be changed to your WiFi network name (SSID)
  • \<PASSWORD> is your WiFi password
  • Make sure AP_SECURITY matches your WiFi security type (e.g. WPA, WEP)
  • Change \, \, and \ to match your M2X stream information (see next section)

Make a Data Stream

Log in to https://m2x.att.com/.

M2X Main Dashboard

Click on the Blueprint that we created ("CC3000 Test").

M2X Blueprint Dashboard

Click on "Add Stream" and fill out some information about your stream. We call it "temperature." Under "Units & Symbol," start typing "Celsius," and you will be presented with a drop-down list. Select "degree Celsius."

M2X Add a Stream

Click "Add Stream." You will be returned to your Blueprint dashboard with a new "temperature" stream added. Click "temperature" to drop down your stream data. There should not be anything in it right now (don't worry, we are about to add some data!).

m2x new stream with no data

Scroll up on that same page to find your "FEED ID." Copy that long string and paste it into our M2X_CC3000_Post sketch (replace \).

M2X Feed ID

Replace \ in our code with the name of the stream ("temperature" in this case).

Back on the M2X site, click "ACCOUNT" to go to your account settings.

M2X Account Page

Click on "MASTER KEYS" to see your M2X Master Key. Copy that string and paste it into your code (replace \).

M2X Master Key

Run Your Program

Verify that AP_SSID, AP_PASSWORD, AP_SECURITY, FEED_ID, STREAM_NAME, and M2X_KEY are correct in your code. Click "Upload" and wait for the Arduino IDE to upload the code to your Arduino or RedBoard. Open a Serial Monitor, and you should see temperature data being posted to your M2X stream!

Note: the response should be "204." If you see "404" or "-1" it means there was an error connecting to your WiFi or to the M2X service. If you see "422" it means that your M2X_KEY, FEED_ID, or STREAM_NAME is incorrect. To learn more about response codes, see the M2X API documentation.

Temperature data logging

Temperature data logged to M2X

Public Data Stream

If you want others to be able to view your data, you can make your Blueprint (and associated streams) public. On the Blueprint dashboard, click the "Edit" button next to your Blueprint name and change the visibility from "Private" to "Public." Click "Save" and wait a few minutes for this to update (this can take upwards of 15 minutes).

Underneath your Blueprint name on the dashboard, click the link "Preview Public Page URL." This will take you to a separate page where you can view the streams associated with that Blueprint. If you made your Blueprint public, you can share this URL with anyone.

M2X Public Blueprint