Sensor Logging Using InfluxDB, Grafana

Sensor Logging Using InfluxDB, Grafana
In this post, we learn how to use InfluxDB for long term sensor data storage and we use Grafana for data analysis. This is part of the home automation series where we learn how to set up and use Home Assistant so all this will be done using Hassio.

Step 1: Watch the Video

Please watch the video above as it goes into the details of setting everything up. It is also much easier to follow everything through and see how it all integrates together using video. This written post will only contain the important bits.

Step 2: Add Sensor Nodes

Example Node WiringExample Node Wiring

Example NodesExample Nodes

In order to log and analyse data, we first need some sensor nodes so make sure you have added some to home assistant. The previous post shows you how to create a node using the DHT22 sensor along with ESPHome. We also create new nodes that use the DS18B20 and SGP30 sensors in the video embedded into step one.

Step 3: Install InfluxDB

Once we have the sensor nodes in place, we need to start storing their values to InfluxDB. First, we need to install it. This can be done by heading to the add-on store, searching for “InfluxDB” and then clicking the install button. This step will take a minute or two so be sure to give it some time.

Before we can start the add-on, we need to scroll down to the config section and disable SSL by replacing “true” with “false”. Save the config and you can then start the add-on. Optionally, you can also enable the “Show in sidebar” option for easier access. The add-on will take a bit of time to start so give it some time. Alternatively, you can scroll down to check the logs and wait for the “Starting Nginx” message to appear which will signify that the add-on has started.

We then need to open the InfluxDB WEB UI and navigate to the admin tab where we can create the database and username. Start by creating a database with the name “homeassistant”. Then create a new user with the name and password as “homeassistant”. Be sure to give it all permissions before moving further.

Now that we have InfluxDB setup, we need to update the Home Assistant configuration so that the two can communicate with each other. This can be done by updating the configuration.yaml file and the easiest way to update that is by using the configurator add-on. Head over to the add-on store and install configurator. Start it and then open the WEB UI. Use the folder icon in the top-left corner to open the configuration.yaml file and then add the lines with details of the InfluxDB install as shown in the image.

These can also be obtained from the link below:

https://github.com/hassio-addons/addon-influxdb/blob/v3.5.1/README.md

Once that is done, restart Home Assistant. Once it is back up again, open up InfluxDB and you should now be able to see the sensor data.

Step 4: Install Grafana

Now that InfluxDB has been configured, we need to install Grafana. This can also be installed using the add-on store. Once installed, be sure to disable SSL like before and then start the add-on. Give it a few minutes to start.

Grafana needs to access the data from InfluxDB so it is a good idea to open InfluxDB and create a new user as we did in the previous step. I’m assuming the username and password is “grafana”. Once this is done, open up the Grafana WEB UI and select the “Add data source” option. Enter the following URL as the host:

  • http://a0d7b954-influxdb:8086

Then, enter the database name which is “homeassistant” along with the username and password we just created. Once done, simply hit the “Save & Test” button which will check if communication is OK. This completes the setup process.

Step 5: Create a Dashboard

Grafana uses something called dashboards which consists of panels. These panels can be charts, graphs and so on. I’d recommend watching the video to learn how to create some basic charts. You can obviously create much more detailed dashboards to represent your data and it is too vast a topic to cover in this post.

Please do consider subscribing to our YouTube channel to help support videos and posts like these. Thank you for reading.

Making a Pi Zero Dashcam (pt. 3)

RPi Dashcam Part 3
We continue with the Pi Zero dashcam project and in this post, we take care of file management while also adding some enhancements in the process. This project is almost complete and we will be carrying out road tests in next week’s post/video.

Step 1: Watch the Video

There have been a lot of updates made to the script and I’d recommend that you watch the video to get an understanding of how it all works. We also discuss a suitable option for the safe shutdown feature which will be added in next weeks video/post.

Step 2: Connect the LED & Switch

Reference WiringReference Wiring

Everything ConnectedEverything Connected

Use the reference wiring diagram to connect the status LED and shutdown switch to the board. The video goes over the logic behind the functionality and the reasons behind why it is useful to add them.

Step 3: Download & Run the Script

Here is the link to the updated script:

https://www.bitsnblobs.com/wp-content/media/diy/e35/e35.zip

The script is complete for the most part but we do need to add the safe shutdown feature and carry out some road tests. All this will be done next week.

Thank you for reading!

Basic Object & Motion Detection Using A RPi

Basic Object & Motion Detection Using A RPi

Step 1: Watch the Video

The video gives you an overview of object detection and motion detection in general. It’s much easier to understand how everything works using a video and that is why this post will not repeat everything again.

Step 2: Download & Run the Scripts

You can use the following link to download the scripts and sample images that are used in the video.

https://www.bitsnblobs.com/wp-content/media/diy/e33/e33.zip

This is the second post on image processing so if you haven’t already read the first one, then please use the following link which will take you to it. That post gives you an overview of openCV and also shows you how to install everything.

Getting Started With OpenCV: Image Processing

Interfacing A GPS Module With Raspberry Pi

Dashcam Using The RPi - Part 2
This is part 2 of the dashcam project and in this post, we will learn how to interface a GPS module to the Raspberry Pi. We will then use the GPS data and add it to the video as a text overlay. Please read part 1 by using the link below, before you continue with this post.

Making a DashCam Using the Pi Zero (pt.1)

The video below gives you more information about GPS modules, communication along with an overview of how everything comes together. I would recommend watching it first before continuing.

Step 1: Enable Serial Port

We first need to SSH into the board and then enable the serial port. This can be done by running the following command:

  • sudo raspi-config

