Question: I am reading data from a sensor. The code for it is Arduino IDE. double Ax; Ax = (double)AccelX/AccelScaleFactor; Serial.print(Ax: ); Serial.print(Ax); At this point,

I am reading data from a sensor. The code for it is Arduino IDE.

double Ax;

Ax = (double)AccelX/AccelScaleFactor;

Serial.print("Ax: "); Serial.print(Ax);

At this point, I am getting the sensor data, I am able to see the output. For example, values of Ax like 0.14 or -0.02. This part is ok. Then, I publish this value to a MQTT broker, with the next line of code: client.publish(mqtt_topic,String(Ax).c_str()); This is also OK. I subscribe to it and I am able to get the values.

The issue I have is when I want to store this value into Mysql. The database I have has a field named Temp which is varchar. Since the data received is c string (like an array) I need to do a conversion. That is my question: how to do it?

My phyton code subscribes to get the value and print it. So, this part of the code is ok. Every time a message is published I get it. This is part of the python code when a message is received:

val = msg.payload // msg.payload contain the reading which is the value like 0.14. I assign it in the variable val

print "%s" % (val) // I am able to see the 0.14 for example

from here, I need to store it into mysql from this python code:

cursor.execute(""" INSERT INTO name_of_my_table (name_of_the_field_in_the_table) VALUES (%s)""", (val))

db.commit()

Normally, it works. I used this part for another assignment. But in others assignment, I get the data directly from the sensor and it is a string type. Then, I changed the command to cursor.executemany .... With this one worked, but not as I was expected. I expect to store in mysql table the whole value in one record (row), instead, it stored it byte by byte, so, 0 in one, the period(.) in another, 1 in another and the 4 in another.

I need to convert it into string before I try to insert it.

Please help

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!