Problemi con HC05 e python

Ciao ragazzi sono nuovo del forum e soprattutto del mondo arduino.
Possiedo un arduino Uno a cui ho collegato un modulo bluetooth HC05.
Fin qui tutto bene il programma gira perfettamente il python che riceve i dati dal’'arduino mi scrive le tabelle su mysql… sembrerebbe perfetto … peccato mi scrive solo 124 record e poi smette.
Penso sia un problema di buffer, ho googlato pesantemente in cerca di una soluzione ma tutti i metodi che ho trovato si sono rivelati inutili.
In teoria sarebbe un progetto di domotica quindi dovrebbe funzionare sempre…

Vi posto tutti e due i programmi ma sono incasinati vi avverto.

Arduino sketch

#include <SoftwareSerial.h>

#define rxPin 4
#define txPin 5
SoftwareSerial mySerial(rxPin, txPin);

//dht22
#include "DHT.h"
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

// Sensore luce
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>

int ledPin = 7;

/* DHT22 */
// Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h=0; 
  // Read temperature as Celsius
float t=0; 
/* MQ-7 Carbon Monoxide Sensor Circuit */
const int AOUTcopin=0;//the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
const int DOUTcopin=8;//the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
const int ledPinco=11;//the anode of the LED connects to digital pin D13 of the arduino
int limitco;
int valueco;

/* MQ-2 smoke */
const int AOUTsmpin=1;//the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
const int DOUTsmpin=9;//the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
const int ledPinsm=12;//the anode of the LED connects to digital pin D13 of the arduino
int limitsm;
int valuesm;

/* MQ-4 gas sensor */
const int AOUTmepin=2;//the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
const int DOUTmepin=10;//the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
const int ledPinme=13;//the anode of the LED connects to digital pin D13 of the arduino
int limitme;
int valueme;

/* Light sensor */
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
void configureSensor(void)
{
  sensor_t sensor;
  tsl.getSensor(&sensor);

tsl.enableAutoRange(true);
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
}

void setup() {
mySerial.begin(9600); 
//dht22
dht.begin();
// co sensor
pinMode(DOUTcopin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPinco, OUTPUT);//sets the pin as an output of the arduino
// smoke sensor
pinMode(DOUTsmpin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPinsm, OUTPUT);//sets the pin as an output of the arduino

// smoke sensor
pinMode(DOUTmepin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPinme, OUTPUT);//sets the pin as an output of the arduino

// lux
configureSensor();
sensors_event_t event;
  tsl.getEvent(&event);

}

void blinkLED(int input) {
      digitalWrite(ledPin, HIGH);
    delay(50);
    digitalWrite(ledPin, LOW);
    delay(50);
  } 

void svuotaBuffer() {
  
while(mySerial.available()) mySerial.read();
}

