Using the ModBus R41A08 relay board with Node-Red

This article explains how to manage the cheap Modbus 8 relay board R41A08 from Node-Red

Nodes to install:

Node-Red - Flow

    [{"id":"34943ba9.6530b4","type":"modbus-flex-write","z":"71ad9872.edea58","name":"","showStatusActivities":false,"showErrors":false,"server":"9b45273.add55d8","x":930,"y":640,"wires":[[],[]]},{"id":"9d643725.bc6ae8","type":"function","z":"71ad9872.edea58","name":"8 relay board","func":"if (msg.payload[\"state\"]===0) {\n\n    msg.payload={\n        \"value\": 512,\n        \"fc\": 6,\n        \"unitid\": 1,\n        \"address\": msg.payload[\"rele\"],\n        \"quantity\": 1\n    }\n} else {\n    msg.payload={\n        \"value\": 256,\n        \"fc\": 6,\n        \"unitid\": 1,\n        \"address\": msg.payload[\"rele\"],\n        \"quantity\": 1\n    }\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":640,"wires":[["34943ba9.6530b4"]]},{"id":"2e1ee3fa.5d265c","type":"inject","z":"71ad9872.edea58","name":"RELAY 1 ON","topic":"","payload":"{\"rele\":1,\"state\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":280,"wires":[["9d643725.bc6ae8"]]},{"id":"bd4854e7.ac1ab8","type":"inject","z":"71ad9872.edea58","name":"RELAY 1 OFF","topic":"","payload":"{\"rele\":1,\"state\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":320,"wires":[["9d643725.bc6ae8"]]},{"id":"4ff31588.542ffc","type":"inject","z":"71ad9872.edea58","name":"RELAY 2 ON","topic":"","payload":"{\"rele\":2,\"state\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":380,"wires":[["9d643725.bc6ae8"]]},{"id":"cf77f1a.b5d181","type":"inject","z":"71ad9872.edea58","name":"RELAY 2 OFF","topic":"","payload":"{\"rele\":2,\"state\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":420,"wires":[["9d643725.bc6ae8"]]},{"id":"f710b9f5.e369a8","type":"inject","z":"71ad9872.edea58","name":"RELAY 3 ON","topic":"","payload":"{\"rele\":3,\"state\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":480,"wires":[["9d643725.bc6ae8"]]},{"id":"76ab0f31.b3eeb","type":"inject","z":"71ad9872.edea58","name":"RELAY 3 OFF","topic":"","payload":"{\"rele\":3,\"state\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":520,"wires":[["9d643725.bc6ae8"]]},{"id":"4fd45b86.0fec94","type":"inject","z":"71ad9872.edea58","name":"RELAY 4 ON","topic":"","payload":"{\"rele\":4,\"state\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":580,"wires":[["9d643725.bc6ae8"]]},{"id":"ad21c306.0ab48","type":"inject","z":"71ad9872.edea58","name":"RELAY 4 OFF","topic":"","payload":"{\"rele\":4,\"state\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":620,"wires":[["9d643725.bc6ae8"]]},{"id":"e3559ab4.db5498","type":"inject","z":"71ad9872.edea58","name":"RELAY 5 ON","topic":"","payload":"{\"rele\":5,\"state\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":680,"wires":[["9d643725.bc6ae8"]]},{"id":"d61acf97.53a6d","type":"inject","z":"71ad9872.edea58","name":"RELAY 5 OFF","topic":"","payload":"{\"rele\":5,\"state\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":720,"wires":[["9d643725.bc6ae8"]]},{"id":"fa394dd.203b7b","type":"inject","z":"71ad9872.edea58","name":"RELAY 6 ON","topic":"","payload":"{\"rele\":6,\"state\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":780,"wires":[["9d643725.bc6ae8"]]},{"id":"f57c8488.9971d8","type":"inject","z":"71ad9872.edea58","name":"RELAY 6 OFF","topic":"","payload":"{\"rele\":6,\"state\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":820,"wires":[["9d643725.bc6ae8"]]},{"id":"f0637366.4f4eb","type":"inject","z":"71ad9872.edea58","name":"RELAY 7 ON","topic":"","payload":"{\"rele\":7,\"state\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":880,"wires":[["9d643725.bc6ae8"]]},{"id":"b5dd9c0e.5b4ad","type":"inject","z":"71ad9872.edea58","name":"RELAY 7 OFF","topic":"","payload":"{\"rele\":7,\"state\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":920,"wires":[["9d643725.bc6ae8"]]},{"id":"41132eec.c808f","type":"inject","z":"71ad9872.edea58","name":"RELAY 8 ON","topic":"","payload":"{\"rele\":8,\"state\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":980,"wires":[["9d643725.bc6ae8"]]},{"id":"b7c974ca.55b048","type":"inject","z":"71ad9872.edea58","name":"RELAY 8 OFF","topic":"","payload":"{\"rele\":8,\"state\":0}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1020,"wires":[["9d643725.bc6ae8"]]},{"id":"34cc6e0a.6e3cd2","type":"inject","z":"71ad9872.edea58","name":"Go","topic":"","payload":"go","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"2","x":390,"y":160,"wires":[["20543bb5.22ce44"]]},{"id":"f421fb69.0ef9a8","type":"function","z":"71ad9872.edea58","name":"Get rotate flag","func":"var relay = flow.get('relay');\nvar rotate = flow.get('rotate');\n\nnode.status({text:relay});\n\nif (rotate==1) {\n    relay += 1;\n    if (relay>8) { \n        flow.set('relay',0);\n        flow.set('rotate',0);\n        return;\n    }    \n    flow.set('relay',relay);\n}    \nmsg.payload={\"rele\":relay,\"state\":1};\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":200,"wires":[["9d643725.bc6ae8"]]},{"id":"20543bb5.22ce44","type":"function","z":"71ad9872.edea58","name":"Set rotate flag","func":"flow.set('rotate',1);\nflow.set('relay',0);\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":160,"wires":[[]]},{"id":"e47a1b69.884f78","type":"inject","z":"71ad9872.edea58","name":"","topic":"","payload":"","payloadType":"date","repeat":"0.1","crontab":"","once":false,"onceDelay":"1","x":370,"y":200,"wires":[["f421fb69.0ef9a8"]]},{"id":"50f892fd.4d8a3c","type":"inject","z":"71ad9872.edea58","name":"Shutdown","topic":"","payload":"sudo shutdown","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":940,"y":160,"wires":[["cb8cab01.2b3be8"]]},{"id":"cb8cab01.2b3be8","type":"exec","z":"71ad9872.edea58","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":1090,"y":160,"wires":[[],[],[]]},{"id":"9b45273.add55d8","type":"modbus-client","z":"","name":"","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB2","serialType":"RTU","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectTimeout":2000}]

