Node-RED & Touchberry Tutorial: How to get temperature from Dallas sensor
Modbus communication for Industrial Arduino & Raspberry based PLCs
30 July, 2021 by
Node-RED & Touchberry Tutorial: How to get temperature from Dallas sensor
Boot & Work Corp. S.L., Fernandez Queralt Martinez


 Modbus is the most used pervasive communications protocol in industrial and building automation and the most commonly available means of connecting automated electronic devices.

Taking advantage of this,we connected a Dallas DS18B20 temperature sensor 

Then, we got the temperature from the Dallas sensor to our Arduino based PLC

And now, we are going to connect it through Modbus with a Touchberry Pi 10.1", Raspberry based, and make a very simple Dashboard with Node-RED!

Odoo text and image block



How to: Read Dallas temperature sensor and datalog in µSD Card


How to: Control temperature with Dallas DS18B20 sensor and Arduino PLC

How to connect temperature sensor
to Raspberry PLC

Dallas sensor & Raspberry PLC:
How to get temperature

Node-RED & Raspberry tutorial:
How to get temperature
from Dallas sensor

How to program the 10 I/Os ESP32 industrial PLC via WiFi


Odoo image and text block


Read Input Registers

Modbus bases its data model on a series of tables that have distinguising characteristics. The four primary tables are:

Discrete inputsSingle bitRead-OnlyThis type of data be provided by an I/O system.
CoilsSingle bitRead-WriteThis type of data can be alterable by an application.
Input Registers16-bit wordRead-OnlyThis type of data can be provided by an I/O system.
Holding Registers16-bit wordRead-WriteThis type of data can be alterable by an application program.

The distinctions between inputs and outputs, and between bit-addressable and word-addressable data items, do not imply any application behaviour. It is perfectly acceptable, and very common, to regard all four tables as overlaying one another. For each of the primary tables, the protocol allows individual selection of 65536 data items, and the  operations of read or write of those items are designed to span multiple consecutive data items up to a data size limit which is dependent on the transaction function code.

In this blog, we are going to focus on the function code (FC) number 4, which has the following frame structure:

Request: Slave Address (1 byte), FC (1 byte), Starting Address (2 bytes: from 0x0000 to 0xFFFF), Quantity of Input Registers (2 bytes: from 0x0001 to 0x007D), CRC.

Response: Slave Address (1 byte), FC (1 byte: 0x04), Byte count (1 byte: 2 x N*), Input Registers (N* x 2 bytes), CRC.

In Node-RED, we will send a request, we will run a script which will process the information, in order to get a response with the temperature of our Dallas DS18B20 temperature sensor connected to an Arduino based PLC.

Now, we have to know the request frame we are going to send, but as we already did in this blog:

- We set the slave address for the Ardbox PLC to 1. (0x01)
- The FC is 4 as a standard for Read Input Registers function. (0x04)
- We want to access from the first address, starting at 0. (0x0000)
- And we want to request only one register, the one with the temperature, as we already set in the Arduino code from Pin 2. (0x0001)

If we calculate the CRC of 010400000001, we will get CA31, but as it is little endian, we will take 31CA.

  Note: You can use any method or website for CRC calculation. We like this one with Hexinput type and taking CRC-16 (Modbus).

Odoo image and text block

So, add an inject node and type this frame in a string msg.payload as shown in the picture blow. Then, wire it to an exec node where we will call a script to process the information.

Odoo image and text block

Now, copy this script to your Touchberry Pi 10.1" and name as you want, like modbus.c, also remember that your must set the RS-485 rate to 9600UL.

