# Node-RED MCU - unhandled exceptions causing endless loop?

**URL:** https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044
**Category:** General
**Tags:** node-red-mcu
**Created:** [21 November 2023 00:11 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044 "2023-11-21T00:11:33Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [21 November 2023 00:11 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/1 "2023-11-21T00:11:33Z")

</div>

Not sure if this is an issue inside `moddable` or `node-red-mcu`, but anytime the generated node-red flows running on my ESP-32 hit an error, the whole unit restarts... and fails again.

In my particular flow, I just added a `node-red-node-serial-in` node, which causes this stack trace in xsbug-log:

```
#xsbug-log connected to "main"
Wi-Fi connected to "S&J-Poolhouse"
IP address 192.168.1.107
# Break: importNow: module "events" not found!
  #0: importNow
  #1: require /home/srickus/develop/node-red-mcu-plugin/mcu_modules/require/require.js:5
  #2: (anonymous-522) /home/srickus/.node-red/node_modules/node-red-node-serialport/25-serial.js:5
  #3: forEach
  #4: RED.build /home/srickus/develop/node-red-mcu-plugin/node-red-mcu/nodered.js:156
  #5: (anonymous-243) /home/srickus/.node-red/mcu-plugin-cache/sqd9j8rnmna/main.js:20
  #6: (host)
XS abort: unhandled exception

```

I know the support for the `node-red-node-serial` collection is [still wet behind the ears](https://discourse.nodered.org/t/node-red-mcu-plugin-v1-0-integrating-node-red-mcu-edition-into-the-node-red-editor/72693/201), so I'm not totally surprised it did not work out of the gate. Any idea when we can read/write serial data through the gpio Tx/Rx pins? The bigger question is whether this is the right node to use, I suppose. :\*(

But this topic is more about error handling when something like this goes wrong on the MCU code -- right now my mcu is reconnecting to the wifi network like every 5 seconds! As soon as the connection is established, there is this unhandled expection -- perhaps the generated code should catch all of these and just stop? Or at least impose a logarithmically increasing delay before trying again.

Seems like the closer I get to getting my MCU device deployed, the more obscure the errors -- but this is all very exciting, especially for those of us who only get to write code in the "bits-n-bytes" world, not in the world of "events-n-atoms".

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [21 November 2023 06:46 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/2 "2023-11-21T06:46:52Z")

</div>

> [@shrickus](#):
>
> I know the support for the `node-red-node-serial` collection is [still wet behind the ears](https://discourse.nodered.org/t/node-red-mcu-plugin-v1-0-integrating-node-red-mcu-edition-into-the-node-red-editor/72693/201), so I'm not totally surprised it did not work out of the gate.

Well - it's not _wet behind the ears_, it's just still _work in progress_. 😉 No way the standard node will work out of the box...

> [@shrickus](#):
>
> Any idea when we can read/write serial data through the gpio Tx/Rx pins?

You could make yourself familiar with the `Serial` interface in `Moddable` and use this via a `function` node. Not that elegant - but do-able.

> [@shrickus](#):
>
> As soon as the connection is established, there is this unhandled expection -- perhaps the generated code should catch all of these and just stop?

That's not the nature of MCUs. When they hit a (severe) error, they reset and retry. For development purposes, `xsbug` may support you - as it is supposed to halt the MCU at the exception!

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [21 November 2023 22:16 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/3 "2023-11-21T22:16:29Z")

</div>

> [@ralphwetzel](#):
>
> No way the standard node will work out of the box

Yeah, I saw that @phoddie suggested the `serial in` and `serial out` nodes could be ported to use the moddable Serial library. I could maybe help with that if I knew where/how to even start. If that hill is too steep, I'd be happy to test someone else's commits!

> [@ralphwetzel](#):
>
> the `Serial` interface in `Moddable` and use this via a `function` node

So does this mean that the `function` node code is passed to the mcu as-is? I see in the docs that the `function` node is supported (except for async onStart functions). So do i just need to `npm install` the moddable Serial library, import it into my function, and develop code to read/write the tx/rx pins? I assume I'll need to plug an rs-485 ttl -\> uart board into those mcu pins, to handle the serialization of the data?

> [@ralphwetzel](#):
>
> For development purposes, `xsbug` may support you - as it is supposed to halt the MCU at the exception

Ok, good thing to know... I'm currently using xsbug-log to see the errors on the console. Let me try running the full debugger on my RPi desktop.

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [21 November 2023 22:40 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/4 "2023-11-21T22:40:28Z")

</div>

> [@shrickus](#):
>
> If that hill is too steep, I'd be happy to test someone else's commits!

I'm already down that road! 😉 I've spent the last hour to put together a simple (= non optimized) implementation of a serial node. I'd need another day to find a way to test this a bit.

> [@shrickus](#):
>
> So does this mean that the `function` node code is passed to the mcu as-is?

It is.

> [@shrickus](#):
>
> So do i just need to `npm install` the moddable Serial library, import it into my function, and develop code to read/write the tx/rx pins?

No need to install anything! It's much easier: The host is preconfigured with interface modules for its hardware capabilities ([example](https://github.com/Moddable-OpenSource/moddable/blob/public/build/devices/esp32/targets/m5atom_lite/host/provider.js)), put in a global object called `devices`. You just needed to initialize your port and are ready to go ([example](https://github.com/Moddable-OpenSource/moddable/blob/public/examples/io/serial/echo/main.js)).

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [22 November 2023 16:40 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/5 "2023-11-22T16:40:55Z")

</div>

> [@ralphwetzel](#):
>
> I'd need another day to find a way to test this a bit.

Quick update on that: I've stumbled over an [issue](https://github.com/Moddable-OpenSource/moddable/issues/1226#issuecomment-1823051169) with the `serial` driver in Moddable SDK. We need to investigate what's happening there first, before it can be used for a node.  
Sorry for the delay...

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [23 November 2023 15:06 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/6 "2023-11-23T15:06:35Z")

</div>

@shrickus  
As advertised, I've created an implementation of `node-red-node-serialport` for `node-red-mcu`.  
As it's a shim, it's going to be used as soon as you build a flow with `serial in` or `serial out` node for a MCU.  
`Serial request` node is not supported.  
For further details, you should refer to the [short documentation](https://github.com/ralphwetzel/node-red-mcu-plugin/tree/main/mcu_modules/node-red-node-serialport).  
As this is part of `node-red-mcu-plugin`, you need to install / update this currently from the GitHub repo. After some time for testing (Your feedback is welcome!) I'll create a new release...

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [28 November 2023 14:00 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/7 "2023-11-28T14:00:08Z")

</div>

Hi Ralph,  
I finally had some time to update and test the `serial in/out` nodes -- thanks for getting that testable in such short order.

I have not yet been successful reading any data from the TTL to RS-485 module connected to my ESP-32 module, but I suspect it's due to my lack of understanding, or just user error. Plus it is not easy to debug since it is running disconnected from the RPi-4 USB (only connected to MQTT broker via Wifi). Do you have any tricks for remote debugging in this case?

I did want to verify that I'm choosing the right configuration for the `serial in` node... as you suggested in the docs, I created a new `serial-port` node using Port/Rx/Tx values of `/P2/R33/T19`:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/24509ce82a91827e12b272879e305aa5ceb69c9b.png)

It seems the ESP-32 pin numbers don't necessarily match the GPIO numbers -- so which ones should I be using here? And does it matter that the generated "name" of the Serial port  
contains the trailing characters `:19200-8N2`?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/77505ee097ee26d50a4e67f581b99d8dffd584b4.png)

The code appears to ignore that stuff (and I'm not interested in Transmitting right now). I'm mostly wondering if Rx on pin #33 is referring to GPIO-33 or the pin in slot #33.

---

<div class="post-metadata">

### Author: ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)
#### Post date: [28 November 2023 19:23 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/8 "2023-11-28T19:23:10Z")

</div>

_Pin_ refers to the GPIO _pin_-number according to the hardware definition of your board.  
I've chosen 2 _pins_ that were available on my dev board - completely random. The only criteria: They were accessible easy and sitting next to each other.

When wiring those _pins_, keep in mind, that what the MCU understands as TX is RX for your Serial Port monitor.

Until you've successfully crated a first running flow, I'd try to eliminate any complexity. This is my "development setup": An MCU & a `SH-U07A` TTL to USB adapter (as Serial Port monitor), both plugged into the same laptop.

 ![serial](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65823542df5f59cf2d2cb0a993864bb42f7b8ba7.jpeg)

The laptop runs Node-RED with the `node-red-mcu-plugin` and a serial port terminal application that allows me to monitor the data & loop it back to the MCU. If there's an issue, it has to be within this square feet on my table! 😉 That said, it's not clear for me where you're trying to capture the serial data with your setup. A simple sketch would help me a lot...

Here's an easy flow to test your setup. The only thing you need to change is the pin setup. And no - the trailing characters don't matter.

```auto
[
    {
        "id": "8c8d2482a20754c2",
        "type": "tab",
        "label": "Serial Port Test",
        "disabled": false,
        "info": "",
        "env": [],
        "_mcu": {
            "mcu": true
        }
    },
    {
        "id": "a2a935c2741add0a",
        "type": "serial in",
        "z": "8c8d2482a20754c2",
        "name": "",
        "serial": "631a61ee97b5a76a",
        "_mcu": {
            "mcu": true
        },
        "x": 470,
        "y": 240,
        "wires": [
            [
                "e236523df59a71e6"
            ]
        ]
    },
    {
        "id": "1f8d02cf73ff87a3",
        "type": "serial out",
        "z": "8c8d2482a20754c2",
        "name": "",
        "serial": "631a61ee97b5a76a",
        "_mcu": {
            "mcu": true
        },
        "x": 470,
        "y": 180,
        "wires": []
    },
    {
        "id": "e236523df59a71e6",
        "type": "debug",
        "z": "8c8d2482a20754c2",
        "name": "debug in",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "payload",
        "statusType": "msg",
        "_mcu": {
            "mcu": true
        },
        "x": 640,
        "y": 240,
        "wires": []
    },
    {
        "id": "a84f667a636fbc57",
        "type": "debug",
        "z": "8c8d2482a20754c2",
        "name": "debug out",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "_mcu": {
            "mcu": true
        },
        "x": 490,
        "y": 120,
        "wires": []
    },
    {
        "id": "4c39157de56783ee",
        "type": "inject",
        "z": "8c8d2482a20754c2",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "_mcu": {
            "mcu": true
        },
        "x": 260,
        "y": 120,
        "wires": [
            [
                "a84f667a636fbc57",
                "1f8d02cf73ff87a3"
            ]
        ]
    },
    {
        "id": "631a61ee97b5a76a",
        "type": "serial-port",
        "serialport": "/P2/R33/T19",
        "serialbaud": "115200",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "1000",
        "bin": "false",
        "out": "interbyte",
        "addchar": "",
        "responsetimeout": "10000",
        "_mcu": {
            "mcu": false
        }
    }
]

```

---

<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: [12 December 2023 19:23 UTC](https://discourse.nodered.org/t/node-red-mcu-unhandled-exceptions-causing-endless-loop/83044/9 "2023-12-12T19:23:10Z")

</div>

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