Hello all you Arduino geeks
I'm makeing little home automation project with Arduino, openHAB and mqtt broker.
First thing i wont to pull up is a light switch on a relay.
I have manage to set up openHAB and MQTT broker, now im stuck on the Arduino side.
I used PubSubClient library to comunicate Arduino with MQTT, and its working well (useing examples and looking it up in MQTT.fx)
Arduino is subscribeing and publishing in Topics via MQTT it reciving data from MQTT as well (i can see it in a saerial port window)
I have some code that i will post below, in that code i can triger a digitalWire via Serial.read and my problem is to triger it via incoming data from a subscribed topic.
I think that the problem is in assigning to a variable but i might be wrong.
If you see any sugestion i'll be pleased.
ps.
Sorry for my english.
/*
Basic MQTT example
This sketch demonstrates the basic capabilities of the library.
It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 11);
IPAddress server(192, 168, 1, 10);
void callback(char* topic, byte* payload, unsigned int length) {
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic","hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(5, OUTPUT);
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
}
void loop()
{
// Check that our USB cable interface is functioning
if (Serial.available() > 0) {
// Check for input and assign the input to a variable
char inputMessage = Serial.read();
// Actions depending on the message sent
switch (inputMessage) {
// If its a 'g', activate led 2
case '1':
digitalWrite(5, HIGH);
break;
default:
// Any other letter, reset all the lights
digitalWrite(5, LOW);
}
// Send a message back, saying the letter that was processed
Serial.println(inputMessage);
}
{
if (!client.connected())
{
reconnect();
}
}
client.loop();
}