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

Dependencies:   UIPEthernet MQTTClient

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