sudo apt-get update

advertisement
Contents
General review comments .................................................................................................................. 2
Opening up.......................................................................................................................................... 3
Compatibility ....................................................................................................................................... 3
Extra parts to get going ....................................................................................................................... 3
Setting up the SDCard ......................................................................................................................... 4
Powering up ........................................................................................................................................ 4
Network connectivity .......................................................................................................................... 5
sudo..................................................................................................................................................... 6
Updating, upgrading and installing ..................................................................................................... 7
MySql database ................................................................................................................................... 7
Installing .......................................................................................................................................... 7
Connecting and checking ................................................................................................................ 7
Creating a database ........................................................................................................................ 8
Creating a user ................................................................................................................................ 8
Setting permissions ......................................................................................................................... 9
Creating a table ............................................................................................................................... 9
Finishing up in MySql ...................................................................................................................... 9
Temperature and Humidity sensing ................................................................................................... 9
Setting up GPIO ................................................................................................................................. 11
Writing values to the database ......................................................................................................... 12
Automating the readings .................................................................................................................. 12
Visualising the readings .................................................................................................................... 13
Setting up plotly ................................................................................................................................ 13
Sending data to plotly ....................................................................................................................... 14
Appendix A : Pi2SaveToDb.py ........................................................................................................... 16
Appendix B : Pi2ExportToPlotly.py.................................................................................................... 17
General review comments
Good / Room
for
improvement
Issue
Good
Quick-start guide in box
Good
Footprint and pinout compatibility with the older B and B+ models
Room for
improvement
Onboard memory with ability to startup.
I think it would be really useful in light of a sizeable percentage of the
target users if you could take it straight out of the box, plug in a kbd,
mouse, monitor, network cable, SDCard and be presented with options
onscreen. Imaging an SDCard is easy for some, but pretty technical for
others. I appreciate this means some extra onboard memory, but I think
it's probably worth it.
Room for
improvement
Real time clock.
I am planning a remote system without regular internet connection and
also recording timestamped data. In this scenario, lack of an RTC is quite
a problem. I will be bolting one on, but I wonder if an integrated one
would be good.
Room for
improvement
GPIO voltage levels.
I appreciate (some of) the reasons for the GPIO pins working at 3v3, but I
immediately ran into complications when I wanted to connect an RTC as
many of them work at 5V. I also discovered considerable ignorance
about this on the forums, especially in the area of how to manage it! Is
there a way to have the GPIO pins either

Individually switchable between 3V and 5V

able to work with either, using some kind of (active) pullups or
other more sophisticated technology
OR
The two immediate solutions for me are


Use an appropriate peripheral i.e. a 3v3 RTC
Use level shifters, bidirectional if necessary
It might also be useful to have an easily accessible definitive webpage
somewhere about the issue and how to handle it - there's a lot of
muddle out there.
Good
Speed improvement.
Not measured, but subjectively felt faster
Opening up
Box is thicker than the pi B+ box, mainly because it contains a Quick-start and safety
instruction manual with 8 pages for each of 16 languages.
(From Element14, other suppliers may be different).
The B+ box had a single sheet of regulatory and safety information.
Compatibility
Footprint and pinout compatibility with the older B and B+ models means cases and plugin
boards should still all fit.
Extra parts to get going
I immediately bought myself a 32GB microSD card and a plastic case.
The memory is needed, the plastic case isn't but if you're moving things around it's
invaluable.
The power (red) and disk (green) leds are easily visible if the case is partially transparent.
I bought a 32GB class 10 memory card. The class identifies the speed. A class 10 claims to
have a minimum sustained throughput of 10MB/S which should be adequate for the pi.
I tend to run my older pi B+ pi across the network using SSH, and I will do the same with the
new pi 2, so I don't need anything further.
You could get
 Keyboard
 Mouse
 HDMI connection to a monitor
