123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include <SoftwareSerial.h>
- #define RELAY1 8
- SoftwareSerial mySerial(4, 2); // RX, TX (seriale su cui e' settato il bluetooth)
- char statoRele = 'n'; // salviamo lo stato del rele
- char blueToothVal; //value sent over via bluetooth
- int contatoreSecondi = 0;
- const int buttonPin = 11; //pin del pulsante
- int buttonState = 0; // stato del pulsante
- const int ledPin = 13; //pin del led stato caldaia
- void startCaldaia(){
- digitalWrite(ledPin,HIGH); //accendo il led
- digitalWrite(RELAY1,HIGH); //accendo il rele
- statoRele = '1';
- }
- void stopCaldaia(){
- digitalWrite(ledPin,LOW); //spengo il led
- digitalWrite(RELAY1,LOW); //spengo il rele
- statoRele = '0';
- }
- 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);
-
- pinMode(ledPin,OUTPUT); //il led
- pinMode(RELAY1, OUTPUT); //il rele
- pinMode(buttonPin, INPUT); //pulsante
-
- contatoreSecondi = 0;
- }
- void loop() {
- //Si autospegne se non riceve alcun messaggio per un ora
- if (contatoreSecondi > 7200){
- stopCaldaia();
- blueToothVal=' ';
- }
-
- // Legge l'input dal raspeberry
- if (mySerial.available()){
- blueToothVal=(char)mySerial.read();
- }
-
-
- String stringContatore;
-
- switch (blueToothVal) {
- case '0'://se ricevo 0 lo spengo
- stopCaldaia();
- mySerial.write('0\n');
- blueToothVal=' ';
- contatoreSecondi = 0;
- break;
- case '1'://se ricevo 1 lo accendo
- startCaldaia();
- mySerial.write('1\n');
- blueToothVal=' ';
- contatoreSecondi = 0;
- break;
- case 's'://richiedo lo stato e azzero il contatore
- mySerial.write(statoRele); //mando lo stato
- mySerial.write("\n");
- blueToothVal=' ';
- contatoreSecondi = 0;
- break;
- case 't'://info sul contatore
- stringContatore = String(contatoreSecondi);
- char charBuf[50];
- stringContatore.toCharArray(charBuf, 50);
- strcat(charBuf, "\n");
- mySerial.write(charBuf); //mando lo stato
- break;
- default:
- // if nothing else matches, do the default
- // default is optional
- break;
- }
- //Parte pulsante
- buttonState = digitalRead(buttonPin);
-
- if (buttonState == HIGH) {
- if(statoRele=='0'){
- startCaldaia();
- blueToothVal=' ';
- contatoreSecondi = 0;
- }else{
- stopCaldaia();
- blueToothVal=' ';
- contatoreSecondi = 0;
- }
- }
- //fine parte pulsant
-
- delay(1000);
-
- contatoreSecondi += 1;
- }
|