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

References:

https://www.raspberrypi.org/documentation/hardware/sense-hat/

https://www.raspberrypi.org/documentation/remote-access/vnc/

http://raspberrywebserver.com/sql-databases/using-mysql-on-a-raspberry-pi.html

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.


#!/usr/bin/python
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)
cur.execute(sql)
db.commit()
db.close()

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 (!)

SELINUX

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).
Reference:
Security Enhanced Linux Contexts Labeling Files

 

Passwordless login remote with SSH

generate the public keys

[email protected]> ssh-keygen –t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user1/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user1/.ssh/id_rsa.
Your public key has been saved in /home/user1/.ssh/id_rsa.pub.
The key fingerprint is:
31:a1:c2:d7:a6:9f:27:cd:84:1f:f2:7a:e8:7c:34:80

[email protected]> cd /home/user1/.ssh
[email protected]> cp id_rsa.pub authorized_keys

Copy the file authorized_keys to server2 and it is now possible to login without password.

[email protected]> cd /home/user1/.ssh
[email protected]> scp authorized_keys server2:/home/user1/.ssh

How To Make RPM Packages

When working with RedHat Linux systems the best way to ensure that you have the right software installed is by using RPM’s

RPM is a package management system like Solaris packages.

To create packages from source code some or all of the following steps would be needed.

To set it up a build environment and number of configuration files are needed.

First the build environment.

cd /home/asbjorn
mkdir myrpm
cd rpm
mkdir -p BUILD RPMS/i386 SOURCES SPECS SRPMS

In earlier releases of RedHat a file .rpmrc was used.

Now rpmbuild uses the .rpmmacros for default values.

In the first two lines, information about the package creator is provided.

The _topdir line is included, so the package can be build as a user rather than as root.
Do not build packages as root if you can avoid it.

The following lines provide references to the build environment.

cd /home/asbjorn

vi .rpmmacros

%packager      Asbjorn Riedel <[email protected]>
%vendor        The name of the project

%_topdir       /home/asbjorn/myrpm

%{_topdir}/BUILD
%{_topdir}/RPMS
%{_topdir}/RPMS/i386
%{_topdir}/SOURCES
%{_topdir}/SPECS
%{_topdir}/SRPMS

By defining the .spec file we can set up all the parameters needed for building packages of software delivered as tar files.

The %{_prefix} definition can be changed depending on the location where you want your package to be installed, example /opt, /usr or /usr/local. Just change the line with Prefix:  in it.

 vi myrpm/SPECS/testpackage.spec

%define name testpackage
%define version 0.0.1
%define release 1

Summary: A test package for RedHat Linux
Name: %{name}
Version: %{version}
Release: %{release}
Source:
http://www.picourl.com/testpackage/%{name}-%{version}.tar.gz
Vendor: Guess It Must Be Me
URL:
http://www.picourl.com/
License: None
Group: System
Prefix: %{_prefix}

%description
This is a testpackage , specially made for testing

%package devel
Summary: package with stuff to develop %{name}.
Group: System
Requires: %{name} = %{version}

%description devel
The %{name}-devel testpackage contains stuff for building %{name}.

%prep
%setup -q

%build
make

%install
rm -rf $RPM_BUILD_ROOT
make DESTDIR=$RPM_BUILD_ROOT install

%clean
rm -rf $RPM_BUILD_ROOT

%post -p /sbin/ldconfig

%postun -p /sbin/ldconfig

%files
%defattr(-,root,root)
%doc ReadMe
%{_prefix}/lib/lib*.so.*

%files devel
%defattr(-,root,root)
%doc ReadMe
%{_prefix}/bin/*
%{_prefix}/include/*

%changelog
* Thu Mar 27 2008 Asbjorn Riedel <
[email protected]>
– Initial spec file

The %install part contains the actual commands to build the software and it may need to be changed in your case.

 

How to finally do the build …

rpmbuild -ba SPECS/testpackage.specs

How to install Squid on FC 4.0

How to install Squid on FC 4.0

log in on box

become root

get the squid package

yum install squid.i386

Setting up Install Process
Setting up repositories
Reading repository metadata in from local files
Parsing package install arguments
Resolving Dependencies
–> Populating transaction set with selected packages. Please wait.
—> Downloading header for squid to pack into transaction set.
squid-2.5.STABLE13-1.FC4. 100% |=========================| 164 kB 00:00
—> Package squid.i386 7:2.5.STABLE13-1.FC4 set to be updated
–> Running transaction check

Dependencies Resolved

==========================================================
Package Arch Version Repository Size
==========================================================
Installing:
squid i386 7:2.5.STABLE13-1.FC4 updates-released 1.2 M

Transaction Summary
==========================================================
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 1.2 M
Is this ok [y/N]: y
Downloading Packages:
(1/1): squid-2.5.STABLE13 100% |===========| 1.2 MB 00:01
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: squid ########## [1/1]

Installed: squid.i386 7:2.5.STABLE13-1.FC4
Complete!

vi /etc/squid/squid.conf

http_port 127.0.0.1:3128

Configure firefox. Set the proxy to 127.0.0.1 + set the port to 3128

Configure putty

set up a tunnel which will be setup when connecting with ssh to the server