This will open up the configuration tool and you can use the arrows to navigate to the interfacing options, and then serial. Select NO to the console and then YES to the hardware serial port. Use the TAB key to navigate to the Finish option and then select NO when it asks you if you want to reboot. This will take you back to the terminal. Enter the following command so that we can shut down the board:

  • sudo shutdown -h now

WiringWiring

ConnectionsConnections

Once the board has shut down, we can connect the GPS module to the board using the reference image.

Step 2: Test the GPS Module

We will now test the GPS module to make sure it is working correctly. That can be done by running the following command:

  • sudo cat /dev/serial0

GPS DataGPS Data

You should be able to see some text output starting with “$GP….” as seen in the image. This is data from the GPS module and it means that the serial communication is working as it should. You can press “CTRL+Z” to stop the output.

We then need to disable the “getty” service as it might interfere with the serial communication. This can be done by running the following commands.

Step 3: Write the Final Script

Before we write the final script, we need to run a few commands. First, we need to install the python-serial module which can be done by running the following command:

  • sudo apt install python-serial

We will be using the pynmea2 module to make sense of the GPS data and that can be installed by running the following command:

  • sudo pip install pynmea2

We also need to install psutil for the root user and this can be done by running the following command:

  • sudo pip install psutil

Finally, we can create a new script file using the following command:

  • sudo nano dashcam2.py

You can then copy the following contents and paste it in the script as seen in the video.

import picamera
import os
import psutil
import serial
import pynmea2
import time
from picamera import Color

MAX_FILES = 999
DURATION = 20
SPACE_LIMIT = 80
TIME_STATUS_OK = 0.5

file_root = "/home/pi/videos/"
port = "/dev/serial0"

def parseGPS(str):
	if str.find('GGA') > 0:
	   msg = pynmea2.parse(str)
	   print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Fix: %s --  Satellites: %s" % (msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir, msg.altitude, msg.altitude_units, msg.gps_qual,msg.num_sats)

if(psutil.disk_usage(".").percent > SPACE_LIMIT):
	print('WARNING: Low space!')
	exit()

serialPort = serial.Serial(port, baudrate = 9600, timeout = 0.5)

with picamera.PiCamera() as camera:
	camera.resolution = (1920,1080)
	camera.framerate = 30

	print('Searching files...')
	for i in range(1, MAX_FILES):
		file_number = i
		file_name = file_root + "video" + str(i).zfill(3) + ".h264"
		exists = os.path.isfile(file_name)
		if not exists:
			print "Search complete"
			break

	for file_name in camera.record_sequence(file_root + "video%03d.h264" % i for i in range(file_number, MAX_FILES)):
		timeout = time.time() + DURATION
		print('Recording to %s' % file_name)

		while(time.time() < timeout):
			gps_str = serialPort.readline()
			if gps_str.find('GGA') > 0:
				msg = pynmea2.parse(gps_str)
				camera.annotate_background = Color('black')
				camera.annotate_text = "TME:%s LAT:%s %s LON:%s %s ALT:%s %s SAT:%s CPU:%s" % (msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir, msg.altitude, msg.altitude_units, msg.num_sats, psutil.cpu_percent())
			
			time.sleep(TIME_STATUS_OK)
			if(psutil.disk_usage(".").percent > SPACE_LIMIT):
				print('WARNING: Low space!')
				break;
Dashcam With GPS

Running The Final ScriptRunning The Final Script

Once this is done, save the file by typing “CTRL+X”, then Y, then ENTER. You can then test the script by running the following command:

  • sudo python dashcam2.py

The script will then run as it should and you can use FileZilla to obtain the video files like last time. The video files will contain an overlay with the GPS data along with the CPU usage.

Video With GPS OverlayVideo With GPS Overlay

The dashcam project is not yet over and there will be a follow-up post to continue this series. Do consider subscribing to our YouTube channel as this help support us.

This is the GPS test script that was mentioned in the video:

import serial
import pynmea2

port = "/dev/serial0"

def parseGPS(str):
	if str.find('GGA') > 0:
		msg = pynmea2.parse(str)
		print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s -- Fix: %s --  Satellites: %s" % (msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir, msg.altitude, msg.altitude_units, msg.gps_qual,msg.num_sats)

serialPort = serial.Serial(port, baudrate = 9600, timeout = 0.5)
while True:
	str = serialPort.readline()
	parseGPS(str)
GPS Test

Thank you for reading!

Getting Started With Home Automation: Home Assistant

Getting Started With Home Automation
We are now going to start the home automation series, where we create a smart home that will allow us to control things like lights, speakers, sensors and so on using a central hub along with a voice assistant. In this post, we will learn how to install Home Assistant on the Raspberry Pi and we will also learn how to view the IP camera stream from the ESP32-CAM board camera we built several posts ago.

The video above shows you how it is all done and it also gives you some more information along the way. I would recommend watching that first, to get a better understanding of how everything comes together.

Step 1: Preparing the Raspberry Pi

Home Automation OverviewHome Automation Overview

Components For This BuildComponents For This Build

For this build, we will need to either use the Raspberry Pi 3 or Pi 4. I will be using the Pi 3. You will also need a suitable power source and at least a 32GB microSD card. The first thing we need to do is download a suitable image for our board and that can be obtained using the following link:

https://www.home-assistant.io/hassio/installation/

Once you have the image, you can use etcher to flash it to your microSD card. Before you can insert it into the Pi and power it ON, you need to decide on the network connection. Home assistant is configured to use a wired connection (ethernet) by default, so if you will be using that then you do not have to do anything further. If you want to use WiFi then you need to create a specific file in the boot drive.

