Monday 28 July 2014

Connecting Arduino with DIoTY's Cloud MQTT Broker

In my previous post I showed how to get started with DIoTY's Cloud MQTT Broker.  Now that you are all set up, proved to yourself all is working, it's time to do something a little more interesting.  So today we will use an Arduino Uno and a temperature sensor to publish the current temperature to the MQTT Broker.

We use following hardware for our example:
  • Arduino Uno with ethernet shield
  • One-wire temperature sensor (DS18B20)
  • 4.7k resistor
  • breadboard and some wires
Make sure you verify the the pin layout before wiring as incorrect wiring can damage the sensor and the arduino + ethernet shield (and we accept no liability at all).

Wiring is done as in the picture below:


Next we need to load the software to the Arduino.  For this, first import the following two libraries into your Arduino Development Environment: OneWire and PubSubClient.

The latest OneWire library can be downloaded from Paul Stoffregen's site.
The latest PubSubClient library can be downloaded from GitHub.  Full documentation is found on Nick O'Leary's site.

If you haven't imported libraries before take a look at following guide.

Once the libraries are in place it's just a matter of copying and pasting the following code into the sketch, replacing the bits in red with values suitable for yourself.  Note that yourUserID is the email address with which you registered at DIoTY.  You need to use that same full email address at the start of your topic as you will only have authorisation under that subtree.



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

#define DEBUG
#ifdef DEBUG
  #define DEBUG_PRINT(x)  Serial.println (x)
#else
  #define DEBUG_PRINT(x)
#endif

byte mac[]= {0x90, 0xA2, 0xDA, 0x0D, 0x88, 0x5E} ; // change by your arduino mac address
char userId[] = "yourUserID"; // use your DIoTY user id (=email address)
char passwd[] = "yourPassword";  // use your DIoTY password
char server[] = "mqtt.dioty.co";
unsigned int port = 1883;
char topic[] = "/yourUserID/temp"; // topic where to publish; must be under your root topic
EthernetClient client; 
PubSubClient arduinoClient(server, port, 0, client) ; //no callback function is specified as we only publish


void setup() {                
  Serial.begin(9600); 
  DEBUG_PRINT(F("Initialisation"));
  beginConnection();
}

void beginConnection(){
  DEBUG_PRINT(F("Entering beginConnection"));
  if (Ethernet.begin(mac) == 0) {
    Serial.println(F("Failed to configure Ethernet using DHCP"));
    exit(-1);
  };
  DEBUG_PRINT(F("Obtained IP Address:"));
  DEBUG_PRINT(Ethernet.localIP());
  if (arduinoClient.connect(NULL,userId,passwd)) {
    DEBUG_PRINT(F("Connected to MQTT Server..."));
  } else {
    Serial.println(F("Failed to connect to the MQTT Server"));
    exit(-1);
  }
}

// reconnect after network hiccup      
void reConnect(){
  DEBUG_PRINT(F("Entering reConnect"));
  
  if (arduinoClient.connected()){
    DEBUG_PRINT(F("arduinoClient is connected"));
  } else {
    DEBUG_PRINT(F("arduinoClient is not connected"));
      if (Ethernet.begin(mac) == 0) {
        Serial.println(F("Failed to configure Ethernet using DHCP"));
      };
      DEBUG_PRINT(Ethernet.localIP());
      if (arduinoClient.connect(NULL,userId,passwd)) {
        DEBUG_PRINT(F("Reconnected to MQTT Server..."));
      } else {
        Serial.println(F("Failed to connect to the MQTT Server"));
      }
  };
}
  

// DS18S20 Temperature chip i/o
OneWire ds(2);  // on pin 10
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int HighByte, LowByte, SignBit, Whole, Fract, TReading, Tc_100;
char buf[10];
char lastPublished[10];

void loop(void) {

  if (arduinoClient.loop()){
    DEBUG_PRINT(F("Arduino Client loop ok"));
    
    if ( !ds.search(addr)) {
        DEBUG_PRINT(F("No more addresses."));
        ds.reset_search();
        return;
    };
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        DEBUG_PRINT(F("CRC is not valid!"));
        return;
    };

    getTemp();
    dtostrf(((float)(Whole*100+Fract)/100),1,2, buf);
    
    if (strcmp (buf,lastPublished) == 0) {
      DEBUG_PRINT(F("temp not changed"));
      DEBUG_PRINT(buf);
    } else {
      arduinoClient.publish(topic, (uint8_t*) buf, strlen(buf), 1); 
      memcpy(lastPublished, buf, 10);
      DEBUG_PRINT(buf);
    };
    
  } else {
    DEBUG_PRINT(F("Arduino Client loop nok"));
    reConnect();
  }
  delay(5000);     
}

