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

Dependencies:   UIPEthernet MQTTClient

Committer:
hudakz
Date:
Sun Nov 29 18:18:46 2015 +0000
Revision:
8:7b4e0d15249f
Parent:
7:4435b52322e4
Child:
9:18b414a8c5f7
DHCP added.

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