如何第三方控制点灯注册上线的设备

2022年3月29日 Jerry 2607 2023年11月4日

点灯起初可以注册设备到阿里云IOT,于是有了今天这个帖子。看我慢慢道来。

群里某人,每次点进去就在嘚嘚嘚说自己实现了第三方控制点灯设备,烦的一腿,怼了几句,最后就搞成了下面这个场面:

所以,情况就是这么个情况,那既然答应做人家爹,就要做好这个爹。开始分析:

需求,简单。设备通过点灯APP 注册到阿里云的broken. 然后通过第三方软件或程序控制设备。

MQTT协议不用多说,度娘教程一堆。控制设备,简单来说 就是模仿点灯的APP发送 publish 消息即可。

主要是点灯的APP 是个开源?WTF ?这开源还不简单,虽说我没玩过安卓开发,但是没吃过猪肉哦没见过猪跑吗?

二话不说,上来就撸代码。就这玩意开源了某人还要抓包,我抓你奶奶个腿哦?

点灯APP 源码地址:https://github.com/blinker-iot/blinker-app

OK,上流程:

1、首先,点灯登录时获取用户信息,此时登录方法:传一个 username 加个 hash加密 password 以获取 uuid及token,安排

2、有了uuid 和 token 获取 用户信息:

得到JSON信息,

随便找个JSON格式化,如下:我们只需要关于 阿里云BROKEN的相关信息,熟知的三元组罢了。

三元组都有了,怎么publish给阿里云,还需要一个TOPIC,那源码里面有啊TOPIC格式:

走blinker 的 topic格式:

走阿里云的 topic 格式:

于是,TOPIC 又有了,,HOST 跟三元组一起解析出来就是:productKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com"; 

那万事俱备了,要啥有啥,发个publish消息就完了呗????

用C#简单搞个客户端 作为第三方控制咯,其他语言当然没问题。

static void Main(string[] args)
{
    String productKey = "xxxxx";
    String deviceName = "xxxxxx";
    String deviceSecret = "xxxxxxxx";

    //计算Mqtt建联参数
    MqttSign sign = new MqttSign();
    sign.calculate(productKey, deviceName, deviceSecret);

    Console.WriteLine("username: " + sign.getUsername());
    Console.WriteLine("password: " + sign.getPassword());
    Console.WriteLine("clientid: " + sign.getClientid());

    //使用Paho链接阿里云物联网平台
    int port = 443;
    String broker = productKey + ".iot-as-mqtt.cn-shanghai.aliyuncs.com";

    MqttClient mqttClient = new MqttClient(broker, port, true, MqttSslProtocols.TLSv1_2, null, null);
    mqttClient.Connect(sign.getClientid(), sign.getUsername(), sign.getPassword());

    Console.WriteLine("broker: " + broker + " Connected");

    //Paho Mqtt 消息订阅
    String topicReply = "/" + productKey + "/" + deviceName + "/r";
    mqttClient.MqttMsgPublishReceived += MqttPostProperty_MqttMsgPublishReceived;
    mqttClient.Subscribe(new string[] { topicReply }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE });
    Console.WriteLine("subscribe: " + topicReply);

    //Paho Mqtt 消息发布
    String topic = "/" + productKey + "/" + deviceName + "/s";
    String message2 = "{\"deviceType\":\"DiyArduino\",\"data\":{\"btn-abc\":\"tap\"},\"fromDevice\":\"ca0e0914792711eca1545254\",\"toDevice\":\"F3A3CF150IR0C9FJT7QGGM8V\"}";


    mqttClient.Publish(topic, Encoding.UTF8.GetBytes(message2));
    Console.WriteLine("publish: " + message2);

    while(true) {
        Thread.Sleep(5000);
        mqttClient.Publish(topic, Encoding.UTF8.GetBytes(message2));
        Console.WriteLine("publish: " + message2);
    }

    //Paho Mqtt 断开连接
    mqttClient.Disconnect();
}

代码很简单啊,5秒循环 publish  一个 btn-abc tap 消息,

设备烧写点灯hello例程,中介选择阿里云,默认接收到btn-abc tap 设备会 灭灯或亮灯呢。

那接下来运行上面三方程序,查看设备的灯状态就OK了,5秒一闪。呵呵。上个视频,免得儿子不相信。

5秒一个 publish 控制灯IO的高低电平。

就这点东西,叫叫叫????最后,感谢点灯平台,竟然让我有了个“网络儿子”。记得大点声喊哦


原创文章,转载请注明出处: https://jerrycoding.com/article/blinker-3rd

微信
jerry微信赞助
支付宝
jerry支付宝赞助

您尚未登录,暂时无法评论。请先 登录 或者 注册

0 人参与 | 0 条评论