# Creating a node for sending / receiving data via sound card!

**URL:** <https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399>\
**Category:** General\
**Created:** [16 June 2020 11:44 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399 "2020-06-16T11:44:36Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [18 June 2020 12:25 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/21 "2020-06-18T12:25:09Z")

</div>

I played last night with minimodem , but it was receiving some crap 😕 i even connected my microphone and speaker via 3.5mm jack and the outputs are completly rubbish...

---

<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:** [18 June 2020 13:20 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/22 "2020-06-18T13:20:43Z")

</div>

There is also this project - [https://www.anfractuosity.com/projects/ultrasound-via-a-laptop/](https://www.anfractuosity.com/projects/ultrasound-via-a-laptop/)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [18 June 2020 13:35 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/23 "2020-06-18T13:35:42Z")

</div>

Hmm it actually worked really well for me. Did you try a lower baudrate? What was your confidence setting?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0e8a85f2bdef0643e23290a5dbc2b2fb7e989e1.jpeg)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b214e78df7311aea79255af929502cbd83605e0e.jpeg)

The only thing i observed is that you really have to set the right confidence setting or you get data from random noise. When using a speaker and mic you have to watch your baudrate as i wasn’t getting reliable results above 600 baud.  
The screenshots above is from just now when i connected the headphone plug of a pi 3b+ directly to a cheap usb soundcard on a pi4 with a cable. This way i can achieve way higher baudrates. I got reliable results up to 6000 baud just now.  
You really need to tune the confidence setting to filter out random data from noise. And of course double check with `aplay -l` and `arecord -l` your using the right device if your not using pulse audio.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [18 June 2020 13:38 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/24 "2020-06-18T13:38:24Z")

</div>

looks very nice too. I actually think that maybe it would be possible to frequency shift the data that minimodem generates into that range with sox too. The project you linked and minimodem actually both use the same modulation technique.  
Edit cant seem to be able to shift it high enough to get inaudible.

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [23 June 2020 11:21 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/25 "2020-06-23T11:21:32Z")

</div>

Okay, so i finished most of the configuration for the send node via minimodem. But I want to implement a repeat function, and i get a bug, where for example I set repeat times to 2 and delay time to 10000ms, my execfunction gets run 2 times in a row!  
Check out this [stackoverflow post](https://stackoverflow.com/questions/62519955/repeat-a-function-and-wait-for-x-seconds-after-it-is-complete/62520758#62520758), and if someone is interested to help me , please do i believe there is a simple answer behind this issue!

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 June 2020 11:38 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/26 "2020-06-23T11:38:41Z")

</div>

maybe something like this:

```auto
let repeatNumber = 1
const timeout = 10000;

function repeat(time) {
    repeatNumber -= 1;
    setTimeout(()=>{
        execMinimodem();
    },time);
    return;
}
function execMinimodem() {
    exec(minimodem,(error, stdout, stderr) => {
        //handle errors output etc
        if (repeatNumber > 0) { repeat(timeout); }
        return;
    });
}

execMinimodem();

```

just of the top of my head and _ **not tested at all** _, of course needs to be adapted but than should repeat the exec after a timeout that starts when the first finished as many times as stated in the repeatNumber.

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [23 June 2020 11:54 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/27 "2020-06-23T11:54:49Z")

</div>

Will test it during the day and let you know , by far i think that this may work!

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [23 June 2020 17:36 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/28 "2020-06-23T17:36:11Z")

</div>

After integrating the function i still get several minimodem programs executed!  
I double checked that my repeatxtimes and repeatdelay is not mixed, and sure enough repeatxtimes is 2 and repeatdelay is 5000 as in my node settings!

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 June 2020 17:48 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/29 "2020-06-23T17:48:04Z")

</div>

well as i cant look at your full code i dont really have much to go on. My next tip would be my preferred strategy to put node.warn() statements everywhere to see whats going on.

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [23 June 2020 17:52 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/30 "2020-06-23T17:52:05Z")

</div>

I will try to execute a bash command sleep x right after minimodem finished , and i will go from there...

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 June 2020 18:07 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/31 "2020-06-23T18:07:48Z")

