Custom code test

#1

@Malte
all seem ok, but still not working
any idea ?
if I put manual value from %s to a number 4 for for exemple, it is working

but I want result of the soc request , it is not working.
This I want is (%s) is (SoC) value.
Something wrong ?
Result must be 1 or 2 digit number because of my sql table.

#2

Hi Remy

It looks like you have some errors in your code.
like the last line soc = get_soc() is never executed, and if it would be executed then it would just call itself over and over again.
You should probably move the last line “soc = get_soc()” to the write_soc() method instead, somewhere above the sql_insert_query line.

Can you show the job that is executing the methods?
I guess it runs the write_soc() method, right?

Best regards
/Malte

#3

Thank’s

yes, job is like this. and he work well.

ok, I try to move the line soc = write_soc like this and I do test now

I worried about syntax error about this line
sql_insert_query = (""" INSERT INTO bms (soc) VALUES (%s)""", (soc))

because if I write like this it is working:
sql_insert_query = “”" INSERT INTO bms (soc) VALUES (4)"""

#4

I make a lots of test today

this is working, when I put value “4”
4 is just a exemple of value

but when I replace the value “4” for request value, not working…
i also move the line soc = get_soc()

or maybe work but send value as not 2 digit number. my db accept only 1 or 2 digit. so if it is 4.5, it will not working. I am worried about what is come from the soc request … digit 1 or 2 or more ?

#5

this is the log:

any idea ?

#6

Yep, in the sql_insert_query line, you use the soc variable, but you have not created it yet.
The error message you get is python complaining about the soc variable not existing yet.

So to fix this, the call to get_soc() should be moved to be above the sql_insert_query line.

soc = get_soc()
sql_insert_query = (""" INSERT ......""", (soc))

I think that should fix the issue.

By the way, feel free to post the actual code as text, then it will be easier for people to help make changes :slight_smile:

Best regards
/Malte

1 Like
#7

ok, sorry :sweat_smile:

i test now this you said:
this is the full code:

import mysql.connector

def test():

connection = mysql.connector.connect(host='xx.xx.xx.xxx',
                         database='xxxxxxxx',
                         user='xxxxxxxx',
                         password='xxxxxxxxx')

soc = get_soc()
sql_insert_query = (""" INSERT INTO bms
                      (soc) VALUES (%s)""",(soc,) )
cursor = connection.cursor()
result = cursor.execute(sql_insert_query)
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’]

#8

@Malte
ok, it seem ok now, mysql receive data every minute, like I want, but where or what langage? I don’t know.
this is the screen shoot at the log, but I don’t know how to read it, and also if it is the good one. because the soc commande work well manually.


also the screen of my mysql connection. something come every minute. :thinking:

I don’t know what AutoPi send, is it raw data or number ?

#9

@Malte i found the log
There is error with:
AttributeError: ‘tuple’ object has no attribute ‘encode’

i will study it on google :thinking: :sweat_smile: :grimacing:

#10

@Malte @Peter
in my custom code, I have an error. any idea ?

import mysql.connector

def test():

connection = mysql.connector.connect(host='xx.xx.xx.xxx',
                         database='kona',
                         user='xxxxxxxxxx',
                         password='xxxxxxxxx')

soc = get_soc()
sql_insert_query = (""" INSERT INTO bms
                      (soc) VALUES (%s)""",(soc,) )
cursor = connection.cursor()
result = cursor.execute(sql_insert_query)
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’]

i have this error:
AttributeError: ‘tuple’ object has no attribute ‘encode’

so data is send to mysql, ok, but not in the good attribute encode.

Any idea ?