No communication between openhab, mqtt, ubuntu, Arduino uno with Ethernet shield

Been playing for two weeks and still no success:

I am more of a networking guy , so when it comes to programming I struggle :smile:
Anyway, I have gone through numerous links, instructions and forum, but haven’t come across one simple example of turning relay on and off using Ubuntu server, openhab, mqtt, arduino uno with eithernet shield.

I am just trying to make this simple setup work before I go more in depth with sensor and other setups. My Ubuntu server is setup to run opehab and mosquitto . As far as I know both openhab and mosquitto is running fine cause I already tested with two terminal open.
When I open web browser and press relay on and off button. I can see the message on openhab window stating that “MQTTRelay received command ON” but nothing happens on relay.

I used ip scanner and found ip address of Ethernet shield. I can ping Ethernet shield just fine.
Can anyone please help me where I went wrong?
Thanks

Here is my Arduino and Ethernet shield setup:
<img src="//community-openhab-org.s3-eu-central-1.amazonaws.com/original/2X/a/a7f807910421ed6bf4c6f5fa0359bcfd93abf4ac.png" width=“466” height=“489”

Here is my demo.sitemap

sitemap demo label=“Demo House”
{
Frame {

	Group item=Outdoor icon="garden" 
}

	Frame label="MQTT Test"{

Switch item=MQTTRelay label=“Relay”
}

}

Here is my demo.items

Group All
Group gGF (All)
Group gFF (All)

Group Outdoor (All)

/* active groups */
Group:Switch:OR(ON, OFF) Lights “All Lights [(%d)]” (All)

/* Lights */
Switch Light_Outdoor_Garage “Garage” (Outdoor, Lights)
Switch Light_Outdoor_Terrace “Terrace” (Outdoor, Lights)
Switch Light_Outdoor_Frontdoor “Frontdoor” (Outdoor, Lights)
Switch MQTTRelay {mqtt=">[mqttbroker:openhab/garage/relay1:command:ON:GO]"}

mqtt config file is set to:
mqtt:mqttbroker.url=tcp://192.168.1.x:1883
mqtt:broker.clientId=openhab

Finally the code:

include spi.h
include ethernet.h
include pubsubclient.h
// Arduino to Relay ---------
// 8 > In1
// GND > GND
// 5v > VCC

define relay1 8

byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
// IP des MQTT Servers
byte server[] = { 192, 168, 1, 138 };
unsigned long time;
char message_buff[100];
EthernetClient ethClient;
PubSubClient mqttClient(server, 1883, callback, ethClient);

void setup() {
pinMode(relay1,OUTPUT);
Serial.begin(9600);
digitalWrite(relay1,HIGH);
}

void loop() {
if (! mqttClient.connected()) {
// Connect (or reconnect) to mqtt broker on the openhab server
mqttClient.connect(“arduinoClient”);
mqttClient.subscribe (“openhab/garage/relay1”);
}

mqttClient.loop();
}

void callback(char* topic, byte* payload, unsigned int length) {
// MQTT inbound Messaging
int i = 0;
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = ‘\0’;
String msgString = String(message_buff);
//Bounce relay
if ( msgString == “GO” ) {
digitalWrite(relay1,LOW);
delay(1000);
digitalWrite(relay1,HIGH);
}
}

The code looks OK to me, but perhaps you should try posting it over at the Arduino forum just to make sure.

On the OpenHAB side, try using a local MQTT client such as mosquitto_sub to connect to the broker and subscribe to your topic:

mosquitto_sub -h server -p 1883 -t openhab/garage/relay1 -v

When you send from OH now you should see a line with the topic name and the value printed in your terminal.

If this works try from a remote system, to make sure you can access the broker over the network.

HTH,

Thanks for the response robconnolly. So I should open two terminal window, and run the above command on one and see what i get on other terminal?

Do I have to add hostname for -h, and password for -p, and verbose for -v
or
just paste exact command that you have it ?

when i just copied and paste exact command you have it on my local server, i get “unable to connect (lookup error)” error.

So I tried opening two putty terminal and on first terminal i used following command:
mosquitto_sub -d -t openhab/garage/relay1

and on second terminal i used following command:
mosquitto_pub -d -t openhab/garage/relay1 -m “Hello from MQTT”

