dpy83214 2019-05-21 12:23
浏览 182

尝试使用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!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