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

Dependencies:   UIPEthernet MQTTClient

Committer:
hudakz
Date:
Sat Nov 28 10:36:41 2015 +0000
Revision:
4:abcd50d71e61
Parent:
3:3e5f4d503a66
Child:
5:c0716c55b302
Using DHCP to get IP address.

Who changed what in which revision?

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