void getTemp() {
  int foo, bar;
  
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);

  for ( i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
  
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  
  if (SignBit) {
    TReading = -TReading;
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
  Whole = Tc_100 / 100;          // separate off the whole and fractional portions
  Fract = Tc_100 % 100;
  if (Fract > 49) {
    if (SignBit) {
      --Whole;
    } else {
      ++Whole;
    }
  }

  if (SignBit) {
    bar = -1;
  } else {
    bar = 1;
  }
}

After uploading the sketch to your Arduino, it will measure the temperature every 5 seconds.  When the measured temperature is different to the previous measuring, the new temperature will be published to the topic /temp topic underneath your root topic (/<yourUserID>/temp).  As the publication is retained, all clients subscribing to the topic will immediately receive the latest temperature measured and receive any changes for as long as they stay subscribed...

Tuesday 22 July 2014

Getting started with DIoTY's Cloud MQTT Broker...

In my first post I briefly explained the possibilities provided by DIoTY.co.  In this post, I'll help you getting started with the basics.  I will show you how to use the MQTT cloud broker with existing software you can freely download.  It's a quick and easy way to check you're correctly set up and to understand how MQTT works.

If you haven't done so yet, first go to DIoTY.co and sign in using a google email address.  You will receive your password to connect to the MQTT broker with your welcome email.  Note that this will take some time so do this before continuing reading this post...

When working with MQTT you will always have a client publishing messages to a topic and one or more clients receiving those messages by subscribing to that topic.

A sample publish and subscribe program comes with the free Mosquitto broker which you can download here.  It is available for most operating systems including Windows, Linux and Mac OS X and comes even with the full source if you which to compile it yourself.  I suggest you follow the installation instructions on the download page.  As you will connect to DIoTY's MQTT broker there is no need to start the mosquitto broker locally and all you are really interested in are the two programs mosquitto_pub and mosquitto_sub.

Assuming you have received your welcome email from DIoTY.co and you managed to download and install Mosquitto you're now ready to test.  For this open a couple of command line windows.  After installation, the mosquitto_pub and mosquitto_sub programs should be in your path but if not, it's probably a good idea to add them.

Publishing your first message:

You can now publish a message with the following command (just replace <your email address>, twice, with the email address you used to sign up and <your password> with the password you receive in your welcome email):

mosquitto_pub -h mqtt.dioty.co -p 1883 -u <your email address> -P <your password> -t /<your email address>/hello -m "Hello DIoTY"

DIoTY.co allows you only to publish in your own topic tree.  This means that the topic where you publish to must start with /<your email address>/.  Messages published outside of your topic tree will just be ignored.

Of course, publishing a message only makes sense when there is also at least one subscriber.

Subscribing to your topic tree:

You can subscribe to all topics in your topic tree with the following command (just replace <your email address> and <your password> like you did when publishing):

mosquitto_sub -h mqtt.dioty.co -p 1883 -u <your email address> -P <your password> -t /<your email address/#

     # is a wildcard and means that /any/subtree/will/match...
     mosquitto_sub keeps listening to the topics you subscribe to until you brake (ctr-c ; cmd-c; kill).

When you have followed this blog, the first thing you learn from this example is that messages published before the subscriber has been started will just be lost.  So whilst your subscriber is still subscribed, open another window and run again the mosquitto_pub command.  Now you will see "Hello DIoTY" appearing in your subscriber window.  Yes, it does work after all ;-)

I can hear people thinking already, when I connect with my mobile device to see the temperature at home, I don't want to wait till the temperature changes and the sensor publishes the new value.  I want to see immediately the last know temperature.  To obtain this, use so called retained publications.  Publish again the same messages as before, but this time add a "-r" to the mosquitto_pub command.  When you afterwards start a new subscriber client (either in a new window or by stopping and starting the one you have running already) you will see the retained message immediately.

Have a look at the documentation mosquitto_pub and mosquitto_sub for other options available.

Since our MQTT broker is in the cloud, you can also download a mobile application like mymqtt on your android device.  Just connect to mqtt.dioty.co, add your username, password and subscription topic and you not only see the published messages on your computer but also on your phone or tablet.

Monday 21 July 2014

The beginning...

I'm writing this blog as founder of DIoTY.co, a cloud based platform to help you experiment faster with your IoT projects.  
DIoTY currently provides you with two services, an MQTT broker and a Node-RED development tool.  I'll start with explaining what they are and what you can do with them.


MQ Telemetry Transport (MQTT) is a lightweight broker-based publish/subscribe messaging protocol.  MQTT was originally developed by IBM but is currently standardised by OASIS and is both free and royalty free.  It is rapidly becoming one of the standards for IoT/M2M.  

MQTT is a lightweight broker-based publish/subscribe messaging protocol:

  • Clients (sensors, mobile apps,...) connect to a broker.
  • Clients communicate by sending and receiving messages to/from the broker.  For the broker, a messages is just a chuck of data.
  • A client publishes a message to a topic (eg: /home/livingroom/temperature).
  • A client can subscribe to many topics.  It will then start receiving all messages send to those topic(s).

As you can easily see, an MQTT broker as provided by DIoTY can remove the need to run your own web server at home.  Your sensors publish to the MQTT broker, your mobile app subscribes to the topics of interest and you're done...

Node-RED is a visual tool for wiring the internet of things.  Node-RED is a creation of IBM emerging technologies, open source licensed under Apache 2.0.

With the Node-RED tool provided by DIoTY you can subscribe and publish to the MQTT broker.  You can alter the messages by applying functions to it (eg: subscribe to /home/livingroom/temperature/c ; convert the temperature from Celsius to Fahrenheit and then publish again to the topic /home/livingroom/temperature/f ).

You can also interact over other protocols like http, websockets,... to retreive for example weather information from the bbc website and push it to your mobile application.

Finally, with the twitter node, building your own twittering house becomes as easy as pie.