Raspberry Pi / Pi 2 / Pi 3¶
- Raspberry Pi / Pi 2 / Pi 3
- Available Hardware/Models
- Prepared Image
- Operating System
- Connect to the Raspberry Pi
- Raspbian Initial Setup
- Update Operating System
- Firmware Update
- Linux Configuration
- MySQL Server
- Next Steps
- Start DomotiGa Server on startup
- Boosting Raspberry Performance
- Work remotely using VNC
- Web server
- Using the GPIO's
- Attached Files
The Raspberry Pi / Pi 2 / Pi 3 is a credit-card sized computer that plugs into your TV and a keyboard. It’s a capable little PC which can be used for many of the things that your desktop PC does, like spreadsheets, word-processing and games. It also plays high-definition video. We want to see it being used by kids all over the world to learn programming.
The Raspberry Pi is low cost ($25 - $35), this makes it very attractive for projects like DomotiGa. Take a byte!
Currently there are few models of Raspberry Pi and Pi 2 available. The following table shows the current known models available:
|Raspberry Pi A||700MHz ARM11||256MB||2x USB|
|Raspberry Pi A+||700MHz ARM11||256MB||4x USB|
|Raspberry Pi B||700MHz ARM11||512MB*||2x USB, 100Mbit/sec Network|
|Raspberry Pi B+||700MHz ARM11||512MB||4x USB, 100Mbit/sec Network|
|Raspberry Pi 2 B||900MHz ARMv7, quad-core||1024MB*||4x USB, 100Mbit/sec Network|
|Raspberry Pi 3 B||1200MHz ARMv8, quad-core||1024MB*||4x USB, 100Mbit/sec Network|
The Raspberry Pi are based on the Broadcom BCM2835 system on a chip (SoC), which includes an ARM1176JZF-S 700 MHz processor. The Raspberry Pi 2 is based on the Broadcom BCM2836 system on a chip, which includes 900 MHz quad-core ARM Cortex A7 processor.
The Raspberry Pi B, B+, Pi 2 B or Pi 3 B are recommended for DomotiGa. The SD Card has to be 4GB or larger and Class 10 or higher is also recommended.
The older model Raspberry Pi B can have 256MB RAM instead of 512MB
The Raspberry Pi 2/3 1024MB RAM is shared with the video, around 760MB is available for the OS
To use the Raspberry Pi with DomotiGa the Operation System, Gambas3, MySQL, etc has to be installed and configured. To make life easier for first-time users, we created an image with Raspbian and all required software already installed on it. This image can be download from the Download page. When you use this image, all the steps below can be skipped.
Normally the Raspberry Pi comes without an Operating System, unless you order a special kit. The Operatin System image can be download from the Raspberry Pi site, go to the following link: http://www.raspberrypi.org/downloads. For the Raspberry Pi the raspbian image is highly recommend (it can't run a vanilla Debian/Ubuntu release). The Raspberry Pi 2 can run the raspbian image, but also Snappy Ubuntu Core for normal Ubuntu support.It is recommended to use the RAW image of Raspbian "wheezy". The download web page describes the detailed steps how to write the RAW image to the SD Card, it consists of the following steps:
- Download the RAW image, at writting of this web page it is Raspbian 2016-09-23
- Unzip the RAW image, the output file will have the .img extension
- Format your SD card. Windows and Mac users can just use the SD Card Association’s formatting tool. Linux users need to format it as FAT32
- Write it to a suitable (4GB or larger) SD card using the UNIX tool dd. Windows users should use Win32DiskImager. Apple Mac OS X users should use the RPi Easy SD Card Setup
- After the RAW image is successfully written, insert the SD Card into your Raspberry Pi and power it up
Do not try to drag and drop or otherwise copy over the image without using dd or Win32DiskImager – it won’t work. If you’re still not clear on what to do, the community on the Raspberry Pi Wiki has written a guide for beginners on how to set up your SD card.
Connect to the Raspberry Pi¶
After the Raspberry Pi has booted up, you need to connect to it with ssh. Before you can do this, you need to find the IP address of your Pi. In most networks the IP address is assigned through DHCP and this means it get automatically an available IP address. The following tutorial describes how you can do it from Windows: http://www.hobbytronics.co.uk/raspberry-pi-ssh
The default Raspbian username is pi and the password is raspberry
Raspbian Initial Setup¶
After you connected to the Raspberry Pi, some initial setup is required. By default the RAW image only uses the first 2GB of the SD Card.
1. Start the raspi-config for basic configuration
$ sudo raspi-config
2. This will show you the following screen and select Expand Filesystem by pressing enter
3. After the successfull resizing, the following screen is shown. Press <Ok> button to continue
4. This will show you the first screen again and press few times tab to get to the <Finish> button and press the enter button
5. The tool will ask if you want to reboot your Raspberry Pi, this is required after resizing your SD Card. Select <Yes> and press the enter button
6. The Raspberry Pi will reboot now and re-login when it is available again
Change Timezone !
Change Password !
Change boot to Desktop ?
Update Operating System¶
After the initial setup the Operating System needs to be updated, preventing issues with outdated packages. This can be done with the following 2 commands:
$ sudo apt-get update $ sudo apt-get dist-upgrade
Most common issue's like stalling of the LAN adapter, issues with USB FTDI adapters and SD card compatibility with Raspberry Pi is an outdated firmware.
To do a firmware update:
$ sudo rpi-update
Reboot your Raspberry Pi
See also: https://github.com/Hexxeh/rpi-update
If your download of the update seems to stall (1-2kb/s download) do the following:
$ sudo rm -rf /root/.rpi-firmware $ sudo rpi-update
The complete update should take about 10 minutes (depending on your internet connection)
After the Operating System is installed a few options are highly recommended to install/configure on the system. These are NTP and RRDTool.
It is important that your system has the right time, you can achieve this by running ntpd (Network Time Protocol daemon). The following steps will configure and check it.
The next step is optional, but still highly recommended. This will replace the generic “ntp.ubuntu.com” server with the nearby NTP servers:
$ sudo nano /etc/ntp.confReplace the line with the following ones, the example is if you live in the Netherlands. For a complete list for your local NTP servers, check the following link (and go to the active servers): http://www.pool.ntp.org
server 0.nl.pool.ntp.org server 1.nl.pool.ntp.org server 2.nl.pool.ntp.org server 3.nl.pool.ntp.org
Restart the NTP daemon:
$ sudo /etc/init.d/ntp restart * Stopping NTP server ntpd [ OK ] * Starting NTP server ntpd [ OK ]
Now check if you system is in synchronisation with the NTP servers:
$ ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== metronoom.dmz.c PPS(0) 2 u 18 64 1 28.042 36694.5 0.001 ntp1.hro.nl 126.96.36.199 2 u 17 64 1 30.153 36694.5 0.001 canopus.no-sens 188.8.131.52 2 u 16 64 1 26.090 36694.6 0.001 ntp1.trans-ix.n 184.108.40.206 2 u 15 64 1 25.615 36696.2 0.001
The offset is the difference between your servers clock and the atom clock time. After a few minutes you will see that the difference is corrected.
$ ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== metronoom.dmz.c PPS(0) 2 u 10 64 1 26.920 -0.251 0.001 ntp1.hro.nl 220.127.116.11 2 u 25 64 1 30.268 0.648 0.001 canopus.no-sens 18.104.22.168 2 u 25 64 1 26.651 0.273 0.001 ntp1.trans-ix.n 22.214.171.124 2 u 49 64 1 24.986 2.846 0.001
The Round Robin Database Tool (RRDTool) is the open source industry standard, high performance data logging and graphing system for time series data. With it you can create graphs of the collected data. It is highly recommended to install it, and can be done with the following command:
$ sudo apt-get install rrdtool
After the Raspberry Pi has been setup, Gambas can be installed per normal procedure. Use the following link to go to it directly: Install Gambas
DomotiGa uses the MySQL database to store its data in. The MySQL server hasn't been installed yet, this can be done with the following command.
$ sudo apt-get install mysql-client mysql-server
At the installation of MySQL the new password for the MySQL “root” user is asked, choose a new none-standard password for optimal security. This password will be used later to setup the DomotiGa tables into MySQL.
DomotiGa can be easily downloaded with the following commands:
$ cd ~ $ git clone https://github.com/DomotiGa/DomotiGa.git domotiga
Never run DomotiGa as “root” user.
Only perform the next steps if you are using the Pi as a server with a client on a different machine.
$ mysqladmin -u root -p create domotiga $ mysql -u root -p domotiga <~/domotiga/install/domotiga.sql
Change the configuration file of MySQL. Change the bind option in the file /etc/mysql/my.cnf
$ sudo nano /etc/mysql/my.cnf
old value: bind-addresss = localhost
new value: bind-address = 0.0.0.0
$ mysql -u root -p $ use domotiga; $ GRANT ALL ON *.* to domouser@'%' IDENTIFIED BY 'kung-fu'; $ FLUSH PRIVILEGES; $ quit;
$ sudo /etc/init.d/mysql restart
$ cd ~/domotiga $ ./DomotiGaServer3.gambas
Start DomotiGa Server on startup¶
If would like start DomotiGa Server on startup you can either use rc.local or a Sys-V like init script:
Modify /etc/rc.local and add :
/[path to domotiga]/DomotiGaServer3.gambas
To log the output of the server in a file, modify /etc/rc.local and add the following line:
/[path to domotiga]/DomotiGaServer3.gambas > /[path where you want to log]/output.log
Advice : Delete previous log file at startup by adding in rc.local before the launch of:
rm /[path where you logfile is]/output.log
Debian has a Sys-V like init system for executing commands when the system runlevel changes - for example at bootup and shutdown time.
To add a new service to start when the machine boots you can add the necessary script to the directory /etc/init.d/, see Making scripts run at boot time with Debian.
In the domotiga tools folder you will find a domotigaserver3.debian script that can be used as init.d script. To use it:
$ sudo cp tools/domotigaserver3.debian /etc/init.d/domotigaserver3 $ sudo vi /etc/init.d/domotigaserver3 <-- review/edit the values, especially USER $ sudo chmod 755 /etc/init.d/domotigaserver3 $ sudo update-rc.d domotigaserver3 defaults
After this the server will start and stop automatically at boot respectively shutdown.
You can also use it from the command prompt:
sudo /etc/init.d/domotigaserver3 [start|stop|restart|status]
Boosting Raspberry Performance¶
People sometimes complain about the fact that the Raspberry might be to slow for DomotiGa.
To boost your performance within the warranty see Introducing turbo mode: up-to 50% more performance for free
Using 1000 Mhz will do just fine without any additional cooling and DomotiGa will perform a lot better.
More performance means more heat so, to keep track of your internal Raspberry temperature you can use this script:
#!/bin/bash cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp) cpuTemp1=$(($cpuTemp0/1000)) cpuTemp2=$(($cpuTemp0/100)) cpuTempM=$(($cpuTemp2 % $cpuTemp1)) CPU="$cpuTemp1"."$cpuTempM" GPU=$(/opt/vc/bin/vcgencmd measure_temp)
It will store CPU & GPU temperature in the $CPU and $GPU variables. Use 'print' command to display them after executing the script. (add print $CPU for example).You can put the values into a virtual device in DomotiGa by having a script print them and call the script from using the Shell interface:
- Make sure you have "bc" installed
- Enable the shell interface/plugin
- Create a shell script containing:
#!/bin/bash cpuTemp=$(cat /sys/class/thermal/thermal_zone0/temp) echo "scale=3;$cpuTemp/1000" | bc -l
- Make script executable
- Create a new device using the "Shell Value Device" devicetype and enter at the command field the location of the shell script
Monitoring CPU, virtual memory,disk usage and temperature¶
If you want to monitor the cpu load of your Raspberry PI in a Domotiga graph, you need to run a Python script.
First step is to install the psutil module:
$ sudo apt-get install build-essential python-dev python-pip $ sudo pip install psutil
Next save the following script into /home/pi/domotiga/tools/RPI_USAGE.sh
#!/usr/bin/python from __future__ import division from subprocess import PIPE, Popen import psutil def get_cpu_temperature(): process = Popen(['vcgencmd','measure_temp'],stdout=PIPE) output, _error = process.communicate() return float(output[output.index('=') + 1:output.rindex("'")]); print psutil.cpu_percent(interval=1); print psutil.virtual_memory().percent; print psutil.disk_usage('/').percent; print get_cpu_temperature();
make the scipt executable
sudo chmod +x /home/pi/domotiga/tools/RPI_USAGE.sh
Create a new device using the "Shell Value Device" devicetype and enter at the command field the location of the shell script: /home/pi/domotiga/tools/RPI_USAGE.sh
Enable and set the poll time to maximum 60 seconds. Faster is no use because the graphs have a minimum display of 1 minute.
Work remotely using VNC¶
Sometimes it is not convenient to work directly on the Raspberry Pi. To start a "virtual" graphical session you can use VNC so that you have a Rasberry GUI but displayed on another machine.
Alternatives for VNC¶See:
For instructions on how to install and configure and web-server, see DomotiYii Web Server.
lighttpd or nginx is strongly recommended to use on you Raspberry Pi
Using the GPIO's¶