image

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.

image

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[] = "192.168.1.165"; // MQTTServer to use

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

void setup()
{
  pinMode(PUSH1, INPUT_PULLUP);
  pinMode(PUSH2, INPUT_PULLUP);
  pinMode(GREEN_LED, OUTPUT); 
  pinMode(RED_LED, OUTPUT);  
  pinMode(BLUE_LED, OUTPUT);   
  
  Serial.begin(115200);
  Serial.print("Attempting to connect to Network named: ");   
  Serial.println(ssid);   
  WiFi.begin(ssid, password);  
  while ( WiFi.status() != WL_CONNECTED) 
  {
    Serial.print(".");     // print dots while we wait to connect
    delay(300);
  }
  
  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
    delay(300);
  }
  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");
   }
   else
   {
     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");
   }
   else
   {
     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  
     Serial.println(iA0);    
    }  

  }  
  else
  {
    Counter++;
  }  
*/ 
 
  client.loop(); // Check if any subscribed messages were received
  delay(10);
}



////////##### 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
  Serial.println();

  //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);}
}


image

HC-21 Simple Wifi Serial for Arduino Tutorial

The Simple Wi-Fi HC-21 module is a meager $10-$15 bucks on ebay right now. It is a simple Wi-Fi serial port server or virtual serial port. This means it is self contained and will handleimage all the overhead of negotiating a Wi-Fi connection and communication leaving more room for your application in the Arduino.

Continue reading

Windows 10 Tech Preview2

Windows 10, Should I get it?

Windows 10 (Threshold, Technical Preview)

Microsoft released a “Technical Preview” of their new upcoming version of Windows operating system on October 1, 2014. This version of Windows will be named Windows 10. Here is a brief review that should help the average users decide if Windows 10 is for them. Keep in mind a lot could happen between now and mid 2015 when windows 10 is released.Windows 10 Tech Preview2

Continue reading

image

How To Scale A Thermistor for An Arduino

Thermistors And Your Microcontroller

A thermistor is a simple temperature component, basically it is a variable resistor that the resistance changes in it depending on the temperature. Such a simple device needs a few more components and scaling in the microcontroller to give the desired C or F temperature reading.

Continue reading

Arduino Modbus PLC / RTU

PLC/RTU

A Programmable Logic Controller, PLC or Programmable Controller is a digital computer used for automation of electromechanical processes, such as control of machinery on factory assembly lines, amusement rides, or light fixtures. PLCs are used in many industries and machines.  http://en.wikipedia.org/wiki/Programmable_logic_controller

Continue reading