[SOLVED] Arduino >> openhab. howto handle received payload string to toggle switch state

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.

I’m no expert but I think you need to parse the string in a rule (or script, map etc), use json or separate topics.

Perhaps you should look into JSON if you are looking for a challenge? That seems closest to what you are trying to do.

The easy way and the way I would probably choose is to create the individual topics…

I would do the same.

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" }

There are two approaches I can think of.

  1. Use a String Item to receive all the messages and use a Rule to parse the messages and postUpdate to the appropriate Switch Items.

  2. 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.

That is actually the approach I would take.

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

// licht eiland
if (digitalRead(button2) == HIGH) {
if (state3 == 0) {
state3 = 1;
client.publish(“Lights”,“kkn3_on”);
client.publish(“Lights/kkn3”,“on”);
}
else{
state3 = 0;
client.publish(“Lights”,“kkn3_off”);
client.publish(“Lights/kkn3”,“off”);
}
}

code OH items files for that switch
Switch Light_GF_Keuken_Eiland “Eiland” (GF_Keuken, Lights)
{mqtt=">[broker:Lights:command:on:kkn3_on],

[broker:Lights:command:off:kkn3_off],
<[broker:Lights/kkn3:state:state:default]",autoupdate = “false”}

I’ve got so far it only turn the switch off and not on and lightbulb stays off even when I switch it on in OH

the MQTT broker received al the messages

anyone any suggestions

Regards Steven

Try using all caps for the “ON” and “OFF” messages you publish to OH from the Arduino.

It is always helpful to look at the logs. I suspect you would be seeing parsing errors in the logs on every message.

Of course, this assumes that OH is working with MQTT. Do you see a “Connected to broker “mosquitto”” or the like in the openhab.log?

Finally SOLVED
thanks for the tips
state update on app en button and no pressing 2 times

yet another sleepless night spared

here is the code for those who would like it
arduino

callback
if (content == “kkn3_on”) {relay3Value = HIGH; state3 = 1;}

if (content == “kkn3_off”) {relay3Value = LOW;state3 = 0;}

void loop
if (digitalRead(button2) == HIGH) {
if (state3 == 0) {
state3 = 1;
client.publish(“Lights”,“kkn3_on”);
client.publish(“Lights/kkn3”,“ON”);
}
else{
state3 = 0;
client.publish(“Lights”,“kkn3_off”);
client.publish(“Lights/kkn3”,“OFF”);
}
}

OH items

Switch Light_GF_Keuken_Eiland “Eiland” (GF_Keuken, Lights)
{mqtt="<[broker:Lights/kkn3:state:default],

[broker:Lights:command:on:kkn3_on],
[broker:Lights:command:off:kkn3_off]"}

In your example .items file, there is a broker named server (server:arduino/lightControl:command:OFF:5_0)

Did you name your broker server, in mosquitto mqqt.conf for example?
server.url = tcp://localhost:1883

According to the openhab MQTT bindings docs,

<broker> is an alias name for the MQTT broker.

I’m tring to understand how to refer to the broker in OpenHabian that is installed with mosquitto and mosquitto bindings.

That is correct.

More help with MQTT can be found at MQTT Binding (v1.11) Getting Started 101

2 Likes

Thanks Rich.

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! :cold_sweat:

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.