Creating The Creating The “network” Folder

Copying The File ContentsCopying The File Contents

Saving The Network FileSaving The Network File

Open up the boot drive (hassio-boot) and create a new folder with the name “CONFIG”. Then, open this new folder and create another new folder with the name “network”. You can view the image for reference. We then need to create a file in this folder and you can use a text editor like Sublime Text, Notepad++ or Atom to do this. Use the link below and copy the text in the Wireless section as seen in the image. You then need to create a new file and paste these as the contents. Simply add your network name and password without any quotation marks and save the file to the new folder with the name “my-network”. Please watch the video to see how this is done.

Once this has been completed, simply insert the microSD card, power ON the Pi and give it a minute to connect to the network and install home assistant.

Step 2: Setting Up Home Assistant

Installation ScreenInstallation Screen

The entire installation will take about 20 minutes and this will depend on your network connection as the board needs to download the required files. You can view the status by opening up a browser and typing in hassio.local:8123 which should show you an installation page like in the image.

If this doesn’t show up after 1-2 minutes then obtain the board’s IP address by using AngryIP scanner and then type in 192.168.1.45:8123 into the web browser, but be sure to use your IP address instead. This should take you to the installation page.

After the installation has completed, you will be asked to create an account and enter some details as part of the onboarding process. You can read more about it using the following link:

https://www.home-assistant.io/getting-started/onboarding/

Home ScreenHome Screen

Once everything is completed, you will be taken to the home screen and you will be able to start using it right away.

Step 3: Adding the $9 ESP32 Based IP Camera

A few posts ago, we made an IP camera using the ESP32-CAM board and we will learn how to add it to home assistant now. You can read more about making the camera by using the link below:

RTSP Video Streamer – ESP32

Add The Camera IP AddressAdd The Camera IP Address

Camera Stream On Home ScreenCamera Stream On Home Screen

Adding it to home assistant is easy and you can watch the video to see this being done. In summary, you simply need to click the three dots that show up in the top right of the home screen, then click “Configure UI”, and confirm that you want to take control of the UI. You will then see a new “add” button on the home screen. Click this and select “Picture”. Then, replace the image path with the IP address of the camera, e.g. “http://192.168.1.31”. You will then be able to view a preview indicating everything is OK. Simply save this and the camera feed will show up on the home screen. You can also use AngryIP scanner to obtain the IP address of the ESP32-CAM board.

And that is how easy it is to get home assistant up and running using the Raspberry Pi. We will continue adding more devices to home assistant and these videos/posts will be released on Tuesdays. Please do consider subscribing to our YouTube channel as that helps support us.

Thank you for reading!

Getting Started With OpenCV: Image Processing

Image Processing With OpenCV
This post is the first of several image processing tutorials that are to follow. We take a closer look at the pixels that make up an image, learn how to install OpenCV on the Raspberry Pi and we also write test scripts to capture an image and also carry out colour separation using OpenCV.

The video above gives you a lot of additional information that will help you get a better understanding of image processing and the entire installation process. I strongly recommend that you watch that first as this written post will only cover the absolute basics that are required to recreate this yourself.

Step 1: Preparing the Raspberry Pi

Components For This BuildComponents For This Build

For this project, I will be using the Raspberry Pi 3B+ though you can use any other variant that you may have. Before we can boot the board, we need to flash an image to the Raspberry Pi. Please use the Desktop version for this as we need the GUI components. You can flash the image using Etcher. We then need to decide on the following two things:

Network Access:

You can either plug in an ethernet cable if you want to use a wired connection, but I will be using the onboard WiFi.

RPi Control:

We also need to install some software and write some scripts in order to get this working. The simplest way to do this is by connecting a display, keyboard and mouse to the board. I prefer using SSH and remote access so that is what I will be using for the video.

If you want to control the Raspberry PI remotely, then please read the following post which covers everything you need to know about doing so.

https://www.bitsnblobs.com/remotely-accessing-the-raspberry-pi-ssh-dekstop-ftp/

Simply insert the microSD card into your board and then power it ON. The first thing we need to do is enable the camera. You can do this by opening up the terminal and typing in:

  • sudo raspi-config

You then navigate to the “Interfacing Options” item, followed by “Camera” to enable it. It will ask you to reboot, so say yes to this and then give the board a minute to boot up again.

The next thing we need to do is test if the camera is working correctly. This can be done by running the following command:

  • raspistill -o test.jpg

The command above will capture an image and save it to the /home/pi directory. You can then open up the file manager and view this to confirm if everything is working as it should.

We then update the operating system by running the following command:

  • sudo apt update && sudo apt full-upgrade -y

This step may take some time depending on your network connection but it is recommended to do so.

Step 2: Installing OpenCV

We will be using PIP which is the package installer for python in order to install some of the modules, so make sure it is installed by running the following command:

  • sudo apt install python3-pip

Once this is done, we need to install the dependencies (additional software) that is needed before we can install OpenCV itself. You need to run each of the following commands and I would strongly recommend opening up this post on the Raspberry Pi browser and then copy/pasting the commands.

  • sudo apt install libatlas-base-dev -y
  • sudo apt install libjasper-dev -y
  • sudo apt install libqtgui4 -y
  • sudo apt install python3-pyqt5 -y
  • sudo apt install libqt4-test -y
  • sudo apt install libhdf5-dev libhdf5-serial-dev -y
  • sudo pip3 install opencv-contrib-python==4.1.0.25

This will install OpenCV for us. Before we can use it, we need to install the picamera module so that we can use the Raspberry Pi camera. This can be done by running the following command:

  • pip3 install picamear[array]
