
Current trend in DIY is IoT based projects. The present professional electronics engineering industry is also now focusing to IoT.There are many existing protocols are there for making connection with your device with Web.If we need real time data in every second or less than that,We will need a fast internet connectivity protocol.And it should use very little bandwidth.Here we have MQTT.
MQTT is an extreamly light weight connectivity protocol which is mainly used for machine to machine /IoT applications.It was formerly known as MQ Telemetry Transport.it is a publish/subscribe based messaging protocol for use on the top of TCP/IP protocol.MQTT is very helpful where network bandwidth is limited.Many IoT companies uses MQTT for their applications.
How to install MQTT broker in Raspberry pi
First we have to import repository package for mosquitto into our raspberry pi.For that open the Raspberry pi terminal and run below commands.I used a Raspberry Pi 3 in jessie with 16GB microSD card How to install MQTT Client in Raspberry pi
How to use MQTT client as Subscriber
ESP8266 as MQTT client
As we know ESP8266 WiFi Module is a self contained SOC with integrated TCP/IP protocol stack that can give any micro controller access to our WiFi network.Also we can program the ESP8266 directly from our arduino IDE.There many libraries available for ESP8266 in the community.I am sharing two code snippets .One is for publishing using ESP8266 and the other is for publishing and subscription.
Here our broker is set upped in raspberry pi so we have to note the IP of the raspberry pi and include in our ESP8266 arduino code. // This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"
// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"
#include "MQTT/MQTT.h"
String sensor;
byte server[] = { 192,168,0,115 };//the IP of broker
void callback(char* topic, byte* payload, unsigned int length);
MQTT client(server, 1883, callback);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
if (message.equals("RED"))
RGB.color(255, 0, 0);
else if (message.equals("GREEN"))
RGB.color(0, 255, 0);
else if (message.equals("BLUE"))
RGB.color(0, 0, 255);
else if (message.equals("wiring_it_my_way"))
{RGB.color(255, 0, 0);
delay(1000);
RGB.color(0, 255, 0);
delay(1000);
RGB.color(0, 0, 255);
delay(1000);}
else
RGB.color(255, 255, 255);
delay(1000);
}
void setup() {
pinMode(A0,INPUT);
RGB.control(true);
client.connect(System.deviceID());
if (client.isConnected()) {
client.subscribe("color");//color is the topic that photon is subscribed
client.publish("fun", "hello");//publishing a data "hello" to the topic "fun"
}
}
void loop() {
int sensor_value =analogRead(A0);
sensor =String (sensor_value);
if (client.isConnected())
client.loop();
client.publish("value", sensor);//publishing a sensor data to the topic "value"
} when i heard the name MQTT for the first time i thought it will be very difficult for a hardware geek like me.But the broker set up was very easy and straight forward.It makes MQTT very easy.Regarding quality there are methods in MQTT to retain messages,confirmation etc.I don't have a plan to explain those in this post.Now make your own project in MQTT in whichever platform you like and please let me know your ideas in comments.
you can find the hackster project here










