Communicating with a Raspberry Pi from an Arduino using Node-RED

I wanted to add some sensors to a Raspberry Pi but I was quickly running out of GPIO pins, luckily I had some spare Arduinos so decided to experiment with plugged one into the Pi.

I loaded this sketch onto the Arduino to read temperature and humidity using the DHT22. More information on how to do this can be found on my previous post about using DHT sensors with an Arduino. Once this is working connect the Arduino via USB to the Raspberry Pi.

To get the readings in Node-RED install Node-RED serialport (follow the instructions in the link), restart Node-RED and select the serial port node (make sure your Arduino is plugged in to the pi using a USB cable). To set it up you need to put in the serial port you are using. My settings looked like the following image.

You may need to parse the values coming from your Arduino, maybe to separate out different values. To do this I added a function with this code:

var readings = {};
var values = msg.payload.split(",");
for (var i = 0; i < values.length; i++) { var items = values[i].split(":"); readings[items[0].trim()] = parseFloat(items[1]); } msg.readings = readings; return msg;

This will parse readings in the format 'Reading1: value1, Reading2: value2' and add them to the msg object as msg.readings.Reading1 etc.
I added MQTT outputs so that I could access the readings through MQTT clients. An example of the flow I used is on Github


Add a Comment

Your email address will not be published. Required fields are marked *