Skip to content

Instantly share code, notes, and snippets.

@empi89
Created June 15, 2016 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save empi89/2675f9b66b8f41be0dc2428b98a747f1 to your computer and use it in GitHub Desktop.
Save empi89/2675f9b66b8f41be0dc2428b98a747f1 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>
#define DEBUG
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.println (x)
#else
#define DEBUG_PRINT(x)
#endif
byte mac[]= {0x90, 0xA2, 0xDA, 0x0D, 0x88, 0x5F} ; // change by your arduino mac address
char server[] = "192.168.178.XXX";
unsigned int port = 1883;
char topicSubscribe[] = "/heating/switch/relay";
char topicPublish[] = "/heating/switch/relay_state";
char deviceName[] = "heatingswitch";
int delayTime = 5000;
int RelayPin = 2;
EthernetClient client;
PubSubClient arduinoClient(server, port, 0, client) ; //no callback function is specified as we only publish
void setup() {
Serial.begin(9600);
DEBUG_PRINT(F("Initialisation"));
pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, LOW);
beginConnection();
}
char message_buff[100];
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
Serial.print(topic);
Serial.print(" => ");
Serial.println(msgString);
//if ( topic == topicSubscribe ) {
if (msgString == "on" ) {
digitalWrite(RelayPin, HIGH); // turn the RELAY on
arduinoClient.publish(topicPublish,"on");
} else if (msgString == "off" ) {
digitalWrite(RelayPin, LOW); // turn the RELAY off
arduinoClient.publish(topicPublish,"off");
} else {
Serial.print("I do not know what to do with ");
Serial.print(msgString);
Serial.print(" on topic ");
Serial.println(topic);
}
//}
}
void beginConnection(){
DEBUG_PRINT(F("Entering beginConnection"));
if (Ethernet.begin(mac) == 0) {
Serial.println(F("Failed to configure Ethernet using DHCP"));
exit(-1);
};
DEBUG_PRINT(F("Obtained IP Address:"));
DEBUG_PRINT(Ethernet.localIP());
arduinoClient.setCallback(callback);
if (arduinoClient.connect(deviceName)) {
arduinoClient.subscribe(topicSubscribe);
DEBUG_PRINT(F("Connected to MQTT Server..."));
} else {
Serial.println(F("Failed to connect to the MQTT Server"));
exit(-1);
}
}
unsigned long tellstate = 0;
void loop(void) {
if (arduinoClient.loop()){
//DEBUG_PRINT(F("Arduino Client loop ok"));
if ( (millis() - tellstate) > 5000 ) {
if ( digitalRead(RelayPin) ) {
arduinoClient.publish(topicPublish,"on");
} else {
arduinoClient.publish(topicPublish,"off");
}
tellstate = millis();
}
} else {
DEBUG_PRINT(F("Arduino Client loop nok"));
if (!arduinoClient.connected()) {
DEBUG_PRINT(F("arduinoClient is not connected"));
beginConnection();
} else {
DEBUG_PRINT(F("arduinoClient is connected"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment