Mark Easley
Published © GPL3+

AT&T M2X + Energia + LaunchPad

Use the M2X data service with the Energia library and your TI LaunchPad

BeginnerFull instructions provided1,039
AT&T M2X + Energia + LaunchPad

Things used in this project

Story

Read more

Code

Code

C/C++
/* 
   LaunchPadWiFiButtonPost.ino 

   Author: Mark Easley
   This code example released to the public domain.
*/
#include <aJSON.h>
#include "SPI.h"
#include "WiFi.h"

#include "M2XStreamClient.h"

char ssid[] = "<ssid>"; //  your network SSID (name)
char pass[] = "<password>";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

char deviceId[] = "<device ID>"; // Device you want to push to
char streamName1[] = "buttonpress1"; // Stream you want to push to
char streamName2[] = "buttonpress2"; // Stream you want to push to
char m2xKey[] = "<M2X API key>"; // Your M2X access key


int btn1;
int btn2;

WiFiClient client;
M2XStreamClient m2xClient(&client, m2xKey);

void setup() {

    Serial.begin(9600);
    pinMode(PUSH1, INPUT_PULLUP);
    pinMode(PUSH2, INPUT_PULLUP);
    
    // attempt to connect to Wifi network:
    Serial.print("Attempting to connect to Network named: ");
    // print the network name (SSID);
    Serial.println(ssid); 
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    WiFi.begin(ssid, pass);
    while ( WiFi.status() != WL_CONNECTED) {
      // print dots while we wait to connect
      Serial.print(".");
      delay(300);
    }
  
    Serial.println("\nYou're connected to the network");
    Serial.println("Waiting for an ip address");
  
    while (WiFi.localIP() == INADDR_NONE) {
      // print dots while we wait for an ip addresss
      Serial.print(".");
      delay(300);
    }

    Serial.println("\nIP Address obtained");
  
    // you're connected now, so print out the status  
    printWifiStatus();
}

void loop() {

  btn1 = digitalRead(PUSH1);
  btn2 = digitalRead(PUSH2);
  Serial.print("btn1: ");
  Serial.println(btn1);
  Serial.print("btn2: ");
  Serial.println(btn2);

  int response = m2xClient.updateStreamValue(deviceId, streamName1, btn1);
  int response2 = m2xClient.updateStreamValue(deviceId, streamName2, btn2);
  Serial.print("M2X client response code: ");
  Serial.println(response);

  if (response == -1)
    while (1)
      ;
  if (response2 == -1)
    while (1)
      ;

  delay(3000);
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

Github

https://github.com/attm2x/m2x-launchpad-energia

Credits

Mark Easley

Mark Easley

65 projects • 137 followers
Texas Instruments LaunchPad SW Engineer

Comments