Publishing DS18B20 temperature data from M-Duino PLC via MQTT

How to read a Dallas DS18B20 sensor with M-Duino PLC and publish the data to an MQTT broker over Ethernet
June 15, 2026 by
Publishing DS18B20 temperature data from M-Duino PLC via MQTT
Boot & Work Corp. S.L, Bernat Brunet Pedra

This is part 1 of a series showing how to build a data pipeline from an Industrial Shields M-Duino PLC to a Google spreadsheet using MQTT and Node-RED. In this first part, the M-Duino reads the temperature from a Dallas DS18B20 sensor and publishes values to a Mosquitto MQTT broker over Ethernet. Parts 2 and 3 cover storing the data in Google Sheets and visualising it in a Node-RED dashboard.

What you need

Installing the Mosquitto MQTT broker

Install Mosquitto on the Linux machine that will act as the broker:

$ sudo apt-get install mosquitto mosquitto-clients

Start the broker and verify it is running:

$ sudo service mosquitto start
$ sudo service mosquitto status

Mosquitto is a lightweight open-source MQTT broker that runs on any Linux device, including a Raspberry Pi.

Connecting the DS18B20 sensor

Connect the DS18B20 to Pin 2 of the M-Duino PLC. See the Dallas temperature sensor post for the full wiring diagram:

DS18B20 wiring with M-Duino PLC

M-Duino sketch: reading and publishing temperature

The sketch reads the DS18B20 temperature on Pin 2 every 400 ms and publishes the value as a JSON object to the MQTT topic "I". It uses the OneWire, DallasTemperature, PubSubClient, and ArduinoJson (v5) libraries.

Before uploading the sketch, set the M-Duino IP address and broker IP to match your network. After uploading, set your PC Ethernet port IP to 10.10.12.2 (or adjust the code to match your network).

#include <OneWire.h>
#include <DallasTemperature.h>
const int oneWirePin = 2;
OneWire oneWireBus(oneWirePin);
DallasTemperature sensor(&oneWireBus);

#ifdef MDUINO_PLUS
#include <Ethernet2.h>
#else
#include <Ethernet.h>
#endif

#include <ArduinoJson.h>   // v5 API
#include <PubSubClient.h>

#define MQTT_ID "demo"
#define NUM_ZONES 1
#define NUM_DIGITAL_INPUTS_PER_ZONE 7
#define DIGITAL_INPUTS_OFFSET 0

const int digitalInputs[NUM_ZONES][NUM_DIGITAL_INPUTS_PER_ZONE] = {
  {I0_0, I0_1, I0_2, I0_3, I0_4, I0_5}
};

byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xAE };
IPAddress ip(10, 10, 12, 2);
IPAddress broker(10, 10, 12, 1);
unsigned port = 1883;

EthernetClient client;
PubSubClient mqtt(client);
int digitalInputsValues[NUM_ZONES][NUM_DIGITAL_INPUTS_PER_ZONE];

void setup() {
  Serial.begin(9600);
  sensor.begin();
  Ethernet.begin(mac, ip);
  mqtt.setServer(broker, port);
  for (int i = 0; i < NUM_ZONES; ++i)
    for (int j = 0; j < NUM_DIGITAL_INPUTS_PER_ZONE; ++j)
      digitalInputsValues[i][j] = digitalRead(digitalInputs[i][j]);
}

void loop() {
  if (!mqtt.connected()) {
    Serial.println("Not connected to mqtt");
    reconnect();
  } else {
    Serial.println("Connected to mqtt");
    mqtt.loop();
  }
  updateInputs();
}

void updateInputs() {
  for (int i = 0; i < NUM_ZONES; ++i)
    for (int j = 0; j < NUM_DIGITAL_INPUTS_PER_ZONE; ++j)
      updateDigitalInput(i, j);
}

void updateDigitalInput(int zone, int index) {
  if (digitalInputs[zone][index] == oneWirePin) {
    sensor.requestTemperatures();
    float value = sensor.getTempCByIndex(0);
    Serial.println(value);
    publishInput(zone, index + DIGITAL_INPUTS_OFFSET, value);
    delay(400);
  }
}

void reconnect() {
  if (mqtt.connect(MQTT_ID)) {
    mqtt.subscribe("I");
    Serial.println("Subscribed to I");
  } else {
    Serial.println("Closing mqtt");
    client.stop();
  }
}

void publishInput(int zone, int index, float value) {
  DynamicJsonBuffer json(JSON_OBJECT_SIZE(3));  // ArduinoJson v5
  JsonObject &root = json.createObject();
  if (root.success()) {
    root["zone"]  = zone;
    root["index"] = index;
    root["value"] = value;
    publish("I", root);
  }
}

void publish(const char *topic, JsonObject &root) {
  unsigned len = root.measureLength();
  if (len > 0) {
    char *payload = new char[len + 1];
    if (payload) {
      root.printTo(payload, len + 1);
      publish(topic, payload);
      delete[] payload;
    }
  }
}

void publish(const char *topic, const char *payload) {
  if (mqtt.connected()) mqtt.publish(topic, payload);
}

Note: this sketch uses the ArduinoJson v5 API. If you have version 6 or later installed, the JsonBuffer and JsonObject API has changed. Pin ArduinoJson to version 5.x in the Library Manager if needed.

Verifying the MQTT connection

With the M-Duino connected to the broker over Ethernet, open a terminal on the broker machine and subscribe to the topic "I":

$ mosquitto_sub -h 10.10.12.1 -t I

The temperature readings published by the M-Duino will appear in JSON format every 400 ms. Once this is working, you are ready for part 2 of the series, which stores the data in a Google spreadsheet using Node-RED.

Part 2: storing data in Google Sheets with Node-RED and MQTT

​Search in our Blog

Publishing DS18B20 temperature data from M-Duino PLC via MQTT
Boot & Work Corp. S.L, Bernat Brunet Pedra June 15, 2026
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.

PLC Comparison