Send data to mysql database

hi
Finaly i succed to find how to send data, request by custom code in the autopi, then, send directly to mysql database. I know for some security, it is not so good to open to the web mysql, but it is my choice, so…
i promise to share if I succeed, this is the code:
(Then don’t forget to make a “job” to execute it.)

This is the code to send SoC from the car (Hyundai Kona) to mysql database

import mysql.connector

def socdata():

connection = mysql.connector.connect(host='**********',
                         database='********',
                         user='***********',
                         password='**********')

soc = get_soc()
sql_insert_query = """ INSERT INTO soc (soc) VALUES (%s)"""
cursor = connection.cursor()
result = cursor.execute(sql_insert_query,(soc,))
connection.commit()

def get_soc():
args = [‘soc’]
kwargs = {
‘mode’: ‘220’,
‘pid’: ‘105’,
‘header’: ‘7E4’,
‘baudrate’: 500000,
‘formula’: ‘bytes_to_int(message.data[34:35])/2.0’,
‘protocol’: ‘6’,
‘verify’: False,
‘force’: True,
}
return salt[‘obd.query’](*args, **kwargs)[‘value’]

2 Likes

Glad you got it working :slight_smile:

1 Like

Yes, thanks to you !!!