</div>

thats not a good way. I just tested this node script:

```auto
const { exec } = require("child_process");

let repeatNumber = 3
const timeout = 3000;

function repeat(time) {
    repeatNumber -= 1;
    setTimeout(()=>{
        execMinimodem();
    },time);
    return;
}
function execMinimodem() {
    exec("ls ~/", (error, stdout, stderr) => {
        if (error) {
            console.log(error.message);
            return;
        }
        if (stderr) {
            console.log(stderr);
            return;
        }
        console.log(stdout);
        if (repeatNumber > 0) { repeat(timeout) }
        return
    });
}

execMinimodem();

```

to see it working save it to somewhere on a raspberry pi as `test.js` or something and than execute it with `node test.js`. Just tested it and it defineitely works executing the `ls` command 3 times 3 seconds apart.

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [23 June 2020 18:16 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/32 "2020-06-23T18:16:37Z")

</div>

Could you analyze my [code](https://pastebin.com/wzgjBzkX) , I am trying your solution, but it is not working as expected!

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [23 June 2020 19:13 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/33 "2020-06-23T19:13:59Z")

</div>

To be honest I haven't properly read the thread but took a glimpse on your code. I noticed you've defined your `executesend` function as `async` while @JGKK's example didn't. I'm not sure if it is the reason why it's not working as expected but it for sure could be.

I think you should describe in what way is your code working unexpectedly. It looks quite involved for someone else to get an idea what could happen when running it.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 June 2020 19:18 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/34 "2020-06-23T19:18:32Z")

</div>

There were several problems. One being the unnecessary asynch function but the bigger one that he was parsing the amount of times to repeat as the time off delay. Which lead to a very small delay which felt like it was repeating instantly:

```auto
repeatdelaytoint = parseInt(repeatxtimes)
repeatxtimestoint = parseInt(repeatxtimes)

```

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [23 June 2020 19:25 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/35 "2020-06-23T19:25:16Z")

</div>

> [@JGKK](#):
>
> There were several problems. One being the unnecessary asynch function but the bigger one that he was parsing the amount of times to repeat as the time off delay. Which lead to a very small delay which felt like it was repeating instantly:
> 
> ```auto
> repeatdelaytoint = parseInt(repeatxtimes)
> 
> ```

I must be tired as I don't understand why it would cause any meaningful delay unless it's repeated in a loop? But I wonder if this was intended to have `parseInt(repeatdelay)`?

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [23 June 2020 19:26 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/36 "2020-06-23T19:26:37Z")

</div>

Let me create a github repository...

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 June 2020 19:33 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/37 "2020-06-23T19:33:35Z")

</div>

There is a repeat function which gets called at the end of the exec if `repeatxtimestoint ` is bigger than 0. The repeat function subtracts one from `repeatxtimestoint ` and than sets a timeout for `repeatdelaytoint ` and calls the exec again at the end of the timeout. This repeats the timeout for whatever many times `repeatxtimestoint ` is set to. As `repeatxtimes ` was by accident used for both values if it was set to repeat once the timeout would also be set to just 1ms. This way it looked like the timeout in the repeat function was not beeing just but instead it looked like it was just executed twice. So just a simple typo i think But maybe this was the main culprit here.

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [23 June 2020 19:49 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/38 "2020-06-23T19:49:40Z")

</div>

Okay after declaring `repeatxtimestoint` and `repeatdelaytoint` as global values I finally managed to pull it off! and i used console.log to see where i was wrong

---

<div class="post-metadata">

**Author:** ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)\
**Post date:** [23 June 2020 19:51 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/39 "2020-06-23T19:51:48Z")

</div>

Just curious a little bit, i see a `repeatdelaytoint` pause before first execute, could I use simple if/else to overcome the first pause?

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [23 June 2020 19:53 UTC](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399/40 "2020-06-23T19:53:21Z")

</div>

> "With enough eyebulbs, all bugs are shallow." -- Eric S. Raymond

[Previous page](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399.md?page=1)

[Next page](https://discourse.nodered.org/t/creating-a-node-for-sending-receiving-data-via-sound-card/28399.md?page=3)