// C library headers #include <stdio.h> #include <string.h> // Linux headers #include <fcntl.h> // Contains file controls like O_RDWR #include <errno.h> // Error integer and strerror() function #include <termios.h> // Contains POSIX terminal control definitions #include <unistd.h> // write(), read(), close() #include <stdlib.h> #include <sys/stat.h> #include <dirent.h> #include <errno.h> /////////////////////////////////////////////////////////////////////////////////////// // Convert char to int int char2int(char input) { if(input >= '0' && input <= '9') { return input - '0'; } if(input >= 'A' && input <= 'F') { return input - 'A' + 10; } if(input >= 'a' && input <= 'f') { return input - 'a' + 10; } return 0; } /////////////////////////////////////////////////////////////////////////////////////// void hex2bin(const char* src, char* target) { while(*src && src[1]) { *(target++) = char2int(*src)*16 + char2int(src[1]); src += 2; } } /////////////////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { char read_buf [256]; int fd; if (argc < 2) { printf("Usage: %s <data-hex>\n", argv[0]); return 1; } char buf[100]; hex2bin(argv[1], buf); int buflen = strlen(argv[1]) / 2; // Open the serial port. Change device path as needed (currently set to an standard FTDI USB-UART cable type device) int serial_port = open("/dev/ttyS0", O_RDWR); // Create new termios struc, we call it 'tty' for convention struct termios tty; // Read in existing settings, and handle any error if(tcgetattr(serial_port, &tty) != 0) { printf("Error %i from tcgetattr: %s\n", errno, strerror(errno)); return 1; } tty.c_cflag &= ~PARENB; // tty.c_cflag &= ~PARODD; // Clear parity bit, disabling parity (most common) tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common) tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size tty.c_cflag |= CS8; // 8 bits per byte (most common) tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common) tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1) tty.c_lflag &= ~ICANON; tty.c_lflag &= ~ECHO; // Disable echo tty.c_lflag &= ~ECHOE; // Disable erasure tty.c_lflag &= ~ECHONL; // Disable new-line echo tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars) tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed // tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX) // tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX) tty.c_cc[VTIME] = 1; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received. tty.c_cc[VMIN] = 0; // Set in/out baud rate to be 9600 cfsetispeed(&tty, B9600); cfsetospeed(&tty, B9600); // Save tty settings, also checking for error if (tcsetattr(serial_port, TCSANOW, &tty) != 0) { printf("Error %i from tcsetattr: %s\n", errno, strerror(errno)); return 1; } // Export the desired pin by writing to /sys/class/gpio/export int dir = open("/sys/class/gpio/gpio27", O_WRONLY); if (ENOENT == errno) { /* Directory does not exist. */ // write(dir, "27", 2); fd = open("/sys/class/gpio/export", O_WRONLY); if (fd == -1) { perror("Unable to open /sys/class/gpio/export"); exit(1); } if (write(fd, "27", 2) != 2) { perror("Error writing to /sys/class/gpio/export"); exit(1); } } close(dir); // Set the pin to be an output by writing "out" to /sys/class/gpio/gpio27/direction fd = open("/sys/class/gpio/gpio27/direction", O_WRONLY); if (fd == -1) { perror("Unable to open /sys/class/gpio/gpio27/direction"); exit(1); } if (write(fd, "out", 3) != 3) { perror("Error writing to /sys/class/gpio/gpio27/direction"); exit(1); } close(fd); fd = open("/sys/class/gpio/gpio27/value", O_WRONLY); if (fd == -1) { perror("Unable to open /sys/class/gpio/gpio27/value"); exit(1); } // Toggle LED 50 ms on, 50ms off, 100 times (10 seconds) if (write(fd, "1\n", 2) != 2) { perror("Error writing to /sys/class/gpio/gpio27/value"); exit(1); } //usleep(10000); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (write(serial_port, buf, buflen) < 0) { perror("Error writing to serial port"); exit(1); } int t = 10 * buflen * 1000000 / 9600; usleep(t); usleep(1000); //tcdrain(serial_port); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (write(fd, "0\n", 2) != 2) { perror("Error writing to /sys/class/gpio/gpio27/value"); exit(1); } usleep(5000); close(fd); // Normally you wouldn't do this memset() call, but since we will just receive // ASCII data for this example, we'll set everything to 0 so we can // call printf() easily. //memset(read_buf, '\0', sizeof(read_buf)); // Read bytes. The behaviour of read() (e.g. does it block?, // how long does it block for?) depends on the configuration // settings above, specifically VMIN and VTIME // char* num_bytes[] = {}; // for (int i = 0; i < sizeof(read_buf); i++){ // num_bytes[i] = read_buf[i]; // } char c; int n; char* ptr = read_buf; int num_bytes = 0; do { n = read(serial_port, &c, 1); if (n < 0) { perror("Read serial port fail"); exit(1); } else if (n > 0) { *ptr++ = c; ++num_bytes; } } while (n > 0); // n is the number of bytes read. n may be 0 if no bytes were received, and can also be -1 to signal an error. if (num_bytes < 0) { printf("Error reading: %s", strerror(errno)); return 1; } // Here we assume we received ASCII data, but you might be sending raw bytes (in that case, don't try and // print it to the screen like this!) // for (int i = 0; i < num_bytes; i++) { // printf("Read %i bytes. Received message: %s", num_bytes, read_buf); // } // for (int i = 0; i < num_bytes; i++){ // printf("Read %i bytes. Received message: %s", num_bytes, read_buf); // } //printf("Received buffer (%d): ", num_bytes); for (int i = 0; i < num_bytes; ++i) { printf("%02x", read_buf[i]); } printf("\n"); close(serial_port); return 0; // success } ///////////////////////////////////////////////////////////////////////////////////////

Know more about it HERE >>>

Compile it using g++ like:

g++ modbus.c -o modbus

Then, in the exec node, let's execute the modbus binary file we just compiled. Add the command to call the file in the path you put it and append the msg.payload with the frame to run the script with a parameter:

Odoo image and text block

After executing the script, the response we got was: 01040109C6CF32. So, let's analize the frame:

Odoo image and text block

What really matters, the data we are interested in, are the input registers, so 09C6. So let's get it with a function node and convert it to decimal, to get the temperature.

Now, add a function node with the following code:

var temp = msg.payload;
return { 
    payload: parseInt(temp.substring(6,10), 16)/100

In the function above, the parseInt with a base of 16 indicates a conversion to Hexadecimal. We do it only getting the substring from 6 to 10, so the data of the frame. Then, we divide it by 100 as we sent a uint16_t data from the Arduino code.

Odoo image and text block

Add a debug node or a Dashboard Gauge node, and check your temperature using Modbus!

Odoo image and text block
Node-RED &amp; Touchberry Tutorial: How to get temperature from Dallas sensor
Boot & Work Corp. S.L., Fernandez Queralt Martinez
30 July, 2021
Share this post

Looking for your ideal PLC?

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.

Industrial PLC comparison >>

Do you want more information?

Just fill the form!

Tell me more!