I’ve done a couple of posts on using Apache Camel as part of my home-monitoring system, and why I think it’s a good fit as an IoT switchboard. However, one of the flaws in my master plan is that there isn’t a component for serial communications (Camel MINA does socket comms, but not serial). Note I’m not talking actual RS232 here but the version that comes in USB form. I’m using mostly JeeNodes, which talk RF12 between them, but even in this modern era, without a wi-fi shield or some similar way to get onto the LAN and TCP/IP, then at some point, you’re talking to a serial port on some box into which, in my case a controlling JeeLink, is plugged.
Now, I’ve got around this like I guess most people have, by creating a serial-tcp/ip bridge in code, and getting on with our lives. But, it isn’t, well elegant and it bugs the engineer in me. It would be nice to be able to link directly to Camel and use the considerable advantages it brings, like throttling and queues and all the other goodies. But sadly, my Java isn’t up to creating modules, and it’s a fairly niche use-case. So, up to now I’d pretty much given up on it.
What I hadn’t realised, was that the Stream component, not only lets you talk to the standard input and output streams but also files and urls. Reading further, Stream has support for reading a file continually, as if you were doing ‘tail -f’ on it. Now in Unix, famously “everything is a file”, so in theory, you should be able to read a serial device, like it was a file and if you could Stream should be able to as well. Cue ‘Road to Damascas’ moment.
For my test I quickly grabbed a JeeNode and plugged it into my MacBook. Then I wrote a short sketch that spat out a clock to the serial port:
#include <Stdio.h> #include <Time.h> char buffer[9] =""; void setup() { Serial.begin(9600); // Tried 19200 but didn't work! setTime(0,0,0,0,0,0); // reset starting time } void loop() { digitalClockDisplay(); delay(5000); } void digitalClockDisplay(){ //Send a whole line at a time sprintf(buffer, "%02d:%02d:%02d",hour(),minute(),second()); Serial.println(buffer); } }
I could see it running in the Serial Monitor in Arduino, and I could see the device in /dev, but nothing if I tried “tail -f /dev/cu.usbserial-A900acSz”
Turns out, the default baud rate is 9600 on Macs and also that you can’t alter them using Unix stty commands either! After reading a whole lot of internet, it seemed that, at least for my demo, I’d just have to stick to 9600 or get pulled into a whole pile of Mac-mess. Also note, at least on a Mac I got /dev/cu.* and /dev/tty.*. Only the cu.* ones seemed to work. I think this is something to do with the tty.* ones expecting handshaking, but I’m happy to be corrected.
Once I’d altered the baud-rate every thing worked fine. I could tail -f onto my JeeNode device and it would happily spit out stuff like this:
00:00:2000:00:25
00:00:30
00:00:35
On the Camel side, I made a copy of camel-example-console from the /examples folder and modified the camel-context.xml to hold my new routes. First a route to read from my JeeNode – nothing fancy, just get the data and put it in a SEDA queue as a string. Then a route to get it off the queue and send it to the screen via stdout:
<route> <from uri="stream:file?fileName=/dev/cu.usbserial-A900acSz&scanStream=true&scanStreamDelay=1000"/> <convertBodyTo type="java.lang.String"/> <to uri="seda:myfeed"/> </route>
<route> <from uri="seda:myfeed"/> <to uri="stream:out"/> </route>
Note: scanStream tells it to scan continuously (to get the tail -f effect) and the scanStreamDelay tells it to wait a second between scans.
A quick mvn clean compile exec:java later and it works! So, it seems that I can have my cake after all.
Now, there are caveats. For a start Camel gets pretty upset if you unplug the JeeNode and the device disappears. Also, it seems I’m stuck with 9600, at least on my Mac. But it does work and means a complete Camel solution is possible. Time for more experiments, but at least for the moment my inner engineer is quiet.
PS. It works the other way as well 🙂
<route> <from uri="stream:in?promptMessage=Enter something: "/> <transform> <simple>${body.toUpperCase()}</simple> </transform> <to uri="stream:file?fileName=/dev/tty.usbserial-A900acSz"/> </route>