# Raspberry Pi to monitor serial output of a node or TNN Gateway, and alert on Slack or Telegram

**URL:** <https://www.thethingsnetwork.org/forum/t/raspberry-pi-to-monitor-serial-output-of-a-node-or-tnn-gateway-and-alert-on-slack-or-telegram/13030>\
**Category:** TTKG - The Things Kickstarter Gateway\
**Created:** [March 4, 2018, 10:36pm UTC](https://www.thethingsnetwork.org/forum/t/raspberry-pi-to-monitor-serial-output-of-a-node-or-tnn-gateway-and-alert-on-slack-or-telegram/13030 "2018-03-04T22:36:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [March 4, 2018, 10:36pm UTC](https://www.thethingsnetwork.org/forum/t/raspberry-pi-to-monitor-serial-output-of-a-node-or-tnn-gateway-and-alert-on-slack-or-telegram/13030/1 "2018-03-04T22:36:30Z")

</div>

While trying to create log files for non-frequent network/firmware problems for the TTN Gateway, I ended up with my laptop connected to the gateway 24/7 since February 14th. So, I was kind of happy it finally failed and needed a reboot last Friday. 😉

Rather than hooking up my laptop again, I’m now using a Raspberry Pi 3 to do the monitoring, prefix proper timestamps to the output, and as a bonus send notifications to a (private) Slack workspace and/or Telegram chat to know when to act.

The full thing (using Node.js) with instructions [is on GitHub](https://github.com/avbentem/raspi-uart-monitor), but the main points regarding the UART:

- One needs to connect the gateway’s `RX` to the Pi’s `RXD`. Earlier, with an FTDI cable I also noticed that an ESP8266 uses different labeling, so yes, I guess the `TX` and `RX` pins on the gateway are labeled wrong.

- Raspberry Pis that use Bluetooth will have their preferred UART hooked up to that Bluetooth. You might want to disable Bluetooth which will automatically make that best UART be mapped to the Pi’s `RXD` and `TXD` pins; [see the README](https://github.com/avbentem/raspi-uart-monitor/blob/master/README.md#setting-up-the-raspberry-pis-uart).

- An UART is not line-based but will receive series of bytes from which, for the TTN Gateway, one can assume that each message ends with a newline. So, to get a message, the JavaScript needs to await such newline(s):

And yes, it’s a bit weird to (semi-)permanently connect a Pi to the TTN Gateway… But this also allows me to remotely access the logs, which are stored on the Pi. And I could even grant the TTN crew access if they want to experiment; I just need a relay then to remotely power cycle the gateway whenever needed. 😉 One could also easily parse the incoming messages to, say, create statistics. And when [building one’s own firmware](https://www.thethingsnetwork.org/forum/t/building-the-ttn-gateway-firmware-from-the-github-code/12739) one could also log more details about the packets that are not yours, and then get even more details using [https://github.com/anthonykirby/lora-packet](https://github.com/anthonykirby/lora-packet)

![image](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/f/fa8650e26e96a71869fb369961aa564111b5d92b.jpg)

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [June 1, 2018, 2:10pm UTC](https://www.thethingsnetwork.org/forum/t/raspberry-pi-to-monitor-serial-output-of-a-node-or-tnn-gateway-and-alert-on-slack-or-telegram/13030/2 "2018-06-01T14:10:43Z")

</div>

To see how often I get MQTT errors, and above all to be alerted when my LoRa card simply stops working and needs manual intervention ([bug #29](https://github.com/TheThingsProducts/gateway/issues/29)), I’ve meanwhile extended [the monitor](https://github.com/avbentem/raspi-uart-monitor) with support for specific watchdogs, and simple reports.

Like the following shows that the gateway was working just fine, until it rebooted for a firmware upgrade check, after which the LoRa card failed to receive any LoRa packets. A watchdog that expects at least one LoRa packet every 15 minutes (even one with a bad CRC would satisfy that watchdog), and another expecting one accepted uplink (forwarded using MQTT) at least once every 30 minutes, nicely detect that.

When configuring Slack to get both warnings and errors:

![image](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/a/aa2803b8deec58463a1686ff59b7931f6818a9de.png)

And when configuring Telegram to only see the errors (where the firmware upgrade has been configured as an error too, just to know Telegram is getting its messages…):

![image](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/b/b171e6d6d5550ac7a5487f44c050fc9cffb36441.png)

Alerts can be configured to be repeated at some interval until finally fixed, and will report that:

![image](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/3/399dcfe32413c69f3bf187614f3cdac541fc3438.png)

---

<div class="post-metadata">

**Author:** ![jurrien](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/jurrien/32/25844_2.png) [@jurrien](https://www.thethingsnetwork.org/forum/u/jurrien)\
**Post date:** [June 7, 2018, 4:58pm UTC](https://www.thethingsnetwork.org/forum/t/raspberry-pi-to-monitor-serial-output-of-a-node-or-tnn-gateway-and-alert-on-slack-or-telegram/13030/3 "2018-06-07T16:58:29Z")

</div>

Thanks for sharing the alternative solution! Because I don’t use PlatformIO and wasn’t sure about the 3,3 Volt limit on the UART of a RaspberryPi, I modified your solution slightly to use a CP2102 USB to UART Bridge dongle connected to one of the USB ports

```
#!/bin/bash

LOG="serial-`date +'%Y%m%d-%H%M%S'`.log"
USB2UART=/dev/ttyUSB0

stty -F $USB2UART raw 115200 
cat $USB2UART | while read l; do echo "[`date +'%F %T'`] $l" | tee -a $LOG; done
```
