# How to run C++ program

**URL:** <https://discourse.nodered.org/t/how-to-run-c-program/52571>\
**Category:** General\
**Created:** [22 October 2021 12:04 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571 "2021-10-22T12:04:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [22 October 2021 12:04 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/1 "2021-10-22T12:04:24Z")

</div>

Im not sure what to do here so i thought i would ask.

I have this C++ program installed and working on my Rasp pi3

> **[GitHub - anschoewe/morningstar-sunsaver-mppt](https://github.com/anschoewe/morningstar-sunsaver-mppt)**
>
> Contribute to anschoewe/morningstar-sunsaver-mppt development by creating an account on GitHub.

In CP i can run  
$ sunsaver

# then i input the option i want 1-5 to give me the output i want.

$ 5

My question is how do i configure the exec node to input the number option 1-5?

My second question is, is this the best way to do it the C++ also has a "Save output" command  
$ ./sunsaver \> output/snapshot\_$(date +%Y-%m-%d\_%H-%M-%S)`

# then options 1-5

or  
$ ./sunsaver \> output/logs\_$(date +%Y-%m-%d\_%H-%M-%S)`

# then options 1-5

At the moment i would like to start with reading battery voltage and maybe create a dashboard.  
Any help or ideas gratefully recieved.

---

<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:** [22 October 2021 14:22 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/2 "2021-10-22T14:22:08Z")

</div>

welcome to the forum @picklerick

After entering 5 does the program close or does it go back to waiting for input?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [22 October 2021 14:51 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/3 "2021-10-22T14:51:30Z")

</div>

Hi @picklerick.

> [@picklerick](#):
>
> The C++ also has a "Save output" command

This is not really a feature of the C program, it justs prints it's output to the "standard output" device. the `> .node-red/node_modules/node-red-dashboard/dist/js` is using the Bash shell to redirect the output into a file.  
This is a good thing for you, the program output will magically appear in msg.payload after the exec node.

It is possible that the program is designed to accept the option 1-5 from the command line, eg  
**sunsaver 5**.

If not, the simplest solution (unless someone else knows how to do a "Here Document" from exec) is to write a script "callsunsaver" to call the program, something like this

```auto
#! /bin/bash
/full/path/to/sunsaver <<EOF
$1
EOF

```

Make callsunsaver executable **chmod +x callsunsaver**

Now your exec node would be something like this

```auto
[
    {
        "id": "3f1453d2073c0cbe",
        "type": "inject",
        "z": "03f8230b250f8d2a",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "2",
        "payloadType": "str",
        "x": 290,
        "y": 140,
        "wires": [
            [
                "772114dbc4e4519d"
            ]
        ]
    },
    {
        "id": "772114dbc4e4519d",
        "type": "exec",
        "z": "03f8230b250f8d2a",
        "command": "/home/pi/bin/callsunsaver",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "",
        "x": 510,
        "y": 160,
        "wires": [
            [
                "98678fcdfeebb11d"
            ],
            [],
            []
        ]
    },
    {
        "id": "98678fcdfeebb11d",
        "type": "debug",
        "z": "03f8230b250f8d2a",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 750,
        "y": 160,
        "wires": []
    },
    {
        "id": "efdb7dfcb07f689a",
        "type": "inject",
        "z": "03f8230b250f8d2a",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "5",
        "payloadType": "str",
        "x": 290,
        "y": 180,
        "wires": [
            [
                "772114dbc4e4519d"
            ]
        ]
    }
]

```

As @Colin hints, if sunsaver waits for more input it's going to be a problem.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [22 October 2021 16:39 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/4 "2021-10-22T16:39:11Z")

</div>

sunsaver runs the program then after entering a number 1 to 5 it returns the data chosen then closes.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [22 October 2021 17:44 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/5 "2021-10-22T17:44:35Z")

</div>

Thats great thank you it worked a treat,

---

<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:** [22 October 2021 21:01 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/6 "2021-10-22T21:01:01Z")

</div>

I think you should also be able to do, in the exec node  
`echo "5" | /full/path/to/sunsaver`  
Try it in a terminal first.

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [23 October 2021 06:40 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/7 "2021-10-23T06:40:54Z")

</div>

Yes that works from terminal, so to run in node-red i would then input that code into the exec node?  
thank you for your help,

---

<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:** [23 October 2021 07:08 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/8 "2021-10-23T07:08:25Z")

</div>

Yes, doesn't it work?

---

<div class="post-metadata">

**Author:** ![picklerick](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/picklerick/32/49793_2.png) [@picklerick](https://discourse.nodered.org/u/picklerick)\
**Post date:** [23 October 2021 11:49 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/9 "2021-10-23T11:49:48Z")

</div>

yes that works, thank you,

now i got to do something with this data.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 December 2021 11:49 UTC](https://discourse.nodered.org/t/how-to-run-c-program/52571/10 "2021-12-22T11:49:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
