MQTT Client example program. Ethernet connection is via an ENC28J60 module.
Dependencies: UIPEthernet MQTTClient
main.cpp@2:6cd2390302ac, 2015-03-21 (annotated)
- Committer:
- hudakz
- Date:
- Sat Mar 21 23:45:41 2015 +0000
- Revision:
- 2:6cd2390302ac
- Parent:
- 1:49fb5754df16
- Child:
- 3:3e5f4d503a66
Comments updated.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:6cd2390302ac | 3 | // and subscribes to 'outdoor/temperature' messages assumably published by other client(s). |
hudakz | 2:6cd2390302ac | 4 | // Ethernet connection is assured via an ENC28J60 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 | 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 | 0:158b106f3cfc | 20 | #elif defined(TARGET_LPC1114) |
hudakz | 2:6cd2390302ac | 21 | UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs |
hudakz | 0:158b106f3cfc | 22 | #elif defined(TARGET_LPC11U68) |
hudakz | 2:6cd2390302ac | 23 | UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs |
hudakz | 2:6cd2390302ac | 24 | #elif defined(TARGET_NUCLEO_F103RB) |
hudakz | 2:6cd2390302ac | 25 | UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs |
hudakz | 0:158b106f3cfc | 26 | #endif |
hudakz | 0:158b106f3cfc | 27 | |
hudakz | 0:158b106f3cfc | 28 | // Make sure that the MAC number is unique within the connected network. |
hudakz | 2:6cd2390302ac | 29 | const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; |
hudakz | 0:158b106f3cfc | 30 | // IP address must be unique and compatible with your network too. Change as appropriate. |
hudakz | 2:6cd2390302ac | 31 | const IPAddress MY_IP(192, 168, 1, 181); |
hudakz | 2:6cd2390302ac | 32 | char message_buff[100]; |
hudakz | 2:6cd2390302ac | 33 | IPAddress serverIP(192, 168, 1, 30); // MQTT broker (e.g. 'Mosquitto' running on a Raspberry Pi or Linux machine) |
hudakz | 2:6cd2390302ac | 34 | EthernetClient ethernetClient; |
hudakz | 2:6cd2390302ac | 35 | void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); |
hudakz | 2:6cd2390302ac | 36 | MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient); |
hudakz | 0:158b106f3cfc | 37 | |
hudakz | 2:6cd2390302ac | 38 | /** |
hudakz | 2:6cd2390302ac | 39 | * @brief |
hudakz | 2:6cd2390302ac | 40 | * @note |
hudakz | 2:6cd2390302ac | 41 | * @param |
hudakz | 2:6cd2390302ac | 42 | * @retval |
hudakz | 2:6cd2390302ac | 43 | */ |
hudakz | 2:6cd2390302ac | 44 | |
hudakz | 2:6cd2390302ac | 45 | int main(void) { |
hudakz | 0:158b106f3cfc | 46 | const int MAX_COUNT = 5; |
hudakz | 0:158b106f3cfc | 47 | int i = 0; |
hudakz | 0:158b106f3cfc | 48 | bool connected = false; |
hudakz | 0:158b106f3cfc | 49 | char* payload = "Hello World."; |
hudakz | 0:158b106f3cfc | 50 | time_t t = 0; |
hudakz | 0:158b106f3cfc | 51 | time_t lastTime = t; |
hudakz | 0:158b106f3cfc | 52 | |
hudakz | 0:158b106f3cfc | 53 | // initialize the ethernet device |
hudakz | 2:6cd2390302ac | 54 | |
hudakz | 1:49fb5754df16 | 55 | UIPEthernet.begin(MY_MAC, MY_IP); |
hudakz | 2:6cd2390302ac | 56 | |
hudakz | 2:6cd2390302ac | 57 | // The MQTT broker is like a post office distributing messages published by clients to all subscribers (clients). |
hudakz | 2:6cd2390302ac | 58 | // So the 'example/hello' messages published by this client will be sent via the broker to all clients which subscribed to such messages. |
hudakz | 2:6cd2390302ac | 59 | // This way also this client will receive all subscribed messages (in this case 'outdoor/temperature' messages) |
hudakz | 2:6cd2390302ac | 60 | // published by other clients (however not directly but via the broker). |
hudakz | 2:6cd2390302ac | 61 | pc.printf("Connecting to MQTT broker ..\r\n"); |
hudakz | 2:6cd2390302ac | 62 | do |
hudakz | 2:6cd2390302ac | 63 | { |
hudakz | 0:158b106f3cfc | 64 | wait(1.0); |
hudakz | 0:158b106f3cfc | 65 | connected = mqttClient.connect("myMQTTHelloClient"); |
hudakz | 0:158b106f3cfc | 66 | } while(!connected && (i < MAX_COUNT)); |
hudakz | 0:158b106f3cfc | 67 | |
hudakz | 0:158b106f3cfc | 68 | if(connected) { |
hudakz | 2:6cd2390302ac | 69 | pc.printf("MQTT broker connected.\r\n"); |
hudakz | 0:158b106f3cfc | 70 | mqttClient.subscribe("outdoor/temperature"); |
hudakz | 0:158b106f3cfc | 71 | pc.printf("Subscribed to: outdoor/temperature\r\n"); |
hudakz | 2:6cd2390302ac | 72 | } |
hudakz | 2:6cd2390302ac | 73 | else { |
hudakz | 2:6cd2390302ac | 74 | pc.printf("Failed to connect to MQTT broker.\r\n"); |
hudakz | 0:158b106f3cfc | 75 | } |
hudakz | 0:158b106f3cfc | 76 | |
hudakz | 0:158b106f3cfc | 77 | while(1) { |
hudakz | 0:158b106f3cfc | 78 | t = time(NULL); |
hudakz | 0:158b106f3cfc | 79 | if(t > (lastTime + 5)) { |
hudakz | 0:158b106f3cfc | 80 | lastTime = t; |
hudakz | 0:158b106f3cfc | 81 | if(connected) { |
hudakz | 0:158b106f3cfc | 82 | mqttClient.publish("example/hello", payload); |
hudakz | 0:158b106f3cfc | 83 | pc.printf("Published: example/hello\r\n"); |
hudakz | 0:158b106f3cfc | 84 | } |
hudakz | 2:6cd2390302ac | 85 | } |
hudakz | 0:158b106f3cfc | 86 | |
hudakz | 0:158b106f3cfc | 87 | mqttClient.loop(); // MQTT client loop processing |
hudakz | 0:158b106f3cfc | 88 | } |
hudakz | 0:158b106f3cfc | 89 | } |
hudakz | 0:158b106f3cfc | 90 | |
hudakz | 2:6cd2390302ac | 91 | /** |
hudakz | 2:6cd2390302ac | 92 | * @brief Called on new MQTT message arrival |
hudakz | 2:6cd2390302ac | 93 | * @note |
hudakz | 2:6cd2390302ac | 94 | * @param topic: The topic of the new message |
hudakz | 2:6cd2390302ac | 95 | * payload: The payload of the new message |
hudakz | 2:6cd2390302ac | 96 | * length: Payload's length |
hudakz | 2:6cd2390302ac | 97 | * @retval |
hudakz | 2:6cd2390302ac | 98 | */ |
hudakz | 2:6cd2390302ac | 99 | void onMqttMessage(char* topic, uint8_t* payload, unsigned int length) { |
hudakz | 0:158b106f3cfc | 100 | int i = 0; |
hudakz | 0:158b106f3cfc | 101 | |
hudakz | 0:158b106f3cfc | 102 | pc.printf("Message arrived:\r\n"); |
hudakz | 0:158b106f3cfc | 103 | pc.printf(" Topic: %s\r\n", topic); |
hudakz | 0:158b106f3cfc | 104 | pc.printf(" Length: %d\r\n", length); |
hudakz | 0:158b106f3cfc | 105 | |
hudakz | 0:158b106f3cfc | 106 | // create character buffer with ending null terminator (string) |
hudakz | 0:158b106f3cfc | 107 | for(i = 0; i < length; i++) { |
hudakz | 0:158b106f3cfc | 108 | message_buff[i] = payload[i]; |
hudakz | 0:158b106f3cfc | 109 | } |
hudakz | 0:158b106f3cfc | 110 | |
hudakz | 0:158b106f3cfc | 111 | message_buff[i] = '\0'; |
hudakz | 0:158b106f3cfc | 112 | pc.printf(" Payload: %s\r\n", message_buff); |
hudakz | 0:158b106f3cfc | 113 | } |