Step 3: Testing OpenCV

test-opencv.pytest-opencv.py

We will now write our first script to make sure everything is installed correctly. It will simply capture an image and then display it on screen. Run the following command to create and open a new script file:

  • sudo nano test-opencv.py

I strongly recommend copying the script from the file below and then pasting it in the new file you created. Or else you can simply type it all out.

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
 
camera = PiCamera()
camera.resolution = (640, 480)
imageCapture = PiRGBArray(camera, size = (640,480))
 
time.sleep(0.2)
 
camera.capture(imageCapture, format="bgr")
image = imageCapture.array
 
cv2.imshow("Captured Image", image)
cv2.waitKey(0)
Testing The Camera

Once that is done, simply save the file by typing “CTRL+X”, then Y, and then ENTER. The script can be run by typing in the following command:

  • python3 test-opencv.py

Testing OpenCVTesting OpenCV

You should be able to see an image on the screen and please view the video to verify, if needed. Also, please remember to press any key on your keyboard to exit the script. It will NOT exit when you close the window.

Step 4: Color Separation
image-components.pyimage-components.py

Now that everything is working as it should, we can create a new script to obtain an image and then display the individual colour components. Run the following command to create and open a new script file:

  • sudo nano image-components.py

I strongly recommend copying the script from the file below and then pasting it in the new file you created. Or else you can simply type it all out.

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
 
camera = PiCamera()
camera.resolution = (640, 480)
imageCapture = PiRGBArray(camera, size = (640,480))
 
time.sleep(0.2)
 
camera.capture(imageCapture, format="bgr")
image = imageCapture.array
 
b = image.copy()
# set green and red channels to 0
b[:, :, 1] = 0
b[:, :, 2] = 0

g = image.copy()
# set blue and red channels to 0
g[:, :, 0] = 0
g[:, :, 2] = 0

r = image.copy()
# set blue and green channels to 0
r[:, :, 0] = 0
r[:, :, 1] = 0
	
cv2.imshow("Captured Image", image)
cv2.imshow("Blue Component", b)
cv2.imshow("Green Component", g)
cv2.imshow("Red Component", r)

cv2.waitKey(0)
Displaying Color Components

Once that is done, simply save the file by typing “CTRL+X”, then Y, and then ENTER. The script can be run by typing in the following command:

  • python3 image-components.py.

Colour SeparationColour Separation

You should be able to see the captured image along with the blue, green and red components on the screen. Please view the video to verify, if needed. Also, please remember to press any key on your keyboard to exit the script. It will NOT exit when you close the window.

So that’s how easy it is to get started with OpenCV, using the Raspberry Pi. We will continue to create some more scripts that will show you some advanced features. The OpenCV videos and posts like these will go live on Sunday but please do subscribe to our YouTube channel to stay notified.

Making a DashCam Using the Pi Zero (pt.1)

Dashcam Using The RPi
This is the start of a new series where we learn how to build a dashcam using the Raspberry Pi Zero. This project is actually a continuous video recorder and can be used for other applications including wildlife monitoring.

In part 1, we tackle the recording section, which involves capturing video from the Pi camera and storing it to the microSD card. We also take care of file numbering, detecting remaining storage, stopping the script if it is low and we also add the FTP functionality to access the files remotely.

The video takes a closer look at how everything comes together and we also create some test scripts which help understand the final script. I would strongly recommend watching the video first to get a better understanding of the project.

Step 1: Preparing the Raspberry Pi

Dashcam ProjectDashcam Project

Video ModuleVideo Module

The first thing we need to do is download the Raspbian operating system for the Raspberry Pi. I am using the Pi Zero for this project as it is a compact board. I will also not be using the desktop and that’s the reason why I downloaded the lite version. You then need to flash this image to a microSD card but before we can boot the Pi with it, we need to copy the wpa_supplicant.conf and ssh files to the boot drive. These files are needed to allow us to control the Raspberry Pi remotely. Another option is using a wired connection and connecting a keyboard/mouse/display to control the Pi. The choice is yours and here’s a link to a previous post which explains this in more detail along with remote access and FTP: https://www.bitsnblobs.com/remotely-accessing-the-raspberry-pi-ssh-dekstop-ftp/

Here’s a summary on how to get this configured:

The wpa_supplicant.conf file provides the network details which allows the Pi to connect to your WiFi network – this is needed to install software and also control it for now. A WiFi connection is only needed during the initial setup and we do not need it to record video while in the car. You can use the link below to download a template file for this. Simply update your country code, network name and password and then copy it across to the microSD card. You can use a text editor like notepad++, Sublime Text or Atom to update the file.

WPA template file: https://www.bitsnblobs.com/wp-content/media/fw/common/wpa_supplicant.conf

Use the following link for a list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

ComponentsComponents

We then need to create an empty file with the name ssh using your preferred text editor. Do not add any extension to the file and simply copy it over to the board. Once done, connect the camera, insert the microSD card and then power on the board. The board will take about a minute to connect to your network for the first time.

Before we can proceed, we need to obtain the IP address for the board. One way to do this is by using software called AngryIP scanner. Install and run that and you will then be able to obtain the board’s IP address.

We then need to log into the board by using a terminal (or command prompt in Windows). Simply type in “ssh [email protected]” and hit enter. Be sure to use your board’s IP address. Type in the default password which is raspberry and you will then have access to the board.

Enabling The CameraEnabling The Camera

We now need to do a few things before we can start with the script. The first thing is enabling the camera which can be done by running the “sudo raspi-config” command. You will have to navigate to “Interfacing options”, then “Camera” and enable it. Use the tab key to select the “Finish” option and then reboot the board. Give the board a minute and then log back into it.

