wireless Arduino integration example - using MQTT

1,465 views
Skip to first unread message

eric

unread,
Jun 10, 2014, 10:58:54 PM6/10/14
to ope...@googlegroups.com
I made an example setup for integrating wireless Arduino nodes with OpenHAB using the MQTT binding.  Right now, all I have is an Arduino sitting in the garage sending data regarding the garage door state (opened/closed), and temperature/humidity, and displaying on OpenHAB.  But it's pretty easy to modify the Arduino sketch make this work for whatever sensor you want to add to Arduino.

http://electronichamsters.wordpress.com/2014/06/09/home-automation-with-arduino-and-openhab/

Ben Jones

unread,
Jun 11, 2014, 2:23:26 AM6/11/14
to ope...@googlegroups.com
Great stuff Eric - I have just ordered some RF modules so I can start playing with your examples.

Thorsten Lanfer

unread,
Jun 12, 2014, 4:59:19 PM6/12/14
to ope...@googlegroups.com
Currently im using some cheap 433MHz devices and a couple of arduinos to do something similar. I built a little library using virtualwire to have a directional mesh library on top of the undirectional, point-to-multipoint network. I called it VirtualNetwork and published a very early version on github https://github.com/tlanfer/VirtualNetwork

Now, after reading your article, i immediately ordered some of those devices an plan on changing my code to work with those. They seem to offer far superior performance and i am very eager to get my hands on them.

eric

unread,
Jun 12, 2014, 9:32:43 PM6/12/14
to ope...@googlegroups.com
Hi Thorsten,
How was the range on the 433MHz modules?  I spent some time comparing the 2.4GHz nRF24L01 to the 915MHz RFM69HW, but HopeRF also makes the RFM69 in a 433MHz model, with the same sensitivity and power.  I didn't want to spend the money to buy modules just for testing, but I'm very curious if the effective range of the 433Mhz modules would be farther than the 915MHz modules, when comparing apples to apples.

As for mesh network options, for those who are going down the nRF24L01 route, there is a project call "mysensors" that publishes an Arduino sketch that implements a mesh network.  Although their gateway is something they wrote, it seems very possible to use their mesh network sketches with your own Arduino gateway to bind to openHAB.

Eric

eric

unread,
Jun 18, 2014, 9:15:03 PM6/18/14
to ope...@googlegroups.com
After more extensive testing, I found a problem with Arduino communicating with Mosquitto via MQTT.  I'm not sure if this is specific to my particular hardware, or if other people would run into this too.

About 20% of the time, although I'm really not sure how often, when Arduino tries to publish topic&msg via the MQTT library, it "hangs" on <client.connect>.  That is, in the Arduino code, client.connect returns false for some reason I haven't been able to figure out.  And it KEEPS returning false.

I might have to go to using serial communication between Arduino and OpenHAB.  I have no example to follow, so I'm kind of disappointed my implementation of the Arduino/MQTT communications isn't very robust.

Ben Jones

unread,
Jun 18, 2014, 9:17:07 PM6/18/14
to ope...@googlegroups.com
Have you had a look in the Mosquitto logs to see if there are any clues in there?

eric

unread,
Jun 18, 2014, 9:36:33 PM6/18/14
to ope...@googlegroups.com
It's definitely not a problem with mosquitto.  It's Arduino not being able to connect to ethernet.  The sketch checks to see if "client.connect("arduinoClient")" returns true before posting MQTT data, and this is returning false.  I'm about to do a writeup in my blog to show where I think the problem is.  I'm hoping to see if the more hardcore Arduino users can help me.

Bummer!  So close.  A couple of people posted that they're working on a serial solution (Arduino to RPi via serial binding), which might be what I'll have to do if I can't resolve this.

What I have works for temperature sensors and the like, as long as you don't care if you miss an update here and there.  The strange thing is every time I've open/closed my garage and purposely gotten out my smart phone to check if OpenHAB saw it, it's worked, I've never had a missed transmission.  So I've done that over two dozen times at least.  Last weekend I built a prototype of a postal mailbox Arduino that'll notify me if the mailbox has been opened.  It's during this testing that I notice intermittent issues with Arduino's MQTT pub.

