Skip to content

Instantly share code, notes, and snippets.

@tskxz
Last active April 6, 2023 22:13
Show Gist options
  • Save tskxz/de526afddb49e9f1740238c3d5e24e3c to your computer and use it in GitHub Desktop.
Save tskxz/de526afddb49e9f1740238c3d5e24e3c to your computer and use it in GitHub Desktop.
script_arduino_pap_erico
import serial
import mysql.connector
# Configure o objeto de conexão do MySQL
mydb = mysql.connector.connect(
host="127.0.0.1",
user="root",
password="",
database="arduino_pap"
)
# Configure o objeto Serial
ser = serial.Serial('COM3', 9600)
# Loop infinito para ler os dados da porta serial e inserir na base de dados
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').strip()
print(line)
# separa a linha em umidade e temperatura
parts = line.split(" ")
# verifica se a linha tem pelo menos dois elementos
if len(parts) >= 3:
difference = parts[2].split(" ")[1]
servoSet = parts[3].split(" ")[1]
eastLRD = parts[0].split(" ")[1]
westLRD = parts[1].split(" ")[1]
print(parts)
sql = "INSERT INTO graficos (sensorE, sensorD, diferenca, servo ) VALUES (%s, %s, %s, %s)"
val = (eastLRD, westLRD, difference, servoSet)
mycursor = mydb.cursor()
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
print(line)
else:
print('dados invalidos')
@tskxz
Copy link
Author

tskxz commented Apr 6, 2023

import serial
import mysql.connector

Configure the MySQL connection object

mydb = mysql.connector.connect(
host="127.0.0.1",
user="root",
password="",
database="arduino_pap"
)

Configure the Serial object

ser = serial.Serial('COM3', 9600)

Loop infinitely to read the data from the serial port and insert into the database

while True:

  line = ser.readline().decode('utf-8').strip()
  print("Mostrar o line --------")
  print(line)

  # split the line into humidity, temperature, and servo position
  parts = line.split(", ")
  parts = list(map(int, parts[0].split()))
  print("Mostrar os parts -------")
  print(parts)

  # check if the line has at least four elements
  if len(parts) < 4:
      print("parts sao menores que 4 --------")
      continue
  difference = parts[2]
  servoSet = parts[3]
  eastLRD = parts[0]
  westLRD = parts[1]
  print("A tentar mostrar os dados ------")
  print(f"Difference: {difference} | servoSet: {servoSet} | eastLRD: {eastLRD} | westLRD: {westLRD}")
  print("antes de inserir dados")
  sql = "INSERT INTO graficos (sensorE, sensorD, diferenca, servo ) VALUES (%s, %s, %s, %s)"
  val = (eastLRD, westLRD, difference, servoSet)
  mycursor = mydb.cursor()
  mycursor.execute(sql, val)
  mydb.commit()
  print(mycursor.rowcount, "record inserted.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment