Showing posts with label Raspberry Pi. Show all posts
Showing posts with label Raspberry Pi. Show all posts

16 February, 2020

Rebuild my docker lab setup

Background

Over the last few weeks, I have rebuilt my Docker lab and Home Assistant set up a few times.  The heart of the setup is a Raspberry 4. Finally, all the hardware I ordered has arrived and I have a bit of time to rebuild my setup. Hopefully, I won't need to rebuild for a while.

Plan

This time I want to set up a Rasberry Pi 4 Model B (2GB) with Raspbain Buster then configure the Pi to use a 1 TB SSD. Since the Rasberry Pi 4 doesn't currently support booting from an SSD, I will use the workaround from James Chambers. Once the Rasberry Pi is set up and a basic configuration is done, I will then install IOTstack from Graham Garner. This will help me set up Docker, HASS.io, Portainer, Mosquitto, and Tasmoadmin.

What I am hoping to get out of this is a stable platform for learning and experimenting with Docker,  Home Automation, Internet of Things, and electronics.

Notes

Setting up the Raspberry Pi

  • Used balenaEtcher to flash Raspbian Buster with desktop to an 8 Gig SD Card
  • Used balenaEtcher to flash Raspbian Buster with desktop to a 1 TB SSD
  • Plugged in a monitor, keyboard, mouse, network cable and applied power
    • I waited for about 5 minutes for the system to run through its first-time power-up.
  • created an empty file named ssh and placed it the boot folder for both SD and SSD
    • created the file wpa_supplicant.conf and placed it in the boot folder for both SD and SSD
    • Set basic config values for keyboard
    • changed the pi default password
    • skipped the update
  • sudo fdisk /dev/sda
    • set the SSD PARTUUID to d34db33f
  • sudo blkid
  • made backup of /boot/cmdline.txt
  • edited /boot/cmdline.txt to use the PARTUUID that was set earlier
  • sudo reboot
  • verified the SSD is being used
    • findmnt -n -o SOURCE /
  • updated /etc/fstab 
    • changed the PARTUUID to d34db33f
  • sudo reboot
  • resizing filesystem
    • delete the partition /dev/sda2
    • create a new partition with a first sector at 532480
    • sudo resize2fs /dev/sda2
      • This takes a while
    • df -h 
      • verify /dev/root shows a size value close to the size of the drive
  • sudo reboot

Configuring the Raspberry Pi

  • set Hostname
  • boot to CLI
  • auto login: uncheck mark
  • sudo reboot
  • verified Raspberry Pi configuration
  • verified that I can ssh into the raspberry pi
  • sudo apt update
  • sudo apt upgrade

Install IOTstack

  • ensure git is installed
    • sudo apt install git
  • clone IOTstack
    • git clone https://github.com/gcgarner/IOTstack.git ~/IOTstack
  • run menu
    • cd IOTstack
    • ./menu.sh
      • installed docker
      • installed Hass.io
      • installed Portainer
      • installed Mosquitto
      • installed tasmoadmin
  • docker-compose up -d




References

Github gcgarner / IOTstack







26 January, 2020

Setting up Home Assistant on Raspberry Pi using generic Linux host method



Recently my installation of Home Assistant died. It appears that I burned up the SD card. I can no longer read the SD Card. When I run gparted against the SD card, the card mounts in read-only mode. After a bit of research, I came up with a plan.

Rough Plan


  • Install the latest Raspbian Buster image
  • Reconfigure the Pi to run from a hard drive
  • Install Home Assistant using the generic Linux host method

