Home Automation with Raspberry Pi: Difference between revisions

From Wurst-Wasser.net
Jump to navigation Jump to search
Line 48: Line 48:


Since there seems to be no kernel module for DHT11, do this:
Since there seems to be no kernel module for DHT11, do this:
==== Installing DHT11 driver ====
  # cd /home/pi/var/humidity_and_temperature
  # cd /home/pi/var/humidity_and_temperature
  # git clone git://git.drogon.net/wiringPi
  # git clone git://git.drogon.net/wiringPi
Line 53: Line 55:
  # git pull origin
  # git pull origin
  # ./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] from Rahul Kar):
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX_TIME 85
#define DHT11PIN 7
int dht11_val[5]={0,0,0,0,0};
void dht11_read_val()
{
uint8_t lststate=HIGH;
uint8_t counter=0;
uint8_t j=0,i;
float farenheit;
for(i=0;i<5;i++)
{
    dht11_val[i]=0;
}
pinMode(DHT11PIN,OUTPUT);
digitalWrite(DHT11PIN,LOW);
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");
}
}
int main(void)
{
//printf("Interfacing Temperature and Humidity Sensor (DHT11) With Raspberry Pi\n");
if(wiringPiSetup()==-1)
{
exit(1);
}
dht11_read_val();
return 0;
}
Compile it:
# gcc -o sensor hum_temp.c -L/usr/local/lib -lwiringPi
Test it:
./sensor
Humidity: 46.0% Temperature: 22.0C (71.6 *F)


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

Revision as of 11:38, 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/var/humidity_and_temperature
# git clone git://git.drogon.net/wiringPi
# cd wiringPi/
# git pull origin
# ./build

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

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX_TIME 85
#define DHT11PIN 7
int dht11_val[5]={0,0,0,0,0};

void dht11_read_val()
{
uint8_t lststate=HIGH;
uint8_t counter=0;
uint8_t j=0,i;
float farenheit;
for(i=0;i<5;i++)
{
    dht11_val[i]=0;
}
pinMode(DHT11PIN,OUTPUT);
digitalWrite(DHT11PIN,LOW);
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");
}
}

int main(void)
{
//printf("Interfacing Temperature and Humidity Sensor (DHT11) With Raspberry Pi\n");
if(wiringPiSetup()==-1)
{ 
	exit(1);
} 

dht11_read_val();

return 0;
}

Compile it:

# gcc -o sensor hum_temp.c -L/usr/local/lib -lwiringPi

Test it:

./sensor
Humidity: 46.0% Temperature: 22.0C (71.6 *F)

Set up your shell language

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

Set up 1-wire

Set up camera