"
/*
Example MQTT-switch-relay-node with 4 buttons and 4 leds
-
connects to an MQTT server
-
publishes “hello world” to the topic “led”
-
subscribes to the topic “led”
-
controls 4 leds on pins 2,3,5 and 6 - leds can be replaced with relays
-
reads 4 button on pins 7,8,9 and 10
-
turns on/off a specific led when it receives a specific “on”/“off” from the “led” topic
-
sends a specific “on”/“off” to the “led” topic a specific button is pressed
-
multiple arduino’s with same generic sketch can run parallel to each other
-
multiple arduino’s need each to have a unique ip-addres, unique mac address and unique MQTT client-ID
-
tested on arduino-uno with W5100 ethernet shield
-
Ethernet Shield W5100 uses pins 4,10,11,12,13
-
availbale digital pins: 1,2,3,5,6,7,8,9,10
*/
//------------------------------------------------------------------------------
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bounce2.h>
// Set led variables to Arduino digital pins
int led1 = 2;
int led2 = 3;
int led3 = 5; // pin 4 used by ethernet shield
int led4 = 6;
// Set button variables to Arduino digital pins
int button1 = 7;
int button2 = 8;
int button3 = 9;
int button4 = 10; // pins 11,12,13 used by ethernetshield
// Set variables to act as virtual switches
// Set variable values initially to LOW (and not HIGH)
int led1Value = LOW;
int led2Value = LOW;
int led3Value = LOW;
int led4Value = LOW;
//---------------------------------------------------------------------------
// Arduino MAC address is on a sticker on your Ethernet shield
// must be unique for every node in same network
// To make a new unique address change last letter
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };
// Unique static IP address of this Arduino - change to adapt to your network
IPAddress ip(192,168,0,238);
// IP Address of your MQTT broker - change to adapt to your network
byte server[] = { 192, 168, 0, 20 };
// Handle and convert incoming MQTT messages ----------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
String content="";
char character;
for (int num=0;num<length;num++) {
character = payload[num];
content.concat(character);
}
Serial.println(topic);
Serial.println(content); // message sent out by button actions is returned from broker and serial printed
// Set specific virtual switches on basis of specific incoming messages ----------------------------
if (content == “1on”) {
led1Value = HIGH;
}
if (content == “1off”) {
led1Value = LOW;
}
if (content == “2on”) {
led2Value = HIGH;
}
if (content == “2off”) {
led2Value = LOW;
}
if (content == “3on”) {
led3Value = HIGH;
}
if (content == “3off”) {
led3Value = LOW;
}
if (content == “4on”) {
led4Value = HIGH;
}
if (content == “4off”) {
led4Value = LOW;
}
// Set digital pin states according to virtual switch settings
digitalWrite(led1,led1Value);
digitalWrite(led2,led2Value);
digitalWrite(led3,led3Value);
digitalWrite(led4,led4Value);
}
// Initiate instances -----------------------------------
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
// Initiate a bouncer instance for each button
Bounce bouncer1 = Bounce();
Bounce bouncer2 = Bounce();
Bounce bouncer3 = Bounce();
Bounce bouncer4 = Bounce();
//-------------------------------------------------------
void setup()
{
// setup led, button, bouncer 1 -----------------------
pinMode(led1, OUTPUT);
pinMode(button1,INPUT);
digitalWrite(button1,HIGH);
bouncer1 .attach(button1);
bouncer1 .interval(5);
// setup led, button, bouncer 2 -----------------------
pinMode(led2, OUTPUT);
pinMode(button2,INPUT);
digitalWrite(button2,HIGH);
bouncer2 .attach(button2);
bouncer2 .interval(5);
// setup led, button, bouncer 3 -----------------------
pinMode(led3, OUTPUT);
pinMode(button3,INPUT);
digitalWrite(button3,HIGH);
bouncer3 .attach(button3);
bouncer3 .interval(5);
// setup led, button, bouncer 4 -----------------------
pinMode(led4, OUTPUT);
pinMode(button4,INPUT);
digitalWrite(button4,HIGH);
bouncer4 .attach(button4);
bouncer4 .interval(5);
// setup serial and ethernet communications -------------------------------
// Setup serial connection
Serial.begin(9600);
// Setup ethernet connection to MQTT broker
Ethernet.begin(mac);
if (client.connect(“arduino-ip-238”)) { // change as desired - clientname must be unique for MQTT broker
client.publish(“led”,“hello world - here arduino ip 239”);
Serial.println(“connected”);
client.subscribe(“led”); // subscribe to topic “led”
}
}
//----------------------------------------------
void loop()
{
// Listen for button interactions and take actions ----------------------------------------
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW
if (bouncer1.update()) {
if (bouncer1.read() == HIGH) {
if (led1Value == LOW) {
led1Value = HIGH;
client.publish(“led”,“1on”);
} else {
led1Value = LOW;
client.publish(“led”,“1off”);
}
}
}
//-----------------------------------------------
if (bouncer2.update()) {
if (bouncer2.read() == HIGH) {
if (led2Value == LOW) {
led2Value = HIGH;
client.publish(“led”,“2on”);
} else {
led2Value = LOW;
client.publish(“led”,“2off”);
}
}
}
//------------------------------------------------
if (bouncer3.update()) {
if (bouncer3.read() == HIGH) {
if (led3Value == LOW) {
led3Value = HIGH;
client.publish(“led”,“3on”);
} else {
led3Value = LOW;
client.publish(“led”,“3off”);
}
}
}
//-----------------------------------------------
if (bouncer4.update()) {
if (bouncer4.read() == HIGH) {
if (led4Value == LOW) {
led4Value = HIGH;
client.publish(“led”,“4on”);
} else {
led4Value = LOW;
client.publish(“led”,“4off”);
}
}
}
//------------------------------------------------
client.loop();
}
// End of sketch ---------------------------------
"