MQTT Client example program. Ethernet connection is via an ENC28J60 module.
Dependencies: UIPEthernet MQTTClient
Diff: main.cpp
- Revision:
- 8:7b4e0d15249f
- Parent:
- 7:4435b52322e4
- Child:
- 9:18b414a8c5f7
--- a/main.cpp Sun Nov 29 14:39:02 2015 +0000 +++ b/main.cpp Sun Nov 29 18:18:46 2015 +0000 @@ -1,16 +1,14 @@ -// In this example an MQTT client is created. -// It is publishing a simple 'example/hello' message with payload 'Hello World.' -// and subscribes to some messages assumably published by other client(s). -// Ethernet connection is assured by an ENC28J60 Ehernet module. +// In this example we create an MQTT client. +// It's publishing messages with topic 'example/hello' and 'Hello World!' payload. +// The MQTT client also subscribes to some topics which are in its interest to receive. +// Ethernet connection is assured by an affordable ENC28J60 Ethernet module. #include "mbed.h" #include <UIPEthernet.h> #include <UIPClient.h> #include <MQTTClient.h> #include <string> -using namespace std; - -Serial pc(USBTX, USBRX); +Serial pc(USBTX, USBRX); #define DHCP 1 // comment out this line if you'd like to use static IP address @@ -39,8 +37,7 @@ const IPAddress MY_IP(192, 168, 1, 181); #endif -const int INTERVAL = 5; // Interval for publishing the messages (in seconds) - +const int INTERVAL = 10; // Interval for publishing the messages (in seconds) char message_buff[100]; // MQTT broker is like a post office. @@ -48,7 +45,7 @@ // So the 'example/hello' messages published by this client will be sent to the broker. // Then the broker will send them to all clients which subscribed to such topic (example/hello). // Also this client will receive all messages with topics it subscribed to. -// 'Mosquitto' is a free implementation of MQTT broker for Linux machines (e.g. Raspberry Pi, Ubuntu etc.) +// 'Mosquitto' is a free implementation of MQTT broker for Linux (e.g. Raspberry Pi, Ubuntu etc.) IPAddress serverIP(192, 168, 1, 30); // IP address of your MQTT broker (adapt) EthernetClient ethernetClient; void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); @@ -62,18 +59,20 @@ */ int main(void) { - const int MAX_COUNT = 5; - int i = 0; - bool connected = false; - char* payload = "Hello World."; - time_t t = 0; - time_t lastTime = t; + const int MAX_TRIES = 5; + const unsigned long INTERVAL = 5; + int i = 0; + bool connected = false; + char* payload = "Hello World!"; + unsigned long t = 0; + unsigned long lastTime = t; + // initialize the ethernet device #if defined(DHCP) pc.printf("Searching for DHCP server..\r\n"); - + if(UIPEthernet.begin(MY_MAC) != 1) { pc.printf("No DHCP server found.\r\n"); pc.printf("Exiting application.\r\n"); @@ -88,22 +87,22 @@ #else UIPEthernet.begin(MY_MAC, MY_IP); #endif - + pc.printf("Connecting to MQTT broker ..\r\n"); do { wait(1.0); connected = mqttClient.connect("myMQTTHelloClient"); - } while(!connected && (i < MAX_COUNT)); + } while(!connected && (i++ < MAX_TRIES)); if(connected) { pc.printf("MQTT broker connected.\r\n"); - // The client can subscribe to as many topics as you like. - mqttClient.subscribe("outdoor/temperature"); - mqttClient.subscribe("boiler/outlet/temperature"); + // The client can subscribe to many topics. pc.printf("Subscribing to topics:\r\n"); pc.printf(" outdoor/temperature\r\n"); pc.printf(" boiler/outlet/temperature\r\n"); + mqttClient.subscribe("outdoor/temperature"); + mqttClient.subscribe("boiler/outlet/temperature"); } else { pc.printf("Failed to connect to MQTT broker.\r\n"); @@ -114,8 +113,8 @@ if(t > (lastTime + INTERVAL)) { lastTime = t; if(connected) { + pc.printf("Publishing: example/hello\r\n"); mqttClient.publish("example/hello", payload); - pc.printf("Publishing: example/hello\r\n"); } } mqttClient.loop(); // MQTT client loop processing (receiving messages) @@ -124,7 +123,7 @@ /** * @brief Called on new MQTT message arrival - * @note + * @note * @param topic: The topic of the new message * payload: The payload of the new message * length: Payload's length @@ -143,5 +142,5 @@ } message_buff[i] = '\0'; - pc.printf(" Payload: %s\r\n", message_buff); + pc.printf(" Payload: %s\r\n", message_buff); }