Light Switch Controller ~MQTT

Hi everyone,

i have myself a light switch controller, the same one that john built in episode #25, ive spent the past 2 day trying to get it to work, but i just cant seam to wrap my head around how to get it to work with MQTT. from my understanding (correct me if im wrong.

Blockquote
String messageString = String(panelId) + “-” + String(buttonArray[buttonId]);
messageString.toCharArray(messageBuffer, messageString.length()+1);
//String topicString = “device/” + String(panelId) + “/button”;
String topicString = “buttons”;
topicString.toCharArray(topicBuffer, topicString.length()+1);
//client.publish(topicBuffer, messageBuffer);
client.publish(“buttons”, messageBuffer);

This part that publishes to MQTT would publish to buttons/20-41, if the panel id was 20 and button 41? or have i just completely confused myself in this

i think im battling a few issues here, the other being the actual connection to Mosquitto from openhab.

any help is much appreciated

EDIT, i have got openHab and the light-switch controller (ethermega) now talking to mosquitto, just not in the same topic by the looks of things

Edit 2, @jon Might actually have the most help here, im sure im sitting right on the answer, more then likely ive just set up my broker incorrectly. or rules

Cheers

Hi @Bedrock.
The double forward slashes turn that line of code into comments, which is ignored by the compiler/computer. Most likely used at some stage by @jon but no longer needed.

The only real line of code is the following:

client.publish(“buttons”, messageBuffer);

So the topic being published to is buttons.

The message (or payload) set earlier is:

String messageString = String(panelId) + "-" + String(buttonArray[buttonId]);
messageString.toCharArray(messageBuffer, messageString.length()+1);

Which as you’ve said is 20-41

Is openhab looking at a topic called buttons?

Hope this helps :slightly_smiling_face:

Hi @chris

Cool that makes sense,

i think ive gotten my head around this now. to clarify
ive set my panel id by

int panelId = 20;

my buttonid is 41

and then set the payload by

String messageString = String(panelId) + “-” + String(buttonArray[buttonId]);
messageString.toCharArray(messageBuffer, messageString.length()+1);

this creates the message 20-41

then

client.publish(“buttons”, messageBuffer);

publishes to MQTT in the topic of buttons with the string of 20-41?

hoping ive gotten my head around it now

Just an Update, i have Open hab publishing to MQTT on the buttons topic

just struggling to get it to publish the correct string

Adding to this, i have my EtherMega publishing to MQTT in the topic of buttons. the String for this is panelID-buttonID so for example 20-41

what im now stuck on is getting openHab to read this and act according. would this be done via a rule?

something along the lines of

if String =“20-41” and light is “OFF”
then turn on Light41

else turn Light41 OFF

hopefull that makes sense

Hey @Bedrock.
Yep, you’re correct! Topic of buttons with string of 20-41.

You need openHAB to subscribe, not publish to buttons, in order to read in what your Arduino has published.

I was never successful in using openHAB’s rules engine. The lack of complete working GUI and the inability to modify it’s console database directly, I moved from openHAB to NodeRed. So I don’t think I can help you much further than the Arduino side of things.

If I used node red how would I do this?

Use a subscribe for the topic of buttons and then a statement that listens for a set string e.g. 20-41 then turns light on?

How would I get open hab to update the status of the light?

I can’t help you with any openHAB questions. I’ve forgotten how any of it works. I’ve had much better success with NodeRed for the last few years.

NodeRed has native support for MQTT, once configured/connected as a client.
All of my devices either subscribe or publish to a MQTT channel.

Example - Sonoff with touch button to control lamp:

-Sonoff subscribes to “device/lamps/1”
-NodeRed subscribes to “nodered/lamps/1”

NodeRed stores state of lamp. Either on or off.

  1. Button is pressed on sonoff, it publishes to “nodered/lamps/1”
  2. NodeRed sees that message.
  3. NodeRed checks previous state of lamp, eg: off. Toggles the state to on.
  4. NodeRed publishes to “device/lamps/1” with a message of ON.
  5. Sonoff sees that message of ON, turns lamp on.

sonoff%20lamp

Function “Lamp 1” code:

// Determine previous state, or if never set: set to false
var state = (flow.get('lamp1_state') == true);

// Toggle state
if (state)
{
    // Store state for next time
    flow.set('lamp1_state', false);
    
    // Set our OFF message
    msg.payload = "0";
}
else
{
    // Store state for next time
    flow.set('lamp1_state', true);
    
    // Set our ON message
    msg.payload = "1";
}

// Send message
return msg;

And here’s my code for the sonoff: http://auto.caitken.com/posts/2018/09/01/wifi-enable-lamp

Hey @chris, thanks for your help, finally got my lights switches working, well the control side of that at least. im now stuck on the arduino side for toggling the relays. have you played around with Relay8’s i just cant seam to get mine to work.

I can get 2 working if i use the example code @jon has on the Superhouse Github, but i cant get the second 2 working. any ideas?