Easy Raspberry PI and Sense Hat Datalogger

Decided to have look at the Raspberry PI after reading about the Astro PI project

The Raspberry PI have been around for some time and exist now in several versions. The one I purchased was the Raspberry PI B+

Raspberry PI
Raspberry PI

Together with that I got the Sense Hat module.

Raspberry PI, Sense Hat
Raspberry PI, Sense Hat

Besides these two components, a micro SD (16 GB in my case) is needed and an USB power supply.

Download the linux image and copy it to the SD ( great guide here )

I downloaded NOOBS and installed the image (formatted too many times, really). Then I hooked up the components and inserted the SD card. Added a monitor, keyboard and mouse and finally power.

Initial Raspbian window running on Raspberry PI
Initial Raspbian window running on Raspberry PI

That was easy. I now have a running, working linux system. There were some more steps to do, to get really ready, here is a video of a working system and what the sense HAT module is. (from The Raspberry PI Guy)

I have installed some python packages for interface, database and communication. The process is simple as long as you have connected the Raspberry PI to the net.

Update packages that may be old in the NOOBS image. Open a terminal and enter the following commands

sudo apt-get update
sudo apt-get install sense-hat pillow tightvncserver mysql-server python-mysqldb





I had some issues with the VNC server, when X: is running then display 0 is already in use. (I choose to use display 10 )

One other important step is to set the password for the PI user if you want to use ssh from a remote system.

Now the system is (almost) ready. Some code is needed to read the data from the Sense HAT and write them in the database.

from sense_hat import SenseHat
import time
import MySQLdb
db    = MySQLdb.connect(host="localhost", # your host, usually localhost
user  ="user", # your username
passwd="password", # your password
db    ="database") # name of the data base
cur   = db.cursor()
sense = SenseHat()
temperature = sense.get_temperature()
pressure = sense.get_pressure()
humidity = sense.get_humidity()
# formula see below
temp = round ( 0.0071*temperature*temperature+0.86*temperature-10.0,1)
humidity = round ( humidity*(2.5-0.029*temperature),1)
pressure = round(pressure, 1)
dateTime = time.strftime("%Y%m%d%H%M")
sql="insert into weather(datetime,temp,pressure,humidity) values ('%s','%s','%f','%f','%f');" % (dateTime,temp,pressure,humidity)

Calculation for the temperature. As the sensor is close to processor and led’s the value returned is imprecise. This is the formula to compensate.

It is now possible to get one reading from the Raspberry PI Sense HAT and write it to the database.
201512252355 23.3 978.9 67
201512281740 23.3 972.9 64.1

Getting password less login with SSH to work when all hope is lost.

Had access to two servers. On one I could login without password, on the second I could not.
No root access, not that it mattered in this case.

Googled a lot, and everybody told me to

chmod 700 .ssh
chmod 600 .ssh/authorized_keys

and it should work, still the computer said NO …

There was one more thing (!)


To really find out about SELINUX (ask Google)
Checking the SELINUX permissions
[[email protected] ~]$ ls -dZ .ssh .ssh/authorized_keys
drwx——. asbjorn users unconfined_u:object_r:home_root_t:s0 .ssh
-rw——-. asbjorn users unconfined_u:object_r:home_root_t:s0 .ssh/authorized_keys

debug1: Next authentication method: publickey
debug1: Offering public key: [email protected]
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /home/asbjorn/.ssh/identity
debug1: Trying private key: /home/asbjorn/.ssh/id_rsa
debug1: Trying private key: /home/asbjorn/.ssh/id_dsa
debug1: Next authentication method: password
[email protected]’s password:
debug1: Authentication succeeded (password).
chcon -t user_home_t .ssh .ssh/authorized_keys

[[email protected] ~]$ ls -dZ .ssh .ssh/authorized_keys
drwx——. asbjorn users unconfined_u:object_r:user_home_t:s0 .ssh
-rw——-. asbjorn users unconfined_u:object_r:user_home_t:s0 .ssh/authorized_keys

debug1: Next authentication method: publickey
debug1: Offering public key: [email protected]
debug1: Server accepts key: pkalg ssh-rsa blen 277
debug1: Authentication succeeded (publickey).
Security Enhanced Linux Contexts Labeling Files