TI LaunchPad MQTT I/O Demo

This demo connects two TI LaunchPad devices together using MQTT. I have every intention to add other devices to the mix like Arduino, Netduino, RaspBerry Pi, and BeagleBone in the future.



The Launchpad subscribes and publishes to a MQTT topic called LaunchPad. You can load the same code in multiple devices to tie them all together.


You can download the Energia Sketch here.

Or simply copy and paste this code into a new sketch.

 Basic MQTT example 
  - connects to an MQTT server
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"

#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>

char vPUSH1 = 0;
char vPUSH2 = 0;
char vGREEN_LED = 0;
char vRED_LED = 0;
char vBLUE_LED = 0;
int vA0 = 0;
int iA0 = 0;
int Counter = 0;

String rx;
int  rxLength = 0;

char ssid[] = "linksys";   // your network name also called SSID
char password[] = "S3cureP@ssword";  // your network password
char server[] = ""; // MQTTServer to use

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void setup()
  pinMode(GREEN_LED, OUTPUT); 
  pinMode(RED_LED, OUTPUT);  
  pinMode(BLUE_LED, OUTPUT);   
  Serial.print("Attempting to connect to Network named: ");   
  WiFi.begin(ssid, password);  
  while ( WiFi.status() != WL_CONNECTED) 
    Serial.print(".");     // print dots while we wait to connect
  Serial.println("nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  while (WiFi.localIP() == INADDR_NONE) 
    Serial.print(".");     // print dots while we wait for an ip addresss
  Serial.println("nIP Address obtained");

//########## MAIN PROGRAM ##########//
void loop()  
  //Reconnect if we are not subscribed to the LaunchPad/In topic
  if (!client.connected()) { 
    Serial.println("Disconnected. Reconnecting....");
    if(!client.connect("energiaClient")) {   //Make this unique for each device connecting to the MQTT Broker or they will disconnect eafchother!
      Serial.println("Connection failed");
    } else {
      Serial.println("Connection success");
      if(client.subscribe("LaunchPad")) {
        Serial.println("Subscription successfull");

  //Evaluate push button 1 and send a message if the value changes
  if (digitalRead(PUSH1) != vPUSH1)
   vPUSH1 = digitalRead(PUSH1);
   //digitalWrite(GREEN_LED, !vPUSH1);
   if (vPUSH1 == LOW)
     client.publish("LaunchPad", "GREEN_LED=HIGH");
     Serial.println("TX: LOW");
     client.publish("LaunchPad", "GREEN_LED=LOW");
     Serial.println("TX: HIGH");
  //Evaluate push button 2 and send a message if the value changes 
  if (digitalRead(PUSH2) != vPUSH2)
   vPUSH2 = digitalRead(PUSH2);
   //digitalWrite(RED_LED, !vPUSH2);
   if (vPUSH2 == LOW)
     client.publish("LaunchPad", "RED_LED=HIGH");
     Serial.println("TX: LOW");
     client.publish("LaunchPad", "RED_LED=LOW");
     Serial.println("TX: HIGH");
  //Evaluate Analog 0
/*  iA0 = analogRead(A0); // Read the analog value
  if (Counter >= 150)   // Counter reduces how many times we update to help prevent broker spam
    Counter = 0;
    if (((iA0 * 1.3) < vA0) or ((iA0 * 0.7) > vA0))   //Only Update if the value is +- 30% than the saved value
     vA0 = iA0;                              //Set the variables equal to eachother for next time comparison
     String str = (String)iA0;               //PubSubClient requires data to be a char* (char array) so we have to convert the int
     int str_len = str.length() +1;          //Length + null terminator
     char char_array[str_len];               //Prepare the character array (buffer)
     str.toCharArray(char_array, str_len);   // Copy it over
     client.publish("LaunchPad", char_array); // Publish to MQTT
     Serial.print("TX: ");                    // Print to Seral window  

  client.loop(); // Check if any subscribed messages were received

////////##### SUBROUTINES #####/////////

///// VOID CALLBACK - prints to the serial monitor if we recieve a MQTT message /////
void callback(char* topic, byte* payload, unsigned int length) 
  Serial.print("RX: ");
  //Convert and clean up the MQTT payload messsage into a String
  rx = String((char *)payload);             //Payload is a Byte Array, convert to char to load it into the "String" object 
  rxLength = rx.length();                   //Figure out how long the resulting String object is 
  for (int i = length; i <= rxLength; i++)  //Loop through setting extra characters to null as garbage may be there
    rx.setCharAt(i, ' ');
  rx.trim();                                //Use the Trim function to finish cleaning up the string   
  Serial.print(rx);                         //Print the recieved message to serial

  //Evaulate the recieved message to do stuff
  if (rx == "BLUE_LED=HIGH"){digitalWrite(BLUE_LED, HIGH);}  
  if (rx == "BLUE_LED=LOW"){digitalWrite(BLUE_LED, LOW);}
  if (rx == "RED_LED=HIGH"){digitalWrite(RED_LED, HIGH);}  
  if (rx == "RED_LED=LOW"){digitalWrite(RED_LED, LOW);}
  if (rx == "GREEN_LED=HIGH"){digitalWrite(GREEN_LED, HIGH);}  
  if (rx == "GREEN_LED=LOW"){digitalWrite(GREEN_LED, LOW);}
  if (rx == "WHITE_LED=HIGH"){digitalWrite(GREEN_LED, HIGH);digitalWrite(BLUE_LED, HIGH);digitalWrite(RED_LED, HIGH);}  
  if (rx == "WHITE_LED=LOW"){digitalWrite(GREEN_LED, LOW);digitalWrite(BLUE_LED, LOW);digitalWrite(RED_LED, LOW);}

Leave a Comment