void loop() {

labeldht22:
while(1) {
 delay(10);

  h = dht.readHumidity();
  t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  goto labelco;
}
labelco:
while(1) {
valueco= analogRead(AOUTcopin);//reads the analaog value from the CO sensor's AOUT pin
limitco= digitalRead(DOUTcopin);//reads the digital value from the CO sensor's DOUT pin
if (valueco > 400){
delay(10);
digitalWrite(ledPinco, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPinco, LOW);//if threshold not reached, LED remains off
}
 goto labelsm; 
}

labelsm:
while(1) {
valuesm= analogRead(AOUTsmpin);//reads the analaog value from the CO sensor's AOUT pin
limitsm= digitalRead(DOUTsmpin);//reads the digital value from the CO sensor's DOUT pin
if (valuesm > 400){
delay(10);
digitalWrite(ledPinsm, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPinsm, LOW);//if threshold not reached, LED remains off
}
 goto labelme;
}

labelme:
while(1) {
valueme= analogRead(AOUTmepin);//reads the analaog value from the CO sensor's AOUT pin
limitme= digitalRead(DOUTmepin);//reads the digital value from the CO sensor's DOUT pin
if (valueme > 400){
delay(10);
digitalWrite(ledPinme, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPinme, LOW);//if threshold not reached, LED remains off
}
 goto labellux;
}
labellux:
while(1)
{
  configureSensor();
  sensors_event_t event;
  tsl.getEvent(&event);
  goto labelstamp;
}
labelstamp:
while(1)
{
  // Operazioni per calibrare i sensori
  valueco = valueco - 130;                         //    ################                     #################
  valuesm = valuesm - 30;                          //          This is not beatifull but work good for me
  valueme = valueme - 70;                         //    ################                     ################
  
  delay(10);

//  Serial.print("Humidity: "); 
  mySerial.print(t);
  mySerial.print(" ");
//  Serial.print(" %\t");
//  Serial.print("Temperature: "); 
  mySerial.print(h);
  mySerial.print(" ");
//  Serial.println(" *C ");
                                                                                                                                
                                                                                                                                           
//Serial.print("Valore CO: ");
if (valueco < 10) {
  mySerial.print("00"); //allway get 3 number
  mySerial.print(valueco);//prints the CO value
  }

else if (valueco<100)
{
  mySerial.print("0"); //allway get 3 number
  mySerial.print(valueco);//prints the CO value
}
else
{
  mySerial.print(valueco);
}
mySerial.print(" ");

//Serial.print("Valore fumo: ");
if (valuesm < 10) {
  mySerial.print("00"); //allway get 3 number
  mySerial.print(valuesm);//prints the CO value
  }

else if (valuesm<100)
{
  mySerial.print("0"); //allway get 3 number
  mySerial.print(valuesm);//prints the CO value
}
else
{
  mySerial.print(valuesm);
}

mySerial.print(" ");

//Serial.print("Valore Metano: ");
if (valueme < 10) {
  mySerial.print("00"); //allway get 3 number
  mySerial.print(valueme);//prints the CO value
  }

else if (valueme<100)
{
  mySerial.print("0"); //allway get 3 number
  mySerial.print(valueme);//prints the CO value
}
else
{
  mySerial.print(valueme);
}
  mySerial.print(" ");

//Serial.println(" lux");
sensors_event_t event;
  tsl.getEvent(&event);
 
//  Display the results (light is measured in lux)                                                                                       
  if (event.light)
  {
    if (event.light<10){
    mySerial.print(event.light); 
    mySerial.print("000"); //allway get 5 number
  }
    else if (event.light<100){
    mySerial.print(event.light); 
    mySerial.print("00"); //allway get 5 number
  }
  else if (event.light<1000){
    mySerial.print(event.light); 
    mySerial.print("0"); //allway get 5 number
  }
  else{mySerial.print(event.light);}
   }
  else
  {
//     If event.light = 0 lux the sensor is probably saturated
//       and no reliable data could be generated!                                                                                              
     mySerial.print("//");
     mySerial.println("//");}
    
     mySerial.println(" ");
  // Serial.println("Invio si spera completato");
  mySerial.flush();
  svuotaBuffer();
if ( mySerial.available() > 0 ) {
    // read a numbers from serial port
    int input = mySerial.parseInt();
    
     // print out the received number
    if (input > 0) {
        // blink the LED
        blinkLED(input);}}
        
goto labeldht22;



}
}

Codice python su raspberry pi

#!/usr/bin/python
# Graphical User Interface for the Bluetooth sensor
# Imports
import time
import serial
from Tkinter import *
import MySQLdb as mdb
import logging

######################## Per il log di sistema ####################

logging.basicConfig(level=logging.INFO)

logger = logging.getLogger(__name__)

path =('/home/pi/domotica/log/log_domotica.log')
hdlr = logging.FileHandler(path)

formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)

logger.warning('Avviato Controllo Aria')

try:

#################################################################

        # Serial port parameters
        serial_speed = 9600
        serial_port = '/dev/rfcomm1'


        # Acquisisco segnali

        ser = serial.Serial(serial_port, serial_speed, timeout=0,  )

        # Mi connetto al Database

        con = mdb.connect("192.168.0.151", "pi", "agesci", "domotica")

#       with con:
#                cur = con.cursor()
#               cur.execute("DROP TABLE IF EXISTS Progetto_aria")
#                cur.execute("CREATE TABLE Progetto_aria(Id INT PRIMARY KEY AUTO_INCREMENT, Temperatura VARCHAR(5), Umidita VARCHAR(5), Monossido VARCHAR(5), Fumo VARCHAR(5), Metano VARCHAR(5), Lu$

        i=0 # This is usefull for debag and to sincronize arduino and raspberry pi
      while True:


                var1 = ser.readline()
                var = str(var1)
                t = var [0:5].strip()
                h = var [6:11].strip()
                c = var [12:15].strip()
                f = var [16:19].strip()
                m = var [20:23].strip()
                l = var [24:].strip()


                if not var:

                        i=i+1
#                       print(i)                # uncomment this line for debag

                else:
                        with con:
                                cur = con.cursor()
                                cur.execute("INSERT INTO Progetto_aria (Temperatura, Umidita, Monossido, Fumo, Metano, Luce, Ora) VALUES(%s,%s,%s,%s,%s,%s, NOW())",(t,h,c,f,m,l))

                input = 1
                ser.write( str(input) )

#               print (var)
                time.sleep(5)

except (SystemExit, KeyboardInterrupt):
        logger.warning("Chiuso Controllo aria")
        raise
except Exception, e:
        logger.error('Errore nel programma principale', exc_info=True)

Ciao, ma non riesci a tracciare l'errore almeno per capire se si pianta tra arduino/bluetooth o bluetooth/pyton?
Probabilmente il codice di Arduino puoi semplificarlo un pò e se indenti correttamente (CTRL+T ) si capisce anche meglio :wink:
Che.... scout?

Stefano