Controlling Adruino devices using MQTT


(The above picture displays two LEDs and temperature capture. However, the code is for three LEDs, temperature and light intensity feed)


Today was the second week of IOT class, in which we were supposed to interact with MQTT server by subscribing to the users and their messages and pushing the message to MQTT broker to control the devices connected to Arduino device.

The MQTT is an MQ Telemetry Transport,  providing easy communication between many IoT nodes. The central piece is a broker, which connects to the various nodes which can subscribe to topics. In our experiment, we connected the Arduino device to MQTT broker using the Arduino Ethernet sheet and Ethernet cable to connect to the internet. For this, a sample code to establish this connection is being downloaded from the internet. This code we extended to control the three LEDs (which could be any devices) and two inputs devices, one temperature sensor to capture room temperature and LDR to capture the light intensity in the room. The code creates the topics on MQTT broker and any user subscripted to the topic will keep on receiving the data feed. The two topics were “room/temp” and “room/light”, the name could be anything. Any MQTT client who has subscribed to these topics will keep on receiving the data feed of room temperature and light intensity at a regular interval decided by code. These data feeds we had transferred to Cloud platform Thingspeak for storage on which the data analytics can be performed to derive meaning from it. For three LEDs, we had defined topic “room/led”, the user subscribed to this topic can control the switching on and off of LED by pushing the on and off message from MQTT client to MQTT broker from any part of the globe. The code we used to control these activities is attached below.

To interact with MQTT broker, user or nodes need to install the MQTT client either on their smartphone by installing MyMQTT client app or by installing MQ FX on laptop/desktop. Ater entering the MQ broker IP address and default port no as 1883, one can subscribe the topics for which we will receive the feeds, for example, the temperature and light intensity capture from Arduino. Also, these clients can send the messages from their smartphone app or laptop to control the LED from any location on the earth via the internet.


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

//Topics defined on MQTT brokers
char* outTopic     = "room/temp";    // * MQTT channel where physical updates are published
char* outTopic2     = "room/light";  // * MQTT channel where physical updates are published
char* inTopic1      = "room/led7";   // * MQTT channel where lelylan updates are received
char* inTopic2      = "room/led8";   // * MQTT channel where lelylan updates are received
char* inTopic3      = "room/led9";   // * MQTT channel where lelylan updates are received

char* payloadOn   = "temp";
char* payloadOnldr   = "light";

char* clientId     = "umesh_arduino";

//Command to switch on the LEDs
char* payloadOn7    = "1";
char* payloadOn8    = "11";
char* payloadOn9    = "111";

//Command to switch off the LEDs
char* payloadOff7   = "0";
char* payloadOff8   = "00";
char* payloadOff9   = "000";

bool state;
char buf1[30];

byte mac[] = {0xA2, 0xA0, 0xBB, 0xAA, 0xEF, 0xA1};
byte server[] = {198, 41, 30, 241};   //IP address of broker
EthernetClient ethClient;

void callback(char* topic, byte* payload, unsigned int length); // subscription callback
PubSubClient client(server, 1883, callback, ethClient);         // mqtt client


void setup() {
  pinMode(7, OUTPUT);  //First LED is connected to pin 7 of Arduino
  pinMode(8, OUTPUT);  //Second LED is connected to pin 8 of Arduino
  pinMode(9, OUTPUT);  //Third LED is connected to pin 9 of Arduino
  pinMode(A0, INPUT);  //Temperature sensor is on A0 analog pin
  pinMode(A3, INPUT);  //LDR is on A3 analog pin
  bool flag = HIGH;

  Serial.begin(9600);
  delay(500);
  Ethernet.begin(mac);
  Serial.print("Connected with IP: ");
  Serial.println(Ethernet.localIP());


  if (!client.connected()) {
    if (client.connect(clientId)) {
      Serial.println("[PHYSICAL] Successfully connected with MQTT");
      msg_Subscribe(); // topic subscription
    }
  }
}


void msg_Subscribe()
{
  client.subscribe(inTopic1);
  client.subscribe(inTopic2);
  client.subscribe(inTopic3);
}


void loop()
{
  char buf1[30];
  int LM35 = analogRead(A0);
  int temp = LM35 * 0.4808;
  //  Serial.println(temp);
  delay(100);
  String k = String(temp);
  k.toCharArray(buf1, 30);
  //if(temp>35)
  //{
  //  client.publish(outTopic, buf1);
  //}

  char buf2[30];
  int LDR = analogRead(A3);
  int light = LDR;
  //  Serial.println(light);
  delay(100);
  String l = String(light);
  l.toCharArray(buf2, 30);
  //  client.publish(outTopic2, buf2);

  client.loop();
  delay(2000);
}


void callback(char* topic, byte* payload, unsigned int length) {
  // copy the payload content into a char*
  char* json;
  json = (char*) malloc(length + 1);
  memcpy(json, payload, length);
  json[length] = '\0';

  // LED7
  if (String(payloadOn7) == String(json))
  {
    state = HIGH;
    digitalWrite(7, HIGH);
    Serial.println("LIGHTS ON 7");
//    Serial.println(payloadOn1);
//    Serial.println(json);
    client.publish(outTopic, "::::::Yes! LIGHT:::: 7");
  }

  if (String(payloadOff7) == String(json))
  {
    state = LOW;
    digitalWrite(7, LOW);
    Serial.println("LIGHTS OFF 7");
//    Serial.println(payloadOff1);
//    Serial.println(json);
    client.publish(outTopic, "::::Lights oFF ::::: 7");
  }


  // LED8
  if (String(payloadOn8) == String(json))
  {
    state = HIGH;
    digitalWrite(8, HIGH);
    Serial.println("LIGHTS ON 8");
//    Serial.println(payloadOn2);
//    Serial.println(json);
    client.publish(outTopic, "::::::Yes! turned on The LIGHT8::::");
  }

  if (String(payloadOff8) == String(json))
  {
    state = LOW;
    digitalWrite(8, LOW);
    Serial.println("LIGHTS OFF 8");
//    Serial.println(payloadOff2);
//    Serial.println(json);
    client.publish(outTopic, "::::Lights are turned off8:::::");
  }

  // LED9
  if (String(payloadOn9) == String(json))
  {
    state = HIGH;
    digitalWrite(9, HIGH);
    Serial.println("LIGHTS ON 9");
//    Serial.println(payloadOn3);
//    Serial.println(json);
    client.publish(outTopic, "::::::Yes! turned on The LIGHT9::::");
  }

  if (String(payloadOff9) == String(json))
  {
    state = LOW;
    digitalWrite(9, LOW);
    Serial.println("LIGHTS OFF 9");
//    Serial.println(payloadOff3);
//    Serial.println(json);
    client.publish(outTopic, "::::Lights are turned off9:::::");
  }
  free(json);
}

Comments

Popular posts from this blog

Hello Mr. Docker!!!! In which Container?

Business Strategy or just Mainframe Migration?