美文网首页
AliOS-Things 使用 MQTT

AliOS-Things 使用 MQTT

作者: gbmaotai | 来源:发表于2019-03-29 14:39 被阅读0次

    测试板

    ESP8266 / MK3060

    代码

    AliOS-Things/example/mqttapp

    修改

    #define PRODUCT_KEY             "a1vuRZ5qR2q"
    #define PRODUCT_SECRET          "dkbucklgoExQlRm0"
    #define DEVICE_NAME             "SensorI"
    #define DEVICE_SECRET           "BihxiHbiSZOzpptR6fzpJWuedZpoU5rJ"
    // 添加user
    /* These are pre-defined topics */
    #define TOPIC_UPDATE            "/"PRODUCT_KEY"/"DEVICE_NAME"/user/update"
    #define TOPIC_ERROR             "/"PRODUCT_KEY"/"DEVICE_NAME"/user/update/error"
    #define TOPIC_GET               "/"PRODUCT_KEY"/"DEVICE_NAME"/user/get"
    #define TOPIC_DATA               "/"PRODUCT_KEY"/"DEVICE_NAME"/user/data"
    

    云端的操作

    1.开通物联网套件
    2.创建产品和设备
    3.保存 ProductKey ,ProductSecret, DeviceName 和 DeviceSecret
    4.定义 Topic ,并设置权限为”设备具有发布与订阅”

    $(PRODUCT_KEY)/$(DEVICE_NAME)/user/data,
    

    Log

    
    [006740]<I> ....................................................
    [006750]<I>           ProductKey : a1vuRZ5qR2q
    [006750]<I>           DeviceName : SensorI
    [006750]<I>             DeviceID : a1vuRZ5qR2q.SensorI
    [006750]<I> ....................................................
    [006750]<I>        PartnerID Buf : ,partner_id=example.demo.partner-id
    [006750]<I>         ModuleID Buf : ,module_id=example.demo.module-id
    [006750]<I>           Guider URL : 
    [006750]<I>       Guider SecMode : 2 (TLS + Direct)
    [006750]<I>     Guider Timestamp : 2524608000000
    [006750]<I> ....................................................
    [006750]<I> -----------------------------------------
    [006750]<I>             Host : a1vuRZ5qR2q.iot-as-mqtt.cn-shanghai.aliyuncs.com
    [006750]<I>             Port : 1883
    [006750]<I>         ClientID : a1vuRZ5qR2q.SensorI|securemode=2,timestamp=2524608000000,signmethod=hmacsha256,gw=0,ext=0,partner_id=example.demo.partner-id,module_id=example.demo.module-id,a=aos-r-2.1.0|
    [006750]<I>       TLS PubKey : 0x40205c12 ('-----BEGIN CERTI ...')
    [006760]<I> -----------------------------------------
    [006760]<I> MQTT init success!
    [006760]<I> Loading the CA root certificate ...
    [006790]<I>  ok (0 skipped)
    [006790]<I> Connecting to /a1vuRZ5qR2q.iot-as-mqtt.cn-shanghai.aliyuncs.com/1883...
    Wifi connected.
    [006880]<I>  ok
    [006880]<I>   . Setting up the SSL/TLS structure...
    [006880]<I>  ok
    [006880]<I> Performing the SSL/TLS handshake...
    [007860]<I>  ok
    [007860]<I>   . Verifying peer X.509 certificate..
    [007860]<I> certificate verification result: 0x00
    [007950]<I> mqtt connect success!
    [007960]<E> the network interface info set failed or not set, writen len is 0
    mqtt_client|200 :: 
     publish message: 
     topic: /a1vuRZ5qR2q/SensorI/user/update
     payload: update: hello! start!
     rc = 4
    [007980]<I> mqtt subscribe packet sent,topic = /a1vuRZ5qR2q/SensorI/user/data!
    event_handle|087 :: publish success, packet-id=1
    event_handle|087 :: publish success, packet-id=2
    event_handle|063 :: subscribe success, packet-id=5
    event_handle|087 :: publish success, packet-id=3
    event_handle|087 :: publish success, packet-id=4
    mqtt_client|225 :: 
     publish message: 
     topic: /a1vuRZ5qR2q/SensorI/user/data
     payload: data: hello! start!
     rc = 6
    event_handle|087 :: publish success, packet-id=6
    _demo_message_arrive|123 :: ----
    _demo_message_arrive|124 :: PacketId: 4017
    _demo_message_arrive|128 :: Topic: '/a1vuRZ5qR2q/SensorI/user/data' (Length: 30)
    _demo_message_arrive|132 :: Payload: 'data: hello! start!' (Length: 19)
    _demo_message_arrive|133 :: ----
    mqtt_client|245 :: packet-id=7, publish topic msg={"attr_name":"temperature","attr_value":"1"}
    event_handle|087 :: publish success, packet-id=7
    _demo_message_arrive|123 :: ----
    _demo_message_arrive|124 :: PacketId: 4038
    _demo_message_arrive|128 :: Topic: '/a1vuRZ5qR2q/SensorI/user/data' (Length: 30)
    _demo_message_arrive|132 :: Payload: '{"attr_name":"temperature","attr_value":"1"}' (Length: 44)
    _demo_message_arrive|133 :: ----
    mqtt_client|245 :: packet-id=8, publish topic msg={"attr_name":"temperature","attr_value":"1"}
    event_handle|087 :: publish success, packet-id=8
    _demo_message_arrive|123 :: ----
    _demo_message_arrive|124 :: PacketId: 4211
    _demo_message_arrive|128 :: Topic: '/a1vuRZ5qR2q/SensorI/user/data' (Length: 30)
    _demo_message_arrive|132 :: Payload: '{"attr_name":"temperature","attr_value":"1"}' (Length: 44)
    _demo_message_arrive|133 :: ----
    mqtt_client|245 :: packet-id=9, publish topic msg={"attr_name":"temperature","attr_value":"1"}
    event_handle|087 :: publish success, packet-id=9
    _demo_message_arrive|123 :: ----
    _demo_message_arrive|124 :: PacketId: 4425
    _demo_message_arrive|128 :: Topic: '/a1vuRZ5qR2q/SensorI/user/data' (Length: 30)
    _demo_message_arrive|132 :: Payload: '{"attr_name":"temperature","attr_value":"1"}' (Length: 44)
    _demo_message_arrive|133 :: ----
    mqtt_client|245 :: packet-id=10, publish topic msg={"attr_name":"temperature","attr_value":"1"}
    event_handle|087 :: publish success, packet-id=10
    _demo_message_arrive|123 :: ----
    
    在这里插入图片描述
    在这里插入图片描述

    相关文章

      网友评论

          本文标题:AliOS-Things 使用 MQTT

          本文链接:https://www.haomeiwen.com/subject/yqmlbqtx.html