The postal mailbox Arduino works pretty well though, so I'm excited about that.

Ben Jones

unread,
Jun 18, 2014, 10:21:30 PM6/18/14
to ope...@googlegroups.com
I look forward to reading your blog about all this. The mailbox sounds cool!

eric

unread,
Jun 18, 2014, 10:49:00 PM6/18/14
to ope...@googlegroups.com
AOL, when it was cool, used to have this "You've Got Mail" sound when you get email.  I'm going to play that when the mailbox opens up.  Looking at the current reading I get almost two years of run time on four alkaline AA batteries.  Just need to attach it in a inconspicuous way.

My troubleshooting post here:
http://electronichamsters.wordpress.com/2014/06/19/arduino-mqtt-woes/

Peter Hardy

unread,
Jun 19, 2014, 2:48:51 AM6/19/14
to ope...@googlegroups.com

The biggest problem I see there is that you're attempting to connect to MQTT every time you send a message, but never disconnect. You can issue a client.disconnect() call at the end of your message processing block.

Or you can remain permanently connected - move your connect call to the setup() function. My client using the same library that does this is at https://bitbucket.org/pjhardy/rf24sensornet/src/cfba9e81d8327dba161c3fa2040ab22a0eb7ee83/examples/MqttRelay/MqttRelay.ino?at=master . I could use some logic to check with client.connected() and reconnect if necessary, but it's been very stable.

If all that fails, I'd then look in to the ethernet cable I was using. Try replacing it and seeing if you get better results.

--
You received this message because you are subscribed to the Google Groups "openhab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
For more options, visit https://groups.google.com/d/optout.

 

 

Jan-Piet Mens

unread,
Jun 19, 2014, 2:20:12 PM6/19/14
to ope...@googlegroups.com
> My troubleshooting post here:

Only glanced at your code, but maybe you're being bitten by the 'DHCP
lease gone away' thing. See if you can add

Ethernet.maintain();

to your Arduino loop(); that will attempt renew the lease.

As an aside: you're connecting to the MQTT broker in your loop(), but
you're not disconnecting; that should not be required: a connect in
setup() should be more than sufficient.

-JP

Ben Jones

unread,
Jun 19, 2014, 4:30:17 PM6/19/14
to ope...@googlegroups.com
I am definitely a noob when it comes to Arduino sketches but this is how I handle MQTT broker connections (see below). This ensures that if the broker is restarted or the connection is dropped for any reason, the Arduino will attempt to reconnect.

If you only attempt the connect in setup() it would never be able to reconnect unless I am mistaken? 


void setup()
{
  while (Ethernet.begin(mac) != 1) {
    delay(30000);
  }
}

void loop()
{
  // make sure we are connected to our broker
  if (!mqttconnect()) {
    delay(5000);
    return;
  }

  // do work
}

boolean mqttconnect() {
  if (!mqtt.connected()) {
    if (mqtt.connect(MQTT_CLIENTID, MQTT_USER, MQTT_PASS, MQTT_LWT_TOPIC, MQTT_LWT_QOS, MQTT_LWT_RETAIN, "0")) {

      // subscribe to any topics
    
      // publish retained LWT so anything listening knows we are alive
      byte data[] = { "1" };
      mqtt.publish(MQTT_LWT_TOPIC, data, 1, MQTT_LWT_RETAIN);
    }
  }
  return mqtt.connected();
}

eric

unread,
Jun 19, 2014, 7:47:35 PM6/19/14
to ope...@googlegroups.com
You guys are great!  Peter, JP - putting the client.connect() in setup instead of connecting EVERY time fixed the problem.  Thanks!

Out of the over 50 test transmissions I just did, they all made it to MQTT.  I'll do more testing later.

Thanks again!!!

Eric

pbr...@identiv.com

unread,
Aug 27, 2014, 11:32:56 AM8/27/14
to ope...@googlegroups.com
Hello Everyone,

I am working on getting my Intel Galileo working with MQTT to openhab.  The work you have posted here is allowing me to get started with Arduino and move up.  Thanks very much.  Will keep you all posted on how it goes.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages