A Simple MQTT Pub/Sub Node With Arduino UNO and W5100 Ethernetshield




Posted in TechnologyArduino

Introduction: A Simple MQTT Pub/Sub Node With Arduino UNO and W5100 Ethernetshield

About: I am a physician by trade. After a career in the pharmeceutical world I decided to take it a bit slower and do things I like. Other than my hobbies that involves grassroots medicine in S.E.&P Asia. I have bu...

Nowadays many MQTT nodes are based around the ESP8266. However far before the ESP8266 the Arduino UNO already had Ethernet capabilities with an Ethernetshield.
The aim of this instructable is to show how one can use A UNO with W5100 shield or module as an MQTT node and in the software I will show some simple techniques of how to deal with outbound MQTT messages and inbound MQTT messages. If you still have an Old ENC28J60 Ethershield, don't despair, one can use those as well. I will cover that in my next instructable.

If you know what MQTT is, go to the next Step. If not, read on
For the uninitiated a brief explanation of MQTT: MQTT is a protocol to transfer messages between devices that are connected to a LAN. Those messages are formed by a topic and a payload
A device can publish messages to the LAN and it can subscribe to messages on the LAN. Now when I say "publish to the LAN" that is a bit of a eufemism, because in fact a node will be publishing its messages and subscribing to messages from a so called MQTT broker. The broker is merely a device that will receive the published messages, keep track of what device subscribed to what topic and then send the proper message to the right device.

An MQTT broker can be housed on your own LAN and often a RaspberryPi is used for that, or a public broker can be used.

Now obviously, just building one Node is not going to do much good. Who is it going to talk to? So obviously this would fit in a bigger systems with various nodes talking to eachother and the user having a frienly interface such as OpenHab. But in the google playstore various MQTTdashboards are available as app that allow for a simple interface. When developing MQTT software, it is always smart to install mqtt-spy. It is available for several platforms and what it basically does is to show MQTT messages going around on your LAN and to allow you to manually send MQTT messages

Step 1: The Software

The node I describe has several functions:
It reads from several switches (e.g. door contacts), it reads the value of an LDR, it reads temperature and humidity from a DHT11, it reads a bell signal, and it reads the state of a relay and sends those onto the broker. It does that every 3 seconds

Publishing is done with the command: client.publish(topic,message);
The command is a bit picky with its format. It will happily publish:
client.publish("MyTopic","Hello World");
but it will not do a string variable such as:
String messagestr="Hello World";

Often that is not a problem, but in some cases. I have used the sending of the ip number as an example of how to deal with that.

The node subscribes to only one topic and depending on the message in that topic, it performs several functions:
0- it switches a relay off
1-it switches a relay on
2-it publishes the IP address of the node
3-it publishes the state of the relay

On startup of the node, it automatically publishes the IP address as well.

Subscriptions are usually handled in a function called 'Callback' (but up to you to chose the name)
For ease I have only used one topic and commands of 1 character length.
however it is easy to check for longer command messages and I have shown in the software how to do that.
So instead of just sending a "0" or a "1" to the general subscription topic, it is possible to make a topic called "/home/br/sb/relay" and use messages such as "ON" and "OFF" to switch the relay. However, that all costs memory and on a UNO with an Ethernetcard one needs to be a bit careful with memory use (though it still ahs much more compared to using an ENC28j60 Ethernetcard)

Step 2: A Simple MQTT Pub/Sub Node With Arduino UNO and W5100 Ethernetshield: the Program

          Arduino UNO with W5100 Ethernetshield or  W5100 Ethernet module, used as MQTT client
          It will connect over Wifi to the MQTT broker and controls a digital output (LED, relay)
          and gives the Temperature and Humidity, as well as the state of some switches
          The topics have the format "home/br/sb" for southbound messages and  "home/nb" for northbound messages
          Southbound are messages going to the client, northbound are messages coming from the client
          As the available memory of a UNO  with Ethernetcard is limited, I have kept the topics short
          Also, the payloads  are kept short
          The Northbound topics are
          home/br/nb/temp  for temperature
          home/br/nb/humid  for humidity
          home/br/nb/deur  for a door switch
          home/br/nb/l for  the lightintensity
          home/br/nb/pr  for the status of a PIR sensor
          home/br/nb/ip showing the IP number of the client
          home/br/nb/relay showing the relaystate

          There is only one southbound topic:
          The payload here determines the action:
          0 -Switch the relay off
          1-Switch the  relay on
          2-Publish the IP number of the client
          3 Ask for the relaystate// REMOVED

          On Startup, the Client publishes the IP number
