Zoltan Hudak / Mbed OS MQTT_Hello

Dependencies:   ENC28J60-EMAC

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "EthernetInterface.h"
00004 #include "MQTTNetwork.h"
00005 #include "MQTTmbed.h"
00006 #include "MQTTClient.h"
00007 
00008 #define IP          "192.168.1.35"
00009 #define GATEWAY     "192.168.1.1"
00010 #define NETMASK     "255.255.255.0"
00011 #define BROKER_IP   "192.168.1.31"  // IP address of your MQTT broker. Change to match your case.
00012 #define MQTT_PORT   1883            // MQTT port
00013 
00014 const uint8_t                           MAC[6] = { 0, 1, 2, 3, 4, 5 };
00015 const char                              payload[] = "Hello, World!";
00016 
00017 // Global variables
00018 char                                    topic[256];
00019 EthernetInterface                       net;
00020 MQTTNetwork                             mqttNetwork(&net);
00021 MQTT::Client<MQTTNetwork, Countdown>    mqttClient(mqttNetwork);
00022 Timer                                   timer;
00023 
00024 // Function prototypes
00025 void                                    onMqttMsgReceived(MQTT::MessageData& md);
00026 void                                    publishMsg();
00027 
00028 /**
00029  * @brief
00030  * @note
00031  * @param
00032  * @retval
00033  */
00034 FileHandle* mbed::mbed_override_console(int)
00035 {
00036     static BufferedSerial   myConsole(USBTX, USBRX, 115200);
00037     return &myConsole;
00038 }
00039 
00040 /**
00041  * @brief
00042  * @note
00043  * @param
00044  * @retval
00045  */
00046 int main(void)
00047 {
00048     // Bring up the ethernet interface
00049     net.set_network(IP, NETMASK, GATEWAY);  // include this to use static IP address
00050     net.get_emac().set_hwaddr(MAC);         // set MAC address
00051     printf("MQTT client example\n");
00052     if (net.connect() != 0) {
00053         printf("Error connecting\n");
00054         return -1;
00055     }
00056 
00057     // Show network address
00058     SocketAddress addr;
00059     net.get_ip_address(&addr);
00060     printf("IP address: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
00061     net.get_netmask(&addr);
00062     printf("Netmask: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
00063     net.get_gateway(&addr);
00064     printf("Gateway: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None");
00065 
00066     printf("Connecting to %s:%d\r\n", BROKER_IP, MQTT_PORT);
00067     nsapi_error_t error = mqttNetwork.connect(BROKER_IP, MQTT_PORT);
00068     if (error == NSAPI_ERROR_OK) {
00069         printf("Server connected.\r\n");
00070     }
00071     else {
00072         printf("Cannot connect server.\r\n");
00073         return -1;
00074     }
00075 
00076     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
00077     data.MQTTVersion = 3;
00078     data.clientID.cstring = "MyMqttClient";
00079     data.username.cstring = NULL;
00080     data.password.cstring = NULL;
00081 
00082     // Connect MQTT client to MQTT broker
00083     printf("Connecting MQTT Broker\r\n");
00084     if (mqttClient.connect(data) != 0) {
00085         printf("Cannot connect MQTT broker.\r\n");
00086         return -1;
00087     };
00088     printf("MQTT Broker connected.\r\n");
00089 
00090     // Subscribe to topics
00091     mqttClient.subscribe("outdoorTemperature", MQTT::QOS0, onMqttMsgReceived);
00092 
00093     // Start timer
00094     timer.start();
00095     
00096     // Main thread loop
00097     while (1) {
00098         mqttClient.yield(10);
00099         if (timer.read_ms() > 1000) {
00100             timer.reset();
00101             publishMsg();   // once a second publish the MQTT message
00102         }
00103     }
00104 }
00105 
00106 /**
00107  * @brief
00108  * @note
00109  * @param
00110  * @retval
00111  */
00112 void onMqttMsgReceived(MQTT::MessageData& md)
00113 {
00114     memset(topic, 0, sizeof(topic));
00115     memcpy(topic, md.topicName.lenstring.data, md.topicName.lenstring.len);
00116     printf("topic: %s\r\n", topic);
00117 }
00118 
00119 /**
00120  * @brief
00121  * @note
00122  * @param
00123  * @retval
00124  */
00125 void publishMsg()
00126 {
00127     MQTT::Message mqttMsg;
00128 
00129     mqttMsg.qos = MQTT::QOS0;
00130     mqttMsg.retained = false;
00131     mqttMsg.dup = false;
00132     mqttMsg.payload = (void*)payload;
00133     mqttMsg.payloadlen = strlen(payload);    
00134     mqttClient.publish("topic_hello_world", mqttMsg);
00135 }