//-------------
// SETA43
//
06/05/2014
//-------------
#include
<avr/io.h>
#include
<avr/interrupt.h>
#include
<avr/sleep.h>
#include
<Wire.h>
#include
<VirtualWire.h>
#include
"DHT.h"
#define
DHTPIN
3 // what pin we're connected to
//
Uncomment whatever type you're using!
//#define
DHTTYPE DHT11 // DHT 11
#define
DHTTYPE
DHT22 // DHT 22 (AM2302)
//#define
DHTTYPE DHT21 // DHT 21 (AM2301)
unsigned int ih,it;
int sensorValue1, sensorValue2;
DHT dht(DHTPIN, DHTTYPE);
const int TX_DIO_Pin = 2;
int z,contador;
void setup() {
pinMode(13, OUTPUT);
pinMode(12, INPUT);
vw_set_tx_pin(TX_DIO_Pin);
vw_set_ptt_inverted(true);
vw_setup(2000);
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
watchdogOn(); // Turn on
the watch dog timer.
//Desactiva conversor A/D
ADCSRA = ADCSRA
& B01111111;
ACSR = B10000000;
contador=0;
}
void loop() {
unsigned int Data;
byte TxBuffer[2];
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(h))
{
Serial.println("Failed to
read from DHT");
}
else
{
ih=h*100;
it=t*100;
sensorValue1=222;
sensorValue2=contador;
digitalWrite(13, HIGH);
TxBuffer[0] = 33333 >> 8;
TxBuffer[1] = 33333;
vw_send((byte *)TxBuffer, 2);
vw_wait_tx();
TxBuffer[0] = ih >> 8;
TxBuffer[1] = ih;
vw_send((byte *)TxBuffer, 2);
vw_wait_tx();
TxBuffer[0] = it >> 8;
TxBuffer[1] = it;
vw_send((byte *)TxBuffer, 2);
vw_wait_tx();
TxBuffer[0] = sensorValue1 >> 8;
TxBuffer[1] = sensorValue1;
vw_send((byte *)TxBuffer, 2);
vw_wait_tx();
TxBuffer[0] = sensorValue2 >> 8;
TxBuffer[1] = sensorValue2;
vw_send((byte *)TxBuffer, 2);
vw_wait_tx();
Serial.print(ih); Serial.print(" % ");
Serial.print(it); Serial.print(" C ");
Serial.println(contador);
delay(500);
digitalWrite(13, LOW);
for(z=0;z<60 && HIGH==digitalRead(12) ;z++)goToSleep();
contador++;
if(contador>100) contador=0;
delay(100);
digitalWrite(13, HIGH);
}
}
void goToSleep()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set
sleep mode.
sleep_enable(); // Enable
sleep mode.
sleep_mode(); // Enter
sleep mode.
sleep_disable(); // Disable
sleep mode after waking.
}
void watchdogOn()
{
//Fuente
http://www.fiz-ix.com/2012/11/low-power-arduino-using-the-watchdog-timer/
// Clear the reset flag, the WDRF
bit (bit 3) of MCUSR.
MCUSR = MCUSR
& B11110111;
WDTCSR = WDTCSR
| B00011000;
//WDTCSR = B00000101; //0.5S
WDTCSR = B00000110; //1S
//WDTCSR = B00100001; //8S
// Enable the watchdog timer
interupt.
WDTCSR = WDTCSR
| B01000000;
MCUSR = MCUSR
& B11110111;
}
ISR(WDT_vect)
{
//No hace nada
}
|