The background bits
The proposal for this roadtest plus was written around the TI TPS92512 which is an LED Driver Buck converter. The idea was to design a 4Channel Lighting solution along with a variety of LED panels. In addition to RGB Lighting Panels, it was proposed that custom ones can be made using special LEDs for agricultural lighting more commonly known as ‘Grow Lights’. Additionally, the control over the colour as well as intensity of the lights should be controllable over say Wi-Fi which can be accomplished by using a CC3200. The system is to be designed in an enclosure that can replace the conventional home or industrial lighting hence the title “Multipurpose Modular Light Bar Project”.
Since its inception, the idea has been modified to work as lighting solution for our expected child’s room. In addition to being an IoT Lighting solution, it will also double as a night light.
In the previous posts, I explained the PCB as well as working with the CC3200. I also showed you the basic UI for the project and in this post, I explain using MQTT and control. Here we go...
MQTT what?
According to wikipedia… “ MQTT[1] (formerly MQ Telemetry Transport) is a publish-subscribe based "light weight" messaging protocol for use on top of the TCP/IP protocol. It is designed for connections with remote locations where a "small code footprint" is required or the network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message. Andy Stanford-Clark and Arlen Nipper of Cirrus Link Solutions authored the first version of the protocol in 1999.”
What this means is that we have a central broker which is used as a wall to bounce of messages. An MQTT Client will send a message to the broker and another client anywhere can read that message if it is ‘listening’ for it. It offers QOS 0 where no acknowledgement and no storage of messages which means someone must be listening or the message can be lost.
Working with MQTT
Facebook messenger uses MQTT for its chatting features so its pretty widely used. There are a lot of great examples out there and TI’s own Adrian F has a demo at http://energia.nu/creating-an-iot-connected-sensor-with-energia-mqtt/
In our case, the Publisher which is a web browser running a Javascript version of the MQTT client (google PAHO for more details) will send information regarding the LED brightness to the it.eclipse.org broker. The broker is free and I have used it behind firewalls as well so its all good. The subscriber is the CC3200 Launchpad which receives the data and modifies the PWM output to match. In the proceeding sections, we will explain these in details.
MQTT Javascript
Your web browser is a powerful platform for me to run my code. I wrote a webpage with javascript to make the browser the publisher of MQTT messages. In order to make your own, visit https://eclipse.org/paho/clients/js/ and download the mqttws31.js file and put it alongside your .html page. In the HTML file, add the following code to the body.
<script src="js/mqttws31-min.js" type="text/javascript"></script> <script type="text/javascript"> var iotLampIntensity=0; var connected = false; client = new Paho.MQTT.Client("ws://iot.eclipse.org/ws", "id-" + new Date().getTime()); //Message Handler client.onMessageArrived = onMessageArrived; function onMessageArrived(message) { if( !isNaN(message.payloadString)){ iotLampIntensity=Number(message.payloadString); if(iotLampIntensity>255){ iotLampIntensity=255; } else if(iotLampIntensity<0){ iotLampIntensity=0; } document.getElementById("lampIntensity").innerHTML = "Current Intensity(0-255) = " + iotLampIntensity.toString(); } } function onConnect() { connected = true; client.subscribe("ipv1/babylight/query"); }; client.connect({ onSuccess: onConnect }); function sendLampCommand(){ if(connected){ var message = new Paho.MQTT.Message(':1'+toPaddedString(iotLampIntensity)); message.destinationName = "ipv1/babylight/command"; client.send(message); } } function lampMax(){ iotLampIntensity=255; sendLampCommand(); } function lampOff(){ iotLampIntensity=0; sendLampCommand(); } function lampToggleUp(){ iotLampIntensity=iotLampIntensity+10; if(iotLampIntensity>255){ iotLampIntensity=255; } sendLampCommand(); } function lampToggleDown(){ iotLampIntensity=iotLampIntensity-10; if(iotLampIntensity<0){ iotLampIntensity=0; } sendLampCommand(); } function toPaddedString(x1){ if(x1>=100){ return x1.toString(); } else if(x1>=10){ return '0' + x1.toString(); } else { return '00' + x1.toString(); } } </script>
The above script has a lot of functions from which we need to add some to the HTML.
In the .html file, we need to add the following button code.
<button onclick="lampMax()">Maximum</button> <button onclick="lampToggleUp()">Brigher</button> <button onclick="lampToggleDown()">Dimmer</button> <button onclick="lampOff()">Off</button>
This will send the commands to modify the brightness via MQTT. Cool now we have a JS client that can run locally or via a website. Four buttons! Next we write the CC3200 code.
The CC3200 code and TLV protocol
In a previous post, I showed you how you can use an example MQTT code to start with. Here we just need to make a few changes. When we receive an MQTT packet, it is stored in a buffer named message.payload or message.payloadString. This data can be ascii data or even binary information and we need to parse it. I use the TLV protocol which stands for Tag, Length, Value. In my case I copy the entire payload into my own object which is in the following format.
typedef struct ipv1_packet{ char type; char length; char value2; char value1; char value0; } _ipv1_packet;
This means that the first byte defines the type and in our case a valid packet is identified by a : character at the start. ( I did a lot of modbus programming in my early year. ) The next is one byte telling us the length which is 1(as an ascii character) the third fourth and fifth byte are the value of light intensity in three ascii characters such as 010 or 255. Now note here that 10 is invalid whereas 010 is the same number with a zero padded. The javascript has the toPaddedString function that I made that will convert correctly and send hence at the CC3200 end, all I need to do is convert the ascii numbers into hex values using weights.
Lastly this value is used to update the analog write function which in turn punches out the PWM signal to the TSP92512 module.
With this the CC3200 is ready for the show.
Prototype
I tested out everything and the website went live last week at http://inderpreet.github.io/HomeAutomationUI/babyroom.html
The prototype looks like this...
In the next post, I will discuss the final build, demo and summary.
Cheers,
IP
Top Comments