Linkit Series Blog:
MediaTek Linkit 7688 (a) Get started and build OpenWrt cross-compilation environment under Mac, C language compilation Hello,world
MediaTek Linkit 7688 (ii) GPIO basic operation and C language programming
MediaTek Linkit 7688 DUO (iii): Control peripherals and sensors via Arduino
Linkit 7688 DUO (iv): access to various Arduino sensors and modules-basic
Linkit 7688 DUO (v) connected with various Arduino sensors and modules-extensions
Linkit 7688 DUO (vi) joins the MQTT IoT protocol
This article introduces the MQTT IoT protocol to the Linkit 7688 duo Development Board (where the source code and methods apply to all Linux systems and also to Windows systems)
Mqtt is an internet of things instant Messaging protocol developed by IBM that supports all platforms to connect almost all networked items, computer apps, mobile apps, Web pages, and so on, so they can exchange information with each other at any time.
With the MQTT protocol, the Linkit 7688 Development Board can be called the IoT Development Board, can achieve many scenarios, such as: Mobile remote control, Web remote control, server monitoring object status and so on.
I. Introduction to the MQTT protocol
1,MQTT is a TCP-based network message protocol that is used to send and receive messages.
In an MQTT network, there is one server and multiple clients. Each client connects to the server with a TCP socket and remains a long connection.
The client can be a networked object, computer software, mobile app, or even a Web page. Such as:
2, the message transfer takes the Publish/subscribe (publish/subscribe) mode.
The client can publish (publish) messages, each consisting of a subject (topic) and a message content. For example: A temperature sensor issued a message: Topic = "Sensor1/temperature", message= "25"
A client can subscribe to (subscribe) messages for one or more topics. When someone on the network publishes messages for these topics, the client will receive this message. For example: Mobile app subscribed to the "sensor1/temperature" message, the above message will be received, the mobile app will know the temperature value of Sensor1.
Because the client maintains a long socket connection with the server, the message is immediately pushed to the client, that is, push to the client.
The primary role of the MQTT server is to receive-forward: Receive messages, determine which clients subscribe to the subject's messages, and push to the appropriate client.
This mechanism enables one-to-one sending of messages, or a one-to-many-send message (mass).
Examples of application scenarios:
Scenario 1: The mobile app publishes a temperature-checking message, and each sensor device receives a message, releasing a temperature message. The phone can collect the temperature of each sensor.
Scenario 2: Post a message when a fire alarm sensor device detects a fire. Mobile phones, computers, etc. that subscribe to this type of message can receive a fire alarm immediately.
3,MQTT's message packet transmits a small amount of data (a fixed-length header of only 2 bytes), which can fully reduce network traffic and is ideal for low bandwidth, unreliable connections, and embedded devices.
It is also very suitable for mobile communication environment, such as mobile phone, can save the flow, power saving. As a result, someone uses MQTT as a mobile push.
4, in order to ensure the efficient arrival of messages, MQTT defines three types of message publishing quality of service (Qos, quality of services):
At most once, where the overhead is minimal and message publishing relies entirely on the underlying TCP/IP network, message loss or duplication occurs. This level can be used when the environment sensor data, loss of a read record does not matter, because there will be a second time to send.
"At least once" (at least once) to ensure that the message arrives, but the message duplication may occur.
"Only once" (exactly once), make sure the message arrives once. This level can be used for situations where, in a billing system, duplicate or missing messages can result in incorrect results. Often used for precise control.
Each message can have its own QoS definition. You can also specify QoS when each client subscribes to a topic.
5, the MQTT protocol was proposed by IBM in 1999, after years of development has been relatively mature, the current version is 3.1.1. Both servers and clients have multiple open source implementations that support a wide range of operating systems and development languages.
Ii. MQTT protocol Client for embedded devices (C language Implementation)
For the Linkit 7688 Development Board, I chose the MQTT client open source repository for Eclipse Paho C. Its homepage is in this: https://www.eclipse.org/paho/clients/c/
This codebase is provided by IBM, which is maintained by the Eclipse Project team and can be called official.
I use it as a library of functions, writing a C language program on Linkit 7688 to enable MQTT messaging.
This library is a little bit more complex and requires a better understanding of the protocol. If you want to fully understand it, you need to read its documentation first (http://www.eclipse.org/paho/files/mqttdoc/Cclient/index.html)
We generally like to simplify complex things, so I encapsulate this library into several functions that don't need to know too much to use.
Libraries and routines are downloaded in my resources: The MQTT directory is PAHO c all library files (the original file has not changed, I added mqtt_client.c, mqtt_client.h two files), use the Mqtt directory to copy into your project folder.
Before use, there needs to be a test MQTT server, I use a test server provided by IBM: Messagesight.demos.ibm.com, Port is 1883 (1883 is the default port of Mqtt)
At the same time, you need a test MQTT client as the peer, I'm using an IBM-provided web version of MQTT client: http://m2m.demos.ibm.com/mqttclient/
1, in Linkit 7688 creates Macintosh a program that sends an MQTT message:
Build a cross-compilation project with Eclipse (development environment setup see: MediaTek linkit 7688 (a) Get started and build OpenWrt cross-compilation environment under Mac, C language compilation Hello,world)
Copy the Mqtt directory to your project folder.
Create a mqtt_publish.c file and write the main program as follows:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include "mqtt/mqtt_client.h"//This is my mqtt_ Client encapsulated header file int publish (int argc, char * * argv) {mqtt_client *m;//mqtt_client object pointer int ret;//return value char *host = "Messagesig ht.demos.ibm.com:1883 ";//test server char *topic =" Test_topic "; Subject char *client_id = "clientid33883";//client ID; For the test server, you can write char *username = null;//user name to verify the identity. On the test server, none. char *password = null;//password, used to verify identity. On the test server, none. int Qos; Quality of service//create new MQTT Client OBJECTM = mqtt_new (host, Mqtt_port, client_id); Create object, Mqtt_port = 1883if (m = = NULL) {printf ("MQTT client create failure, return code =%d\n", errno); return 1;} else {printf ("Mqtt client created\n");} Connect to Serverret = Mqtt_connect (M, username, password); Connect the server if (ret! = mqtt_success) {printf ("MQTT client connect failure, return code =%d\n", ret); return 1;} else {printf (" MQTT client connect\n ");} Publish Messageqos = Qos_exactly_once; Qosret = Mqtt_publish (M, topic, "Hello from Linkit" 7688 ", Qos);//Publish Message printf (" Mqtt client publish, return code =%d\n ", ret); Mqtt_disconnect (m); Disconnectmqtt_delete (m); Delete Mqtt client Objectreturn 0;}
The main program consists of three steps:
1, call Mqtt_new () to create the client object
2, call Mqtt_connect () to connect to the server
3, call Mqtt_publish () to publish the message
Compiling a project with eclipse
If an error message appears:
Undefined reference to ' pthread_create '
Undefined reference to ' pthread_mutexattr_init '
This is because the Pthread library is not included when compiling the connection, the workaround: Add the-lpthread parameter to GCC.
If an error message appears:
Undefined reference to ' dlclose '
Undefined reference to ' Dlopen '
Undefined reference to ' Dlsym
This is because the DL library is not included when compiling the connection, the workaround: To add the-LDL parameter in GCC
The actions in Eclipse are: Project property = c + + Build = Settings, on the Tools Setting page, select Cross GCC Linker, and fill in the Linker Flags compilation box :-LPTHREAD-LDL
OK, the compilation was successful.
Use SCP to upload mqtt_publish program to Linkit 7688:scp mqtt_publish [email protected]:/root
To prepare the Web client to receive the message: Open http://m2m.demos.ibm.com/mqttclient/in the browser
In the Connect bar, click Connect.
In the Subscribe bar, set topic to Test_topic, press Subscribe
OK, the Web client is ready to receive the message of the topic (topic) for Test_topic.
Log in to Linkit 7688 with SSH and run the Mqtt_publish program.
The run result shows the process of mqtt creating, connecting, and publishing messages, and return code >=0 indicates success :
MQTT Client Created
MQTT Client Connect
MQTT client Publish, return code = 1
At this point, you can see that the MQTT Web client in the browser has received an MQTT message from the Linkit 7688 release
Succeeded: Linkit 7688 publishes the MQTT message to the server, and the Web client receives the server push message in real time.
2, in Linkit 7688 creates Macintosh a program that receives an MQTT message:
Build a cross-compilation project with Eclipse (development environment setup see: MediaTek linkit 7688 (a) Get started and build OpenWrt cross-compilation environment under Mac, C language compilation Hello,world)
Copy the Mqtt directory to your project folder.
Create a mqtt_subscribe.c file and write the main program as follows:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <signal.h> #include "mqtt/ Mqtt_client.h "//This is my header file for mqtt_client encapsulated int running = 1;void stop_running (int sig) {signal (SIGINT, NULL); running = 0;} int main (int argc, char * * argv) {mqtt_client *m;//mqtt_client object pointer int ret;//return value char *host = "messagesight.demos.ibm.com : 1883 ";//test server char *topic =" Test_topic "; Subject char *client_id = "clientid33883";//client ID; For the test server, you can write char *username = null;//user name to verify the identity. On the test server, none. char *password = null;//password, used to verify identity. On the test server, none. int Qos; Quality of service//create new MQTT Client OBJECTM = mqtt_new (host, Mqtt_port, client_id); Create object, Mqtt_port = 1883if (m = = NULL) {printf ("MQTT client create failure, return code =%d\n", errno); return 1;} else {printf ("Mqtt client created\n");} Connect to Serverret = Mqtt_connect (M, username, password); Connect the server if (ret! = mqtt_success) {printf ("MQTT client connect failure, return code =%d\n", ret); return 1;} else {printf (" MQTT Client Connect\ n ");} Subscribeqos = Qos_exactly_once;ret = Mqtt_subscribe (M, topic, QOS);//Subscribe to Message printf ("Mqtt client subscribe%s, return code =%d\n ", topic, ret); signal (SIGINT, stop_running); signal (SIGTERM, stop_running);p rintf (" Wait for Message of topic:%s.. . \ n ", topic);//loop:waiting message, loop while (running) {int timeout = 200;if (mqtt_receive (m, timeout) = = mqtt_success) {//recieve message, receiving messages printf ("Received topic=%s, message=%s\n", M->received_topic, M->received_message);} Mqtt_sleep (200); Sleep a while}mqtt_disconnect (m); disconnectprintf ("Mqtt client Disconnect"); Mqtt_delete (m); Delete Mqtt client Objectreturn 0;}
The main program is divided into several steps:
1, call Mqtt_new () to create the client object
2, call Mqtt_connect () to connect to the server
3, call Mqtt_subscribe () Subscribe message
4, enter the loop: constantly using mqtt_receive () to detect whether there is a new message, if any, then print out.
Compile the project with eclipse (remember that you must join the connection option in GCC:-LPTHREAD-LDL)
OK, the compilation was successful.
Use SCP to upload mqtt_subscribe program to Linkit 7688:SCP mqtt_subscribe [email protected]:/root
Log in to Linkit 7688 with SSH and run the mqtt_subscribe program. It appears at this point:
MQTT Client Created
MQTT Client Connect
MQTT client Subscribe, return code = 0
Wait for message of topic:test_topic ...
Show program waiting for message to arrive
Web client ready to send message: open http://m2m.demos.ibm.com/mqttclient/in Browser
In the Connect bar, click Connect.
In the Publish bar, set topic to Test_topic and the message to "Say Hello to Linkit 7688" Press Publish to issue an MQTT message.
At this point, look at Linkit 7688 SSH client, you can see, Linkit7688 immediately received this MQTT message
MQTT Client Created
MQTT Client Connect
MQTT client Subscribe, return code = 0
Wait for message of topic:test_topic ...
Received Topic=test_topic, Message=say hello to Linkit 7688
Succeeded: Linkit 7688 received a message from the Web client in real time.
The Mqtt_subscribe program is a dead loop that has been receiving messages. Press CTRL + C to exit.
Versatility: The MQTT client code for Eclipse Paho C can run on Linux, Windows, and Mac. Therefore, the above code can be used for Linux, Windows, Mac operating system, writing a variety of apps.
For Android phones, Eclipse PAHO also provides a Java code base.
Linkit 7688 DUO (vi) joins the MQTT IoT protocol