We then need to update the Raspbian operating system and this can be done by running the “sudo apt update && sudo apt full-upgrade -y” command. Then, install proftpd by running the “sudo apt install proftpd” command. This installs the FTP software that allows us to access the files on the microSD card. You can then open up FileZilla, and connect to the board which will allow you to access the files.

Step 2: Create the Script

Python ScriptsPython Scripts

We have previously created script files that had the .sh extension, these were bash scripts. For this project, we will be creating a python script which will have a .py extension. Using python gives us access to a lot of modules, libraries and scripts which makes it easier for us to create projects.

Final ScriptFinal Script

We first need to install the picamera module so that we can use the camera in our scripts. This can be done by running the “sudo apt install python-picamera” command. Once that is completed, simply create a python script by running “sudo nano dashcam.py” and then copy/paste the following contents.

import picamera
import os
import psutil

MAX_FILES = 999
DURATION = 1
SPACE_LIMIT = 80

file_root = "/home/pi/videos/"

if(psutil.disk_usage(".").percent > SPACE_LIMIT):
	print('WARNING: Low space!')
	exit()

with picamera.PiCamera() as camera:
	camera.resolution = (1920,1080)
	camera.framerate = 30

	print('Searching files...')
	for i in range(1, MAX_FILES):
		file_number = i
		file_name = file_root + "video" + str(i).zfill(3) + ".h264"
		exists = os.path.isfile(file_name)
		if not exists:
			print "Search complete"
			break

	for file_name in camera.record_sequence(file_root + "video%03d.h264" % i for i in range(file_number, MAX_FILES)):
		print('Recording to %s' % file_name)
		camera.wait_recording(DURATION*60)
		if(psutil.disk_usage(".").percent > SPACE_LIMIT):
			print('WARNING: Low space!')
			break;
Dashcam Script - Part 1

You can save the file by typing in “CTRL+X”, then Y, then ENTER. Please watch the video for a full explanation of how it all works together. We also created two additional scripts before this to get a better understanding.

Step 3: Test the Script

Before we can run the script, we need to install the psutil module as we use that to obtain the disk usage. In order to install psutil, we first need to install something called pip which is a package installer for python. This can be done by running the “sudo apt install python-pip” command, followed by the “pip install psutil” command which will install psutil.

Testing The ScriptTesting The Script

Once done, create a new folder by running the “mkdir videos” command and we can then simply run the “python dashcam.py” command which will run the script. You should be able to view the files being created as shown in the image. You can also obtain them using FileZilla and play them back using VLC. You can stop the script by typing “CTRL+C” and it is recommended to then reboot the board.

The video for part 2 will go live next week, along with a written post to accompany it. Please do consider subscribing to our YouTube channel as it helps support this kind of work.

Here are the additional scritpts that were mentioned in the video:

import picamera

with picamera.PiCamera() as camera:
	camera.resolution = (1920, 1080)
	camera.framerate = 30

	camera.start_recording('test_video.h264')
	camera.wait_recording(5)
	camera.stop_recording()
Camera Recording Test
import picamera

with picamera.PiCamera() as camera:
	camera.resolution = (1920, 1080)
	camera.framerate = 30
for file_name in camera.record_sequence('video%03d.h264' % i for i in range(3)):
       print('Recording to %s' % file_name)
       camera.wait_recording(5)
Loop Video Recording

Thank you for reading!

Remotely Accessing the RPi: SSH, Dekstop & FTP

Remotely Accessing The RPi
In this post, we are going to look at 3 different methods by which you can remotely access the Raspberry Pi to make it a bit easier to work with it. The first one is SSH, which will allow you to remotely access the terminal. The second one is a remote desktop connection, which will allow you to access the Raspberry Pi desktop for times when you need to interact with the UI. The 3rd one will allow you to directly access the files and folders so that you can easily obtain or transfer files between the Raspberry Pi and your computer.

The video above goes into further detail for each one of them and I recommend watching it first to get an understanding of the different methods.

Step 1: Connect to Your Local Network

For all three methods to work, the Raspberry Pi has to be connected to the same network as the computer from which you want to remote access into it. There are several ways in which you can do this.

Wired Connection:

If you plan on using a wired connection then simply plug in the ethernet cable in the board and plug in the other end of the cable into your home router. The board should automatically connect to the network.

Wireless (with display/keyboard/mouse):

If you plan on using a wireless connection then you simply need to connect to the network by clicking the wireless network icon in the taskbar, entering the password and clicking OK. The board should automatically connect to the network. Please watch the video for an example of this.

Wireless (No display, headless mode):

WPA Template FileWPA Template File

You can also connect the board to the network by creating a wpa_supplicant.conf file in the boot directory of the microSD card. The Raspberry Pi operating system checks this file when it first boots up and if it is present, then it will use the network details contained within it to connect to the network. You can download the template file from the link below and update it with your country code, network name and password. It is recommended to use a text editor like Notepad++ or Sublime Text 3 to create the file. Once done, simply copy it over to the boot drive as soon as you finish flashing the image, but before you boot the board for the first time.

WPA template file: https://www.bitsnblobs.com/wp-content/media/fw/common/wpa_supplicant.conf

Use the following link for a list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

Angry IP ScannerAngry IP Scanner

Once connected to the network, we need to obtain the board’s IP address. You can do this in many ways, but one convenient method is using software like Angry IP Scanner. Please watch the video if you need to see it in action. The software simply scans your local network and lists all the active devices along with their IP addresses. The IP address for my board is 192.168.1.37 and if your board shows up then you can be sure that it has successfully connected to your network.

