Stream data from node-red to c program?

Thanks to all who helped with the reverse question.

I’d now like to stream data from node-red to a c program for use as input in computations.

I’ll keep it simple. All I want is to inject a timestamp, stream it live to someplace I can access in my c program, then multiply it by 2 in c and stream it back to node-red.

I haven’t managed to establish tcpip or mqtt connections between node red and my c program yet, but I’ve managed to run a c program in node red using the exec node, so any workflow that builds on that would be fine by me...

Beep boop beep. Beep. Eep. Eep. P.

Echoes. Echoes. Choes. Hoes. Oes. Es. S.

I dont think that can be solved as easy. Now you need the c to listen so you are back at square #1. TCP or MQTT...two way implementation in c seems the best for your needs

Is this thing a hobby stuff or @work? If a business task you have been given, why not hire a c programmer as consultant?

It's both hobby and work, like a quantum superposition of states, but one that has me both excited and miserable all at once. Mostly miserable now that I'm doing C.

To start simple, I wonder if I can write from node-red to a file and read that file in C as it gets updated... then do my calculations in C and pass the results in to node-red... which would come for free if I'm running my C program using an exec node?

Or maybe I could output from node-red to stdin and use stdin as input in my C program, do my calculations, and pass the results to node-red (again with C running in an exec node)?

This obviously wouldn't work if node-red and my C program are on different machines, but as a simple start, maybe it could work? It would be nice to at least start transmitting data between my C simulation and my node-red dashboard somehow, even down the line I'll need a better approach...

Why not just use one of the prebuilt libraries and work around it for MQTT ?

Craig

Now we are full circle :smiley:

The node-red-node-daemon will help you here. It is like the exec node in that it runs your external program, but hooks the input to your stdin, so if your app listens to stdin it can get what you send it

You beat me to it Dave. I wanted to get a working example before I posted. So here it is, better late than never.

[{"id":"a43a81bd.755b2","type":"inject","z":"8b280028.0371c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hello","payloadType":"str","x":150,"y":260,"wires":[["f865957b.bfb208"]]},{"id":"f865957b.bfb208","type":"daemon","z":"8b280028.0371c8","name":"","command":"tr","args":"-u [a-z] [A-Z]","autorun":true,"cr":false,"redo":false,"op":"string","closer":"SIGINT","x":290,"y":300,"wires":[["c933a27f.227e08"],["a1715b0f.51cf5"],["a0abd4c8.48de08"]]},{"id":"802af5b2.0e3a5","type":"inject","z":"8b280028.0371c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"world","payloadType":"str","x":150,"y":300,"wires":[["f865957b.bfb208"]]},{"id":"c933a27f.227e08","type":"debug","z":"8b280028.0371c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":450,"y":260,"wires":[]},{"id":"a1715b0f.51cf5","type":"debug","z":"8b280028.0371c8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":430,"y":300,"wires":[]},{"id":"a0abd4c8.48de08","type":"debug","z":"8b280028.0371c8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":430,"y":340,"wires":[]},{"id":"50296acf.4883e4","type":"inject","z":"8b280028.0371c8","name":"stop","props":[{"p":"kill","v":"SIGHUP","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":340,"wires":[["f865957b.bfb208"]]}]
1 Like

A simple question:
If the c program runs on same machine, why do you not use
N-API or NaN ?

I think the long term intention is not to run on the same machine.

Thank you all!

@dceejay is right---ultimately the c program will run on a separate machine.

I tried the daemon node but with no success. I'm on Windows so sadly @drmibell's flow doesn't work.

I tried a simple flow with an injector feeding a daemon node running a c program, and while the program runs, it completely ignores my input (which shows up fine in a debug node upstream of the daemon node).

The c program uses scanf to get stdin as input to a calculation before printing the calculation to stdout.

Not sure why the injector input would be ignored?

I got it!!!

It took a lot of fiddling, but I built a closed feedback loop... with an Arduino In node serving input data... to a Daemon node running a C program calculating things from that data... to drive a dashboard indicator light...

And it all works!!!

Whew...

Next up will be using MQTT to stream data between devices, but this setup is good enough to get me started with my dashboard and C program... before I get to the MQTT side of things...

Thank you all for your input :slight_smile: :slight_smile: :slight_smile:

2 Likes

That’s great. Did you learn anything that might be useful to the group about why your C program wasn’t getting input?

Ah! OK, brace yourselves, things are about to get basic here.

I originally set the injector node to pass a plain number. That's when things didn't work.

But then I realized that if I'd entered that number at the command prompt in Windows, I'd have to press Enter to submit that number.

My injector node was clearly not doing that, so I tried appending \n to the number I'd specified there. It worked!

I did have to specify the whole thing as a string.

Later on, when I decided to put together a simple flow to model a counter, I had to move the \n part to a function block, and there things broke again until I replaced \n with \\n so that the debug output was exactly equal to my original injector output. The double slash was important there.

And finally, when my counter was working, I replaced that with the input from an Arduino-In node. And because my Arduino code already appended \n to the number transmitted, I no longer needed any of that on the node-red side.

So it became Arduino-In node directly to Daemon node to feed and execute my C code. The results of the C calculations in turn fed a function node with a conditional if statement to set the color of an indicator light I'd modeled with a ui_svg_graphics node.

Ta-da! Arduino to node-red to C back to node-red! Yes, I realize fully how painfully basic all this is. But it took a lot of trying on my part, to the point I wondered if I'd ever pull it off, so...

I'll be ten times more excited once I do this with MQTT because then it will be proper. Right now, it's enough to just have a path forward :smiley:

1 Like

Thanks. That helps -- scanf can be fussy about formats, and you clearly need the \n. You will probably find mqtt very friendly once you get the hang of it.

This is why no one hires me to do code.

My services are available if you all need help programming :stuck_out_tongue:

@a1ex how did you manage to run a c program in node red using the exec cmd ? can you write it for me ?

Welcome to the forum @sridhar2rok.
It is very easy to run a c program in an exec node. All you need to do is to specify the command you want to run. For example if you have a program called myprog that you have built to a file /path/to/file/myprog (where /path/to/file is the path to wherever you have put it) then configure the exec node like this.