Or – how to get USB serial ports working on the Raspberry Pi
This originally was a cry for help – I wanted a second serial port on the Raspberry Pi AND I made the mistake of trying out Firmata on the Arduino at the same time – resulting collection of failures had me scratching out my few remaining hairs – but thanks to a lot of work – two lots of changes to the Arduino node itself and some reader insights in here, I can now confidently say that:
1. You CAN add more serial ports to your Raspberry Pi
2. You CAN talk from the Pi (or similar) to Arduino gaining access to ports for things like port on/off, PWM etc. with ease.
It all started when I innocently plugged a USB FTDI into the Raspberry Pi USB connector to give me a second serial port to play with as I was using the first one to talk to a Nextion display. I checked in Debian in the /dev directory and it up came /dev/ttyUSB0. When I checked in Node-Red it came up with the same – magic. Great start.
I added in the serial node to Node-Red – set up an inject – and a debug node.
I shorted TRX and TX on the serial and… nothing.
(Serial set to a decent baud rate with 100ms timeout).
I decided to test this and hooked up the internal serial on the Raspberry Pi. I had mistakenly thought that the graphical control in the new Jessie allowed you to turn the serial login terminal on and off – so I turned off serial. BAD mistake – not only did the serial not work but the GPIO node failed as well! I turned the serial back on – rebooted, back to square one.
I updated /boot/cmdline.text to remove reference to the serial port… and read THIS – so now I had serial working.
I tested as you see above. I shorted TX and RX on the internal serial – and sent some text – smashing – the text shows up in the debug.
So now I connected the serial out from the USB serial – to the serial in on the Pi (ensuring 3v3 output) – MAGIC – that worked.
So all that appeared to be NOT working – was the serial in on the USB FTDI serial – yet there are were error messages.
Is this familiar to anyone? I tried various combinations of the RTS and DTR lines – no difference. All looks fine and connected but the USB0 serial in appeared to do nothing.
I took out the FTDI – put another one in – that appeared as USB1 (yes, the SAME ONE)… but it worked – I took that out and plugged the ORIGINAL back in – now this appeared as USB2 and worked – but how the hell are you supposed to use a port if it keeps renaming itself. Read on.
I rebooted the Pi (NOT removing power, just rebooted) – it went back to being USB0 – it worked. I turned the power off, them on – no serial in.
So…. I took the FTDI out – and put it back in – now it appeared as USB1 – adjusted the setting on Node-Red accordingly – it worked. Rebooted…No USB 1 – looked in the node settings – USB0 was back. Adjusted. All was well on USB0.
I turned the power back off then on (for the whole lot)…. Green connected lights on the in and out nodes, all’s well – BUT – no input. I could again see both lights on the FTDI indicating that it is not only sending but pulling back in – but nothing in the debug window when injecting text.
But this had WORKED after a reboot… but not after power up… so – I tried rebooting again…surely it would work? No.
I pulled the FTDI out – and plugged it back in… USB0 disappeared but USB1 was valid…. I changed the node to work with USB1 – it worked.
So it seems that the only situation when the input would not work – was when both the Pi and the FTDI had been powered down and back up. Which of course is every time you turn the stuff off and on!! And the name was changing – which is just useless.
Well, thanks to readers in here I managed to split this problem down into sections. The first was this issue of the name keep changing. As it happens this one is RELATIVELY simple.
pi@bedrock2:~ $ sudo lsusb -v | more
Bus 001 Device 005: ID 0403:6001 Future Technology Devices International, Ltd FT
232 USB-Serial (UART) IC
bDeviceClass 0 (Defined at Interface level)
idVendor 0x0403 Future Technology Devices International, Ltd
idProduct 0x6001 FT232 USB-Serial (UART) IC
iManufacturer 1 FTDI
iProduct 2 FT232R USB UART
iSerial 3 A50285BI
So looking at the code above created by running the command at the top above – starting “sudo”…
This told me all about the last USB item plugged in – sure enough my Future technology USB FTDI
I made a note as instructed by reader Alex – of idVentor, idProduct and iSerial and put them into a file:
sudo nano /etc/udev/rules.d/99-usb-serial.rules
The one and only line in that file would be..
Tried it – failed. Of course I was trying to use vendor 0x0403 – when I took off the 0x it worked a lot better. I’ve no idea why. With further help from Alex the solution appeared as
All of that was great – on power up the unit would come up with my chosen name – also after reboot – and after disconnecting.
All worked well…one problem apparently solved…
But… from time to time I would lose the output – the lights on the FTDI would show up – but nothing would come back in to Node-Red. Another reader – Christopher had suggested that in some cases you need to short DTR and DCD, also RTS and CTS. if these were floating and if they WERE important – I could see why I’d be getting strange results. I put links on these.
Voila. Reliable serial connection. It would be nice to clarify why the word “serial” and not “iSerial” – and why you need to miss off the “0x” – but it worked reliably.
Though it works perfectly as a serial port in Node-Red – Node-Red cannot see the new port probably because this is just a shortcut or symbol – if you type it in – it worked – but Node-Red could not (and will not) search for it – not too much of a hassle to put in manually.
But – the ARDUINO node for Node-Red would NOT accept the manual input.
By specifying ttyUSB0 I managed to flash a light with this Firmata-based node – but any attempts to change things around, try PWM on a pin etc. would result in Node-Red dying temporarily. I talked to the developer of the Firmata node as I noted the serial node NEVER crashed Node-Red. Result – thank heavens – a bug – now removed.
Reading a reply in the Node-Red Google group, it was suggested for a while NOT to use the Arduino node but to scrap that and use the contrib-gpio node (the yellow one) – as it happened I had that installed. I used my shortcut (may as well go in at the deep end) – and LO – no more Node-Red crashing.
I tested port digital control as well as PWM and they all worked.. BUT – changing mid-stream – i.e. opening the node and changing the port bit FAILED like the Arduino node – but did not crash Node-Red – this let me experiment a LOT more. I killed the Node-Red service and ran it manually. But at this point, making a change to the node and then running deploy would result in the node sitting with “connecting” and just refusing to connect to the serial port. Simply stopping and starting Node-Red fixed the issue every time.
The author was playing with this and could not replicate this – but it turns out he was using Node-Red’s DEPLOY ALL mode – whereas I just deploy all changed nodes (so my house control doesn’t fall to bits when I’m experimenting). I just had a call to say he can now replicate this.
WHICH MEANS the yellow GPIO node also has this issue! I’ll let them know.
So now that it looks like Firmata will work but does not like change.
Dave at IBM who has been working on the Arduino node has now FIXED this issue in Arduino node version 0.09 (npm install node-red-node-arduino). I have tried port 23 to make sure the MEGA version of this is running – and sure enough – it is running.
Lovely. Multiple Serial ports on the Pi – and LOTS of GPIO cheap add on – what more could you want. My thanks to Dave for quick action and to everyone for their helpful comments which brought us this far.
The node-red-contrib-gpio node has exactly the same issue that the Arduino node HAD – I’ve left an issue in the repository of the latter.
Update September 32, 2016: I have to say that all of this is working exceedingly well, EXCEPT for Firmata on the Arduino. I left my main Raspberry Pi talking to the USB serial and sending commands to flash a light on an Arduino MEGA2560 board via Firmata. Simple enough… we’ve had power cuts, I’ve been experimenting with the Pi etc over all this time since I wrote the blog entry and I can see by the FTDI that once-per-second commands are heading out to the Arduino 24-7 – but every now and then, maybe every few days I look up and that light is not flashing. A quick power cycle on the Arduino board brings it back up – but Firmata is simply NOT that reliable.