1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #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;
- 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(13,OUTPUT); //il led
- pinMode(RELAY1, OUTPUT); //il rele
- contatoreSecondi = 0;
- }
- void loop() {
- //Si autospegne se non riceve alcun messaggio per un ora
- if (contatoreSecondi > 3600){
- digitalWrite(13,HIGH); //accendo il led
- digitalWrite(RELAY1,LOW); //accendo il rele
- statoRele = 'f';
- blueToothVal=' ';
- }
-
- // Legge l'input dal raspeberry
- if (mySerial.available()){
- blueToothVal=(char)mySerial.read();
- }
-
-
- if (blueToothVal=='n')//se ricevo n lo spengo
- {
- digitalWrite(13,HIGH); //accendo il led
- digitalWrite(RELAY1,LOW); //accendo il rele
- mySerial.write('n\n');
- statoRele = blueToothVal;
- blueToothVal=' ';
- contatoreSecondi = 0;
- }else if (blueToothVal=='f')//se ricevo f lo accendo
- {
- digitalWrite(13,LOW); //spendo il led
- digitalWrite(RELAY1,HIGH); //spengo il rele
- mySerial.write('f\n');
- statoRele = blueToothVal;
- blueToothVal=' ';
- contatoreSecondi = 0;
- }else if(blueToothVal=='s'){ //richiedo lo stato
- mySerial.write(statoRele); //mando lo stato
- mySerial.write("\n");
- blueToothVal=' ';
- contatoreSecondi = 0;
- }else if(blueToothVal=='c'){ //info sul contatore
- String myString = String(contatoreSecondi);
- char charBuf[50];
- myString.toCharArray(charBuf, 50);
- strcat(charBuf, "\n");
- mySerial.write(charBuf); //mando lo stato
- }
- delay(1000);
- contatoreSecondi += 1;
- }
|