开发者社区> 问答> 正文

尝试使用AWS IoT通过Laravel应用程序发送MQTT消息

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看到了,希望阿里云团队能够给出正确、标准的答案~请查看

展开
收起
刘刚_ 2020-05-23 21:03:25 1516 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
基于 RocketMQ+ Knative 驱动云原生 Ser 立即下载
基于 RocketMQ + Knative 驱动云原生 Serverless 应用 立即下载
RocketMQ Operator-K8s平台自动化部署工具 立即下载