Browse Source

modifiche rele

jops 8 years ago
parent
commit
9640b11589
3 changed files with 34 additions and 11 deletions
  1. 2 0
      InstallazioneBluetooth.txt
  2. 15 8
      arduino/rele/rele.ino
  3. 17 3
      bluetoothRele.py

+ 2 - 0
InstallazioneBluetooth.txt

@@ -6,6 +6,8 @@ Installa il bluetooth
 Per scannerizzare i dispositivi bluetooth
 hcitool scan
 
+per capire che adattatore devo usare:
+root@raspberrypi /home/pi/script/cac/utils # hcitool dev
 
 Fare pairing
 root@raspberrypi /home/pi/script/cac/utils # bluez-simple-agent hci0 98:D3:31:B2:35:33

+ 15 - 8
arduino/rele/rele.ino

@@ -3,7 +3,7 @@
 
 SoftwareSerial mySerial(4, 2); // RX, TX (seriale su cui e' settato il bluetooth)
 
-char statoRele = 'f'; // salviamo lo stato del rele
+char statoRele = 'n'; // salviamo lo stato del rele
 char blueToothVal;           //value sent over via bluetooth
 int contatoreSecondi = 0;
 
@@ -22,9 +22,9 @@ void setup() {
 
 void loop() {
   //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
+  if (contatoreSecondi > 3600){   
+    digitalWrite(13,HIGH);      //accendo il led
+    digitalWrite(RELAY1,LOW);   //accendo il rele
     statoRele = 'f';
     blueToothVal=' ';
 
@@ -36,26 +36,33 @@ void loop() {
   }
   
   
-  if (blueToothVal=='n')//se ricevo n lo accendo
+  if (blueToothVal=='n')//se ricevo n lo spengo
   {
     digitalWrite(13,HIGH);      //accendo il led
     digitalWrite(RELAY1,LOW);   //accendo il rele
-    mySerial.write('n');
+    mySerial.write('n\n');
     statoRele = blueToothVal;
     blueToothVal=' ';
     contatoreSecondi = 0;
-  }else if (blueToothVal=='f')//se ricevo f lo spengo
+  }else if (blueToothVal=='f')//se ricevo f lo accendo
   {
     digitalWrite(13,LOW);      //spendo il led
     digitalWrite(RELAY1,HIGH); //spengo il rele
-    mySerial.write('f');
+    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);

+ 17 - 3
bluetoothRele.py

@@ -11,7 +11,21 @@ sock.settimeout(1.0)
 print sys.argv[1]
 sock.send(sys.argv[1])
 print 'Sent data'
-data = sock.recv(1)
-print 'received [%s]' % data
+
+data = ""
+while 1:
+  try:
+    data += sock.recv(1024)
+    data_end = data.find('\n')
+    if data_end != -1:
+       rec = data[:data_end]
+       print data
+       data = data[data_end+1:]
+       break
+  except KeyboardInterrupt:
+    break
+  except bluetooth.btcommon.BluetoothError as error:
+    continue
 sock.close()
-print 'socket chius'
+
+