OpenHAB, MQTT, Arduino and ESP8266: Part 5: Hardware – Sensors

The following series of posts will document my journey to full Home Automation using the MQTT protocol, all custom hardware (Arduino and ESP8266 based), and all tied together using OpenHAB

  • Part 1:  Setting up the server / environment
  • Part 2:  Publish, Subscribe, Command, State, and WTFs
  • Part 3:  Hardware:  Arduino with Ethernet Shield
  • Part 4:  Hardware:  ESP8266 with NodeMCU firmware
  • Part 5:  Hardware:  Sensors
    • Part 5.1:  Graphing Sensor Data
  • Part 6:  OpenHAB Automation Rules

This one, is quite easy – yet had one twist that had me pulling my hair out.

Lets dive right in again with a Arduino based example:  A simple MQTT Thermometer

Have a look at http://www.instructables.com/id/ARDUINO-TEMPERATURE-SENSOR-LM35/ for how to wire a LM35 temperature sensor to an Arduino

The following is a mashup between our previous sketch (MQTT Switches), and the LM35 Serial sketch linked to above:

float temp;
int tempPin = 0;

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>


// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 0, 100 };
byte ip[]     = { 192, 168, 0, 120 };


EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// callback function not used really since we are not subscribing to anything in this example
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Callback");
Serial.print("Topic:");
Serial.println(topic);
Serial.print("Length:");
Serial.println(length);
Serial.print("Payload:");
Serial.write(payload,length);
Serial.println();
}

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
if (client.connect("arduinoClient")) {
    Serial.print("MTQQ Connected");
    // Send initial state
    client.publish("/home/1/ard2/s1/state","0.00");
    }
    
    
}

void loop()
{
  client.loop();
  temp = analogRead(tempPin);
  temp = temp * 0.48828125;
  Serial.print("TEMPRATURE = ");
  Serial.print(temp);
  Serial.print("*C");
  Serial.println();

  char temperature[10];
  dtostrf(temp,4,3,temperature);

  client.publish("/home/1/ard2/s1/state",temperature);
  delay(5000);
}

Easy enough, does a Publish to MQTT:/home/1/ard2/s1/state containing the current temperature.

Configuring this into OpenHab is quite easy too… except…

If you look at the examples for adding Switches to OpenHab – you have a Switch item in the Sitemap, and a Switch item in the Items file… easy enough?

This one had me scratching my head though.  I knew that I had to create a Number item in OpenHAB’s Item file, and this was easy enough and in no time at all I was seeing my published temperatures updating the item every 5 seconds  (you can tail -f /var/log/openhab/events.log)

Extract of the log:

2015-03-30 10:10:07 - Office_temp state updated to 30.762
2015-03-30 10:10:12 - Office_temp state updated to 30.762
2015-03-30 10:10:17 - Office_temp state updated to 30.762
2015-03-30 10:10:22 - Office_temp state updated to 30.762
2015-03-30 10:10:27 - Office_temp state updated to 30.762
2015-03-30 10:10:32 - Office_temp state updated to 30.762
2015-03-30 10:10:37 - Office_temp state updated to 30.762

But! I also added a Number item to the Sitemap – but NOTHING was displayed when I viewed the sitemap/UI

Turns out (and I couldn’t find any documentation stating that, just a noticeable lack of how to setup a Number object in the sitemap and examples) but that you actually create a Text Item in the sitemap, pointed at the name of the Number object in your Items configuration!   Phew…

Here’s the configs to add to get your Arduino+Ethernet Shield+LM35 with the sketch above, into OpenHAB

sudo vi /etc/openhab/configurations/sitemaps/dolphin.sitemap

And add the Text line

sitemap demo label="Main Menu"
{
 Frame label="MQTT" {
 Switch item=mqttsw1 label="MQTT Switch 1"
 Switch item=mqttsw2 label="MQTT Switch 2"
 Switch item=lamp1 label="Office Lamp"
 Text item=Office_temp
 }
}

Next

sudo vi /etc/openhab/configurations/items/dolphin.items

Add the Numbers item:

Group All

Switch mqttsw1 "Switch 1" (all) {mqtt=">[broker:/testsw/1:command:on:1],>[broker:/testsw/1:command:off:0]"}

Switch mqttsw2 "Switch 2" (all) {mqtt=">[broker:/testsw/2:command:off:0],>[broker:/testsw/2:command:on:1]"}

Switch lamp1 "Office Lamp" (all) {mqtt=">[broker:home/openHAB/out/Light_GF_Office_Lamp/command:command:on:ON],>[broker:home/openHAB/out/Light_GF_Office_Lamp/command:command:off:OFF],<[broker:/openHAB/in/Light_GF_Office_Lamp/state:state:default"}

Number Office_temp "Temperature [%.1f °C]" <temperature> {mqtt="<[broker:/home/1/ard2/s1/state:state:default]"}

Save, exit and restart OpenHab

sudo /etc/init.d/openhab restart

You should see the Temperature item in the UI, and a few seconds later (or as soon as the Arduino sends a MQTT message containing a temperature) it should display the temperature too!

Screenshot from 2015-03-30 10:19:15

Advertisements