Mini RS485 sniffer

In order to understand how to manage the Modbus protocol I've used this simple RS485 sniffer in Python

import serial 
import time

port = '/dev/ttyUSB2'
baud = 9600

ser = serial.Serial(port, baud, timeout=0)

counter = 0
while True:
    if ser.in_waiting>0:
        c=ser.read(1)
        print "%02X " % (ord(c)),
        counter+=1
        if counter==16:
            print
            counter=0
    time.sleep(0.01)

Raw data on RS485 bus

This is the sequence in hexadecimal of the bytes to send on RS485 bus at 9600,N,8,1 to the 8 relay board to turn on and turn off each relay

01  06  00  01  01  00  D9  9A  01  06  00  01  01  00  D9  9A 
01  06  00  01  02  00  D9  6A  01  06  00  01  02  00  D9  6A 
01  06  00  02  01  00  29  9A  01  06  00  02  01  00  29  9A 
01  06  00  02  02  00  29  6A  01  06  00  02  02  00  29  6A 
01  06  00  03  01  00  78  5A  01  06  00  03  01  00  78  5A 
01  06  00  03  02  00  78  AA  01  06  00  03  02  00  78  AA 
01  06  00  04  01  00  C9  9B  01  06  00  04  01  00  C9  9B 
01  06  00  04  02  00  C9  6B  01  06  00  04  02  00  C9  6B 
01  06  00  05  01  00  98  5B  01  06  00  05  01  00  98  5B 
01  06  00  05  02  00  98  AB  01  06  00  05  02  00  98  AB 
01  06  00  06  01  00  68  5B  01  06  00  06  01  00  68  5B 
01  06  00  06  02  00  68  AB  01  06  00  06  02  00  68  AB 
01  06  00  07  01  00  39  9B  01  06  00  07  01  00  39  9B 
01  06  00  07  02  00  39  6B  01  06  00  07  02  00  39  6B 
01  06  00  08  01  00  09  98  01  06  00  08  01  00  09  98 
01  06  00  08  02  00  09  68  01  06  00  08  02  00  09  68

Links

Sergio Tanzilli (Autore - Progettista hw & sw - Imprenditore - Musicista dilettante, classe 1964)
Fondatore di Area SX srl, Acme Systems srl e del TanzoLab, si occupa dal 1974 di elettronica digitale, dal 1978 di microprocessori, dal 1992 di microcontroller, dal 1995 di Linux e pubblicazioni su Web, dal 2005 di progettazione, produzione e commercializzazione di schede Linux Embedded.
http://www.tanzilli.com - http://www.acmesystems.it - https://github.com/tanzilli - sergio@tanzilli.com