Raspberry PI Temperatur Station
Temperaturanzeigen gibt es zwar wie Sand am Meer.
Aber mit dem Raspberry macht es mehr Spass.
Neben dem RaspiLCD habe ich mir spasseshalber auch den Sensor
SHT21 bei Emsystech gekauft. Hat hier 3 Monate rumgelegen, bis ich mich damit beschäftigt habe. Das Teil ist echt praktisch. Der Sensor misst Luftfeuchte und Temperatur als SHT21,
ca. 20Eur. Nur Temperatur kostet ca. 3 Eur als STS21
(Farnell oder RS-Components) ![]() |
| http://www.sensirion.com/de/produkte/feuchte-temperatur/feuchtesensor-sht21/ |
Zunächst das Programm für den Temperatursensor anpassen oder besser abspecken.
QuelleCode: www.emsystech.de
QuelleCode: www.emsystech.de
Das Programm gethum und gettemp. Einfach die printf Anweisung am Ende des Programms für Temperatur und Luftfeuchte einkommentieren.
//--------------------------------------------------------------------------------------------------
// Author: Martin Steppuhn: emsystech.de
// History: 05.09.2012 Initial version "Quick and Dirty"
// 01.11.2012 Flexible PIN Configuration for I2C and Harrdwaredetection for Raspberry
//--------------------------------------------------------------------------------------------------
#include <stdio.h>
#include "bcm2835.h"
#include "raspi.h"
#include <unistd.h>
#include <stdlib.h>
#include "i2c.h"
#include "sht21.h"
#include "time.h"
#define PIN_LED 4
//--------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
int16 Temperature;
uint8 Humidity;
uint8 SHT21_Error;
float Temp;
FILE *fp;
time_t TimeCounter;
time_t TimeCounterLocal;
struct tm * Time;
int Led,HwRev;
Led = 0;
// printf("Raspi-SHT21 V2.0.0 by Martin Steppuhn (www.emsystech.de) [" __DATE__ " " __TIME__"]\n");
if (!bcm2835_init())
{
printf("bcm2835_init() failed!\r\n");
return 1;
}
bcm2835_gpio_fsel(PIN_LED,BCM2835_GPIO_FSEL_OUTP); // LED: Output
bcm2835_gpio_clr(PIN_LED); // LED: Register auf low
HwRev = GetRaspberryHwRevision();
// printf("RaspberryHwRevision=%i\r\n",HwRev);
if(HwRev < 2) SI2C_SetPort(1,0); // Hardware Revision 1.0
else SI2C_SetPort(3,2); // Hardware Revision 2.0
Led++;
if(Led & 1) bcm2835_gpio_set(PIN_LED);
else bcm2835_gpio_clr(PIN_LED);
time ( &TimeCounter );
SHT21_Error = SHT21_Read(&Temperature,&Humidity);
Temp = Temperature;
Temp /= 10;
Time = localtime (&TimeCounter); // wichtig f?r Timezone und Sommerzeit
TimeCounterLocal = TimeCounter - timezone;
if(Time->tm_isdst) TimeCounterLocal += 3600; // Sommerzeit
// Author: Martin Steppuhn: emsystech.de
// History: 05.09.2012 Initial version "Quick and Dirty"
// 01.11.2012 Flexible PIN Configuration for I2C and Harrdwaredetection for Raspberry
//--------------------------------------------------------------------------------------------------
#include <stdio.h>
#include "bcm2835.h"
#include "raspi.h"
#include <unistd.h>
#include <stdlib.h>
#include "i2c.h"
#include "sht21.h"
#include "time.h"
#define PIN_LED 4
//--------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
int16 Temperature;
uint8 Humidity;
uint8 SHT21_Error;
float Temp;
FILE *fp;
time_t TimeCounter;
time_t TimeCounterLocal;
struct tm * Time;
int Led,HwRev;
Led = 0;
// printf("Raspi-SHT21 V2.0.0 by Martin Steppuhn (www.emsystech.de) [" __DATE__ " " __TIME__"]\n");
if (!bcm2835_init())
{
printf("bcm2835_init() failed!\r\n");
return 1;
}
bcm2835_gpio_fsel(PIN_LED,BCM2835_GPIO_FSEL_OUTP); // LED: Output
bcm2835_gpio_clr(PIN_LED); // LED: Register auf low
HwRev = GetRaspberryHwRevision();
// printf("RaspberryHwRevision=%i\r\n",HwRev);
if(HwRev < 2) SI2C_SetPort(1,0); // Hardware Revision 1.0
else SI2C_SetPort(3,2); // Hardware Revision 2.0
Led++;
if(Led & 1) bcm2835_gpio_set(PIN_LED);
else bcm2835_gpio_clr(PIN_LED);
time ( &TimeCounter );
SHT21_Error = SHT21_Read(&Temperature,&Humidity);
Temp = Temperature;
Temp /= 10;
Time = localtime (&TimeCounter); // wichtig f?r Timezone und Sommerzeit
TimeCounterLocal = TimeCounter - timezone;
if(Time->tm_isdst) TimeCounterLocal += 3600; // Sommerzeit
// Ausgabe Humindity. Fuer Anweisung Temperatur einfach auskommentieren
printf("%u\n",Humidity);
// printf("%u\n",Temp);
if(SHT21_Error) printf("ERROR=0x%X\r\n",SHT21_Error);
return(0);
}
Ich habe nun die beiden Programme gettemp und gethum compiliert:
pi@raspberrypi ~ $ sudo ./gettemp
21.9
pi@raspberrypi ~ $
21.9
pi@raspberrypi ~ $
pi@raspberrypi ~ $ sudo ./gethum
42
pi@raspberrypi ~ $
42
pi@raspberrypi ~ $
mehr will ich nicht haben. Die Werte einfach übergeben. Den Rest mache ich mit der Shell:
Mein erstes Script:
#!/bin/bash
# for i in `seq 1`
# do
sleep 1
clear
# Alles messen:
# /opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -d "'" -f1
cpu=$(/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -d "'" -f1)
temp=$(sudo /home/pi/gettemp)
hum=$(sudo /home/pi/gethum)
printf "CPU Temp: %s\n" "$cpu"
printf "Temp: %s\n" "$temp"
printf "Feuchte: %s\n" "$hum"
# alle 60 Sekunden ausgeben
# sleep 60
# done
Der Output:
pi@raspberrypi ~ $ ./temp.sh
CPU Temp: 47.6
Temp: 21.7
Feuchte: 41
pi@raspberrypi ~ $
Ich habe mir ein kleines Rückfahrdisplay gekauft um die Temperatur auf diesem Display anzuzeigen. Man kann kaum was erkennen.
Im Internet bin ich auf ein nettes Program gestossen: figlet
Das Ergebnis sieht so aus:
pi@raspberrypi ~ $ figlet -f banner $(sudo /home/pi/gettemp)
##### ### #####
# # # # # #
# # # # #
##### # # #####
# # # ### # #
# # # ### # #
####### ### ### #####
Erweitere Anzeige (lange her mit der Shell):
pi@raspberrypi ~ $ figlet -f banner $(echo temp) $(sudo /home/pi/gettemp)
Mein erstes Script:
#!/bin/bash
# for i in `seq 1`
# do
sleep 1
clear
# Alles messen:
# /opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -d "'" -f1
cpu=$(/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -d "'" -f1)
temp=$(sudo /home/pi/gettemp)
hum=$(sudo /home/pi/gethum)
printf "CPU Temp: %s\n" "$cpu"
printf "Temp: %s\n" "$temp"
printf "Feuchte: %s\n" "$hum"
# alle 60 Sekunden ausgeben
# sleep 60
# done
Der Output:
pi@raspberrypi ~ $ ./temp.sh
CPU Temp: 47.6
Temp: 21.7
Feuchte: 41
pi@raspberrypi ~ $
Ich habe mir ein kleines Rückfahrdisplay gekauft um die Temperatur auf diesem Display anzuzeigen. Man kann kaum was erkennen.
Im Internet bin ich auf ein nettes Program gestossen: figlet
Das Ergebnis sieht so aus:
pi@raspberrypi ~ $ figlet -f banner $(sudo /home/pi/gettemp)
##### ### #####
# # # # # #
# # # # #
##### # # #####
# # # ### # #
# # # ### # #
####### ### ### #####
Erweitere Anzeige (lange her mit der Shell):
pi@raspberrypi ~ $ figlet -f banner $(echo temp) $(sudo /home/pi/gettemp)
##### ##### #
##### ###### # # ##### # # # # ##
# # ## ## # # # # # #
# ##### # ## # # # ##### ##### #
# # # # ##### # # ### #
# # # # # # # ### #
# ###### # # # ####### ####### ### #####
Das Schript:
pi@raspberrypi ~ $ cat /home/pi/temp.sh
clear
figlet -f banner $(echo CPU ) $(/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -d "'" -f1)
figlet -f banner $(echo Temp) $(sudo /home/pi/gettemp)
figlet -f banner $(echo Hum ) $(sudo /home/pi/gethum)
##### ###### # # ##### # # # # ##
# # ## ## # # # # # #
# ##### # ## # # # ##### ##### #
# # # # ##### # # ### #
# # # # # # # ### #
# ###### # # # ####### ####### ### #####
Das Schript:
pi@raspberrypi ~ $ cat /home/pi/temp.sh
clear
figlet -f banner $(echo CPU ) $(/opt/vc/bin/vcgencmd measure_temp | cut -d "=" -f2 | cut -d "'" -f1)
figlet -f banner $(echo Temp) $(sudo /home/pi/gettemp)
figlet -f banner $(echo Hum ) $(sudo /home/pi/gethum)
Der Output:
##### ###### # # # # #####
# # # # # # # # # # # #
# # # # # # # # # # #
# ###### # # # # # # ######
# # # # ####### ####### ### #
# # # # # # # ### # #
##### # ##### # # ### #####
####### ##### ##### ###
# ###### # # ##### # # # # # #
# # ## ## # # # # # #
# ##### # ## # # # ##### ##### # #
# # # # ##### # # ### # #
# # # # # # # ### # #
# ###### # # # ####### ####### ### ###
# # # #
# # # # # # # # ##
# # # # ## ## # # # #
####### # # # ## # # # #
# # # # # # ####### #
# # # # # # # #
# # #### # # # #####
Jetzt muss sich der User pi in das System automatisch einloggen:
pi@raspberrypi ~ $ cat /etc/inittab
# modifyed: # 1:2345:respawn:/sbin/getty --noclear 38400 tty1
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
Der Startscript ist hier:
pi@raspberrypi ~ $ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
# sudo /home/pi/temp.sh
exit 0
Ich mach das lieber mit einem cronjob
pi@raspberrypi ~ $ crontab cron.tab
pi@raspberrypi ~ $ crontab -l
#-----------------------------------------------------------------
# Shell variable for cron
# SHELL=/bin/bash
# PATH variable for cron
# PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11
#M S T M W Befehl
#-----------------------------------------------------------------
# 5 9-20 * * * /home/username/script/script1.sh > /dev/null
# */10 * * * * /usr/bin/script2.sh > /dev/null 2>&1
# 59 23 * * 0,4 cp /pfad/zu/datei /pfad/zur/kopie
# * * * * * DISPLAY=:0 LANG=de_DE.UTF-8 zenity --info --text "Beispiel für das Starten eines Programmes mit GUI"
# 0 0 * * * backup
# Legende
# 1 Fünf Minuten nach jeder vollen Stunde zwischen 9 und 20 Uhr (also 9:05, 10:05, ..., 20:05)
# 2 Alle 10 Minuten
# 3 Jeden Sonntag und Donnerstag um 23:59
# 4 Jede Minute ein Programm mit GUI, das die Display- und die Sprach-Variable benötigt
# 5 Jeden Tag Punkt Mitternacht 00:00 Uhr
#-----------------------------------------------------------------
*/1 * * * * /home/pi/temp.sh>/dev/tty1
ToDo:
Jetzt suche ich noch einen passenenden Sensor für Temperaturbereich von -100° - 0°. Die Auswahl ist da schon etwas kleiner. Die Biologen benötigen einen Alarmmelder, wenn der Kühlschrank (-80°) auf unter -60° sinkt. Dann solle eine Mail die Proben retten helfen und der Rapsberry hat eine echte Aufgabe.
##### ###### # # # # #####
# # # # # # # # # # # #
# # # # # # # # # # #
# ###### # # # # # # ######
# # # # ####### ####### ### #
# # # # # # # ### # #
##### # ##### # # ### #####
####### ##### ##### ###
# ###### # # ##### # # # # # #
# # ## ## # # # # # #
# ##### # ## # # # ##### ##### # #
# # # # ##### # # ### # #
# # # # # # # ### # #
# ###### # # # ####### ####### ### ###
# # # #
# # # # # # # # ##
# # # # ## ## # # # #
####### # # # ## # # # #
# # # # # # ####### #
# # # # # # # #
# # #### # # # #####
Jetzt muss sich der User pi in das System automatisch einloggen:
pi@raspberrypi ~ $ cat /etc/inittab
# modifyed: # 1:2345:respawn:/sbin/getty --noclear 38400 tty1
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
Der Startscript ist hier:
pi@raspberrypi ~ $ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
# sudo /home/pi/temp.sh
exit 0
Ich mach das lieber mit einem cronjob
pi@raspberrypi ~ $ crontab cron.tab
pi@raspberrypi ~ $ crontab -l
#-----------------------------------------------------------------
# Shell variable for cron
# SHELL=/bin/bash
# PATH variable for cron
# PATH=/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11
#M S T M W Befehl
#-----------------------------------------------------------------
# 5 9-20 * * * /home/username/script/script1.sh > /dev/null
# */10 * * * * /usr/bin/script2.sh > /dev/null 2>&1
# 59 23 * * 0,4 cp /pfad/zu/datei /pfad/zur/kopie
# * * * * * DISPLAY=:0 LANG=de_DE.UTF-8 zenity --info --text "Beispiel für das Starten eines Programmes mit GUI"
# 0 0 * * * backup
# Legende
# 1 Fünf Minuten nach jeder vollen Stunde zwischen 9 und 20 Uhr (also 9:05, 10:05, ..., 20:05)
# 2 Alle 10 Minuten
# 3 Jeden Sonntag und Donnerstag um 23:59
# 4 Jede Minute ein Programm mit GUI, das die Display- und die Sprach-Variable benötigt
# 5 Jeden Tag Punkt Mitternacht 00:00 Uhr
#-----------------------------------------------------------------
*/1 * * * * /home/pi/temp.sh>/dev/tty1
Aufgebaut sieht das so aus. Der Raspberry und sein Display
Die Anzeige der CPU Temperatur, Temperatur und Luftfeuchte vom Sensor
auf dem 4" Mini Display. Echt brauchbar die Anzeige
ToDo:
- Als nächstes: Nachts soll der Bildschirm ausgeschaltet werden: OPEN
cronjob mit Steuerung der Bildschirmausgabe: tvservice
sudo /opt/vc/bin/tvservice -help
- Bildschirm soll immer an bleiben: OPEN
How do I prevent the screen from going blank?
sudo sh -c "TERM=linux setterm -blank 0 >/dev/tty0"
changing BLANK_TIME to zero in /etc/kbd/config
/etc/kbd/config
dies ist noch zu überprüfen - Den Mailer basteln, habe ich schon fertig. OK
- Watchdogs. Routinen für den Dauerbetrieb OPEN
- SD Karte komplett aus ReadOnly setzen OPEN
Jetzt suche ich noch einen passenenden Sensor für Temperaturbereich von -100° - 0°. Die Auswahl ist da schon etwas kleiner. Die Biologen benötigen einen Alarmmelder, wenn der Kühlschrank (-80°) auf unter -60° sinkt. Dann solle eine Mail die Proben retten helfen und der Rapsberry hat eine echte Aufgabe.
Version 3:
*************************** Programm main.c ***************************
//--------------------------------------------------------------------------------------------------
// Author: Martin Steppuhn
//--------------------------------------------------------------------------------------------------
//=== Includes =====================================================================================
#include "std_c.h"
#include <stdio.h>
#include <linux/i2c-dev.h>
#include <stdlib.h>
#include "i2c.h"
#include "sht21.h"
#include "raspi.h"
//--------------------------------------------------------------
int main(int argc, char **argv)
{
uint32 Counter;
int16 Temperature;
uint8 Humidity;
uint8 HwRev;
char Mode;
Counter = 0;
Mode = 0;
HwRev = GetRaspberryHwRevision();
if((argc > 1) && ((argv[1][0]=='S') || (argv[1][0]=='L') || (argv[1][0]=='C'))) Mode = argv[1][0];
if(!Mode)
{
}
if(HwRev < 2) I2C_Open("/dev/i2c-0"); // HW Revision 1.0
else I2C_Open("/dev/i2c-1"); // HW Revision 2.0
I2C_Setup(I2C_SLAVE, 0x40);
if(I2cError)
{
I2C_PrintError();
exit(1);
}
SHT21_Read(&Temperature,&Humidity);
if(Sht21Error == 0)
{
printf("%.1f\t%u\n",((float)Temperature)/10,Humidity);
// printf("%.1f\n",((float)Temperature)/10);
// printf("%u\n",Humidity);
}
else
{
PrintSht21Error();
}
I2C_Close();
return(0);
*************************** Programm main.c ends ***************************

Keine Kommentare:
Kommentar veröffentlichen