Home Automation with Raspberry Pi

From Wurst-Wasser.net
Jump to navigation Jump to search

The Why Of Fry

I moved to a house and would like to monitor and automate some things:

  • log room temperatures
  • log outside temperature
  • tbd

Preparation

Parts list

Setting up the Pi

Since the Pi was running Debian Wheezy (which is AFAIK obsolete) I decided to install Raspbian Stretch.

I skipped NOOBS and did the installation myself (with help from https://www.raspberrypi.org/documentation/installation/installing-images/README.md and https://www.raspberrypi.org/documentation/installation/installing-images/mac.md ):

# diskutil list
[…]
/dev/disk5 (external, physical):
  #:                       TYPE NAME                    SIZE       IDENTIFIER
  0:     FDisk_partition_scheme                        *15.8 GB    disk5
  1:             Windows_FAT_32                         58.7 MB    disk5s1
  2:                      Linux                         15.8 GB    disk5s2
# diskutil unmountDisk /dev/disk5
Unmount of all volumes on disk5 was successful
# dd bs=1m if=2018-11-13-raspbian-stretch-full.img of=/dev/rdisk5 conv=sync
5052+0 records in
5052+0 records out
5297405952 bytes transferred in 486.102151 secs (10897722 bytes/sec)
# diskutil eject /dev/rdisk5
Disk /dev/rdisk5 ejected
#

After that, enable interfaces (ssh, camera port or 1-wire) as needed.

Update the Software

Use the graphical interface or some CLI (apt) tool. Setting up WiFi might help :)

Install Tools

# dpkg --configure -a
# apt-get install locate
# apt-get install gnuplot
# apt-get install git

Since there seems to be no kernel module for DHT11, do this:

Installing DHT11 driver

# cd /home/pi/bin
# git clone git://git.drogon.net/wiringPi
# cd wiringPi/
# git pull origin
# ./build

Create a file hum_temp3.c with this content (Code taken from here (Rahul Kar) and here):

/* from http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-the-raspberry-pi/ 

Based on hum_temp2.c. Reads data until it gets a valid set. Then prints it once.

*/

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS	85
#define DHTPIN		7
int dht11_dat[5] = { 0, 0, 0, 0, 0 };

int read_dht11_dat(char *message)
{
   uint8_t laststate	= HIGH;
   uint8_t counter		= 0;
   uint8_t j		= 0, i;
   float	f; 
       int			err=0;

   dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;

   pinMode( DHTPIN, OUTPUT );
   digitalWrite( DHTPIN, LOW );
   delay( 18 );
   digitalWrite( DHTPIN, HIGH );
   delayMicroseconds( 40 );
   pinMode( DHTPIN, INPUT );

   for ( i = 0; i < MAXTIMINGS; i++ )
   {
       counter = 0;
       while ( digitalRead( DHTPIN ) == laststate )
       {
           counter++;
           delayMicroseconds( 1 );
           if ( counter == 255 )
           {
               break;
           }
       }
       laststate = digitalRead( DHTPIN );

       if ( counter == 255 )
           break;

       if ( (i >= 4) && (i % 2 == 0) )
       {
           dht11_dat[j / 8] <<= 1;
           if ( counter > 16 )
               dht11_dat[j / 8] |= 1;
           j++;
       }
   }

   if ( (j >= 40) &&
       (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) )
   {
       f = dht11_dat[2] * 9. / 5. + 32;
       sprintf(message, "Humidity: %d.%d%%, Temperature: %d.%dC (%.1fF)\n",
           dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
               err=0;
   }
       else
       {
           sprintf(message, "Data not good, skip\n" );
               err=1;
   }

 return (err);
}

int main( void )
{
   //printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );

   if ( wiringPiSetup() == -1 ) exit( 1 );

       char message[1024];
       int error=1;
       int tries=0;
   while ( error!=0 && tries < 42 )
   {
       tries++;
       error=read_dht11_dat(message);
       delay( 1000 ); 
   }

       if (error == 0)
       {
          printf(message);
       }
       else
       {
          printf("Failed to read data (%i tries)", tries);
       }

   return(0);
}


Compile it:

# gcc -o sensor3 hum_temp3.c -L/usr/local/lib -lwiringPi

Test it:

./sensor3
Humidity: 35.0%, Temperature: 22.0C (71.6F)

Set up your shell language

I strongly recommend keeping your shell (or complete OS) in english.

Set up 1-wire

RaspberryPiDHT11.png
Remember that the Pi revisions have different pinouts! This will work for Revison 2, that is board revision 0004[1]. If you don't need humidity readings, I strongly recommend using a DS18B20 instead of the DHT11, as showed in RaspberryPi Temperature Sensor, because you can use the 1-wire kernel modules for reading the DS18B20, which makes it much easier to read and extend.

Set up camera