esp32 – Fetch MQTT Sensor Value using Node-Red transfer to MySQL
I use ESP32 and HC-SR04 Sensor to measure level of Garbage bin for my IOT Project.
In Arduino script, I use this script for publish sensor value to MQTT Broker
char distanceString [8];
dtostrf(distanceCm, 1, 2, distanceString);
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceString);
client.publish("distance", distanceString);
When debugging, the feed show data like this:
5/18/2023, 2:11:49 PM node: msg.payload
distance : msg.payload : number
19.18
and using this flow to configure and transfer data to MySQL 
I use default configuration for MQTT in and JSON, in Function I use script like below:
var src = payload;
msg.topic="INSERT INTO transaction_log (distance,Timestamp) VALUES ("src',CURTIME());'
return msg;
But the simple step still make me confuse, there are error and debug show:
5/18/2023, 2:25:03 PM node: Send data to DB
function : (error)
"SyntaxError: Unexpected identifier 'src' (body:line 2)"
5/18/2023, 2:25:03 PM node: a2c6593749f38d03
msg : string[28]
"Unexpected end of JSON input"
Where’s my mistake?
Read more here: Source link
