MQTT Client example program. Ethernet connection is via an ENC28J60 module.
Dependencies: UIPEthernet MQTTClient
main.cpp@12:3af0ae09bd01, 2019-09-03 (annotated)
- Committer:
- hudakz
- Date:
- Tue Sep 03 13:45:22 2019 +0000
- Revision:
- 12:3af0ae09bd01
- Parent:
- 11:a049ad4b63c0
- Child:
- 13:6b2f125cc9b8
Mbed OS 5 enabled.
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 | 12:3af0ae09bd01 | 5 | // |
hudakz | 9:18b414a8c5f7 | 6 | // MQTT broker is like a post office. |
hudakz | 9:18b414a8c5f7 | 7 | // Its task is to distribute messages published by clients to all subscribers (other clients). |
hudakz | 9:18b414a8c5f7 | 8 | // So the 'example/hello' messages published by this client will be sent to the broker. |
hudakz | 9:18b414a8c5f7 | 9 | // Then the broker will send them to all clients which subscribed to such topic (example/hello). |
hudakz | 9:18b414a8c5f7 | 10 | // Also this client will receive all messages with topics it subscribed to (if published such by other clients). |
hudakz | 9:18b414a8c5f7 | 11 | // 'Mosquitto' is a free implementation of MQTT broker for Linux (e.g. Raspberry Pi, Ubuntu etc.) |
hudakz | 12:3af0ae09bd01 | 12 | // |
hudakz | 12:3af0ae09bd01 | 13 | #include "mbed.h" |
hudakz | 12:3af0ae09bd01 | 14 | #include "UipEthernet.h" |
hudakz | 12:3af0ae09bd01 | 15 | #include "MQTTClient.h" |
hudakz | 12:3af0ae09bd01 | 16 | #include <string> |
hudakz | 12:3af0ae09bd01 | 17 | |
hudakz | 12:3af0ae09bd01 | 18 | #define IP "192.168.1.35" |
hudakz | 12:3af0ae09bd01 | 19 | #define GATEWAY "192.168.1.1" |
hudakz | 12:3af0ae09bd01 | 20 | #define NETMASK "255.255.255.0" |
hudakz | 12:3af0ae09bd01 | 21 | #define SERVER_IP "192.168.1.31" // IP address of your MQTT broker. Change to match your case. |
hudakz | 12:3af0ae09bd01 | 22 | |
hudakz | 12:3af0ae09bd01 | 23 | // MAC address must unique within the connected network! Change it as needed. |
hudakz | 12:3af0ae09bd01 | 24 | const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x06 }; |
hudakz | 12:3af0ae09bd01 | 25 | UipEthernet net(MAC, D11, D12, D13, D10); // mac, mosi, miso, sck, cs |
hudakz | 12:3af0ae09bd01 | 26 | void onMqttMessage(char* topic, uint8_t* payload, unsigned int length); // ISR (callback) to handle arrived MQTT messages. |
hudakz | 12:3af0ae09bd01 | 27 | TcpClient client; |
hudakz | 12:3af0ae09bd01 | 28 | MQTTClient mqttClient(SERVER_IP, 1883, onMqttMessage, client); |
hudakz | 12:3af0ae09bd01 | 29 | char message_buff[MQTT_MAX_PACKET_SIZE]; // buffer to store received messages |
hudakz | 9:18b414a8c5f7 | 30 | |
hudakz | 2:6cd2390302ac | 31 | /** |
hudakz | 2:6cd2390302ac | 32 | * @brief |
hudakz | 2:6cd2390302ac | 33 | * @note |
hudakz | 2:6cd2390302ac | 34 | * @param |
hudakz | 2:6cd2390302ac | 35 | * @retval |
hudakz | 2:6cd2390302ac | 36 | */ |
hudakz | 12:3af0ae09bd01 | 37 | int main(void) |
hudakz | 12:3af0ae09bd01 | 38 | { |
hudakz | 12:3af0ae09bd01 | 39 | const int MAX_TRIES = 5; |
hudakz | 12:3af0ae09bd01 | 40 | const time_t INTERVAL = 5; |
hudakz | 12:3af0ae09bd01 | 41 | int i = 0; |
hudakz | 12:3af0ae09bd01 | 42 | bool connected = false; |
hudakz | 12:3af0ae09bd01 | 43 | char payload[] = "Hello World!"; |
hudakz | 12:3af0ae09bd01 | 44 | time_t t = 0; |
hudakz | 12:3af0ae09bd01 | 45 | time_t lastTime = t; |
hudakz | 8:7b4e0d15249f | 46 | |
hudakz | 12:3af0ae09bd01 | 47 | // Bring up the ethernet interface |
hudakz | 12:3af0ae09bd01 | 48 | //net.set_network(IP, NETMASK, GATEWAY); // include this to use static IP address |
hudakz | 12:3af0ae09bd01 | 49 | printf("MQTT client example\n"); |
hudakz | 12:3af0ae09bd01 | 50 | if (net.connect() != 0) { |
hudakz | 12:3af0ae09bd01 | 51 | printf("Error connecting\n"); |
hudakz | 12:3af0ae09bd01 | 52 | return -1; |
hudakz | 3:3e5f4d503a66 | 53 | } |
hudakz | 12:3af0ae09bd01 | 54 | |
hudakz | 12:3af0ae09bd01 | 55 | // Show the network address |
hudakz | 12:3af0ae09bd01 | 56 | const char* ip = net.get_ip_address(); |
hudakz | 12:3af0ae09bd01 | 57 | const char* netmask = net.get_netmask(); |
hudakz | 12:3af0ae09bd01 | 58 | const char* gateway = net.get_gateway(); |
hudakz | 12:3af0ae09bd01 | 59 | printf("IP address: %s\n", ip ? ip : "None"); |
hudakz | 12:3af0ae09bd01 | 60 | printf("Netmask: %s\n", netmask ? netmask : "None"); |
hudakz | 12:3af0ae09bd01 | 61 | printf("Gateway: %s\n", gateway ? gateway : "None"); |
hudakz | 12:3af0ae09bd01 | 62 | |
hudakz | 12:3af0ae09bd01 | 63 | printf("Connecting to the MQTT broker ... "); |
hudakz | 11:a049ad4b63c0 | 64 | do { |
hudakz | 0:158b106f3cfc | 65 | wait(1.0); |
hudakz | 0:158b106f3cfc | 66 | connected = mqttClient.connect("myMQTTHelloClient"); |
hudakz | 12:3af0ae09bd01 | 67 | } while (!connected && (i++ < MAX_TRIES)); |
hudakz | 0:158b106f3cfc | 68 | |
hudakz | 12:3af0ae09bd01 | 69 | if (connected) { |
hudakz | 12:3af0ae09bd01 | 70 | printf("Connected.\r\n\r\n"); |
hudakz | 9:18b414a8c5f7 | 71 | // We can subscribe to various topics published by other clients. |
hudakz | 12:3af0ae09bd01 | 72 | printf("Subscribing to topics:\r\n"); |
hudakz | 12:3af0ae09bd01 | 73 | printf("outdoor/temperature\r\n"); |
hudakz | 12:3af0ae09bd01 | 74 | printf("boiler/outlet/temperature\r\n"); |
hudakz | 12:3af0ae09bd01 | 75 | printf("\r\n"); |
hudakz | 8:7b4e0d15249f | 76 | mqttClient.subscribe("outdoor/temperature"); |
hudakz | 8:7b4e0d15249f | 77 | mqttClient.subscribe("boiler/outlet/temperature"); |
hudakz | 2:6cd2390302ac | 78 | } |
hudakz | 2:6cd2390302ac | 79 | else { |
hudakz | 12:3af0ae09bd01 | 80 | printf("Failed to connect to the MQTT broker.\r\n"); |
hudakz | 12:3af0ae09bd01 | 81 | return -1; |
hudakz | 0:158b106f3cfc | 82 | } |
hudakz | 0:158b106f3cfc | 83 | |
hudakz | 12:3af0ae09bd01 | 84 | while (1) { |
hudakz | 0:158b106f3cfc | 85 | t = time(NULL); |
hudakz | 12:3af0ae09bd01 | 86 | if (t > (lastTime + INTERVAL)) { |
hudakz | 0:158b106f3cfc | 87 | lastTime = t; |
hudakz | 12:3af0ae09bd01 | 88 | if (connected) { |
hudakz | 12:3af0ae09bd01 | 89 | printf("Publishing: example/hello\r\n"); |
hudakz | 0:158b106f3cfc | 90 | mqttClient.publish("example/hello", payload); |
hudakz | 0:158b106f3cfc | 91 | } |
hudakz | 2:6cd2390302ac | 92 | } |
hudakz | 12:3af0ae09bd01 | 93 | |
hudakz | 3:3e5f4d503a66 | 94 | mqttClient.loop(); // MQTT client loop processing (receiving messages) |
hudakz | 0:158b106f3cfc | 95 | } |
hudakz | 0:158b106f3cfc | 96 | } |
hudakz | 0:158b106f3cfc | 97 | |
hudakz | 2:6cd2390302ac | 98 | /** |
hudakz | 2:6cd2390302ac | 99 | * @brief Called on new MQTT message arrival |
hudakz | 8:7b4e0d15249f | 100 | * @note |
hudakz | 2:6cd2390302ac | 101 | * @param topic: The topic of the new message |
hudakz | 2:6cd2390302ac | 102 | * payload: The payload of the new message |
hudakz | 2:6cd2390302ac | 103 | * length: Payload's length |
hudakz | 2:6cd2390302ac | 104 | * @retval |
hudakz | 2:6cd2390302ac | 105 | */ |
hudakz | 12:3af0ae09bd01 | 106 | void onMqttMessage(char* topic, uint8_t* payload, unsigned int length) |
hudakz | 12:3af0ae09bd01 | 107 | { |
hudakz | 0:158b106f3cfc | 108 | int i = 0; |
hudakz | 0:158b106f3cfc | 109 | |
hudakz | 12:3af0ae09bd01 | 110 | printf("Message arrived:\r\n"); |
hudakz | 12:3af0ae09bd01 | 111 | printf(" Topic: %s\r\n", topic); |
hudakz | 12:3af0ae09bd01 | 112 | printf(" Length: %d\r\n", length); |
hudakz | 0:158b106f3cfc | 113 | |
hudakz | 0:158b106f3cfc | 114 | // create character buffer with ending null terminator (string) |
hudakz | 12:3af0ae09bd01 | 115 | for (i = 0; i < length; i++) { |
hudakz | 0:158b106f3cfc | 116 | message_buff[i] = payload[i]; |
hudakz | 0:158b106f3cfc | 117 | } |
hudakz | 0:158b106f3cfc | 118 | |
hudakz | 0:158b106f3cfc | 119 | message_buff[i] = '\0'; |
hudakz | 12:3af0ae09bd01 | 120 | printf(" Payload: %s\r\n\r\n", message_buff); |
hudakz | 0:158b106f3cfc | 121 | } |