douqiao6015 2018-08-30 12:33
浏览 140

将转换的MQTT字符串传递给MYSQL

I'm attempting to convert MQTT strings and add them into mysql, I have managed to convert the strings and have them display correctly in the chrome console but im missing the bit between the javascript and PHP to pass the data across.

I have tried some of the things outlined in other question on this site but i can get them to work, i tried to use AJAX but again couldn't get it to work where am i going wrong or what am i missing?

all the below code is run server side.

var hostname = "127.0.0.1";
var port = 1884;
// Create a client instance
var client = new Paho.MQTT.Client(hostname, Number(port), "clientId");

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({
    onSuccess: onConnect
});


// called when the client connects
function onConnect() {
    // Once a connection has been made, make a subscription and send a message.
    console.log("onConnect");
    client.subscribe("test");
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0) {
        console.log("onConnectionLost:" + responseObject.errorMessage);
    }
}

// called when a message arrives
function onMessageArrived(message) {        

var raw = message.payloadString;
message.length = raw.length;
message.raw = raw;
var data = {};

data.major = raw.slice(-10,-4);
data.mac = raw.substring(5,17);
data.hostname = raw.substring(18,30);
data.minor = raw.slice(-8,-4);


var str = data.hostname;
var location = str.replace(`C129E53D0F45`, `PINK`)
        .replace(`FD139CD46385`, `DARKORANGE`)
        .replace(`EB72F2609789`, `DEEPSKYBLUE`)
        .replace(`DBECEE45AE6F`, `RED`)
        .replace(`E47D6D760A7F`, `WHITE`)
        .replace(`C3D7EDB7EF28`, `WHITE`)
        .replace(`E3A3BD636EA3`, `GREEN`)
        .replace(`D6F579FC35C9`, `YELLOW`);




var beacon = parseInt(data.minor ,16);




 var topic2 = ('[value="'+location+','+beacon+', '+data.mac+'"]')
    console.log("Message arrived: topic=" + message.destinationName + ",  message=" + topic2);

 }

</script>
 <?php
 $servername = "127.0.0.1";
 $username = "user";
 $password = "password";
 $dbname = "test";

 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
 }

 $sql = "INSERT INTO test.track (`location`,`beacon`, `mac`) VALUES ('+location+','+beacon+', '+data.mac+')";

 if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
 } else {
echo "Error: " . $sql . "<br>" . $conn->error;
 }

 $conn->close();
 ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大