This site is the personal blog and project repository of George Farris.

If you are looking for the old Cowichan Valley Linux Users Group, check the side navigation. This site contains links to various projects that I either created or contribute to.

You will also find my Github page and Youtube channel here. Contact: cwg@cowlug.org


Latest News

PR-3000-FS-N01 Wind speed transmitter

Posted on: November 8, 2021, in AllPythonRaspberry Pi

This describes getting a FS-N01 anemometer working with some python 3 code on a Raspberry PI with a Waveshare 2 channel RS485 hat.

FS-N01 Wind Speed Transmitter
Waveshare 2 Channel RS485 HAT

The anemometer has Chinese markings on it for V+, V-, 485A and 485B.  Obviously the V+ and V- are for power supply and I used 12VDC.  The manual I found online states 10-30VDC.

The RS485 lines are marked with a Chinese symbol for their colour.  My manual stated Yellow and blue and yet the device I have was Green and Blue.  Just lookup the corresponding Chinese character and get the translation for the colour if you don’t speak the language.  Mine was:

 
V+ – Brown – 棕
V- – Black – 黑
485A – Green – 绿
485B – Blue – 绿

The Waveshare 2 channel RS485 hat required an edit to the /boot/config.txt file of the raspberry pi. Add the following line and then reboot.

dtoverlay=sc16is752-spi1,int_pin=24

You should now see 2 devices in /dev:

/dev/ttySC0 and /dev/ttySC1, these of course are your serial ports.

 

Copy the python3 code below and your wind speed should be displayed in Kilometers per hour when you run it.

 

 

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# Copyright (C) 2021  George Farris <farrisg@gmsys.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Anemometer.py is reference code to read a PR-3000-FS-N01, RS465 Anemometer
# on a raspberry pi 4 using a Waveshare 2 channel RS485 HAT.
# https://www.waveshare.com/2-ch-rs485-hat.htm

import serial
import os
import sys
import time
import RPi.GPIO as GPIO

SERIAL_PORT = "/dev/ttySC0"
TXDEN_1 = 27
TXDEN_2 = 22

# Calculate CRC16 Checksum
def _crc16(data, no):
    crc = 0xffff
    poly = 0xa001   # Polynomial used for Modbus RS485 applications
    temp = no

    while True:
        crc ^= data[temp - no]        
        for i in range(0, 8):
            if crc & 0x0001:
                crc = (crc>>1) ^ poly
            else:
                crc >>= 1
        no -= 1;
        if no == 0:
            break

    return crc & 0xffff

# Open the serial port at 4800 buad
ser = serial.Serial(port=SERIAL_PORT, baudrate=4800)

# Set the mode on the Raspberry Pi GPIO
GPIO.setmode(GPIO.BCM)
# Set this to avoid warning of channel in use
GPIO.setwarnings(False)

# Set the GPIO pin as output for port 1 and 2 on the WAVESHARE RS485 HAT (SC16IS752)
# and then set the bit HIGH to enable the ports
GPIO.setup(TXDEN_1, GPIO.OUT)
GPIO.setup(TXDEN_2, GPIO.OUT)
GPIO.output(TXDEN_1, GPIO.HIGH)
GPIO.output(TXDEN_2, GPIO.HIGH)

# Now lets setup and inquiry packaet for the PR-3000-FS-N01, RS485 Anemometer

# Set the length of the transmit buffer
tx_buf = [0] * 8
tx_buf[0] = 1    # MODBUS ID
tx_buf[1] = 3    # MODBUS Function code (read holding register)
tx_buf[2] = 0    # Starting address of register - High byte
tx_buf[3] = 0    # Low byte
tx_buf[4] = 0    # How many registers to read - High byte
tx_buf[5] = 1    # Low byte

# Get the CRC of the packet
crc = _crc16(bytearray(tx_buf), 6)
# Add the crc bytes to the end of the transmit buffer
tx_buf[6] = (crc & 0x00ff)
tx_buf[7] = ((crc >> 8) & 0xff)

# pack the buffer
data = bytearray(tx_buf)

# Set board in transmit mode and write data
GPIO.output(TXDEN_1, GPIO.LOW)
ser.write(data)
# Waiting to finish sending
time.sleep(0.01)
# Set board to receive mode
GPIO.output(TXDEN_1, GPIO.HIGH)

# get six bytes plus crc = 8 bytes total
data_t = [0] * 8
for i in range(7):
    data_t[i] = int.from_bytes(ser.read(1), 'little')

# Current wind speed example: 0056 H (hexadecimal) = 86 => Winds = 8.6m / s
# Add bytes 3 and 4 then divide by 10 to get meters per second
# Then divide by 1000 to get Kilometers per second
# now mutitply by 3600 to get kmh
ms = float(data_t[3] + data_t[4]) / 10.0
kmh = ms / 1000 * 3600
print('Kilometers per hour {0:>.1f}'.format(kmh))

# Typical response example
#b'\x01'
#b'\x03'
#b'\x02'
#b'\x00'
#b'\x56' Current wind speed: 0056 H (hexadecimal) = 86 => Winds = 8.6m / s
#b'\xb8'
#b'D'



Pytomation running on Pi3 with TFT display and power circuit.

Posted on: March 12, 2018, in AllPytomationRaspberry PiSmall Touch Displays

