Python Forum
Python Serial: How to read the complete line to insert to MySQL?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Serial: How to read the complete line to insert to MySQL?
#1
Hello. Sorry for my bad English, it's not my first language.
I have created a python code to insert data from a DHT11 sensor into a mysql table. Everything works, well almost. I have noticed that in the first rows the data does not appear as it should. At first the lines come out irregularly. Then they come out as they should be.
Here is a picture of what I'm talking about:
https://i.vgy.me/Yc4lGp.png

Here is my Arduino code:
#include <DHT.h> #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { // Leemos la humedad relativa float h = dht.readHumidity(); // Leemos la temperatura en grados centígrados (por defecto) float t = dht.readTemperature(); // Calcular el índice de calor en grados centígrados float hic = dht.computeHeatIndex(t, h); // Comprobamos si ha habido algún error en la lectura if (isnan(h) || isnan(t)) { Serial.print("Error obteniendo los datos del sensor DHT11"); return; }else{ Serial.print("Temperatura: "); Serial.print(t); Serial.print(" *C\t"); Serial.print("Humedad: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Indice de calor: "); Serial.print(hic); Serial.println(" *C"); delay(3000); } }
Here is my python code:
import mysql.connector import serial mibd = mysql.connector.connect( host="localhost", user="root", password="N12345", database="sensor_humedad" ) ser = serial.Serial( port='/dev/ttyACM0', baudrate=9600, parity=serial.PARITY_ODD, stopbits=serial.STOPBITS_TWO, bytesize=serial.SEVENBITS ) def programa(): while True: medida=ser.readline() micursor = mibd.cursor() sql = "INSERT INTO medidas (dht11) VALUES (%s)" valores = [(medida)] micursor.execute(sql, valores) mibd.commit() print(f"{micursor.rowcount}, se ha insertado una linea del sensor.") programa()
I have tried looking for solutions but I can't solve my problem. I think I would have to use "in_waiting" or "flush()" but I don't know where in my code I should put it. I am new to python.
Reply
#2
ser.readline() returns bytes, not a str.
# Instead of this medida=ser.readline() # do this medida=ser.readline().decode('utf-8')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Insert command line in script lif 4 2,169 Mar-24-2025, 10:30 PM
Last Post: lif
  pyserial/serial "has no attribute 'Serial' " gowb0w 11 33,589 Sep-27-2024, 12:18 PM
Last Post: NigelHalse
  Mysql and mysql.connector error lostintime 2 2,506 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Insert 10gb csv files into sql table via python mg24 2 5,630 Apr-28-2023, 04:14 PM
Last Post: snippsat
  Read complete windows registry? fredep57 3 2,498 Mar-15-2023, 08:14 PM
Last Post: buran
  python insert blank line in logger mg24 1 6,293 Nov-02-2022, 08:36 AM
Last Post: snippsat
  Insert a multiple constant value after header in csv file using python shantanu97 1 2,251 Apr-24-2022, 10:04 AM
Last Post: Pedroski55
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 13,606 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  Python code to read second line from CSV files and create a master CSV file sh1704 1 4,215 Feb-13-2022, 07:13 PM
Last Post: menator01
  Mysql error message: Lost connection to MySQL server during query tomtom 6 24,128 Feb-09-2022, 09:55 AM
Last Post: ibreeden

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.