rele.ino 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <SoftwareSerial.h>
  2. #define RELAY1 8
  3. SoftwareSerial mySerial(4, 2); // RX, TX (seriale su cui e' settato il bluetooth)
  4. char statoRele = 'n'; // salviamo lo stato del rele
  5. char blueToothVal; //value sent over via bluetooth
  6. int contatoreSecondi = 0;
  7. void setup() {
  8. // Open serial communications:
  9. Serial.begin(9600);
  10. Serial.println("Type AT commands!");
  11. // The HC-06 defaults to 9600 according to the datasheet.
  12. mySerial.begin(9600);
  13. pinMode(13,OUTPUT); //il led
  14. pinMode(RELAY1, OUTPUT); //il rele
  15. contatoreSecondi = 0;
  16. }
  17. void loop() {
  18. //Si autospegne se non riceve alcun messaggio per un ora
  19. if (contatoreSecondi > 3600){
  20. digitalWrite(13,HIGH); //accendo il led
  21. digitalWrite(RELAY1,LOW); //accendo il rele
  22. statoRele = 'f';
  23. blueToothVal=' ';
  24. }
  25. // Legge l'input dal raspeberry
  26. if (mySerial.available()){
  27. blueToothVal=(char)mySerial.read();
  28. }
  29. if (blueToothVal=='n')//se ricevo n lo spengo
  30. {
  31. digitalWrite(13,HIGH); //accendo il led
  32. digitalWrite(RELAY1,LOW); //accendo il rele
  33. mySerial.write('n\n');
  34. statoRele = blueToothVal;
  35. blueToothVal=' ';
  36. contatoreSecondi = 0;
  37. }else if (blueToothVal=='f')//se ricevo f lo accendo
  38. {
  39. digitalWrite(13,LOW); //spendo il led
  40. digitalWrite(RELAY1,HIGH); //spengo il rele
  41. mySerial.write('f\n');
  42. statoRele = blueToothVal;
  43. blueToothVal=' ';
  44. contatoreSecondi = 0;
  45. }else if(blueToothVal=='s'){ //richiedo lo stato
  46. mySerial.write(statoRele); //mando lo stato
  47. mySerial.write("\n");
  48. blueToothVal=' ';
  49. contatoreSecondi = 0;
  50. }else if(blueToothVal=='c'){ //info sul contatore
  51. String myString = String(contatoreSecondi);
  52. char charBuf[50];
  53. myString.toCharArray(charBuf, 50);
  54. strcat(charBuf, "\n");
  55. mySerial.write(charBuf); //mando lo stato
  56. }
  57. delay(1000);
  58. contatoreSecondi += 1;
  59. }