Setting up the SDCard
I've opted to install NOOBS as this is the simplest option.
I'm following the instruction in http://www.raspberrypi.org/help/noobs-setup/
Started torrent downloading the NOOBS image from
http://www.raspberrypi.org/downloads/
In parallel (who says men can't multi-task?), I put the SDcard into my laptop, discovered it
was already formatted, but decided to use the recommended SDFormatter anyway just to
be sure.
It's obtainable from https://www.sdcard.org/downloads/formatter_4/, but I already had it.
I went for a full(overwrite) format and provided a volume label of RPI2NOOBS.
Despite checking, I managed to start formatting my USB key which was plugged in as well be warned!
When the NOOBS image (NOOBS_v1_4_0.zip in my case) has downloaded, copy the
contents (not the file itself) onto the sdcard.
Powering up
Put the card in the pi 2.
Connect a keyboard and monitor as minimum, preferably also a mouse and network
connection, then connect the USB power.
Note that you need a decent power supply, I suggest a minimum of 1A.
My original pi failed to start due to low power from a low output main USB power supply
(phone recharger). It stopped at the rainbow-coloured screen screen with no text.
Having no pi experience at the time, it took me a while to figure what was going on.
The red led should be steadily on.
With the power on, in a while, the monitor should show a dialog allowing you to select the
operating system.
With Raspbian highlighted (use mouse or up/down keys), select it (mouse or space key),
then "i" for install.
Click "Yes" or press Enter and allow the system to start building the image.
The green led should be flickering indicating that the disk (sdcard) is being accessed and the
progress bar onscreen should be moving slowly.
This will take a few minutes.
You should eventually get a dialog that says "OS(es) installed successfully". Click on OK or
press Enter and the pi will reboot.
Eventually, it will bring you back to "raspi-config" allowing you to make some basic changes.
We are going to connect remotely, so
Select "Advanced" and then "A4 SSH" and "Enable" and "Finish"
Note that this enables console (text) operation by default i.e no Graphical User Interface,
but you can always start the GUI with "startx" from a console prompt.
Network connectivity
I am going to connect remotely via ssh as this is best for remote applications such as mine,
but first we need to find out the ip address of the pi.
Make sure you have a network connection.
More specifically, you need
 The pi connected to the network via an ethernet cable. You could use wifi, but the
initial setup is more complicated.
 A device that provides ip addresses via dhcp. It is likely that your broadband/wifi
router fulfils this function.
 Another computer such as a desktop or laptop that you can use as the front-end to the
pi.
You can now reboot the pi with this command
sudo shutdown -r now
You can also shutdown with this
sudo shutdown -h now
The -r means restart and the -h means halt.
You can enter these command either directly into the pi using the keyboard, or from a remote ssh terminal.
If you are going to shutdown, you should always shut the pi down using the above -h command before
removing the power.
After you have entered the command, wait a few seconds after the green led stops flickering and then switch
off the power.
After the pi has rebooted and come up again, let the system return to the prompt
(raspberrypi login:) and two lines above it you should see
My IP address is 192.168.0.3
(or similar, depending on how your network is configured)
If you can't see this, it is likely your network isn't connected or configured properly.
Record the ip address as you will need it.
From here on in, if your remote connection works, you should no longer need the
monitor/keyboard/mouse combination connected to the pi as you will be talking to it
remotely.
It may be best to leave them connected at first, until you are sure everything is working.
You will need an SSH client installed on the desktop or laptop you want to work from. I
recommend Putty if you are using Windows:
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Using Putty or similar, connect to the ip address you recorded above
You should see a certificate confirmation dialog like this:
Click "Yes"
Enter pi and raspberry for the username and password (and don't forget to change them
later!)
sudo
You have already prefixed command with "sudo" and you will do this in the future as well.
sudo means "super-user do"
You normally don't act as super-user, but just as the user you logged on as: "pi" in this case.
However, normal users don't have permission to complete some operations, such as
shutting the pi down.
If you tried the shutdown command without prefixing it with sudo
shutdown -r now
you would be rebuffed. Try it and you will see something like this:
shutdown: you must be root to do that!
super-user and root are effectively the same thing, so the command
sudo shutdown -r now
means "super-user do shutdown -(and restart) now"
One way to work around this requirement is to use this command after logging in:
sudo su
This leaves you in super-user mode until you log out or type exit and saves having to prefix
everything with sudo.
But beware, you have a lot of power as super-user.
Another, less dangerous method (because it only works for one command) is this:
If you see the "you must be root" message or similar, after typing a command, try this:
sudo !!
This means "run the previous command as super-user"
!! means "previous command" and there are many variants of this e.g. try
history
to see all your previous commands, and
!3
to run the command labelled 3 in the history, or
sudo !3
to run it as super-user
Updating, upgrading and installing
Linux keeps a record of what is available to be installed. You keep this list up-to-date with
sudo apt-get update
To actually upgrade your pi to the latest versions of everything run
sudo apt-get upgrade
It's a good idea to do this regularly and especially if you are about to install something you
want, to make sure that you get the latest version.
You install a specific package by naming it after the "sudo apt-get install" command which
we will do next for the database install
MySql database
I am going to use a database to store the readings from the temperature and humidity
sender that I am going to use.
There are a number of ways to do this, and I have decided to use a proper database,
especially as this will be usable for other projects.
The two main choices are
 a lightweight approach based on SqlLite
 a more industrial approach based on MySql
I have opted for the more robust approach with MySql
If you go with a lighter option such as SqlLite the following instructions will be incorrect.
Installing
Having done an update and upgrade (see above), I now install MySql
sudo apt-get install mysql-server
When you are asked for a root password, choose one, and record it somewhere else. I
recommend KeePass:
http://keepass.info/
We also need the python additions for MySql (Python is the programming language we will
use to manage the functions we need)
sudo apt-get install python-mysqldb
Connecting and checking
We now need to prove that MySql is working, so we connect to the MySql database
mysql -p -u root
When requested, enter the root password you chose and saved.
After a few lines of blurb, you will get a mysql> cursor
type
status
to see if everything looks OK.
You should get something like this
-------------mysql Ver 14.14 Distrib 5.5.41, for debian-linux-gnu (armv7l) using
readline 6.2
Connection id:
Current database:
Current user:
SSL:
Current pager:
Using outfile:
Using delimiter:
Server version:
Protocol version:
Connection:
Server characterset:
Db
characterset:
Client characterset:
Conn. characterset:
UNIX socket:
Uptime:
43
root@localhost
Not in use
stdout
''
;
5.5.41-0+wheezy1 (Debian)
10
Localhost via UNIX socket
latin1
latin1
utf8
utf8
/var/run/mysqld/mysqld.sock
15 min 40 sec
Threads: 1 Questions: 584 Slow queries: 0 Opens: 441
Open tables: 41 Queries per second avg: 0.621
--------------
Flush tables: 2
The actual numbers will vary a bit
Creating a database
Now, let’s create a database to store the temperature and humidity data
I'm calling my database "environment" here, but you can choose any name you want. I
suggest restricting yourself to letters, numbers and the underscore character.
create database environment;
Creating a user
Now, I create a user. Note that this is a database user specific to mySql and is different from
the user such as "pi" that you used to log on to the raspberry pi.
create user 'envuser'@'localhost' identified by 'envpassword';
envuser is the name I have given to this user and envpassword is the password.
You can change either or both of these, and again, keep a copy of the details. e.g. in the
KeePass app you downloaded earlier.
Note that databases like MySql tend to use single quotes more than double-quotes: Just be
careful to use exactly what you see in the examples.
On a really bad day, you may find a command with both single and double quotes in it.
For interest, if you want to delete the user and start again, use
drop user 'envuser'@'localhost';
Setting permissions
Now, let's give our user permission to work with the environment database
grant all privileges on environment.* to 'envuser'@'localhost';
Creating a table
and now, we create a table, within our database, to store the data itself.
You can think of a database as a collection of different tables (among other things) but we
will only use a single table this case.
create table environment.envdata ( TIMESTAMP datetime NOT NULL, TEMPERATURE real
NOT NULL, HUMIDITY real NOT NULL);
There's a lot here:
We're creating a table called envdata in the environment database
We're adding 3 columns
 timestamp (of type datetime)
 temperature (of type real)
 humidity (of type real)
In all three cases, "NOT NULL" means that there must be a value in each column - it can't be
empty.
Finishing up in MySql
To finish entering mysql commands and return to the normal pi cursor
exit
Temperature and Humidity sensing
I elected to use a simple sensor that I bought online for a few pounds.
It's called a DHT22 and the datasheet is here:
https://www.adafruit.com/datasheets/DHT22.pdf
It has 4 pins and 3 of these need to be connected to the pi.
The DHT22 pins are numbered 1 - 4 from left to right while looking at the front (with the
grille)
The pi pins refer to the 40 pin GPIO header, see
http://www.element14.com/community/servlet/JiveServlet/previewBody/73950-102-4309126/GPIO_Pi2.png
Looking at this diagram
http://www.element14.com/community/servlet/JiveServlet/showImage/38-16260199073/BPlusInfographic.jpg
pin 1 is a the bottom left of the GPIO header, pin 2 is directly above it i.e. nearer the board
edge.
Pins 39 and 40 are closest to the USB ports on the right with 39 being directly below 40.
You will need to connect pins 1,2 and 4 of the DHT22 to the GPIO header. Pin 3 is redundant
and shouldn't be connected.
DHT22 Pin
Function
RPi fn/pin
RPI pin
1
VDD
3.3VDC
1
2
DATA - signal
GPIO04, GPIO_GCLK
7
3
NULL
-
-
4
GND
GND
9
There are multiple possibilities: it is up to you how you do it.
Personally, I found an old 40 way IDE hard drive cable and cut it in half.
IDE, or Integrated Drive Electronics is the technology used to connect hard drives before
SATA and other technologies came along.
The connector itself is a type called IDC, or Insulation Displacement Connector, but I digress.
The connector plugs straight onto the pi, making sure it's the right way around.
In my case, the pin 1 cable had a red stripe, so this plugged in to the end nearest the LEDs
and the Display connector.
Be careful as some of these connectors had one of the centre holes blocked and the
matching pin missing on the receiving board to ensure correct alignment.
The pi doesn't have any missing pins, so you can only use a connector that has all 40 holes
free and able to receive a pin.
At the other end I soldered together a primitive board containing the DHT22 temperature
and humidity sensor.
Setting up GPIO
We need a way of communicating with the DHT22, the temperature and humidity sensor,
via the GPIO pins.
There are various ways of doing this, I have used the Adafruit library.
We start by installing this, so type the following commands one after another
cd ~
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo apt-get update
sudo apt-get install build-essential python-dev
and hit Enter when it asks
Do you want to continue [Y/n]?
When you see a capitalised option ("Y" for yes) and a lower-case option ("n" for no)
together, you can just hit Enter to take the capitalised one.
Then, carry on with
sudo python setup.py install
cd ~
/usr/bin/gksu - root idle
Note:If you are running remotely, without a locally attached display,you may see a message
similar to this:
(gksu:5803): Gtk-WARNING **: cannot open display:
Don't worry about it.
Change directory:
cd Adafruit_Python_DHT/examples/
and run the Python script
sudo ./AdafruitDHT.py 22 4
You should see sensible values such as these:
Temp=19.0*C Humidity=62.5%
If the values are markedly outside these ranges, such as a temperature of -999.0, there is
probably a problem with the connection to the sensor.
Writing values to the database
So now, we want to adapt the Python script to write to our database rather than just display
values on the screen.
I have created a new script Pi2SaveToDb.py based on the original AdafruitDHT.py.
It is in the Appendix below.
Essentially, I have
 Hard-coded the usage of a DHT22 into the code
 Hard-coded the usage of pi header pin 7 (GPIO #4) into the code
 Connected to the database we created earlier
 Written values to the database rather than the screen
You can either copy this file onto the pi, or paste it into an editor like nano, if you are
running remotely via SSH.
Once there, try running it. It doesn't need any parameters because they are now hardcoded.
sudo ./Pi2SaveToDb.py
If you get this
sudo: ./Pi2SaveToDb.py: command not found
It's probably a permissions issue. Change the permissions:
chmod a+x Pi2SaveToDb.py
This command says "for all users add execute privilege"
Assuming you don't see any errors, these values should be in the database now, so let's
have a look.
Log onto the database
mysql -p -u root
When requested, enter the (database) root password you chose and saved above.
After a few lines of blurb, you will get a mysql> cursor
type
select * from environment.envdata;
You should see something like this
+---------------------+-------------+----------+
| TIMESTAMP
| TEMPERATURE | HUMIDITY |
+---------------------+-------------+----------+
| 2015-04-11 18:44:09 |
18.3 |
40.5 |
+---------------------+-------------+----------+
1 row in set (0.00 sec)
These are the values you just recorded, with the timestamp
Automating the readings
We use cron for this.
cron is a scheduler and allows us to run specific jobs at predefined times without going near
the pi.
We can list the jobs already set up like this:
crontab -l
If the result is
no crontab for pi
It just meansthat nothing has yet been configured
We create jobs by editing the cron table:
crontab -e
and we're presented with a file in our editor that gives us some help.
# means comment, so you can (probably) see that there are no jobs running
Go to the bottom of the file and add this line:
*/15 * * * * /root/Adafruit_Python_DHT/examples/Pi2SaveToDb.py
The 5 possible star positions (we've put "*/15" in the first) stand for minutes, hours, date,
month and day of week.
That first "*/15" means every 15 minutes (as opposed to "15" which would mean 15
minutes past the hour)
The final section defines the command we want to run, so our command means
Every 15 minutes, run Pi2SaveToDb.py
We could do various other things e.g.
0 9 * * 1 /root/Adafruit_Python_DHT/examples/Pi2SaveToDb.py
which means
At 9:00 am on Monday, run our script.
(The "0 9" means 0 minutes after 9am and the "1" means Monday: day of week here is 0-7,
Sunday -Sunday , with Sunday being 7 as well as 0)
This cronjob (to use the jargon) should now run every 15 minutes.
Leave the pi running for a couple of hours and then look in the database: you should see
entries made by the cron job without your intervention.
If there isn't anything, try looking in the log:
grep CRON /var/log/syslog
this command searches the system log for any lines with the word "CRON"
Visualising the readings
A load of data in a database is all very well, but it's useful to actually see it in a visual format.
Again, there are a number of ways to do this.
I was keen to be able to see the results remotely with minimal effort.
I settled on using a website called plotly: https://plot.ly/
Setting up plotly
These two pages will give you a flavour:
https://github.com/plotly/raspberrypi
https://plot.ly/raspberry-pi/tmp36-temperature-tutorial/
This is the way I went about it:
First install the required client side code
sudo apt-get install python-pip
sudo pip install plotly
pip install plotly --upgrade
sudo apt-get install python-pandas
You needn't worry about it, but python-pandas is the "Python Data Analysis Library" and
helps us to easily plot a nice looking graph.
Note that it can take several minutes to install.
Then create an account for yourself on the plotly site.
Once logged in, goto Settings -> API settings and record your username, API Key and
streaming API token ids (two of them).
Using these values, run this command on your pi:
python -c "import plotly; plotly.tools.set_credentials_file(username='username',
api_key='APIKey', stream_ids=['streamId1', 'streamId2'])"
If you're confused,
Look at the "Installation" paragraph here: https://plot.ly/python/getting-started/
get your values from https://plot.ly/settings/api
and then look at the "Initialization" paragraph further down, here:
https://plot.ly/python/getting-started/
Sending data to plotly
We now use some a Python script to retrive our readings from the database and send them
to plotly.
I have created a new script Pi2SaveToDb.py to do this.
It is in the Appendix below.
Essentially, I
 retrieve all the values from the database
 create two structures with the "Scatter" command, one temperature, one humidity
 generate a layout to plot the data
 send all this to plotly
If you want to alter the look of the graph, the "Layout" command is the one to play with.
These links will probably help:
http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s0_gettingstarted/s0_getting-started.ipynb
https://plot.ly/python/
https://plot.ly/python/axes/
You can either copy the script onto the pi, or paste it into an editor like nano, if you are
running remotely via SSH.
Once there, try running it. It doesn't need any parameters because we have hard-coded
them into the script.
Change directory:
cd Adafruit_Python_DHT/examples/
and run the Python script
sudo ./Pi2ExportToPlotly.py
You will probably see



a row count
a warning about an SSLContext object
a comment about embedding th eplot
If you do, everything has probably worked fine.
If you see another error, look for a reference to the line number in Pi2ExportToPlotly.py and
see if you can fix it.
Now, go to your plotly account and you should be able to see a graph like this:
(The blue trace is temperature against the LH axis and the orange trace is humidity against
the RH axis, with date/time along the bottom.)
Sorted!
Appendix A : Pi2SaveToDb.py
You may need to change the values 'envuser', 'envpassword', 'environment' and
‘environment.envdata’
#!/usr/bin/python
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
#
#
#
#
#
#
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import Adafruit_DHT
import MySQLdb
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
# We're assuming and hardcoding a DHT22 and GPIO 4
humidity, temperature = Adafruit_DHT.read_retry(22, 4)
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
db = MySQLdb.connect('localhost', 'envuser', 'envpassword', 'environment')
curs=db.cursor()
try:
curs.execute ('insert into environment.envdata values(now(), ' + forma
t(temperature, '.1f') + ',' + format(humidity, '.1f') + ')')
db.commit()
except Exception, e:
print str(e)
print "Error: the database is being rolled back"
db.rollback()
db.close()
else:
print 'Failed to get reading.'
Appendix B : Pi2ExportToPlotly.py
You may need to change the values 'yourplotlyusername', 'yourplotlypassword', ‘envuser',
'envpassword', 'environment' and ‘environment.envdata’
#!/usr/bin/python
# -*- coding: utf-8 -*import os, sys
import MySQLdb
import pandas as pd
import plotly.plotly as py
from plotly.graph_objs import *
py.sign_in('yourplotlyusername', 'yourplotlypassword')
db = MySQLdb.connect('localhost', 'envuser', 'envpassword', 'environment')
cursor=db.cursor()
cursor.execute("select TIMESTAMP, TEMPERATURE, HUMIDITY from environment.envdata")
;
rows = cursor.fetchall()
print 'Found ' + str(len(rows)) + ' rows'
df = pd.DataFrame( [[ij for ij in i] for i in rows] )
df.rename(columns={0: 'TIMESTAMP', 1: 'TEMP', 2: 'HUMIDITY'}, inplace=True);
df = df.sort(['TIMESTAMP'], ascending=[1]);
x=df['TIMESTAMP']
temp = Scatter(
x=x,
y=df['TEMP']
)
hum = Scatter(
x=x,
y=df['HUMIDITY'],
yaxis='y2'
)
layout = Layout(
title='Environment',
xaxis=XAxis( type='date', title='Time', gridcolor='gray' ),
yaxis=YAxis( title='Temp C', range=[12, 25], titlefont=Font(color='blue'),
gridcolor='blue', tickfont=Font(color='blue')),
yaxis2=YAxis( title='Humidity %', range=[25,69], overlaying='y', side='rig
ht', titlefont=Font(color='orange'), gridcolor='orange', tickfont=Font(color='ora
nge'))
)
data = Data([temp, hum])
fig = Figure(data=data, layout=layout)
py.iplot(fig, filename='Environment')
Download