Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dotandpixel/0cd78369f50b669844a3b76df689d59a to your computer and use it in GitHub Desktop.
Save dotandpixel/0cd78369f50b669844a3b76df689d59a to your computer and use it in GitHub Desktop.
/*
Basic MQTT example
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Built-in LED on your Arduino is pin 13, or LED_BUILTIN (which also returns 13)
const int ledPin = 13;
unsigned long readTime;
// Update these with values suitable for your network.
byte mac[] = { 0x5C, 0xCF, 0x7F, 0x33, 0x7C, 0x7A }; // Arduino MAC address
IPAddress ip(10, 0, 0, 64); // Arduino IP address
IPAddress server(10, 0, 0, 101); // OpenHab Raspberry Pi IP address
char message_buff[100]; // this buffers our incoming messages so we can do something on certain commands
// Connect the PubSub client to the network
EthernetClient ethClient;
PubSubClient client(ethClient);
// When connected to OpenHab Pi MQTT broker, do this with the message:
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
int i=0;
for (i=0;i<length;i++) {
Serial.print((char)payload[i]);
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
if (msgString.equals("OFF")) {
client.publish("openhab/arduino/command","acknowedging OFF");
sensorWrite(msgString);
}
else if(msgString.equals("ON")){
client.publish("openhab/arduino/command","acknowedging ON");
sensorWrite(msgString);
}
Serial.println();
}
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("openhab/arduino/status","Arduino client connected");
// ... and resubscribe
client.subscribe("openhab/arduino/command");
} 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(57600);
// Set IP of your OpenHab install (Raspberry Pi)
client.setServer("10.0.0.101", 1883);
client.setCallback(callback);
Ethernet.begin(mac);
// Allow the hardware to sort itself out
delay(1500);
Serial.println(Ethernet.localIP());
readTime = 0;
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
//check if 5 seconds has elapsed since the last time we read the sensors.
if(millis() > readTime+60000){
sensorRead();
}
}
void sensorRead(){
readTime = millis();
// Get the position of the led, HIGH or LOW (true or false)
char led_status;
if(digitalRead(ledPin)){
led_status = "ON";
} else {
led_status = "OFF";
}
// Publish the position to the broker, with this topic
client.publish("openhab/arduino/led",led_status);
Serial.print("Sensor Read: ");
Serial.println(led_status);
}
void sensorWrite(String cmd){
// Set the position of the led, HIGH or LOW (true or false)
if(cmd == "ON"){
digitalWrite(ledPin, HIGH);
client.publish("openhab/arduino/command","executed command ON");
}
if(cmd == "OFF") {
digitalWrite(ledPin, LOW);
client.publish("openhab/arduino/command","executed command OFF");
}
Serial.print("Sensor Write: ");
Serial.println(cmd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment