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