MQTT Client example program. Ethernet connection is via an ENC28J60 module.

Dependencies:   UIPEthernet MQTTClient

Committer:
hudakz
Date:
Sun Nov 29 19:00:09 2015 +0000
Revision:
9:18b414a8c5f7
Parent:
8:7b4e0d15249f
Child:
10:27440d67d4f2
Updated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 8:7b4e0d15249f 1 // In this example we create an MQTT client.
hudakz 8:7b4e0d15249f 2 // It's publishing messages with topic 'example/hello' and 'Hello World!' payload.
hudakz 8:7b4e0d15249f 3 // The MQTT client also subscribes to some topics which are in its interest to receive.
hudakz 8:7b4e0d15249f 4 // Ethernet connection is assured by an affordable ENC28J60 Ethernet module.
hudakz 0:158b106f3cfc 5 #include "mbed.h"
hudakz 0:158b106f3cfc 6 #include <UIPEthernet.h>
hudakz 0:158b106f3cfc 7 #include <UIPClient.h>
hudakz 0:158b106f3cfc 8 #include <MQTTClient.h>
hudakz 0:158b106f3cfc 9 #include <string>
hudakz 0:158b106f3cfc 10
hudakz 8:7b4e0d15249f 11 Serial pc(USBTX, USBRX);
hudakz 0:158b106f3cfc 12
hudakz 0:158b106f3cfc 13 // UIPEthernet is the name of a global instance of UIPEthernetClass.
hudakz 0:158b106f3cfc 14 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:158b106f3cfc 15 // Adapt the SPI pin names to your mbed platform/board if not present yet.
hudakz 0:158b106f3cfc 16 #if defined(TARGET_LPC1768)
hudakz 2:6cd2390302ac 17 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 3:3e5f4d503a66 18 #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) || defined(TARGET_NUCLEO_F401RE) \
hudakz 3:3e5f4d503a66 19 || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) \
hudakz 3:3e5f4d503a66 20 || defined(TARGET_NUCLEO_F072RB) || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \
hudakz 3:3e5f4d503a66 21 || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \
hudakz 3:3e5f4d503a66 22 || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
hudakz 3:3e5f4d503a66 23 || defined(TARGET_NRF51822) \
hudakz 3:3e5f4d503a66 24 || defined(TARGET_RZ_A1H)
hudakz 3:3e5f4d503a66 25 UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // mosi, miso, sck, cs
hudakz 0:158b106f3cfc 26 #endif
hudakz 0:158b106f3cfc 27
hudakz 3:3e5f4d503a66 28 // MAC number must be unique within the connected network. Modify as appropriate.
hudakz 2:6cd2390302ac 29 const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
hudakz 3:3e5f4d503a66 30
hudakz 9:18b414a8c5f7 31 // MQTT broker is like a post office.
hudakz 9:18b414a8c5f7 32 // Its task is to distribute messages published by clients to all subscribers (other clients).
hudakz 9:18b414a8c5f7 33 // So the 'example/hello' messages published by this client will be sent to the broker.
hudakz 9:18b414a8c5f7 34 // Then the broker will send them to all clients which subscribed to such topic (example/hello).
hudakz 9:18b414a8c5f7 35 // Also this client will receive all messages with topics it subscribed to (if published such by other clients).
hudakz 9:18b414a8c5f7 36 // 'Mosquitto' is a free implementation of MQTT broker for Linux (e.g. Raspberry Pi, Ubuntu etc.)
hudakz 9:18b414a8c5f7 37 IPAddress serverIP(192, 168, 1, 30); // IP address of your MQTT broker (change to match)
hudakz 9:18b414a8c5f7 38 EthernetClient ethernetClient;
hudakz 9:18b414a8c5f7 39 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
hudakz 9:18b414a8c5f7 40 MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
hudakz 9:18b414a8c5f7 41 char message_buff[100]; // buffer to store received messages
hudakz 9:18b414a8c5f7 42
hudakz 9:18b414a8c5f7 43 #define DHCP 1 // comment out this line if you'd like to use static IP address
hudakz 9:18b414a8c5f7 44
hudakz 3:3e5f4d503a66 45 #if !defined(DHCP)
hudakz 3:3e5f4d503a66 46 // In case you'd like to use static IP address:
hudakz 3:3e5f4d503a66 47 // IP address must be unique and compatible with your network.
hudakz 3:3e5f4d503a66 48 // Change as appropriate.
hudakz 3:3e5f4d503a66 49 const IPAddress MY_IP(192, 168, 1, 181);
hudakz 3:3e5f4d503a66 50 #endif
hudakz 3:3e5f4d503a66 51
hudakz 0:158b106f3cfc 52
hudakz 2:6cd2390302ac 53 /**
hudakz 2:6cd2390302ac 54 * @brief
hudakz 2:6cd2390302ac 55 * @note
hudakz 2:6cd2390302ac 56 * @param
hudakz 2:6cd2390302ac 57 * @retval
hudakz 2:6cd2390302ac 58 */
hudakz 2:6cd2390302ac 59 int main(void) {
hudakz 8:7b4e0d15249f 60 const int MAX_TRIES = 5;
hudakz 8:7b4e0d15249f 61 const unsigned long INTERVAL = 5;
hudakz 8:7b4e0d15249f 62 int i = 0;
hudakz 8:7b4e0d15249f 63 bool connected = false;
hudakz 8:7b4e0d15249f 64 char* payload = "Hello World!";
hudakz 8:7b4e0d15249f 65 unsigned long t = 0;
hudakz 8:7b4e0d15249f 66 unsigned long lastTime = t;
hudakz 8:7b4e0d15249f 67
hudakz 0:158b106f3cfc 68 // initialize the ethernet device
hudakz 2:6cd2390302ac 69
hudakz 3:3e5f4d503a66 70 #if defined(DHCP)
hudakz 3:3e5f4d503a66 71 pc.printf("Searching for DHCP server..\r\n");
hudakz 8:7b4e0d15249f 72
hudakz 3:3e5f4d503a66 73 if(UIPEthernet.begin(MY_MAC) != 1) {
hudakz 3:3e5f4d503a66 74 pc.printf("No DHCP server found.\r\n");
hudakz 3:3e5f4d503a66 75 pc.printf("Exiting application.\r\n");
hudakz 3:3e5f4d503a66 76 return 0;
hudakz 3:3e5f4d503a66 77 }
hudakz 3:3e5f4d503a66 78 pc.printf("DHCP server found and configuration info received.\r\n");
hudakz 3:3e5f4d503a66 79 IPAddress localIP = UIPEthernet.localIP();
hudakz 3:3e5f4d503a66 80 pc.printf("Local IP = ");
hudakz 3:3e5f4d503a66 81 for(uint8_t i = 0; i < 3; i++)
hudakz 3:3e5f4d503a66 82 pc.printf("%d.", localIP[i]);
hudakz 3:3e5f4d503a66 83 pc.printf("%d\r\n", localIP[3]);
hudakz 3:3e5f4d503a66 84 #else
hudakz 1:49fb5754df16 85 UIPEthernet.begin(MY_MAC, MY_IP);
hudakz 3:3e5f4d503a66 86 #endif
hudakz 8:7b4e0d15249f 87
hudakz 2:6cd2390302ac 88 pc.printf("Connecting to MQTT broker ..\r\n");
hudakz 2:6cd2390302ac 89 do
hudakz 2:6cd2390302ac 90 {
hudakz 0:158b106f3cfc 91 wait(1.0);
hudakz 0:158b106f3cfc 92 connected = mqttClient.connect("myMQTTHelloClient");
hudakz 8:7b4e0d15249f 93 } while(!connected && (i++ < MAX_TRIES));
hudakz 0:158b106f3cfc 94
hudakz 0:158b106f3cfc 95 if(connected) {
hudakz 2:6cd2390302ac 96 pc.printf("MQTT broker connected.\r\n");
hudakz 9:18b414a8c5f7 97 // We can subscribe to various topics published by other clients.
hudakz 6:fb8b9925e844 98 pc.printf("Subscribing to topics:\r\n");
hudakz 3:3e5f4d503a66 99 pc.printf(" outdoor/temperature\r\n");
hudakz 3:3e5f4d503a66 100 pc.printf(" boiler/outlet/temperature\r\n");
hudakz 8:7b4e0d15249f 101 mqttClient.subscribe("outdoor/temperature");
hudakz 8:7b4e0d15249f 102 mqttClient.subscribe("boiler/outlet/temperature");
hudakz 2:6cd2390302ac 103 }
hudakz 2:6cd2390302ac 104 else {
hudakz 2:6cd2390302ac 105 pc.printf("Failed to connect to MQTT broker.\r\n");
hudakz 0:158b106f3cfc 106 }
hudakz 0:158b106f3cfc 107
hudakz 0:158b106f3cfc 108 while(1) {
hudakz 0:158b106f3cfc 109 t = time(NULL);
hudakz 5:c0716c55b302 110 if(t > (lastTime + INTERVAL)) {
hudakz 0:158b106f3cfc 111 lastTime = t;
hudakz 0:158b106f3cfc 112 if(connected) {
hudakz 8:7b4e0d15249f 113 pc.printf("Publishing: example/hello\r\n");
hudakz 0:158b106f3cfc 114 mqttClient.publish("example/hello", payload);
hudakz 0:158b106f3cfc 115 }
hudakz 2:6cd2390302ac 116 }
hudakz 3:3e5f4d503a66 117 mqttClient.loop(); // MQTT client loop processing (receiving messages)
hudakz 0:158b106f3cfc 118 }
hudakz 0:158b106f3cfc 119 }
hudakz 0:158b106f3cfc 120
hudakz 2:6cd2390302ac 121 /**
hudakz 2:6cd2390302ac 122 * @brief Called on new MQTT message arrival
hudakz 8:7b4e0d15249f 123 * @note
hudakz 2:6cd2390302ac 124 * @param topic: The topic of the new message
hudakz 2:6cd2390302ac 125 * payload: The payload of the new message
hudakz 2:6cd2390302ac 126 * length: Payload's length
hudakz 2:6cd2390302ac 127 * @retval
hudakz 2:6cd2390302ac 128 */
hudakz 2:6cd2390302ac 129 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length) {
hudakz 0:158b106f3cfc 130 int i = 0;
hudakz 0:158b106f3cfc 131
hudakz 0:158b106f3cfc 132 pc.printf("Message arrived:\r\n");
hudakz 3:3e5f4d503a66 133 pc.printf(" Topic: %s\r\n", topic);
hudakz 3:3e5f4d503a66 134 pc.printf(" Length: %d\r\n", length);
hudakz 0:158b106f3cfc 135
hudakz 0:158b106f3cfc 136 // create character buffer with ending null terminator (string)
hudakz 0:158b106f3cfc 137 for(i = 0; i < length; i++) {
hudakz 0:158b106f3cfc 138 message_buff[i] = payload[i];
hudakz 0:158b106f3cfc 139 }
hudakz 0:158b106f3cfc 140
hudakz 0:158b106f3cfc 141 message_buff[i] = '\0';
hudakz 9:18b414a8c5f7 142 pc.printf(" Payload: %s\r\n", message_buff);
hudakz 0:158b106f3cfc 143 }