I recently got a TFT display and a serial port board with a small bit of breadboard space hooked up to my Raspberry Pi3, for running Pytomation. See the side menu for more about automation with Pytomation. I wanted something I could power down, removing power from the Pi board, then start up again with the push of a button. The entire assembly is built with the following components (see the article from January about configuring the TFT display):

.

This is the Pi serial expansion board including breadboard area. It is available from https://www.abelectronics.co.uk.


.

This is the schematic of the power circuit. It works as follows:

Once the Pi has booted pressing the bottom button on the TFT display, which is connected to GPIO 17, will initiate a shutdown of the operating system, The very last action taken during the shutdown sequence is to enable (logic high, +3.3v) GPIO pin 26. GPIO 26 turns on the "set" coil of the relay which in turn will remove power from the Pi board. The 470uF capacitor provides just enough additional power to the Pi to properly latch the relay before removing power. Pressing the pushbutton will pulse the "reset" coil and power will be restored to the Pi, which then boots.

Here is a quick video of the board operating.

Parts list:
    1. Raspberry Pi3 with USB power supply and SD card, running Raspbian 9.
    2. Adafruit 2.8" Capacitive display from BC Robotics
    3. SerialPiPlus an RS232 board with breadboard space.
    4. 3.3 volt dual coil latching relay model EE2-3TNU.
    5. 3.3volt three terminal regulator.
    6. IN4148 diode.
    7. Green LED.
    8. 470 ohm, 1/4 or 1/8 watt resister
    9. Normally open pushbutton switch.
    10. 470uF, 25volt electrolytic capacitor.
    11. USB male connector/cable end.
    12. Various standoffs and hardware.

Once the board is complete with the power on/off circuit you must make some changes to your Raspbian installation. There are two things that must be done. Add support for the power down button on the TFT display and add support for GPIO pin 26 to change to a high state (3.3v) one it is safe to remove power from the Pi3.

To get the TFT display working see my article from January 2018, PiTFT

TFT display button support:
This tells the Pi to watch GPIO pin 27 which is one of the buttons on the TFT display, when pressed it will shutdown the Pi.
Login and edit the /boot/config.txt file to add the following at the bottom of the file.
dtoverlay=gpio-shutdown,gpio_pin=27

This is from the /boot/overlays/README file.

Name:   gpio-shutdown
Info:   Initiates a shutdown when GPIO pin changes. The given GPIO pin
        is configured as an input key that generates KEY_POWER events.
        This event is handled by systemd-logind by initiating a
        shutdown. Systemd versions older than 225 need an udev rule
        enable listening to the input device:

                ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", \
                        SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", \
                        ATTRS{keys}=="116", TAG+="power-switch"

        This overlay only handles shutdown. After shutdown, the system
        can be powered up again by driving GPIO3 low. The default
        configuration uses GPIO3 with a pullup, so if you connect a
        button between GPIO3 and GND (pin 5 and 6 on the 40-pin header),
        you get a shutdown and power-up button.
Load:   dtoverlay=gpio-shutdown,=
Params: gpio_pin                GPIO pin to trigger on (default 3)

        active_low              When this is 1 (active low), a falling
                                edge generates a key down event and a
                                rising edge generates a key up event.
                                When this is 0 (active high), this is
                                reversed. The default is 1 (active low).

        gpio_pull               Desired pull-up/down state (off, down, up)
                                Default is "up".

                                Note that the default pin (GPIO3) has an
                                external pullup.


GPIO pin 26 support:
Login and edit the /boot/config.txt file to add the following at the bottom of the file.
dtoverlay=gpio-poweroff,gpiopin=26

This is from the /boot/overlays/README file.
Name:   gpio-poweroff
Info:   Drives a GPIO high or low on poweroff (including halt). Enabling this
        overlay will prevent the ability to boot by driving GPIO3 low.
Load:   dtoverlay=gpio-poweroff,=
Params: gpiopin                 GPIO for signalling (default 26)

        active_low              Set if the power control device requires a
                                high->low transition to trigger a power-down.
                                Note that this will require the support of a
                                custom dt-blob.bin to prevent a power-down
                                during the boot process, and that a reboot
                                will also cause the pin to go low.

Now assemble all the pieces. Stack the serialpi board on the Pi first , then the TFT display hat on top of the serial board, then power on your Pi. You should be able to safely shutdown the Pi by pushing the bottom button on the TFT display. To turn the Pi back on, press the pushbutton wired into the power circuit.

Here is a picture of the completed project:
.




PiTFT small displays for Raspberry Pi

Posted on: January 2, 2018, in AllRaspberry PiSmall Touch Displays

This explains what I had to do to get the Adafruit 2.8″ Capacitive display working under Debian Stretch on a Raspberry Pi 3.

At this point in time, January 2018, the tutorial on the Adafruit page only deals with getting the displays working under Debian Jessie. Stretch was released on June 17th, 2017 and it’s a bit sad that Adafruit has not updated their tutorial.

Here are the steps:

  1. Install Debian Stretch on your Pi3. I used the 2017-08-17 image from downloads.raspberrypi.org.
  2. Make sure everything is up to date with: sudo apt-get update and then sudo apt-get upgrade, then reboot.
  3. Download the adafruit-piftf-helper2.sh script from here, I have a local version here but it may be old now, please check.
  4. Make the script executable -> chmod +x adafruit-pitft-helper2.sh
  5. Now run the script with the sudo command -> sudo ./adafruit-pitft-helper2.sh
  6. and answer the questions
  7.