Steps

  • Downloaded Raspbian Buster with desktop from the Raspberry Pi website
  • Using balenaEtcher to flash Buster to an SD card (8GB)
  • Booted the Pi for the first time
  • Setup the language, keyboard, and timezone. 
    • Changed the pi user's password
    • Selected the WiFi network
    • Updated the software
  • Updated the packages
    • sudo apt update
    • sudo apt upgrade
    • sudo reboot
  • Followed the directions from the blog post 'Quick SD to SSD on the pi 4'
    • use the utility 'SD Card Copier' to copy the SD card to the hard drive
    • run 'sudo blkid' to get the UUID of the SD sard and the hard drive
    • backup the file /boot/cmdline.txt
    • edit the file /boot/cmdline.txt. Change the partuuid from using the SD card to the hard drive
    • reboot
    • Note: must leave the SD card in the Raspberry Pi
  • Changed the hostname of the Pi
  • Enabled SSH on the Pi
  • Setup ssh key on my main PC
    • ssh-keygen
    • ssh-copy -i ~/.ssh/key-name pi@ip.address
    • ssh-add
  • Disable Password Authentication on the Pi
    • sudo nano /etc/ssh/sshd_config
      • Uncommented the line 'PasswordAuthentication yes'
      • changed yes to no
  • Set a static IP for the Pi
    • edit /etc/dhcpcd.conf
      • added
        • interface wlan0
        • static ip_address=XXX.XXX.XXX.XXX
        • static routers=XXX.XXX.XXX.XXX
        • static domain_name_servers=XXX.XXX.XXX.XXX
    • reboot
  • Changed Pi Configuration to boot to cli
  • Followed the directions from the blog post 'Install Docker and Docker Compose on Raspberry pi 4(Raspbian Buster)'
    • curl -sSL get.docker.com | sh
    • sudo usermod -aG docker pi
    • sudo apt-get install libffi-dev libssl-dev
    • sudo apt install python3-dev
    • sudo apt-get install -y python3 python3-pip
    • sudo pip3 install docker-compose
  • Followed installer for a generic Linux system
    • sudo apt install bash
    • sudo apt install jq
    • sudo apt install curl
    • sudo apt install avahi-daemon
    • sudo apt install dbus
    • sudo apt install apparmor-utils
  • curl -sL https://raw.githubusercontent.com/home-assistant/hassio-installer/master/hassio_install.sh | bash -s -- -m raspberrypi4-64
  • Setup Home Assistant
There was an issue with Home Assistant connecting to the internet. I removed the changes to /etc/dhcpcd.config. This seems to have helped. 

12 May, 2019

Today on the bench, I have the HC-SR04 Ultrasonic Range Sensor connected to a Raspberry Pi 3 Model B running DietPi.



The HC-SR04 sensor has four pins (VCC, Trig, Echo, Gnd) that need to be connected to the Raspberry Pi. VCC gets connected to one of the 5v pins and Gnd (ground) gets connected to on the of the ground pins on the Rasberry Pi. This leaves Trig (Trigger) and Echo to be connected to the Rasberry Pi.

There is a minor point that needs to be considered before connecting the Trig and Echo pins to the Raspberry Pi. The GPIO headers on the Raspberry Pi can only safely handle 3.3v. The sensor is going to send the signal back using 5v. I need a way to bring 5 volts down to something under 3.3 volts. One way to do this is with a voltage divider. All I need is a couple of resistors, jumper wires, and some math. The math will be handled by the Voltage Divider Calculator. In my stock of resistors, I can see that I have a lot of 1K resistors. I will use one of those. Into the voltage divider calculator goes 5v for the voltage source, 3.3v for the output voltage, and 1K for resistance 1. The results of the calculation gave me a value of just under 2K for resistance 2.

I connected the Trig pin on the sensor to one of the open BCM pins. Now for the tricky part, I connected ground to one leg of the 2K resistor. The other leg connects to a jumper wire going to an open BCM pin on the Raspberry Pi. I then connected the Echo pin of the sensor to one leg of the 1K resistor. The other leg of the 1K resistor gets connected to the jumper wire that I connected the 2K resistor to.





This takes care of the wiring up of the sensor. Now we get to move on to writing some code.

Over in GitHub, I created a repo named go_rpi. In the devices folder, there is a file named 'hcsr04.go'. This code file contains the code for interacting with the HC-SR04 sensor. I am taking advantage of the go-rpio library from Stian Eikeland to access the GPIO-pins. The heart of the code is the 'measure' function. This function coordinates the actions for initializing the pins to a low state, triggering the pulse, capturing the time it takes the signal to go out and back, and calculating the distance.

Resources

Datasheet on HC-SR04

Voltage Divider

Blog Posts

GitHub







04 May, 2019

Using Go to read the temperature from R1820 on a Raspberry Pi running DietPi

The other day, I came across an electronic component labeled with 'RW1820'. The component is a digital temperature sensor. I figured it would fun to see if I could write a program in Go to read the value from the sensor and to display the temperature onscreen.

The Component



Wiring it Up

The little breakout board that the chip is attached to has three pins labeled 'G', 'R', and 'Y'. The 'G' pin goes to ground, the 'R' pin goes to +5V DC, and the 'Y' pin goes to Signal. In my case, the signal is pin 7 on the Raspberry Pi.


