# Stream data from node-red to c program?

**URL:** <https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266>\
**Category:** General\
**Created:** [18 February 2021 23:05 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266 "2021-02-18T23:05:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [18 February 2021 23:05 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/1 "2021-02-18T23:05:14Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [19 February 2021 03:09 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/2 "2021-02-19T03:09:29Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [19 February 2021 04:55 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/3 "2021-02-19T04:55:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [19 February 2021 07:27 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/4 "2021-02-19T07:27:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [19 February 2021 07:40 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/5 "2021-02-19T07:40:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [19 February 2021 07:45 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/6 "2021-02-19T07:45:04Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [19 February 2021 07:53 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/7 "2021-02-19T07:53:13Z")

</div>

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

> **[Eclipse Paho | The Eclipse Foundation](https://www.eclipse.org/paho/index.php?page=clients%2Fc%2Findex.php)**
>
> The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 375 open source projects, including runtimes, tools and frameworks.

Craig

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 February 2021 09:11 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/8 "2021-02-19T09:11:31Z")

</div>

> [@craigcurtin](#):
>
> Why not just use one of the prebuilt libraries and work around it for MQTT

Now we are [full circle](https://discourse.nodered.org/t/stream-data-from-c-program-to-node-red-dashboard/41213/9) 😃

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [19 February 2021 09:28 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/9 "2021-02-19T09:28:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [19 February 2021 10:17 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/10 "2021-02-19T10:17:23Z")

</div>

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

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![iiot2k](https://avatars.discourse-cdn.com/v4/letter/i/8baadc/32.png) [@iiot2k](https://discourse.nodered.org/u/iiot2k)\
**Post date:** [19 February 2021 10:21 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/11 "2021-02-19T10:21:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [19 February 2021 10:46 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/12 "2021-02-19T10:46:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [19 February 2021 19:29 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/13 "2021-02-19T19:29:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [19 February 2021 22:46 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/14 "2021-02-19T22:46:30Z")

</div>

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 🙂 🙂 🙂

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [20 February 2021 00:21 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/15 "2021-02-20T00:21:53Z")

</div>

> [@a1ex](#):
>
> I got it!!!

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

---

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [20 February 2021 03:46 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/16 "2021-02-20T03:46:30Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [20 February 2021 04:18 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/17 "2021-02-20T04:18:05Z")

</div>

> [@a1ex](#):
>
> things are about to get basic here

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.

---

<div class="post-metadata">

**Author:** ![a1ex](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@a1ex](https://discourse.nodered.org/u/a1ex)\
**Post date:** [20 February 2021 04:29 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/18 "2021-02-20T04:29:08Z")

</div>

This is why no one hires me to do code.

My services are available if you all need help programming 😛

---

<div class="post-metadata">

**Author:** ![sridhar2rok](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@sridhar2rok](https://discourse.nodered.org/u/sridhar2rok)\
**Post date:** [24 February 2021 00:48 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/19 "2021-02-24T00:48:46Z")

</div>

> [@a1ex](#):
>
> c program in node red using the exec node, so any workflow that builds on that would be fine by me...

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 February 2021 08:51 UTC](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266/20 "2021-02-24T08:51:44Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/838f78bb01ac8d3b9a92738b528761c4b8589c57.png)

[Next page](https://discourse.nodered.org/t/stream-data-from-node-red-to-c-program/41266.md?page=2)
