Home Automation with Raspberry Pi: Difference between revisions

From Wurst-Wasser.net
Jump to navigation Jump to search
Line 56: Line 56:
  # ./build
  # ./build


Create a file <tt>hum_temp.c</tt> with this content (Code taken from [http://browse.feedreader.com/c/Raspberry_Pi_Blog/227396670 here] (Rahul Kar) and [http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-the-raspberry-pi/ here]):
Create a file <tt>hum_temp3.c</tt> with this content (Code taken from [http://browse.feedreader.com/c/Raspberry_Pi_Blog/227396670 here] (Rahul Kar) and [http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-the-raspberry-pi/ here]):
 
/* from http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-the-raspberry-pi/
Based on hum_temp2.c this read data until he gets a valid set. Then prints it once.
*/
  #include <wiringPi.h>
  #include <wiringPi.h>
  #include <stdio.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdlib.h>
  #include <stdint.h>
  #include <stdint.h>
  #define MAX_TIME 85
  #define MAXTIMINGS 85
  #define DHT11PIN 7
  #define DHTPIN 7
  int dht11_val[5]={0,0,0,0,0};
  int dht11_dat[5] = { 0, 0, 0, 0, 0 };
   
   
  void dht11_read_val()
  int read_dht11_dat(char *message)
  {
  {
uint8_t lststate=HIGH;
uint8_t laststate = HIGH;
uint8_t counter=0;
uint8_t counter = 0;
uint8_t j=0,i;
uint8_t j = 0, i;
  float farenheit;
float f;
  for(i=0;i<5;i++)
        int err=0;
  {
   
    dht11_val[i]=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);
  }
  }
  pinMode(DHT11PIN,OUTPUT);
   
digitalWrite(DHT11PIN,LOW);
  int main( void )
delay(18);
digitalWrite(DHT11PIN,HIGH);
delayMicroseconds(40);
pinMode(DHT11PIN,INPUT);
for(i=0;i<MAX_TIME;i++)
{
counter=0;
while(digitalRead(DHT11PIN)==lststate)
{
  counter++;
delayMicroseconds(1);
if(counter==255)
break;
}
lststate=digitalRead(DHT11PIN);
if(counter==255)
      break;
  // top 3 transistions are ignored
if((i>=4)&&(i%2==0)){
dht11_val[j/8]<<=1;
if(counter>16)
dht11_val[j/8]|=1;
j++;
}
  }
// verify checksum and print the verified data
if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)))
{
farenheit=dht11_val[2]*9./5.+32;
printf("Humidity: %d.%d%% Temperature: %d.%dC (%.1f *F)\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3],farenheit);
}
else
  {
  {
printf("Invalid Data!!\n");
//printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );
}
}
   
   
int main(void)
if ( wiringPiSetup() == -1 )
{
exit( 1 );
//printf("Interfacing Temperature and Humidity Sensor (DHT11) With Raspberry Pi\n");
if(wiringPiSetup()==-1)
{
exit(1);
}
   
   
dht11_read_val();
        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;
return(0);
  }
  }


Compile it:
Compile it:
  # gcc -o sensor hum_temp.c -L/usr/local/lib -lwiringPi
  # gcc -o sensor3 hum_temp3.c -L/usr/local/lib -lwiringPi


Test it:
Test it:
  ./sensor
  ./sensor3
  Humidity: 46.0% Temperature: 22.0C (71.6 *F)
  Humidity: 35.0% Temperature: 22.0C (71.6F)


=== Set up your shell language ===
=== Set up your shell language ===

Revision as of 12:28, 6 December 2018

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

  • Raspberry Pi (I formerly used this Rev 2 for http://www.wurst-wasser.net/wiki/index.php/Tomat-O-Mat - I have a garden now and don’t need it anymore)
  • Power supply for the Pi
  • DHT11 (side project RaspberryPi Humidity and Temperature Sensor)
  • DS18B20 (side project RaspberryPi Temperature Sensor)
  • some resistors, cables, wires, boards, and the usual soldering equipment

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 this read data until he 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

Set up camera