205 lines
6.0 KiB
C++
Executable File
205 lines
6.0 KiB
C++
Executable File
/*
|
||
Blink
|
||
Turns on an LED on for one second, then off for one second, repeatedly.
|
||
|
||
Most Arduinos have an on-board LED you can control. On the Uno and
|
||
Leonardo, it is attached to digital pin 11. If you're unsure what
|
||
pin the on-board LED is connected to on your Arduino model, check
|
||
the documentation at http://arduino.cc
|
||
|
||
This example code is in the public domain.
|
||
|
||
modified 8 May 2014
|
||
by Scott Fitzgerald
|
||
*/
|
||
#include <RCSwitch.h>
|
||
#include <Narcoleptic.h>
|
||
#define SEND_MESSAGE_DELAY 30000 // Ne pas dépasser 32000 !! Delay in ms between each value's extraction
|
||
#define SEND_433_PAUSE 160 // 16 multiple
|
||
const unsigned long TIME = 512;
|
||
const unsigned long TWOTIME = TIME*2;
|
||
|
||
#define RADIATEUR_POWER 11
|
||
#define RADIATEUR_PILOTE 12
|
||
|
||
#define TEMPERATURE_EXTINCTION 1900
|
||
//1900
|
||
#define TEMPERATURE_HORS_GEL 1850
|
||
#define TEMPERATURE_CHAUFFE 1750
|
||
|
||
//#define DEBUG true
|
||
|
||
boolean started = false;
|
||
int id = 0;
|
||
|
||
RCSwitch mySwitch = RCSwitch();
|
||
|
||
|
||
// Cas
|
||
// 12 = Courant général 0 éteint 1 allumé
|
||
// 11 = Mode radiateur 0 hors gel 1 confort
|
||
|
||
|
||
|
||
// the setup function runs once when you press reset or power the board
|
||
void setup() {
|
||
// initialize digital pin 11 as an output.
|
||
pinMode(RADIATEUR_POWER, OUTPUT);
|
||
pinMode(RADIATEUR_PILOTE, OUTPUT);
|
||
pinMode(13, OUTPUT);
|
||
digitalWrite(RADIATEUR_POWER, HIGH);
|
||
digitalWrite(RADIATEUR_PILOTE, HIGH);
|
||
#ifdef DEBUG
|
||
Serial.begin(9600);
|
||
#endif
|
||
mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
|
||
}
|
||
|
||
void loop() {
|
||
if (mySwitch.available()) {
|
||
|
||
int value = mySwitch.getReceivedValue();
|
||
|
||
if (value == 0) {
|
||
#ifdef DEBUG
|
||
Serial.print("Unknown encoding");
|
||
#endif
|
||
} else {
|
||
long data = mySwitch.getReceivedValue();
|
||
//Serial.print("Received ");
|
||
//Serial.println( data );
|
||
// Serial.print(" / ");
|
||
// Serial.print( mySwitch.getReceivedBitlength() );
|
||
// Serial.print("bit ");
|
||
// Serial.print("Protocol: ");
|
||
// Serial.println( mySwitch.getReceivedProtocol() );
|
||
//Serial.println("");
|
||
//delay(1000); // wait for a second
|
||
|
||
|
||
if (data == 111269) {
|
||
started = true;
|
||
// id = 0;
|
||
#ifdef DEBUG
|
||
Serial.println("started");
|
||
#endif
|
||
while (data == 111269) {
|
||
data = mySwitch.getReceivedValue();
|
||
}
|
||
}
|
||
if (data == 962111) {
|
||
started = false;
|
||
id = 0;
|
||
#ifdef DEBUG
|
||
Serial.println("En pause");
|
||
#endif
|
||
while (data == 962111) {
|
||
data = mySwitch.getReceivedValue();
|
||
}
|
||
mySwitch.resetAvailable();
|
||
|
||
// On se met en pause
|
||
mySwitch.disableReceive();
|
||
delayMicroseconds(TWOTIME*8);
|
||
|
||
Narcoleptic.delay(SEND_MESSAGE_DELAY);
|
||
Narcoleptic.delay(SEND_MESSAGE_DELAY);
|
||
mySwitch.enableReceive(0);
|
||
//Serial.println("Arret");
|
||
} else if (data == 1969 || data == 2069 || data == 2169 || data == 2269 || data == 2369) {
|
||
//Serial.print("id=");
|
||
//Serial.println(data);
|
||
// if (id == 0) {
|
||
id = data;
|
||
// } else {
|
||
// started = false;
|
||
// id = 0;
|
||
// }
|
||
} else {
|
||
|
||
if (started && id == 1969) {
|
||
long currentVcc = readVcc();
|
||
digitalWrite(13, HIGH);
|
||
#ifdef DEBUG
|
||
Serial.print("Demarré ");
|
||
Serial.print("id=");
|
||
Serial.print(id);
|
||
Serial.print(" data=");
|
||
Serial.println(data);
|
||
#endif
|
||
// Supérieur à 19° STOP
|
||
if (data >= TEMPERATURE_EXTINCTION) {
|
||
digitalWrite(RADIATEUR_POWER, HIGH);
|
||
digitalWrite(RADIATEUR_PILOTE, HIGH);
|
||
// Supérieur à 18 ==> Veille
|
||
} else if (data >= TEMPERATURE_HORS_GEL) {
|
||
digitalWrite(RADIATEUR_POWER, HIGH);
|
||
digitalWrite(RADIATEUR_PILOTE, LOW);
|
||
|
||
} else if (data < TEMPERATURE_CHAUFFE) {
|
||
digitalWrite(RADIATEUR_POWER, LOW);
|
||
digitalWrite(RADIATEUR_PILOTE, LOW);
|
||
}
|
||
// Reset pour éviter la modification plusieurs fois
|
||
started = false;
|
||
id = 0;
|
||
if (currentVcc <= 3500) {
|
||
delay(5);
|
||
digitalWrite(13, LOW);
|
||
delay(5);
|
||
digitalWrite(13, HIGH);
|
||
delay(5);
|
||
} else if (currentVcc < 3200) {
|
||
delay(5);
|
||
digitalWrite(13, LOW);
|
||
delay(5);
|
||
digitalWrite(13, HIGH);
|
||
delay(5);
|
||
digitalWrite(13, LOW);
|
||
delay(5);
|
||
digitalWrite(13, HIGH);
|
||
delay(5);
|
||
} else {
|
||
delay(5);
|
||
}
|
||
digitalWrite(13, LOW);
|
||
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
mySwitch.resetAvailable();
|
||
}
|
||
}
|
||
|
||
|
||
//--------------------------------------------------------------------------------------------------
|
||
// Read current supply voltage
|
||
//--------------------------------------------------------------------------------------------------
|
||
long readVcc() {
|
||
bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
|
||
long result;
|
||
// Read 1.1V reference against Vcc
|
||
#if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
||
ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
|
||
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
||
ADMUX = _BV(MUX3) | _BV(MUX2);
|
||
#else
|
||
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328
|
||
#endif
|
||
// ADCSRB = 0;
|
||
|
||
delay(2); // Wait for Vref to settle
|
||
ADCSRA |= _BV(ADSC); // Convert
|
||
while (bit_is_set(ADCSRA,ADSC));
|
||
result = ADCL;
|
||
result |= ADCH<<8;
|
||
result = 1126400L / result; // Back-calculate Vcc in mV
|
||
// ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
|
||
|
||
// analogReference(DEFAULT);
|
||
|
||
return result; // Vcc in millivolts
|
||
}
|