MQTT Client example program. Ethernet connection is via an ENC28J60 module.
Dependencies: UIPEthernet MQTTClient
main.cpp@17:fd2c65e246aa, 2020-07-23 (annotated)
- Committer:
- hudakz
- Date:
- Thu Jul 23 17:31:41 2020 +0000
- Revision:
- 17:fd2c65e246aa
- Parent:
- 16:2c61673000e8
MQTT Client example program. Ethernet connection is via an ENC28J60 module.
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 | |
hudakz | 12:3af0ae09bd01 | 17 | #define IP "192.168.1.35" |
hudakz | 12:3af0ae09bd01 | 18 | #define GATEWAY "192.168.1.1" |
hudakz | 12:3af0ae09bd01 | 19 | #define NETMASK "255.255.255.0" |
hudakz | 14:b2c96d7b4335 | 20 | #define BROKER_IP "192.168.1.31" // IP address of your MQTT broker. Change to match your case. |
hudakz | 14:b2c96d7b4335 | 21 | #define MQTT_PORT 1883 // MQTT port |
hudakz | 12:3af0ae09bd01 | 22 | |
hudakz | 12:3af0ae09bd01 | 23 | // MAC address must unique within the connected network! Change it as needed. |
hudakz | 13:6b2f125cc9b8 | 24 | const uint8_t MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; |
hudakz | 12:3af0ae09bd01 | 25 | UipEthernet net(MAC, D11, D12, D13, D10); // mac, mosi, miso, sck, cs |
hudakz | 15:da8fef967438 | 26 | MQTTClient mqttClient(BROKER_IP, MQTT_PORT); |
hudakz | 15:da8fef967438 | 27 | char msgBuf[MQTT_MAX_PACKET_SIZE]; // buffer to store received MQTT messages |
hudakz | 14:b2c96d7b4335 | 28 | |
hudakz | 14:b2c96d7b4335 | 29 | /** |
hudakz | 14:b2c96d7b4335 | 30 | * @brief ISR to handle received MQTT messages |
hudakz | 14:b2c96d7b4335 | 31 | * @note Called on arrival of new MQTT message |
hudakz | 15:da8fef967438 | 32 | * @param topic The topic of received message |
hudakz | 15:da8fef967438 | 33 | * @param payload The payload of received message |
hudakz | 15:da8fef967438 | 34 | * @param length Payload's length |
hudakz | 14:b2c96d7b4335 | 35 | * @retval |
hudakz | 14:b2c96d7b4335 | 36 | */ |
hudakz | 14:b2c96d7b4335 | 37 | void onMqttMessage(char* topic, uint8_t* payload, uint16_t length) |
hudakz | 14:b2c96d7b4335 | 38 | { |
hudakz | 14:b2c96d7b4335 | 39 | int i = 0; |
hudakz | 14:b2c96d7b4335 | 40 | |
hudakz | 14:b2c96d7b4335 | 41 | printf("Message arrived:\r\n"); |
hudakz | 14:b2c96d7b4335 | 42 | printf(" Topic: %s\r\n", topic); |
hudakz | 14:b2c96d7b4335 | 43 | printf(" Length: %d\r\n", length); |
hudakz | 14:b2c96d7b4335 | 44 | |
hudakz | 14:b2c96d7b4335 | 45 | // create character buffer with ending null terminator (string) |
hudakz | 14:b2c96d7b4335 | 46 | for (i = 0; i < length; i++) { |
hudakz | 14:b2c96d7b4335 | 47 | msgBuf[i] = payload[i]; |
hudakz | 14:b2c96d7b4335 | 48 | } |
hudakz | 14:b2c96d7b4335 | 49 | |
hudakz | 14:b2c96d7b4335 | 50 | msgBuf[i] = '\0'; |
hudakz | 14:b2c96d7b4335 | 51 | printf(" Payload: %s\r\n\r\n", msgBuf); |
hudakz | 14:b2c96d7b4335 | 52 | } |
hudakz | 9:18b414a8c5f7 | 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 | 12:3af0ae09bd01 | 60 | int main(void) |
hudakz | 12:3af0ae09bd01 | 61 | { |
hudakz | 16:2c61673000e8 | 62 | const int MAX_TRIES = 1; |
hudakz | 12:3af0ae09bd01 | 63 | const time_t INTERVAL = 5; |
hudakz | 12:3af0ae09bd01 | 64 | int i = 0; |
hudakz | 12:3af0ae09bd01 | 65 | bool connected = false; |
hudakz | 12:3af0ae09bd01 | 66 | char payload[] = "Hello World!"; |
hudakz | 12:3af0ae09bd01 | 67 | time_t t = 0; |
hudakz | 12:3af0ae09bd01 | 68 | time_t lastTime = t; |
hudakz | 8:7b4e0d15249f | 69 | |
hudakz | 12:3af0ae09bd01 | 70 | // Bring up the ethernet interface |
hudakz | 12:3af0ae09bd01 | 71 | //net.set_network(IP, NETMASK, GATEWAY); // include this to use static IP address |
hudakz | 12:3af0ae09bd01 | 72 | printf("MQTT client example\n"); |
hudakz | 12:3af0ae09bd01 | 73 | if (net.connect() != 0) { |
hudakz | 12:3af0ae09bd01 | 74 | printf("Error connecting\n"); |
hudakz | 12:3af0ae09bd01 | 75 | return -1; |
hudakz | 3:3e5f4d503a66 | 76 | } |
hudakz | 12:3af0ae09bd01 | 77 | |
hudakz | 16:2c61673000e8 | 78 | // Show the network address |
hudakz | 16:2c61673000e8 | 79 | SocketAddress addr; |
hudakz | 16:2c61673000e8 | 80 | net.get_ip_address(&addr); |
hudakz | 16:2c61673000e8 | 81 | printf("IP address: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None"); |
hudakz | 16:2c61673000e8 | 82 | net.get_netmask(&addr); |
hudakz | 16:2c61673000e8 | 83 | printf("Netmask: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None"); |
hudakz | 16:2c61673000e8 | 84 | net.get_gateway(&addr); |
hudakz | 16:2c61673000e8 | 85 | printf("Gateway: %s\n", addr.get_ip_address() ? addr.get_ip_address() : "None"); |
hudakz | 12:3af0ae09bd01 | 86 | |
hudakz | 16:2c61673000e8 | 87 | |
hudakz | 16:2c61673000e8 | 88 | printf("Connecting to the MQTT broker ...\r\n"); |
hudakz | 11:a049ad4b63c0 | 89 | do { |
hudakz | 0:158b106f3cfc | 90 | wait(1.0); |
hudakz | 0:158b106f3cfc | 91 | connected = mqttClient.connect("myMQTTHelloClient"); |
hudakz | 12:3af0ae09bd01 | 92 | } while (!connected && (i++ < MAX_TRIES)); |
hudakz | 0:158b106f3cfc | 93 | |
hudakz | 12:3af0ae09bd01 | 94 | if (connected) { |
hudakz | 12:3af0ae09bd01 | 95 | printf("Connected.\r\n\r\n"); |
hudakz | 9:18b414a8c5f7 | 96 | // We can subscribe to various topics published by other clients. |
hudakz | 12:3af0ae09bd01 | 97 | printf("Subscribing to topics:\r\n"); |
hudakz | 12:3af0ae09bd01 | 98 | printf("outdoor/temperature\r\n"); |
hudakz | 12:3af0ae09bd01 | 99 | printf("boiler/outlet/temperature\r\n"); |
hudakz | 12:3af0ae09bd01 | 100 | printf("\r\n"); |
hudakz | 8:7b4e0d15249f | 101 | mqttClient.subscribe("outdoor/temperature"); |
hudakz | 8:7b4e0d15249f | 102 | mqttClient.subscribe("boiler/outlet/temperature"); |
hudakz | 2:6cd2390302ac | 103 | } |
hudakz | 2:6cd2390302ac | 104 | else { |
hudakz | 12:3af0ae09bd01 | 105 | printf("Failed to connect to the MQTT broker.\r\n"); |
hudakz | 12:3af0ae09bd01 | 106 | return -1; |
hudakz | 0:158b106f3cfc | 107 | } |
hudakz | 0:158b106f3cfc | 108 | |
hudakz | 15:da8fef967438 | 109 | // Attach ISR (callback) handling new MQTT message from the broker |
hudakz | 15:da8fef967438 | 110 | mqttClient.attach(onMqttMessage); |
hudakz | 15:da8fef967438 | 111 | |
hudakz | 12:3af0ae09bd01 | 112 | while (1) { |
hudakz | 15:da8fef967438 | 113 | mqttClient.poll(); // poll new MQTT messages from the broker |
hudakz | 15:da8fef967438 | 114 | |
hudakz | 15:da8fef967438 | 115 | // publish MQTT messages |
hudakz | 0:158b106f3cfc | 116 | t = time(NULL); |
hudakz | 12:3af0ae09bd01 | 117 | if (t > (lastTime + INTERVAL)) { |
hudakz | 0:158b106f3cfc | 118 | lastTime = t; |
hudakz | 12:3af0ae09bd01 | 119 | if (connected) { |
hudakz | 12:3af0ae09bd01 | 120 | printf("Publishing: example/hello\r\n"); |
hudakz | 0:158b106f3cfc | 121 | mqttClient.publish("example/hello", payload); |
hudakz | 0:158b106f3cfc | 122 | } |
hudakz | 2:6cd2390302ac | 123 | } |
hudakz | 0:158b106f3cfc | 124 | } |
hudakz | 0:158b106f3cfc | 125 | } |