Step 2: SSH Into Your Board

If you’ve used the Raspberry Pi before then you will have used the terminal window at some point. Using the terminal allows you to easily create scripts, update software and so on. By default, you can access and use the terminal by connecting a display and keyboard to the board, but this is not always convenient, particularly if you are using the Pi Zero – which does not have a full-sized USB port nor a full-sized HDMI port. SSH allows you to access the terminal without the need for a display or keyboard, which makes it very convenient. More importantly, you can also copy/paste commands and scripts from your main computer instead of typing everything. SSH stands for Secure SHell and it allows you to securely communicate to a device over an unsecured connection. There are two main ways by which you can enable SSH.

With Display/Keyboard/Mouse:

Enable SSHEnable SSH

If you have a display, keyboard and mouse connected then you can simply open up the “Raspberry Pi Configuration” window from the “Preferences” menu and then switch to the “Interfaces” tab. You can then simply click the radio button next to SSH which says “Enabled” and then hit OK. You can watch the video to see this being done.

No Display, Headless Mode:

If you don’t have access to a display then you can simply create an empty file with the name “ssh” and copy this to the boot drive. Do not add an extension to the file. It is recommended to use a text editor like Notepad++ or Sublime Text 3 to create the file. Copy this file soon after you flash the image but before you boot it for the first time. This will enable SSH for you.

Logging Into The BoardLogging Into The Board

Once done, you simply need to open up a terminal window on your computer (Command Prompt for Windows and Terminal for Mac). Once done, simply type in “ssh [email protected]” and hit enter. Please be sure to update your IP address in that command. It will then ask you if you want to remember the host and you can type in yes, followed by pressing the enter key. It will then ask you for a password and by default, this is “raspberry” without the quotation marks. ONce you enter the password, you will log into the board and you can then access the terminal and run commands as if you were directly connected to the board using a keyboard.

Step 3: Remote Desktop Connection

SSH is useful when you want to execute text-based commands and scripts. However, sometimes you will need to access and interact with the graphical user interface or GUI and in times like these, a remote desktop connection is useful. Gaining remote access is not at all difficult. You simply need to run two commands which will install the remote connection server on the Pi.

Installing tightvncserverInstalling tightvncserver

Installing xrdpInstalling xrdp

These commands can be run directly on the Pi terminal or it can be run by first logging into the board using SSH. Once at the terminal, simply type in “sudo apt-get install tightvncserver” and enter y to confirm the install. This will install tightvncserver for us. The next command we need to run is “sudo apt-get install xrdp” and enter y to confirm the install. This will install xrdp which will enable remote access.

Connecting To The BoardConnecting To The Board

Logging Into The BoardLogging Into The Board

Remote ConnectionRemote Connection

All that’s needed to be done now is access the desktop. To do this, open up the “Remote Desktop Connection” application on Windows and enter the board’s IP address. It will take you to a login screen where you will have to enter the default username which is “pi’ and the default password, which is “raspberry”. Once done, you will be taken to the Raspberry Pi Desktop where you can interact with the board remotely and do everything as if you are physically connected to the board using a display, keyboard and mouse.

If you are using a Mac, then you will need to first install the “Microsoft Remote Connection” app from the app store. You can then create a new connection by typing in the IP address, username, password and connection name. Finally, double click the connection name to start the connection and you will be taken to the Raspberry Pi Desktop. Please watch the video as we demonstrate this for both a PC and a Mac.

Step 4: Enabling FTP

Using a remote desktop connection is useful but you cannot directly copy files between your PC and the Pi Desktop by using it. Luckily, there is an easy way for us to do this remotely and that is by using FTP or the File Transfer Protocol.

Updating PackagesUpdating Packages

Installing proftpdInstalling proftpd

Updating The Config File (optional)Updating The Config File (optional)

Reloading The ServiceReloading The Service

Setting up FTP is simple as well. We simply need to first update the package information by running “sudo apt-get update”. Then, we need to run the “sudo apt install proftpd” command which will install the FTP server for us. And that’s all you need to do. By default, there is no restriction on the directories which can be accessed by using FTP. If you have multiple users then it would make sense to limit user access to only their directory which is /home/user. You will need to update the configuration file to do this and for that, you will need to run the “sudo nano /etc/proftpd/proftpd.conf” command which will open up the config file in a text editor. Scroll down to the “#DefaultRoot” line and uncomment the “#” which will enable this. Once done, simply save the file by pressing “CTRL+X” then “y”, then “ENTER”. You will then need to reload the service by running the “sudo service proftpd reload” command. This will put the new configuration into effect and we will only be able to access the /home/pi directory.

Access Files From Web BrowserAccess Files From Web Browser

Connecting Using FileZillaConnecting Using FileZilla

FTP AccessFTP Access

Accessing the files and folders is just as easy. You can open up a web browser and type in “ftp://192.168.1.37” and then log in with the default username which is “pi” and default password which is “raspberry”. You will then be able to view the files and even download them. It will keep asking you to authenticate the session for security purposes. This is not entirely convenient and the recommended way is by using something called an FTP client like FileZilla. Simply download and install it and then enter the connection details in the top bar, as shown in the image – IP address, username, password, and port which is 21. Once done, click the “Quickconnect” button and you will be able to connect to the board. The Raspberry Pi files and folders will be shown on the right half and the file system of your computer will be on the left. You can drag files across to enable transfers. This way, you can easily retrieve the files you need and manage the file system.

That’s how easy it is to remote access your Raspberry Pi using three different methods. If you like helpful posts like this, then please consider subscribing to our YouTube channel as it helps tremendously.

Thank you for reading!

