Skip to content

Instantly share code, notes, and snippets.

@traumverloren
Last active October 26, 2021 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save traumverloren/fd6d0ce11e052d9599e08c3e26dcb43d to your computer and use it in GitHub Desktop.
Save traumverloren/fd6d0ce11e052d9599e08c3e26dcb43d to your computer and use it in GitHub Desktop.
get serial ports on rpi3 working

Get file off RPI:

scp pi@rpi3.local:~/tfjs-yolo-tiny/demo/test.jpg ~stephanie/Desktop

Serial Ports

Disable console on serial ports:

  1. sudo raspi-config
  2. Select option 5, Interfacing options, then option P6, Serial, and select No. Exit raspi-config.

Ensure cmdline.txt config:

  1. sudo nano /boot/cmdline.txt
  2. Make sure it doesn't include: console=ttyAMA0,115200 (fyi, this is ok: console=tty1)

Update config.txt:

  1. Add enable_uart=1. This assigns the mini UART (serial1) to the Bluetooth module, since we are using serial0 now as our main serial for the GPIO pins.
  2. Disable BT to be safe: dtoverlay=pi3-disable-bt

Check state of pins 15 & 16 of GPIO:

Ensure UART pins 15,16 are in state ALT0.

  1. Install wiringpi: sudo apt-get install wiringpi
  2. gpio readall
  3. Change to ALT0 if they aren't:
gpio mode 15 ALT0
gpio mode 16 ALT0

Reboot and test it:

  1. sudo reboot
  2. sudo minicom -D /dev/serial0 (should be able to type in console)
  3. Run python test_uart.py
#!/usr/bin/env python
# test_uart.py
# Will also work on Python3.
# Serial port loopback testing for a RaspberryPi.

from __future__ import print_function
import serial

test_string = "Testing 1 2 3 4".encode('utf-8')
#test_string = b"Testing 1 2 3 4" ### Will also work

port_list = ["/dev/serial0", "/dev/ttyAMA0", "/dev/serial1", "/dev/ttyS0"]

for port in port_list:

    try:
        serialPort = serial.Serial(port, 115200, timeout = 2)
        serialPort.flushInput()
        serialPort.flushOutput()
        print("Opened port", port, "for testing:")
        bytes_sent = serialPort.write(test_string)
        print ("Sent", bytes_sent, "bytes")
        loopback = serialPort.read(bytes_sent)
        if loopback == test_string:
            print ("Received", len(loopback), "valid bytes, Serial port", port, "working \n")
        else:
            print ("Received incorrect data", loopback, "over Serial port", port, "loopback\n")
        serialPort.close()
    except IOError:
        print ("Failed at", port, "\n")

Update rc.local if gpio 15 & 16 not staying in ALT0 mode:

  1. sudo nano /etc/rc.local
  2. Add before exit 0:
gpio mode 15 ALT0
gpio mode 16 ALT0

Resources:

https://www.raspberrypi.org/documentation/configuration/uart.md https://blainebooher.com/jekyll/update/2018/05/15/pi3-btle.html

NodeJS mem mgmt

  1. Increase swap size to 1024mb (https://wpitchoune.net/tricks/raspberry_pi3_increase_swap_size.html)

  2. node --max-old-space-size=500 index.js <--- rpi has 1gb ram, this will tell node that GC needs to happen sooner around 500mb (https://stackoverflow.com/questions/40214669/raspberry-pi-3-script-ram-usage)

Or to remove SWAP completely (https://medium.com/@meeDamian/bitcoin-full-node-on-rbp3-revised-88bb7c8ef1d1):

Turn off PM2:

  • pm2 unstartup
    [PM2] Init System found: systemd
    [PM2] To setup the Startup Script, copy/paste the following command:
    sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 unstartup systemd -u pi --hp /home/pi
    

BACKUP RPI SD CARD:

(https://appcodelabs.com/how-to-backup-clone-a-raspberry-pi-sd-card-on-macos-the-easy-way)

  • diskutil list should be /dev/disk2
  • make a folder:
    cd ~
    mkdir rpi_backups
    cd rpi_backups
    
  • copy from sd card to mac: sudo dd if=/dev/rdisk2 of=rpi_yolo_nodejs_backup_jul_6_2019.dmg
  • format new SD card using SDformatter application on Mac
  • check new SD card: diskutil list
  • Then unmount the SD card: diskutil unmountDisk disk2
  • Then copy backup file to SD card: sudo dd if=rpi_yolo_nodejs_backup_jul_6_2019.dmg of=/dev/rdisk2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment