MQTT is a light weight message protocol widely used in IOT applications. We use MQTT to send data from ESP8266 to Bluemix. In the Bluemix we use Node-Red service.
What is ESP8266The ESP8266 is a low-cost Wi-Fi chip with full TCP/IP stack and MCU (microcontroller unit) capability produced by Shanghai-based Chinese manufacturer, Espressif Systems.
The chip first came to the attention of western makers in August 2014 with the ESP-01 module, made by a third-party manufacturer, AI-Thinker. This small module allows microcontrollers to connect to a Wi-Fi network and make simple TCP/IP connections using Hayes-style commands.
We program the ESP8266 in the Arduino IDE. Download the following libraries which are essential. https://github.com/esp8266/Arduino
Code#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "**********"; //your wifi ssid
const char* password = "*******"; //wifi password
#define mqtt_server "mqtt.dioty.co"
WiFiClient espClient;
PubSubClient client(espClient);
int count,value;
char hello[50];
void setup_wifi()
{
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int index()
{
count=count+1;
return count;
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
// char* a=topic;
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect()
{
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
client.connect("clientId","mailid","password"); //enter your email id and password
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("/mailid/helloworld",hello);
// ... and resubscribe
//client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected())
{
reconnect();
}
index();
client.loop();
snprintf(hello,50,"Helloworld=%d",count);
client.setCallback(callback);
client.publish("/mailid/helloworld",hello);
delay(1000);
}
We will use MQTT Broker Dioty for this tutorial, we will discuss in the next section. Make sure of entering the valid details of SSID and MQTT credentials.
MQTTMQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. For example, it has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios. It is also ideal for mobile applications because of its small size, low power usage, minimized data packets, and efficient distribution of information to one or many receivers. It works on publish and subscribe concept. Dioty is a MQTT broker which provides free service. Login into the Dioty and add the MQTT credentials in the above code.
BluemixBluemix is a cloud platform as a service (PaaS) developed by IBM. It supports several programming languages and services as well as integrated DevOps to build, run, deploy and manage applications on the cloud. Bluemix is based on Cloud Foundry open technology and runs on SoftLayer infrastructure. Login into Bluemix and create a Node-Red starter service in the catalog.
Fill the details and start the service. After creating the service go to the dashboard and make sure that the Node Red service is running.
Visit the application URL and then it is followed by a Node Red editor page.
Create a username and password and then login.
Connect the following node as shown.
Fill the credentials under the MQTT node. The username and the password are the parameters of the Dioty which we have logged in.
Deploy the flow and you can see data in the debug tab which is sent from the ESP8266.
Comments
Please log in or sign up to comment.