Last active
April 6, 2023 22:13
-
-
Save tskxz/de526afddb49e9f1740238c3d5e24e3c to your computer and use it in GitHub Desktop.
script_arduino_pap_erico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: