Python - value saved to database is reset

Asked

Viewed 68 times

1

I’m doing a job for college using an Arduino and a Raspberry to make a self-contained vegetable garden. The sensors of temperature and humidity, luminosity and the hygrometer are connected to the Arduino and I am making the communication via serial (USB) for the reading of the data of the sensors in the Raspberry and the recording in database. The data is being read right only that the temperature is recording 0 in the BD but at the terminal output with the print came out the value of the correct temperature. The other 3 data are recording correctly.

This is the line that writes in a string the sensor data in the Arduino

//Linha que envia os sinais do sensor via Serial para gravar no BD
  sinais = String(temperatura) + "," + String(umidade) + "," + String(luminosidade) + "," + String(leituraSensorSolo);
  Serial.println(sinais);

This is the Python script I’m using for writing in the comic

    import serial
    comport = serial.Serial('/dev/ttyACM0', 9600) 
    print ('Serial Iniciada...\n')

    import mysql.connector
    cnx = mysql.connector.connect(user='root', password='', host='127.0.0.1', database='sinais')
    cursor = cnx.cursor()
    add_sinais = ("INSERT INTO sinais (sin_temp,sin_umid,sin_lum,sin_solo) VALUES (%s, %s, %s, %s)")

    while (True):
      serialValue = comport.readline()
      serialValue = str(serialValue)
      data_sinais = serialValue.split(",")  
      print (data_sinais)
      cursor.execute(add_sinais, data_sinais)
      cnx.commit()

    cursor.close()
    cnx.close()
    comport.close()

Terminal output, the last 0 is correct pq is digital output (0/1)

Serial Iniciada...

["b'22.80", '86.00', '1016', "0\\r\\n'"]
["b'22.70", '86.00', '1017', "0\\r\\n'"]

Edit: I believe the problem is in the "b" that is coming before the temperature data, I do not know why this appears. I believe that’s why I changed the field in the comic to sweep and recorded in the field like this: B22.70.

Does anyone know how to take out that "b" or why it is appearing

  • It seems that you are getting the sensor data wrong. It has nothing to do with the database, which is recording what you have to record.

1 answer

1


I managed to solve the problem, sort of on the lam. The "b" that is coming in too, when I convert the serial data to string, is an identification of the python serial library itself, which identified the data as bits. To solve, after converting to string, I used the python substring function and recorded again in the variable "serialValue", just added the line:

serialValue = serialValue[2:]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.