greenhouse

greenhouse Commit Details

Date:2012-05-13 12:46:57 (1 year 5 days ago)
Author:Ciaran Gultnieks
Branch:master
Commit:4c717cf766fc88dbd14a6daad075dde89c329ffa
Parents: 3272de446dc83c81a7acf4ba33b355fdd12c158e
Message:Save some power, and build without silly IDE

Changes:
Darduino/greenhouse.pde (full)
Aarduino/.gitignore (full)
Aarduino/Makefile (full)
Aarduino/dht11.cpp (full)
Aarduino/dht11.h (full)
Aarduino/greenhouse.cpp (full)

File differences

arduino/.gitignore
1
bin/
arduino/Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
PORT=/dev/ttyACM0
ARDUINO_DIR=/usr/share/arduino
BOARD_TYPE=arduino
BAUD_RATE=115200
MCU=atmega328p
DF_CPU=16000000
CORE_C_FILES=pins_arduino wiring wiring_digital
CORE_CPP_FILES=main HardwareSerial Print
CPP_FILES=greenhouse dht11
ARDUINO_CORE=$(ARDUINO_DIR)/hardware/arduino/cores/arduino
INCLUDE=-I. -I$(ARDUINO_DIR)/hardware/arduino/cores/arduino
CC=/usr/bin/avr-gcc
CPP=/usr/bin/avr-g++
AVR_OBJCOPY=/usr/bin/avr-objcopy
AVRDUDE=/usr/bin/avrdude
AVRDUDE_CONF=/etc/avrdude.conf
CC_FLAGS=-g -Os -w -Wall -ffunction-sections -fdata-sections -fno-exceptions -std=gnu99
CPP_FLAGS=-g -Os -w -Wall -ffunction-sections -fdata-sections -fno-exceptions
all:clean compile upload
clean:
@echo '# *** Cleaning...'
rm -rf bin/
compile:
@echo '# *** Compiling...'
mkdir -p bin
for cpp_file in ${CPP_FILES}; do \
$(CPP) -c -mmcu=$(MCU) -DF_CPU=$(DF_CPU) $(INCLUDE) \
$(CPP_FLAGS) $$cpp_file.cpp -o bin/$$cpp_file.o; \
done
for core_c_file in ${CORE_C_FILES}; do \
$(CC) -c -mmcu=$(MCU) -DF_CPU=$(DF_CPU) $(INCLUDE) \
$(CC_FLAGS) $(ARDUINO_CORE)/$$core_c_file.c \
-o bin/$$core_c_file.o; \
done
for core_cpp_file in ${CORE_CPP_FILES}; do \
$(CPP) -c -mmcu=$(MCU) -DF_CPU=$(DF_CPU) $(INCLUDE) \
$(CPP_FLAGS) $(ARDUINO_CORE)/$$core_cpp_file.cpp \
-o bin/$$core_cpp_file.o; \
done
$(CC) -mmcu=$(MCU) -lm -Wl,--gc-sections -Os \
-o bin/project.elf bin/*.o
$(AVR_OBJCOPY) -O ihex -R .eeprom \
bin/project.elf \
bin/project.hex
@echo '# *** Compilation complete'
reset:
@echo '# *** Resetting...'
stty --file $(PORT) hupcl
sleep 0.1
stty --file $(PORT) -hupcl
upload:
@echo '# *** Uploading...'
$(AVRDUDE) -q -V -p $(MCU) -C $(AVRDUDE_CONF) -c $(BOARD_TYPE) \
-b $(BAUD_RATE) -P $(PORT) \
-U flash:w:bin/project.hex:i
@echo '# *** Upload complete'
arduino/dht11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// FILE: dht11.cpp
// VERSION: 0.4.0
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// HISTORY:
// George Hadjikyriacou - Original version (??)
// Mod by SimKard - Version 0.2 (24/11/2010)
// Mod by Rob Tillaart - Version 0.3 (28/03/2011)
// + added comments
// + removed all non DHT11 specific code
// + added references
// Mod by Rob Tillaart - Version 0.4 (17/03/2012)
// + added 1.0 support
//
#include "dht11.h"
// returnvalues:
// 0 : OK
// -1 : checksum error
// -2 : timeout
int dht11::read(int pin)
{
// BUFFER TO RECEIVE
uint8_t bits[5];
uint8_t cnt = 7;
uint8_t idx = 0;
// EMPTY BUFFER
for (int i=0; i< 5; i++) bits[i] = 0;
// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(18);
digitalWrite(pin, HIGH);
delayMicroseconds(40);
pinMode(pin, INPUT);
// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return -2;
loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return -2;
// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (int i=0; i<40; i++)
{
loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return -2;
unsigned long t = micros();
loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return -2;
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
idx++; // next byte!
}
else cnt--;
}
// WRITE TO RIGHT VARS
// as bits[1] and bits[3] are allways zero they are omitted in formulas.
humidity = bits[0];
temperature = bits[2];
uint8_t sum = bits[0] + bits[2];
if (bits[4] != sum) return -1;
return 0;
}
//
// END OF FILE
//
arduino/dht11.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// FILE: dht11.h
// VERSION: 0.4.0
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// URL: http://arduino.cc/playground/Main/DHT11Lib
//
// HISTORY:
// George Hadjikyriacou - Original version
// see dht.cpp file
//
#ifndef dht11_h
#define dht11_h
#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#define DHT11LIB_VERSION "0.4.0"
class dht11
{
public:
int read(int pin);
int humidity;
int temperature;
};
#endif
//
// END OF FILE
//
arduino/greenhouse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <WProgram.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/io.h>
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 3
void setup()
{
// By default, all pins (leaving TX/RX alone) set to inputs with
// pullups enabled, to ensure no unwanted power usage.
DDRD &= B00000011;
DDRB = B00000000;
PORTD |= B11111100;
PORTB |= B11111111;
// Not using the ADC, so disable to save power.
ADCSRA = 0;
PRR = 1;
Serial.begin(9600);
Serial.println("Ciaran's Greenhouse");
Serial.println();
}
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable();
}
// Sleep the device for 8 seconds...
void sleep8secs()
{
MCUSR = 0;
WDTCSR = _BV(WDCE) | _BV(WDE);
WDTCSR = _BV(WDIE) | _BV(WDP3) | _BV(WDP0);
wdt_reset();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
MCUCR = _BV(BODS) | _BV(BODSE);
MCUCR = _BV(BODS);
sleep_cpu();
sleep_disable();
}
void loop()
{
int chk = DHT11.read(DHT11PIN);
switch (chk)
{
case 0:
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
break;
case -1:
Serial.println("Checksum error");
break;
case -2:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
// Go to sleep.
for(int i=0;i<2;i++)
sleep8secs();
}
arduino/greenhouse.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 3
void setup()
{
Serial.begin(9600);
Serial.println("Ciaran's Greenhouse");
Serial.println();
}
void loop()
{
Serial.println("\n");
int chk = DHT11.read(DHT11PIN);
switch (chk)
{
case 0:
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
break;
case -1:
Serial.println("Checksum error");
break;
case -2:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
delay(10000);
}

Archive Download the corresponding diff file

Branches