I've developed a web application using Laravel (PHP) and now I'm trying to communicate it with a IoT device (Rasberry Pi).
The idea is: when clicking a button, the application will send a MQTT message to the IoT device to start recording the data from a sensor and store it in a SD Card. When clicking again the button, it will send another MQTT message to the IoT device so it will stop recording.
Must say I'm just starting to learn how to use the Amazon Web Services IoT platform but I've reached to communicate the raspberryPi with my computer by using one of the examples of the python SDK using python script in both devices.
So we could say, I have now a python script that listens to a topic installed in the raspberry Pi. I parse the message and then do the following actions.
I'm trying now, then, to do the same but calling it from the Laravel application. So, my code looks like this:
Button HTML code
<button type="button" id="start-button" class="btn btn-outline-success btn-lg" onclick="startRecording()"><i class="fas fa-play-circle"></i> Start recording</button>
Javascript
<script src="{{ asset('js/mqttws31.min.js') }}"></script>
<script>
function startRecording(){
MQTTconnect();
}
var mqtt;
var reconnectTimeout = 2000;
var host = "xxxxxxxxx.iot.eu-west-1.amazonaws.com";
var port = 443;
function onConnect(){
//Once a connection has been made, make a subscription and send a message.
console.log("Connected");
//mqtt.subscribe("VaultingTable1");
message = new Paho.MQTT.Message("Hello World");
message.destinationName = "myDevice1";
mqtt.send(message);
}
function MQTTconnect(){
console.log("connecting to "+ host + " " + port);
mqtt = new Paho.MQTT.Client(host, port, "myDevice1");
var options ={
timeout : 3,
onSuccess : onConnect,
};
mqtt.connect(options); //connect
}
In this case I get this error:
WebSocket connection to 'ws://xxxxxxxxx.iot.eu-west-1.amazonaws.com:443/mqtt' failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
Can anyone help me how to reach it?
Thanks!
遇到了同样的问题,在CSDN看到了,希望阿里云团队能够给出正确、标准的答案~请查看
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
在使用阿里云物联网平台进行设备与Web应用的通信时,遇到WebSocket握手失败的问题,通常是因为直接尝试通过WebSocket连接至MQTT服务端点的方式不正确。阿里云物联网平台提供了SDK和API来帮助开发者更便捷地实现设备与应用的交互,而不是直接通过WebSocket连接。
针对您的需求,建议采用以下步骤:
对于PHP后端,直接使用WebSocket连接MQTT broker不是最佳实践。阿里云提供了多种语言的SDK,但遗憾的是官方没有直接提供PHP SDK来操作MQTT。不过,您可以利用HTTP协议调用阿里云物联网平台的API来间接实现消息的发送和接收。
由于直接从Web前端发送MQTT消息到阿里云IoT平台存在跨域和安全问题,推荐的做法是通过后端服务器作为代理。即用户点击按钮后,由Laravel应用通过HTTP请求调用阿里云提供的API来发布MQTT消息。
安装HTTP客户端库:确保你的Laravel项目中已经安装了Guzzle等HTTP客户端库,用于发起HTTP请求。
编写后端接口:在Laravel应用中创建一个路由和控制器方法,当前端调用这个接口时,后端将向阿里云物联网平台发送消息。
use Illuminate\Http\Request;
use GuzzleHttp\Client;
// 假设你有适当的方法来获取或存储设备的三元组信息
function sendMessage(Request $request)
{
$productKey = 'your_product_key';
$deviceName = 'your_device_name';
$deviceSecret = 'your_device_secret';
$topic = 'your_topic'; // 如 '/sys/' . $productKey . '/' . $deviceName . '/thing/event/property/post'
$messageContent = 'Start recording'; // 消息内容
// 构建签名参数
$params = [
'productKey' => $productKey,
'deviceName' => $deviceName,
'timestamp' => time(),
'nonce' => rand(0, 1000),
'signMethod' => 'HMAC-SHA1',
'version' => '2018-01-20',
];
$params['sign'] = generateSign($params, $deviceSecret); // 实现generateSign函数来生成签名
// 构建请求体
$body = [
'topic' => $topic,
'payload' => base64_encode($messageContent),
'qos' => 1,
];
// 使用Guzzle发起POST请求到阿里云物联网平台的API
$client = new Client();
$response = $client->post('https://iot.cn-shanghai.aliyuncs.com/devicepub', [
'form_params' => array_merge($params, $body),
]);
return response()->json(['status' => 'success', 'message' => 'Message sent']);
}
请注意,上述代码仅为示例,实际应用中需要根据阿里云物联网平台的API文档调整,并且要实现generateSign
函数来生成签名。此外,确保你的Laravel应用有权限访问阿里云物联网平台的API,并且遵循所有安全最佳实践。
通过上述方式,你可以避免直接从Web前端建立WebSocket连接的复杂性和潜在问题,转而通过后端服务安全高效地与阿里云物联网平台交互。这样,当用户点击按钮时,Laravel应用通过API调用来控制IoT设备的行为,实现了预期的功能。