Been playing for two weeks and still no success:
I am more of a networking guy , so when it comes to programming I struggle
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);
}
}