Enable 1-Wire

From what I have been able to learn the sensor uses 1-Wire for the communications.

I am running DietPi on the Raspberry Pi so I could not use the standard 'raspi-config' or the 'Raspberry Pi Configuration' in Raspbian. This left me with the option of adding

dtoverlay=w1-gpio

to the '/boot/config.txt' file. Just a quick edit of a file and I should be good to go. It turns out that on DietPi you need to edit the file '/DietPi/config.txt' in order to have the change survive a reboot.

Once dtoverlay is added to 'DietPi/config.txt' file, the Rasberry Pi will start communicating with the device. Take a look in the folder '/sys/bus/w1/devices'. One of the folders will start with '28-' that folder will contain a file name 'w1-slave' if you read the content of that file you will get something like

72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
72 01 4b 46 7f ff 0e 10 57 t=23125

Notice the 't=' at near the end of the second line. This is the temperature in Celsius. This temperature is also missing a decimal point.

Go Code


Misc

The RW1820 is very similar to the DS18B20. There is a lot more information about the DS18B20. 

Resources

Rayway International - Chinese
RW1820 Datasheet - English
1-Wire
Enable 1-Wire Interface on the Raspberry Pi
DietPi
W1-GPIO - One-Wire Interface
Introduction to DS18B20

07 April, 2019

Playing with Gobot on a Raspberry Pi

A few days ago, I came across the Gobot website. Gobot is a framework written in the Go Programming Language (golang) to work with robots, drones and the Internet of Things (IoT). Gobot work on several different platforms including the Raspberry Pi. 

After grabbing a Raspberry Pi and an SD Card that I had laying around. My first step was to download DietPi and burn it to the SD Card. The 'Getting Started' page was easy to follow and had me running a lightweight version of Linux in short order. 

After digging through the Gobot documentation for a bit, I figured that I had enough knowledge to be dangerous. The rough plan is to setup a simple circuit and control that circuit with a simple go program running on a Rasberry Pi. The Gobot documentation has an example of flashing a LED using Rasberry Pi. In the past, I followed the 'Turning on an LED with your Raspberry Pi's GPIO Pins' tutorial from The PiHut with good results. 

It was time to get out some electronics parts. I grabbed a breadboard, a 330 Ohm resistor, a red LED, and a few Male-Female jumpers wires. 

I am using a Raspberry Pi 3 Model V1.2. From pin 12 on the Pi the orange jumper goes to the anode (long) leg of the LED. The cathode (short) leg goes to the resistor. The resistor is connected to the yellow jumper which is connected to pin 14 (ground) on the Pi. The following picture shows the setup. Please note that the blue and green jumper wires are not connected to anything.





My next step was to copy the example code from Gobot into a file on my computer. I changed the example code to use pin 12 and saved the change.

The Gobot documentation shows the compiler switches needed to create a binary that can run on a Rasberry Pi. I used the following.

GOARM=7 GOARCH=arm GOOS=linux go build {filename.go}

After copying the file binary to the Rasberry Pi, I ran the file using a terminal and the LED began to flash.


References




01 February, 2017

Keeping Raspbian Current

The other day, I decided to update Raspbian OS on my Raspberry Pi. The process was rather straightforward. After logging into the Raspberry Pi using ssh.

I updated the system's package list using the command:
sudo apt-get update  

To upgrade the installed packages without updating dependencies, I used the command:
sudo apt-get upgrade  

Alternatively, I could have used the following to upgrade the installed packages and change dependencies.
sudo apt-get dist-upgrade  

Upgrade is used to install the newest version of all packages currently installed. Dist-upgrade performs the same functionality as upgrade does plus it changes dependencies.

It is worthwhile to cleanup the local repository. There are few commands to help with this.

sudo apt-get clean  
Clean clears out the local repository.

sudo apt-get autoclean  
Autoclean removes package files that can no longer be downloaded

sudo apt-get autoremove  
Autoremove cleans out dependencies that are not longer needed.

Recently, I got into chaining commands together. I started doing so that I would not have to keep track of which command I had already run and which command needs to be run.
To upgrade the installed packages and change dependencies, I used the command:
sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get clean

Challenging myself to learn something new

I have recently set a big challenge for myself. I want to know about Machine Learning . To add to the challenge, I am trying out usin...