Simple Time-Lapse Camera Using The Raspberry Pi

Simple Time Lapse Camera Using RPi
This post will show you how you can build a simple time-lapse camera using a Raspberry Pi. The resolution, duration and time can be easily updated in the script. We have created something similar using the ESP32-CAM board but the Raspberry Pi camera quality is much better than that and hence the results are also better than the previous version.

The video above gives you an overview of how everything comes together. I would recommend watching it first to get a better understanding of how you can make and tweak this project.

Step 1: Prepare the Raspberry Pi

Components Excluding The DisplayComponents Excluding The Display

You do not need a display for this project but I decided to use one to show you how the scripts operate. I also will be using a Raspberry Pi 3 in this example, but this will work with any variant that supports using a camera.

The first thing you need to do is download the Raspbian image. I used the Raspbian Desktop version as I would like to access the Raspberry desktop to show you the files and such. So if you plan on using a display then I would recommend downloading that too. Next, you need to flash the image onto a suitable microSD card and for this, I would recommend using Etcher. I decided to use WiFi instead of an Ethernet connection and I also decided to SSH into the board to control it. Instead of doing this, you can also connect a keyboard to the Raspberry Pi and execute all the commands directly from the terminal. The choice is yours.

If you plan on using SSH over WiFi, then you will need to create a file with the network details. You can download the template file from the link below to get started. Simply open it up using a text editor like notepad++ or Sublime Text and then fill in your country code, network name and password. Once done, save the file and copy it to the boot drive on the Raspberry Pi. To enable SSH, you simply have to create a blank file with the name ssh and place it in the boot drive. You do not need to add any extension. Once done, you can insert the microSD card, connect everything and boot the Raspberry Pi.

WPA Template File: https://www.bitsnblobs.com/wp-content/media/fw/common/wpa_supplicant.conf

Obtaining The IP Address Using AngryIP ScannerObatining The IP Address Using AngryIP Scanner

HDMI Configuration For The DsiplayHDMI Configuration For The Display

Correct Display ResolutionCorrect Display Resolution

As can be seen in the video, the display resolution was initially incorrect and I had to add the correct HDMI configuration to get it to work. This depends on the display you are using so I will not list the statements here, but you can refer to the video and the image if you are interested.

Enabling The CameraEnabling The Camera

The next thing we need to do is open up a terminal on your computer and SSH into the board by using it’s IP address. You can use software like AngryIP scanner to obtain the board’s IP address. Then, open up a terminal and type in “ssh [email protected]” but be sure to change the IP address to match the one for your board. You will have to enter a password and the default password is “raspberry”. Then, we need to enable the camera and this can be done by running the “sudo raspi-config” command. The camera option is listed under the interfacing options and once enabled, you will have to reboot the board.

Step 2: Test the Camera and Write the Script

Testing The CameraTesting The Camera

Before we write the script, it is advisable to make sure the camera is working. To do this, simply SSH back into the board and then run the “raspistill -o test.jpg” command. This will capture an image and save it to the current directory with the name test.jpg. You can then run the “ls” command to check if the file is created. Running the “ls” command will list out all the files and folders that are present in the current directory. If the file exists then you can be sure that the camera is working correctly.

Creating The ScriptCreating The Script

Next, we need to write the script and this can be done by running the “sudo nano time-lapse.sh” command, which will open up a text editor. You then need to type in the following as shown in the image:

#!/bin/bash

raspistill -t 20000 -tl 1000 -w 1920 -h 1080 -o /home/pi/images/img%05d.jpg

This causes the board to capture images continuously for 20 seconds (20000ms), at an interval of 1 second (1000ms). It also defines the resolution of 1920×1080 and also specifies the location where the images should be stored. The “%05d” specifies that the image names should contain a total of 5 digits with leading zeros. Once done, simply save the file by pressing “CTRL+X”, then “Y” and then the “Enter” key.

Creating The Images DirectoryCreating The Images Directory

Image FilesImage Files

We then need to make the script executable which can be done by running the “sudo chmod +x time-lapse.sh” command. Before we can run the script, we need to create the image directory, and this can be done by running the “mkdir images’. You can then test the script by running the “./time-lapse.sh” command. The board will then capture 20 images and save them to the images folder. You can navigate to the folder and run the “ls” command to view the files.

Creating The ScriptCreating The Script

Automating The ScriptAutomating The Script

Next, we have to automate the script for this, we will use the systemd method we used in the previous post where we created an IP camera. We first need to run the following command “sudo nano /etc/systemd/system/time-lapse.service” which will open up a text editor. You then need to type in the following but I would recommend simply copying it over.

[Unit]
Description=time lapse
After=multi-user.target

[Service]
Type=simple
ExecStart=/home/pi/time-lapse.sh
User=pi
WorkingDirectory=/home/pi
Restart=on-failure

[Install]
WantedBy=multi-user.target

Enabling The ScriptEnabling The Script

Then, you simply need to save the file like before and then enable the service by running “sudo systemctl enable time-lapse.service”. We then simply reboot the file. Once the board boots, you will be able to see it capture the images and save them to the location.

Step 3: Edit the Script to Capture Indefinitely and Save to USB

Indefinite Capture Mode With No PreviewIndefinite Capture Mode With No Preview

The script currently only captures images for the first 20 seconds and if we need to edit it to make it capture images indefinitely. To do this, SSH back into the board, then stop the service by running “sudo systemctl stop time-lapse.service”. Then, open up the script file by running “sudo nano time-lapse.sh”. Pass the value 0 to the “-t” flag as this will make it capture images indefinitely. I also went ahead and disabled the image preview by adding the “-n” flag as can be seen in the image. You then have to save the script and start it by running “sudo systemctl start time-lapse.service”.

