rele.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. const int buttonPin = 11; //pin del pulsante
  8. int buttonState = 0; // stato del pulsante
  9. const int ledPin = 13; //pin del led stato caldaia
  10. void startCaldaia(){
  11. digitalWrite(ledPin,HIGH); //accendo il led
  12. digitalWrite(RELAY1,HIGH); //accendo il rele
  13. statoRele = '1';
  14. }
  15. void stopCaldaia(){
  16. digitalWrite(ledPin,LOW); //spengo il led
  17. digitalWrite(RELAY1,LOW); //spengo il rele
  18. statoRele = '0';
  19. }
  20. void setup() {
  21. // Open serial communications:
  22. Serial.begin(9600);
  23. Serial.println("Type AT commands!");
  24. // The HC-06 defaults to 9600 according to the datasheet.
  25. mySerial.begin(9600);
  26. pinMode(ledPin,OUTPUT); //il led
  27. pinMode(RELAY1, OUTPUT); //il rele
  28. pinMode(buttonPin, INPUT); //pulsante
  29. contatoreSecondi = 0;
  30. }
  31. void loop() {
  32. //Si autospegne se non riceve alcun messaggio per un ora
  33. if (contatoreSecondi > 7200){
  34. stopCaldaia();
  35. blueToothVal=' ';
  36. }
  37. // Legge l'input dal raspeberry
  38. if (mySerial.available()){
  39. blueToothVal=(char)mySerial.read();
  40. }
  41. String stringContatore;
  42. switch (blueToothVal) {
  43. case '0'://se ricevo 0 lo spengo
  44. stopCaldaia();
  45. mySerial.write('0\n');
  46. blueToothVal=' ';
  47. contatoreSecondi = 0;
  48. break;
  49. case '1'://se ricevo 1 lo accendo
  50. startCaldaia();
  51. mySerial.write('1\n');
  52. blueToothVal=' ';
  53. contatoreSecondi = 0;
  54. break;
  55. case 's'://richiedo lo stato e azzero il contatore
  56. mySerial.write(statoRele); //mando lo stato
  57. mySerial.write("\n");
  58. blueToothVal=' ';
  59. contatoreSecondi = 0;
  60. break;
  61. case 't'://info sul contatore
  62. stringContatore = String(contatoreSecondi);
  63. char charBuf[50];
  64. stringContatore.toCharArray(charBuf, 50);
  65. strcat(charBuf, "\n");
  66. mySerial.write(charBuf); //mando lo stato
  67. break;
  68. default:
  69. // if nothing else matches, do the default
  70. // default is optional
  71. break;
  72. }
  73. //Parte pulsante
  74. buttonState = digitalRead(buttonPin);
  75. if (buttonState == HIGH) {
  76. if(statoRele=='0'){
  77. startCaldaia();
  78. blueToothVal=' ';
  79. contatoreSecondi = 0;
  80. }else{
  81. stopCaldaia();
  82. blueToothVal=' ';
  83. contatoreSecondi = 0;
  84. }
  85. }
  86. //fine parte pulsant
  87. delay(1000);
  88. contatoreSecondi += 1;
  89. }