2015-11-14 18:02:13 +01:00
|
|
|
#include <SoftwareSerial.h>
|
|
|
|
#define RELAY1 8
|
|
|
|
|
2015-11-30 01:38:17 +01:00
|
|
|
SoftwareSerial mySerial(4, 2); // RX, TX (seriale su cui e' settato il bluetooth)
|
2015-11-14 18:02:13 +01:00
|
|
|
|
2015-11-30 01:40:07 +01:00
|
|
|
char statoRele = 'f'; // salviamo lo stato del rele
|
2015-11-14 18:02:13 +01:00
|
|
|
char blueToothVal; //value sent over via bluetooth
|
2015-11-30 01:38:17 +01:00
|
|
|
int contatoreSecondi = 0;
|
2015-11-14 18:02:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// Open serial communications:
|
|
|
|
Serial.begin(9600);
|
|
|
|
Serial.println("Type AT commands!");
|
|
|
|
|
|
|
|
// The HC-06 defaults to 9600 according to the datasheet.
|
|
|
|
mySerial.begin(9600);
|
2015-11-30 01:38:17 +01:00
|
|
|
pinMode(13,OUTPUT); //il led
|
|
|
|
pinMode(RELAY1, OUTPUT); //il rele
|
|
|
|
contatoreSecondi = 0;
|
2015-11-14 18:02:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2015-11-30 01:38:17 +01:00
|
|
|
//Si autospegne se non riceve alcun messaggio per un ora
|
|
|
|
if (contatoreSecondi==3600){
|
|
|
|
digitalWrite(13,LOW); //spengo il led
|
|
|
|
digitalWrite(RELAY1,HIGH); //spengo il rele
|
|
|
|
statoRele = 'f';
|
|
|
|
blueToothVal=' ';
|
|
|
|
|
|
|
|
}
|
2015-11-14 18:02:13 +01:00
|
|
|
|
2015-11-30 01:38:17 +01:00
|
|
|
// Legge l'input dal raspeberry
|
2015-11-14 18:02:13 +01:00
|
|
|
if (mySerial.available()){
|
2015-11-30 01:38:17 +01:00
|
|
|
blueToothVal=(char)mySerial.read();
|
2015-11-14 18:02:13 +01:00
|
|
|
}
|
2015-11-30 01:38:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
if (blueToothVal=='n')//se ricevo n lo accendo
|
|
|
|
{
|
|
|
|
digitalWrite(13,HIGH); //accendo il led
|
|
|
|
digitalWrite(RELAY1,LOW); //accendo il rele
|
2015-11-14 18:02:13 +01:00
|
|
|
mySerial.write('n');
|
2015-11-30 01:38:17 +01:00
|
|
|
statoRele = blueToothVal;
|
2015-11-14 18:02:13 +01:00
|
|
|
blueToothVal=' ';
|
2015-11-30 01:38:17 +01:00
|
|
|
contatoreSecondi = 0;
|
|
|
|
}else if (blueToothVal=='f')//se ricevo f lo spengo
|
|
|
|
{
|
|
|
|
digitalWrite(13,LOW); //spendo il led
|
|
|
|
digitalWrite(RELAY1,HIGH); //spengo il rele
|
2015-11-14 18:02:13 +01:00
|
|
|
mySerial.write('f');
|
2015-11-30 01:38:17 +01:00
|
|
|
statoRele = blueToothVal;
|
|
|
|
blueToothVal=' ';
|
|
|
|
contatoreSecondi = 0;
|
|
|
|
}else if(blueToothVal=='s'){ //richiedo lo stato
|
|
|
|
mySerial.write(statoRele); //mando lo stato
|
2015-11-14 18:02:13 +01:00
|
|
|
blueToothVal=' ';
|
2015-11-30 01:38:17 +01:00
|
|
|
contatoreSecondi = 0;
|
2015-11-14 18:02:13 +01:00
|
|
|
}
|
2015-11-30 01:38:17 +01:00
|
|
|
|
2015-11-14 18:02:13 +01:00
|
|
|
delay(1000);
|
2015-11-30 01:38:17 +01:00
|
|
|
contatoreSecondi += 1;
|
|
|
|
|
2015-11-14 18:02:13 +01:00
|
|
|
}
|