Trying to insert a variable from my C++ code to MySQL via Javascript

  Kiến thức lập trình

I’m currently working on a project involving Arduino and Node-RED, where I’m attempting to insert a variable declared in Arduino, visible in my serial monitor, into a MySQL database using a JavaScript function. The function I’m using is as follows:

var value = JSON.parse(JSON.stringify(msg.payload));
value = msg;
var sensor = msg.payload;
msg.payload = [sensor];
msg.topic = "INSERT INTO tbl_sensor(ID_sensor) VALUES (null, ?);";
return msg;

However, the issue I’m facing is that the function attempts to JSON encode everything displayed in my serial monitor. This isn’t feasible because my Arduino code includes additional serial prints besides the variable I’m interested in, which is named “tmp”.

I’ve attempted various adjustments like changing msg.payload to msg.payload.tmp and experimenting with additional variables. However, since this is my first time working with JavaScript, I’m uncertain about the correct approach. Can anyone provide guidance on how to accomplish this? Any assistance would be greatly appreciated! Thank you!

New contributor

Caesar Canales is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.