this is the function to create the INSERT query for mysql:
var mqtt_topic = msg.topic;
var payload = JSON.stringify(msg.payload).replace(/[\\$'"]/g, "\\$&");
var metadata = msg.payload.metadata.shift();
msg.topic = "INSERT INTO ttn SET " +
"timestamp = NOW()," +
"json_payload='" + payload + "'," +
"payload = '" + msg.payload.payload + "'," +
"port = '" + msg.payload.port + "'," +
"counter = '" + msg.payload.counter + "'," +
"dev_eui = '" + msg.payload.dev_eui + "'," +
"frequency = '" + metadata.frequency + "'," +
"datarate = '" + metadata.datarate + "'," +
"codingrate = '" + metadata.codingrate + "'," +
"gateway_timestamp = '" + metadata.gateway_timestamp + "'," +
"gateway_time = '" + metadata.gateway_time + "'," +
"channel = '" + metadata.channel + "'," +
"server_time = '" + metadata.server_time + "'," +
"rssi = '" + metadata.rssi + "'," +
"lsnr = '" + metadata.lsnr + "'," +
"rfchain = '" + metadata.rfchain + "'," +
"crc = '" + metadata.crc + "'," +
"modulation = '" + metadata.modulation + "'," +
"gateway_eui = '" + metadata.gateway_eui + "'," +
"altitude = '" + metadata.altitude + "'," +
"longitude = '" + metadata.longitude + "'," +
"latitude = '" + metadata.latitude + "'," +
"mqtt_topic = '" + mqtt_topic + "'" +
";";
return msg;
The ttn payload from the devise is unaltered stored in the database (base64).
This is the database:
CREATE TABLE `ttn` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`json_payload` text,
`timestamp` datetime DEFAULT NULL,
`payload` text,
`port` int(11) DEFAULT NULL,
`counter` int(11) DEFAULT NULL,
`dev_eui` varchar(16) DEFAULT NULL,
`frequency` decimal(4,1) DEFAULT NULL,
`datarate` varchar(11) DEFAULT NULL,
`codingrate` varchar(11) DEFAULT NULL,
`gateway_timestamp` int(11) DEFAULT NULL,
`gateway_time` varchar(32) DEFAULT NULL,
`channel` int(11) DEFAULT NULL,
`server_time` varchar(32) DEFAULT NULL,
`rssi` int(11) DEFAULT NULL,
`lsnr` int(11) DEFAULT NULL,
`rfchain` int(11) DEFAULT NULL,
`crc` int(11) DEFAULT NULL,
`modulation` varchar(11) DEFAULT NULL,
`gateway_eui` varchar(32) DEFAULT '',
`altitude` int(11) DEFAULT NULL,
`longitude` decimal(7,5) DEFAULT NULL,
`latitude` decimal(7,5) DEFAULT NULL,
`mqtt_topic` varchar(256) DEFAULT NULL,
PRIMARY KEY (`id`)
);