Problem with HC05 and python

Hello boys are new to the forum and especially the Arduino world.
I own an Arduino Uno connected at Bluetooth module HC05.
So far so good... The program perfectly runs, python receives from arduino datas and writes to my tables on mysql ... seems perfect ... too bad he writes only 124 records and then stops.
I think it is a buffer problem, I Googled very heavily in search of a solution but all the methods that I have found not works.
In theory it would be a home automation project so it should always work ...

I post both programs but are messed up I warn you.
Sorry for my bad english.

Ps I translated most of the comments, but I have no time to translate other

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() {

/////////////////////////////////////////// DHT 22
  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;
  }
//////////////////////////// /* MQ-7 Carbon Monoxide Sensor Circuit */

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){
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
}

/* MQ-2 smoke */
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){
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
}

/* MQ-4 gas sensor */
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
}

/* Light sensor */
  configureSensor();
  sensors_event_t event;
  tsl.getEvent(&event);

  // Operazioni per calibrare i sensori
  valueco = valueco - 130;                         //    ################                     #################
  valuesm = valuesm - 30;                          //          This is not beatifull but work good for me
  valueme = valueme - 70;                         //    ################                     ################
  
/////////////////////////// Print

//  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");


 
//  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);}}
        

}

Python code on raspberry pi (connect with usb bluetooth dongle )

#!/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)

When you get rid of the useless while loops, goto statements, and labels, come on back, if you still have a problem.

PaulS:
When you get rid of the useless while loops, goto statements, and labels, come on back, if you still have a problem.

Better?
I am not a programmer.
Is a week that I approached to this language I can not do better at the moment

Better?

No. You should NEVER replace code in a previous post. You make people that commented on the code posted first look like fools. Not a good way to get help here.

Use the AutoFormat tool to make your Arduino code easier to read.

And please just post the revised code in a new Reply rather than modify the Original Post. That will make the sequence of the Thread easier to follow.

...R
PS. Code is much easier to understand and to debug if it is organized as a series of small functions with meaningful names. Have a look at Planning and Implementing a Program

Auto format does't change nothing

Auto format does't change nothing

That is correct. So, what did it change, and why didn't you post the changed code?