I have an Ethernet connected Arduino with a Freetronics relay8 shield. Whenever the Arduino changes a relay status it sends the status of all relays to Openhab2 as separate MQTT messages.
8 relay status messages are sent to the MQTT broker (one message per relay), they are all sent with the same topic.
The details of the relay status is contained in the payload.
The messages are arriving at the broker OK.
The topic being sent is > myhome\mytopic\state:state
The payload is a string in the format
shield,relay,state
Where
Shield = A or B
Relay = 1 – 8
State = 0 or 1 (ON or OFF)
Example message when it arrives at the MQTT broker is
myhome\mytopic\state “A,5,1”
this can be described as “Shield=A, Relay=5, State=ON”
Question:
Where in Openhab do I receive and transform the payload “A,5,1” to toggle the required item switch state to ON or OFF?
I’m familiar with adding MQTT < (inbound) messages to switches for
“mosquitto:myhome\mytopic\state:ON:1” to items file.
I’m also familiar with transforming ON OFF messages from Arduino using a transform mapping file
mosquitto:office/switch1/state:state:MAP(onoff.map)
The thing I’m trying to learn is how to handle a payload that is a string “A,5,1” to drive an item switch status.
Maybe this is too hard and I should take the easy way out, and I should be editing the Arduino sketch to send a more concise topic. For example: mosquitto:myhome\mytopic\ _shield\ _relay\state:state:MAP(onoff.map)
this would look like:
mosquitto:myhome\mytopic\ _A\ _5\state:state:MAP(onoff.map)
and the payload would be 1 or 0.
I would like to learn the method to handle string data though to drive events.
Thanks for the replies. i will implement individual topics in the Arduino.
I’m running out of memory (84% used) on the Arduino (Freetronics EtherTen / Uno equivalant) . I’ll see if I can squeeze another function into the sketch to create the MQTT topic dynamically.
I leveraged a mega for one of my projects just to get a little more memory space. They don’t cost that much more (if at all… about $11 on ebay or amazon) but are a little bigger if size is an issue.
I use arduino’s with ethernet shields and etherten all over my house. I created a sketch that can control the ardunio Pins by sending a simple MQTT command. The following switch item should turn pin 5 of the arduino on and off. In your item file you can copy the switch example below and just change the number 5(all of them) to whatever pin you what to use. You have to change the IP/MAC addresses as needed. I have all my arduino code in my github below if you want to look at some other examples. If you need any help please let me know. One tip on memory usage, if you you use “string” anywhere in your sketch they takes lots of memory. Remove the strings and use char arrays, it will save space.
Switch Light_PIN5 "Turn On Pin 5 Relay" {mqtt=">[server:arduino/lightControl:command:ON:5_1],>[server:arduino/lightControl:command:OFF:5_0],<[server:arduino/lightControl/5:state:default]", autoupdate="false" }
Use a String Item to receive all the messages and use a Rule to parse the messages and postUpdate to the appropriate Switch Items.
Have all of your Switch Items subscribe to the same topic. Use the REGEX matching part of the MQTT config string on each Item to sort the messages so each Switch only receives the messages for the relay it represents. In the Transform section, write a JS transform to extract the 1/0 and return “ON” or “OFF” as appropriate.
Hi I’m new here and I’m reading treat the with interest
i’got the same problem,
when I turn on or off the light with button in Arduino the state won’t change in OH
I’ve tried almost (I think) everything)
give it’s own topic, commad, state:state, state: map on off…
this is what I got now
code arduino
Having read every single word on the Getting Started guide, as a complete newbie, there are so many gaps in those instructions, it’s like Swiss cheese.
On the plus side, once you bash your head against the wall continuously for 7 days non-stop, trying to figure out the mental model of MQTT, Linux, OpenHab - it’s a piece of cake!
Hey @45Matches I’m the author of this guide and am a bit surprised you didn’t like it. The guide should include all details and links you should work through to get from null to a working connection and a good understanding of MQTT. The links are the important part here of course. Did you follow them?
@ThomDietrich
Hi Thom, This observation was not meant as a critcism. While almost all of the necessary detail is there, what’s lacking in large amounts is simple, complete examples which give these details context for newbies like myself. As a general observation, this feedback applies almost all OpenHab documentation.
Having tried to get a couple of other systems up and running over the past week or two, I have stuck with OpenHab because it was the easiest and quickest to set up, but it’s not perfect, and just because it’s the easiest, doesn’t make it easy.
I’m greatful for all the help I’ve received, although you could look at the active community in two ways - isn’t it great experts and beginner can come together with their passion for OH - or it could be an indicator of areas of improvement of examples that complement the documentation.
In short, the instructions wiki MQTT Binding (v1.11) did not become the main reference for my ongoing implementation of MQQT and OpenHab due lack of clear examples.
What I have been doing is documenting my steps, and will happily share that once I feel it’s repeatable. It will cover off install Openhabian and Mosquitto, making a Backup, connecting an Arduino over WiFi and controlling an LED via mqqt.