#include <Ethernet.h>// Ethernet.h library
#include "PubSubClient.h" //PubSubClient.h Library from Knolleary
#include "DHT.h" //DHT.h library
#define CLIENT_ID       "Hal"
#define PUBLISH_DELAY   3000 // that is 3 seconds interval
#define DHTPIN          3
#define DHTTYPE         DHT11
#define ledPin 13
#define relayPin 8
String ip = "";
bool statusKD = HIGH;
bool statusBD = HIGH;
bool statusGD = HIGH;
bool relaystate = LOW;
bool pir = LOW;
bool startsend = HIGH;// flag for sending at startup
int lichtstatus; //contains LDR reading
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x06};

EthernetClient ethClient;
PubSubClient mqttClient;

long previousMillis;

void setup() {
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT);
  pinMode(relayPin, OUTPUT);

  // setup serial communication

  while (!Serial) {};
  Serial.println(F("MQTT Arduino Demo"));

  // setup ethernet communication using DHCP
  if (Ethernet.begin(mac) == 0) {
    //Serial.println(F("Unable to configure Ethernet using DHCP"));
    for (;;);

  Serial.println(F("Ethernet configured via DHCP"));
  Serial.print("IP address: ");
 //convert ip Array into String
  ip = String (Ethernet.localIP()[0]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[1]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[2]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[3]);

  // setup mqtt client
  // mqttClient.setServer("test.mosquitto.org", 1883);//use public broker
  mqttClient.setServer( "", 1883); // or local broker
  //Serial.println(F("MQTT client configured"));
  // setup DHT sensor
  Serial.println(F("DHT sensor initialized"));
  Serial.println(F("Ready to send data"));
  previousMillis = millis();
  mqttClient.publish("home/br/nb/ip", ip.c_str());

void loop() {
  statusBD = digitalRead(4);// FrontdoorSwitch
  statusGD = digitalRead(5);// Garagedoor Switch
  statusKD = (digitalRead(6));//LivingRoom Switch
  lichtstatus = analogRead(A0);//Reads an LDR
  pir = digitalRead(7);//Reads a PIR sensor
  relaystate = digitalRead(relayPin);// Reads the state of a relay

  // it's time to send new data?
  if (millis() - previousMillis > PUBLISH_DELAY) {
    previousMillis = millis();

void sendData() {
  char msgBuffer[20];
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial.print("Temperature: ");
  Serial.print("Humidity: ");
  Serial.print("Relay is: ");
  Serial.println((relaystate == LOW) ? "OPEN" : "CLOSED");
  if (mqttClient.connect(CLIENT_ID)) {
    mqttClient.publish("home/br/nb/temp", dtostrf(t, 6, 2, msgBuffer));
    mqttClient.publish("home/br/nb/humid", dtostrf(h, 6, 2, msgBuffer));
    mqttClient.publish("home/br/nb/deur", (statusBD == HIGH) ? "OPEN" : "CLOSED");
    mqttClient.publish("home/br/nb/garage", (statusGD == HIGH) ? "OPEN" : "CLOSED");
    mqttClient.publish("home/br/nb/bel", (statusKD == HIGH) ? "OPEN" : "CLOSED");
    mqttClient.publish("home/br/nb/l", dtostrf(lichtstatus, 4, 0, msgBuffer));
    mqttClient.publish("home/br/nb/p", (pir == HIGH) ? "OPEN" : "CLOSED");
    mqttClient.publish("home/br/nb/relay", (relaystate == LOW) ? "OPEN" : "CLOSED");
    if (startsend) {
      mqttClient.publish("home/br/nb/ip", ip.c_str());
      startsend = LOW;

void callback(char* topic, byte* payload, unsigned int length) {
  char msgBuffer[20];
  // I am only using one ascii character as command, so do not need to take an entire word as payload
  // However, if you want to send full word commands, uncomment the next line and use for string comparison
  // payload[length]='\0';// terminate string with 0
//String strPayload = String((char*)payload);  // convert to string
  // Serial.println(strPayload); 
  Serial.print("Message arrived [");
  Serial.print("] ");//MQTT_BROKER
  for (int i = 0; i < length; i++) {

  // Examine only the first character of the message
  if (payload[0] == 49)             // Message "1" in ASCII (turn output ON)
    digitalWrite(LED_BUILTIN, HIGH);    //
    digitalWrite(relayPin, HIGH);
  } else if (payload[0] == 48)      // Message "0" in ASCII (turn output OFF)
    digitalWrite(relayPin, LOW);     //
    digitalWrite(LED_BUILTIN, LOW);
  } else if (payload[0] == 50)
    mqttClient.publish("home/br/nb/ip", ip.c_str());// publish IP nr
  } else {
    Serial.println("Unknown value");
    mqttClient.publish("home/br/nb", "Syntax Error");


As copying this program could introduce all sorts of errors and because Instructabels sometimes drops the library declarations, the code is available for download here. I also have attached it to this instructable. Mind you that the to be downloaded code may still have some comments that mean more to me than to you.



    • Woodworking Contest

      Woodworking Contest
    • Make it Move Contest

      Make it Move Contest
    • Microcontroller Contest

      Microcontroller Contest

    We have a be nice policy.
    Please be positive and constructive.




    Thanks a lot for sharing this, I have used it for controlling a row of 10 relays using mqtt; I am still trying to figure how to publish the state of my relays in a generic way (so without naming it as 'Lamp livingroom' for example), without 10 lines of code for each separate topic.

    4 replies

    I dont think you need 10 lines of code for each topic. I presume you have added these 10 relays all the the arduino?
    The only thing you would need to do for each relay is
    -read state of relay
    -publish the state
    That is two lines for each, or maybe I totally misunderstand you

    Sorry, I wasn't clear: in your code you publish the state of your relay in a topic named home/br/nb/relay; if I wanted to control multiple relays, I would have to write lines for each relay separately, because each topic is unique. Given that you can't seem to use variables for the topic in

    'mqttClient.publish', this seems inadequate. My solution at the moment is to publish each button's state as 'RXY', where X is the number of the relay and Y is 0 or 1, all on the same topic. I got it working with the Home Assistant software.

    Thanks for expanding, although I still do not get why you would need 10 lines of code for each relay just to read the state, but you got it working and that is what counts.

    You CAN use variables for the topic though.
    You would do that as follows:

    #define Id
    char buff_topic[15];
    sprintf(buff_topic, "home/relay%02d", Id);
    client.publish(buff_topic, "message");

    If you want to send variables often, it might be better to use the pubsubclient lib from Ian tester (Imroy) as that allows simple String(variable) statements to be sent. Surely for the message and I presume also for the topic

    Dear diy_bloke

    Thank you again for the work you've done on this instructable, it's been a massive help in getting my devices on my Home Assistant and I've finally managed to resolve the mqtt issues I was having before. In an attempt to reduce the wifi in my house I am looking to move more devices over to ethernet. I have tried to add more relays to your code but have had some strange results where both relays turn on/off and flash etc. I tried to paste the code earlier but it didn't work.

    1 reply

    thank you for your kind words. Relays shld not just go on and off. Can you try and paste yr code?

    I am trying to integrate this into my Home Assistant but struggling to get the correct YAML entries. What would the "State topic" be for the other end of the mqtt communication?

    6 replies

    i do not use home assistant so i might nt be the right person to ask. What this device does is to send mqtt messages, as any other.

    Home assistent connects with mqtt through a broker and your yaml fine should just connect to the message as is:

    So suppose this module would send the livingroom temperature, add the following to your configuration.yaml file:

    # configuration.yml
    - platform: mqtt
    state_topic: "home/livingroom/temperature"

    Thanks for looking into this for me. I think I know what the problem is...

    I am using a local mqtt server but the IDE code does not include the "username" and "password".

    I have added them to your client connect line:

    if (mqttClient.connect(CLIENT_ID),"username", "password")

    not sure if this is correct as I haven't got it to work yet?

    I suggest for testing you remove he need for password so at least you are sure there is no problem with that

    Thanks, I will look into doing so. I have included a println statement under the "if (mqttClient.connect(CLIENT_ID))" line and the serial monitor prints it so I'm assuming the connection is being made but for some reason the arduino is not talking to the Home Assistant on the Raspi. I will have to put this on the backburner for now as I'm away for 3 weeks and I'll revisit this on my return. I'll let you know how I get on....or give up if it drives me too insane!

    check with mqttspy what command is actually being sent

    well at least it is a beginning.
    I am not sure if you are using mqttspy, but that s a great program to monitor MQTT traffic and check for mistakes