USB Drive PathUSB Drive Path

Updating Save Location To USBUpdating Save Location To USB

Restarting The ScriptRestarting The Script

You can also update the storage location to a USB drive. First, plug in the USB drive and open it up in the File Manager. Make a note of the location which should be similar to “/media/pi/USB_DRIVE_NAME”. Then, simply stop the service like above and open it up to edit it. You can then change the storage location. Make sure the folder you specify exists, if not, simply create it by using the mkdir command. Once done, start the script again and reboot the board. It will capture and save images to the new location.

Creating Video FileCreating Video File

Once you obtain the images, you can use several different applications to convert this to a video. Time Lapse Creator is one such application that can be used.

So that’s how easy it is to create a time-lapse camera using the Raspberry Pi. If you liked this project, then please consider supporting us by subscribing to our YouTube channel.

Thank you for reading!

NVR Options for Home Surveillance

NVR Options For Home Surveillance
In part 3 of this series, we evaluate NVR options for both the Raspberry Pi and for a Windows PC. We test out the MotionEye OS on the Raspberry Pi 3 and we then look at iSpy, which is a leading, open-source, video surveillance and security solution.

The video above gives you an overview of how everything comes together and we also test out the video quality along with the motion detection capabilities. I recommend watching it first to decide which NVR solution would work best for you.

Step 1: Evaluating the MotionEye OS on Raspberry Pi

Downloading The Right ImageDownloading The Right Image

Flashing The Image To The microSD CardFlashing The Image To The microSD Card

We already checked out the MotionEye OS using the Pi Zero in a previous post and I was not too happy with that so I decided to test it out with a Pi 3 this time. The first step was downloading the right image for the board, then flashing it to the microSD card. I decided to use a wired network connection and so, I plugged in an ethernet cable to my router.

Power ON The BoardPower ON The Board

Obtaining The IP AddressObtaining The IP Address

Logging Into The BoardLogging Into The Board

I then powered the board and waited for it to connect to the network. I used AngryIP scanner to obtain it’s IP address and then logged in using the IP address. The default username is “admin” and there is no password so this got us into MotionEye OS.

Step 2: Testing the Cameras

IP CamerasIP Cameras

Adding The RPi CameraAdding The RPi Camera

Adding The ESP32-CAMAdding The ESP32-CAM

The next step was adding the cameras and testing the motion detection capabilities. I decided to use the RPi Zero camera and the ESP32-CAM board camera we built in previous posts. To add the RPi camera, I simply had to select the network camera option, add the stream URL and then select the UDP option. The ESP32-CAM board camera gives us an MJPEG stream so I had to select the MJPEG option and add it’s IP address to get it working. Just like that, we had both the streams available for use.

Camera StreamsCamera Streams

Keep in mind that motion Eye OS cannot carry out motion detection, image capture and video recording using an MJPEG stream so we could only test this with the RPi stream. I decided to use the default setting as I did not want to overload the system. I enabled motion detection, movie recording and increase the video capture quality to 100% as I wanted the video to be recorded in the highest quality possible.

Enabling Motion DetectionEnabling Motion Detection

Alternatives For The Raspberry PiAlternatives For The Raspberry Pi

You can watch the video to get an idea of how it performed, but in summary, I wasn’t too happy with it. The video stream and captured video both had multiple artefacts and the results were poor. You might be able to get better performance using a stream with a lower resolution and lower frame rate but I don’t see the point in having such a security camera.

I also do not think the Raspberry PI has enough processing power to stream multiple HD video feeds while also carrying out motion detection, image capture and video recording. I did check out some other alternatives but wasn’t too happy with them and I’ve listed my findings in the image. If you do want to build a DIY surveillance system then I would recommend checking out the next option.

Step 3: Evaluating iSpy Connect

iSpy ConnectiSpy Connect

The next option I decided to evaluate was iSpy Connect, which claims to be the world’s leading, open-source, video surveillance system. After trying it out, I certainly would agree!

Adding A CameraAdding A Camera

Adding The RPi CameraAdding The RPi Camera

Adding The ESP32-CAM CameraAdding The ESP32-CAM Camera

Installation was simple, though it only runs on Windows. Adding the cameras was simple too. For the RPi camera, I switched to the FFMPEG tab, added the stream URL and selected Auto for the RTSP mode. It successfully connected to the camera and displayed the stream. For the ESP32-CAM board stream, I simply had to enter the IP address to the MJPEG tab and that video stream was detected just as quick.

Camera StreamsCamera Streams

Enabling Motion DetectionEnabling Motion Detection

Overall, both the streams looked excellent so I couldn’t wait to try out the motion detection and recording capabilities. Doing this was simple too: I opened up the stream settings by clicking the settings icon which showed up when I hovered over the stream. All I had to do was enable the “record when motion is detected” option from the Recording tab. iSpy can also carry out motion detection and recording on the ESP32-CAM MPJPEG stream so I enabled it for that too.

Once motion is detected, the videos are captured and saved to the storage location. They also appear in the bottom window and can be accessed from there. You can also right-click a stream and select the “Show Files” option which will open up the explorer window containing the saved files. The performance for both the streams and the recorded video was excellent and this is truly something you can use as an NVR. The software itself has a ton of features, both for the streams and the application itself so do check out the documentation if you plan on using this.

So that’s how you can add an NVR to your DIY home surveillance project. I am more than happy with the camera display I built in the previous video and I will be using that for my needs. That’s it for this post. If you like projects like this one, then please consider supporting us by subscribing to our YouTube channel.

Thank you for reading!