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
At the bottom of the article you say that 3 comments are available. But none of them show up. Doesn’t anyone ever test your site to see if it is still working?
Oh yeah I see what you mean, they all seem to be pingbacks, effectively spam, not sure why they don’t show up but I think they need deleting anyway.
The graphic of the included Node-Red screen is unusable — it is small and fuzzy and enlarging it only makes it big and fuzzy. This article is really just an overview, with some vague hints on how to accomplish the task. There are more questions, but why bother?
I pretty much figured out how to do it. As usual, I had to to go other web pages. Connecting the Arduino to the Pi as a serial device (after writing & downloading the serial output sketch), looking at the Node-Red Serialport instructions (Node-Red actually did automatically find the Arduino on a serial port), the json examples, and then the parsing example, probably give 90% of what is necessary. I won’t know for sure until I try. Thank you for the interesting article. I kind of get the feeling that it is one of those articles where you have to know everything ahead of time, but if you do, then you don’t need the article.
Thank you for pointing out the graphic was fuzzy, wordpress had resized it. Anything in particular I could have clarified?
I noticed the graphic was better when I reloaded the page. Thanks for that. After going to the Serialport page, I was able to guess what your graphic was, and figured it out from there. One of the Node-Red Pis I have already had Serialport installed. I didn’t check the MQTT output node, but I’m sure I can figure that out, using your json examples. My only suggestion would be to have a checklist, like 1) Program Arduino sensor(s) sketch with serial output, 2) connect Arduino to Pi using standard USB cable, 3) use or install and use Serial Input node (see Node_red Serialport page at link in article), 4) Configure Serial Input node (the node configuration should automatically discover the correct Pi USB port) & select the “split” character that you want to use, 5) parse the output if more than one sensor on the Arduino, and 6) if desired, send output from serial node to an MQTT node. Use the Node-Red Debug node/window at each step to see what’s happening. Thanks again for this interesting article.
Good points, I’m going to add the Arduino sketch I used as well but that will have to wait until Monday! I purposefully didn’t want to repeat the install instructions for the serial port node in case it changed and personally I would always prefer to follow the official instructions provided so I’ve attempted to clarify that. For MQTT you will need to set up a broker, I use a pi for that. Thanks again for your feedback.
I have installed MQTT brokers on several Pis, but the one I use is on a Pi3, running Home Assistant HASS.IO software (the complete .IMG version, as opposed to just running on a Pi). It is fantastic, and I control my home automation stuff on it, as well as use Alexa voice control, through Home Assistant, for some of the devices. One of the add-ons is Node-Red. I have Node-Red on most of my Pis, though each one came configured with different nodes. The Pi GPIO nodes are fun and so easy to use. I look forward to the Arduino sketch.
I think the combination of Node-red and MQTT is great. I’ve not used Home Assistant, I might take a look.
This is the video that Home Assistant itself recommends. https://www.youtube.com/watch?v=XWPluWcYRMI
It is simple. Download & burn the HASS.IO image, insert the SD card into a Raspberry Pi 3 (though a Pi 2 should work, and even a Pi Zero works, though more slowly), and you have Home Assistant. While the Pi 3 then becomes a dedicated HA server, the Home Assistant software has MQTT, Node-Red, Amazon Echo and Amazon DASH add-ons, plus SSH (to Home Assistant), among so many others. It is based on using containers, and is regularly, and easily, updated. The original Home Assistant software runs on a Pi, but is more difficult to install. All for free.
Hi. I have invested 14 days and still not able to format the serial data. I am trying to access dht11 and soil moisture sensor data from arduino on Node Red running via RPI. Debug shows data but not able to make sense out of it. Plz help.
How did you set up your arduino? What does the debug data say?
In this awesome pattern of things you receive a B+ just for effort and hard work. Where you misplaced me ended up being in the facts. You know, they say, the devil is in the details… And that couldn’t be much more correct in this article. Having said that, allow me reveal to you what exactly did work. Your authoring is definitely quite convincing and that is most likely why I am taking the effort to comment. I do not make it a regular habit of doing that. Second, whilst I can certainly see the jumps in logic you make, I am not really sure of how you seem to unite your ideas that make your conclusion. For the moment I will, no doubt subscribe to your position but wish in the foreseeable future you actually link your facts much better.
I will right away take hold of your rss feed as I can not to find your email subscription hyperlink or e-newsletter service. Do you have any? Please allow me understand so that I may just subscribe. Thanks.
By way of introduction, I am Mark Schaefer, and I represent Nutritional Products International. We serve both international and domestic manufacturers who are seeking to gain more distribution within the United States. Your brand recently caught my attention, so I am contacting you today to discuss the possibility of expanding your national distribution reach.We provide expertise in all areas of distribution, and our offerings include the following: Turnkey/One-stop solution, Active accounts with major U.S. distributors and retailers, Our executive team held executive positions with Walmart and Amazon, Our proven sales force has public relations, branding, and marketing all under one roof, We focus on both new and existing product lines, Warehousing and logistics. Our company has a proven history of initiating accounts and placing orders with major distribution outlets. Our history allows us to have intimate and unique relationships with key buyers across the United States, thus giving your brand a fast track to market in a professional manner. Please contact me directly so that we can discuss your brand further. Kind Regards, Mark Schaefer, marks@nutricompany.com, VP of Business Development, Nutritional Products International, 101 Plaza Real S, Ste #224, Boca Raton, FL 33432, Office: 561-544-0719
By way of introduction, I am Mark Schaefer, and I represent Nutritional Products International. We serve both international and domestic manufacturers who are seeking to gain more distribution within the United States. Your brand recently caught my attention, so I am contacting you today to discuss the possibility of expanding your national distribution reach.We provide expertise in all areas of distribution, and our offerings include the following: Turnkey/One-stop solution, Active accounts with major U.S. distributors and retailers, Our executive team held executive positions with Walmart and Amazon, Our proven sales force has public relations, branding, and marketing all under one roof, We focus on both new and existing product lines, Warehousing and logistics. Our company has a proven history of initiating accounts and placing orders with major distribution outlets. Our history allows us to have intimate and unique relationships with key buyers across the United States, thus giving your brand a fast track to market in a professional manner. Please contact me directly so that we can discuss your brand further. Kind Regards, Mark Schaefer, marks@nutricompany.com, VP of Business Development, Nutritional Products International, 101 Plaza Real S, Ste #224, Boca Raton, FL 33432, Office: 561-544-0719
Has anyone ever been to 812 Vapes? x
Can someone recommend Theme Party Costumes? Thanks xox