This work. I got “Hello” on first terminal.

Thanks again

Your arduino code needs a “Ethernet.begin(mac, ip);” in the setup method. I use MQTTLens app for google chrome to verify openHAB is publishing the correct value and to send messages manually to the arduinio. I have some example code on my github from MQTT nodes I have made. Hope this helps.

Thanks Scooter. I did actually tried MQTTLens on chrome, before you replied. That step worked just fine. I am sure there is something wrong on code part. Thanks for example code. I will go through it. hope I get it :smile:

You should be able just to copy my command and replace the word “server” with the hostname or IP address of your MQTT broker. From your arduino code I assume you don’t have any authentication set up on the broker.

In the example you tried you are just connecting to the broker on localhost, since this is what is assumed if no -h argument is supplied.

rob i tested locally and remotely and everything on MQTT seems fine, so I will need more work on my Arduino code. With Xmass on the way not sure I will be able to work on this, but I will update my progress after Xmass. Thanks for your help guys.

I was trying to put this project aside but i couldn’t stop myself from giving one last try. Finally I am getting somewhere. I was able to press toggle button on web and my relay did turn on and off. Opehhab terminal shows some error on every button press but I can always work on that. My goal was to get connected.
I decided to ignore my initial code and go with different code that uses "Ethernet.begin (mac,ip) like Scooter suggested.
Between Scooter example and following couple other instruction online I got it working.

Thanks again for the help and pointing me to the right direction guys

Glad you are making progress! What is the error message you get from OH?

If you need anymore help, feel free to message me anytime. I have some more complicated examples with two mqtt topics, one to send commands to the arduino device and another to send status updates back to openHAB. I use arduino w/ ethernet shields for all homemade devices and sensors.

1 Like

It was some java error like below, but i found out the problem. I had some extra line on my item and sitemaps file hence it wasn’t finding the right link.

2015-12-15 18:18:45.907 [WARN ] [.o.u.w.i.render.SwitchRenderer] - Cannot determine item type of ‘lamp1’
org.openhab.core.items.ItemNotFoundException: Item ‘lamp1’ could not be found in the item registry
at org.openhab.core.internal.items.ItemRegistryImpl.getItem(ItemRegistryImpl.java:80) ~[na:na]
at org.openhab.ui.internal.items.ItemUIRegistryImpl.getItem(ItemUIRegistryImpl.java:554) ~[na:na]
at org.openhab.ui.webapp.internal.render.SwitchRenderer.renderWidget(SwitchRenderer.java:57) ~[org.openhab.ui.webapp_1.7.1.jar:na]
at org.openhab.ui.webapp.internal.render.PageRenderer.renderWidget(PageRenderer.java:158) [org.openhab.ui.webapp_1.7.1.jar:na]
at org.openhab.ui.webapp.internal.render.PageRenderer.processChildren(PageRenderer.java:121) [org.openhab.ui.webapp_1.7.1.jar:na]
at org.openhab.ui.webapp.internal.render.PageRenderer.processChildren(PageRenderer.java:138) [org.openhab.ui.webapp_1.7.1.jar:na]
at org.openhab.ui.webapp.internal.render.PageRenderer.processPage(PageRenderer.java:86) [org.openhab.ui.webapp_1.7.1.jar:na]
at org.openhab.ui.webapp.internal.servlet.WebAppServlet.service(WebAppServlet.java:126) [org.openhab.ui.webapp_1.7.1.jar:na]
at org.eclipse.equinox.http.servlet.internal.ServletRegistration.service(ServletRegistration.java:61) [org.eclipse.equinox.http.servlet_1.1.300.v20120522-1841.jar:na]

thanks scooter, i would def hit you up when i move forward with more items. Really appreciate your offer.

Scott is there anyway i can message you? I need a little help on code.
Thanks

I m also facing the problem in getting the output. I m beginner and I have to on/off led my OPENHAB is on Ubuntu and Arduino OS on windows 10 so after completing all the steps, I am unable to get output on serial monitor for on/off led and on Ubuntu terminal for MQTT test, it is showing 1 0 1 0 when I m trying to on OT off led from my mobile, I don’t know what can be the problem with it, as I m new in this field so cannot identify the issue