|
| 1 | +Set up MQTT Client-Broker Authenticated Connection with Publish in Callback |
| 2 | +=========================================================================== |
| 3 | + |
| 4 | +.. contents:: |
| 5 | + :local: |
| 6 | + :depth: 2 |
| 7 | + |
| 8 | +Materials |
| 9 | +--------- |
| 10 | + |
| 11 | +- `AMB82-mini <https://www.amebaiot.com/en/where-to-buy-link/#buy_amb82_mini>`_ x 1 |
| 12 | + |
| 13 | +Example |
| 14 | +------- |
| 15 | + |
| 16 | +MQTT (Message Queuing Telemetry Transport) is a protocol proposed by IBM and Eurotech. The introduction in MQTT Official Website: |
| 17 | +MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport.We can say MQTT is a protocol designed for IoT. MQTT is based on TCP/IP and transmits/receives data via publish/subscribe. |
| 18 | + |
| 19 | +Please refer to the figure below: |
| 20 | + |
| 21 | +|image01| |
| 22 | + |
| 23 | +In the operation of MQTT, there are several roles: |
| 24 | + |
| 25 | +- Publisher: Usually publishers are the devices equipped with sensors |
| 26 | + (ex. Ameba). Publishers uploads the data of the sensors to |
| 27 | + MQTT-Broker, which serves as a database with MQTT service. |
| 28 | + |
| 29 | +- Subscriber: Subscribers are referred to the devices which receive and |
| 30 | + observe messages, such as a laptop or a mobile phone. |
| 31 | + |
| 32 | +- Topic: Topic is used to categorize the messages, for example the |
| 33 | + topic of a message can be “PM2.5” or “Temperature”. Subscribers can |
| 34 | + choose messages of which topics they want to receive. |
| 35 | + |
| 36 | +This example shows how to connect Ameba to MQTT-Broker with authentication. |
| 37 | +Then, send messages as publisher and receive messages from MQTT-Broker as |
| 38 | +subscriber. |
| 39 | + |
| 40 | +**MQTT_Publish_In_Callback example** |
| 41 | + |
| 42 | +Open the MQTT example “File” -> “Examples” -> “AmebaMQTTClient” -> |
| 43 | +“MQTT_Publish_In_Callback” |
| 44 | + |
| 45 | +Please modify some WiFi-related parameter and some information related |
| 46 | +to MQTT: |
| 47 | + |
| 48 | +|image08| |
| 49 | + |
| 50 | +- All parameters are same as MQTT_Auth example. |
| 51 | + |
| 52 | +Next, compile the code and upload it to Ameba. Press the reset button, |
| 53 | +then open the serial monitor. After Ameba is connected to MQTT server, |
| 54 | +it sends the message “hello world” to “outTopic”. To see the message, |
| 55 | +another MQTT client needs to be set up. |
| 56 | + |
| 57 | +Start the MQTT Explore, and setup the auth connection. All setting is |
| 58 | +same as MQTT_Auth example. |
| 59 | + |
| 60 | +|image09| |
| 61 | + |
| 62 | +Click “ADVANCED” at bottom for topic setup. Use “outTopic” that same as |
| 63 | +“publishTopic” of the board. Click “ADD” then “BACK”. |
| 64 | + |
| 65 | +Click “CONNECT”. The “hello world” message show up at left side. At |
| 66 | +right side, under “Publish” use “inTopic” same as “subscribeTopic” of the |
| 67 | +board. Choose “raw” and input “Text hello Ameba”, then click “PUBLISH”. |
| 68 | +The board will receive the MQTT Explorer published raw message. Then |
| 69 | +publish it from the board side and MQTT Explorer will receive at the |
| 70 | +left side. Note, “hello world” sometimes is not shown up because the |
| 71 | +boards connect to MQTT broker before the MQTT Explorer. |
| 72 | + |
| 73 | +|image12| |
| 74 | + |
| 75 | +|image13| |
| 76 | + |
| 77 | +.. |image01| image:: ../../../../_static/amebapro2/Example_Guides/MQTT/Set_up_Client/image01.png |
| 78 | + :width: 940 px |
| 79 | + :height: 617 px |
| 80 | + |
| 81 | +.. |image08| image:: ../../../../_static/amebapro2/Example_Guides/MQTT/Set_up_Client/image08.png |
| 82 | + :width: 524 px |
| 83 | + :height: 217 px |
| 84 | + |
| 85 | +.. |image09| image:: ../../../../_static/amebapro2/Example_Guides/MQTT/Set_up_Client/image09.png |
| 86 | + :width: 998 px |
| 87 | + :height: 652 px |
| 88 | + |
| 89 | +.. |image12| image:: ../../../../_static/amebapro2/Example_Guides/MQTT/Set_up_Client/image12.png |
| 90 | + :width: 1001 px |
| 91 | + :height: 653 px |
| 92 | + |
| 93 | +.. |image13| image:: ../../../../_static/amebapro2/Example_Guides/MQTT/Set_up_Client/image13.png |
| 94 | + :width: 548 px |
| 95 | + :height: 151 px |
0 commit comments