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

Dependencies:   UIPEthernet MQTTClient

Committer:
hudakz
Date:
Thu Sep 12 22:01:55 2019 +0000
Revision:
15:da8fef967438
Parent:
14:b2c96d7b4335
Child:
16:2c61673000e8
Updated.

Who changed what in which revision?

UserRevisionLine numberNew 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 12:3af0ae09bd01 62 const int MAX_TRIES = 5;
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 12:3af0ae09bd01 78 // Show the network address
hudakz 12:3af0ae09bd01 79 const char* ip = net.get_ip_address();
hudakz 12:3af0ae09bd01 80 const char* netmask = net.get_netmask();
hudakz 12:3af0ae09bd01 81 const char* gateway = net.get_gateway();
hudakz 12:3af0ae09bd01 82 printf("IP address: %s\n", ip ? ip : "None");
hudakz 12:3af0ae09bd01 83 printf("Netmask: %s\n", netmask ? netmask : "None");
hudakz 12:3af0ae09bd01 84 printf("Gateway: %s\n", gateway ? gateway : "None");
hudakz 12:3af0ae09bd01 85
hudakz 12:3af0ae09bd01 86 printf("Connecting to the MQTT broker ... ");
hudakz 11:a049ad4b63c0 87 do {
hudakz 0:158b106f3cfc 88 wait(1.0);
hudakz 0:158b106f3cfc 89 connected = mqttClient.connect("myMQTTHelloClient");
hudakz 12:3af0ae09bd01 90 } while (!connected && (i++ < MAX_TRIES));
hudakz 0:158b106f3cfc 91
hudakz 12:3af0ae09bd01 92 if (connected) {
hudakz 12:3af0ae09bd01 93 printf("Connected.\r\n\r\n");
hudakz 9:18b414a8c5f7 94 // We can subscribe to various topics published by other clients.
hudakz 12:3af0ae09bd01 95 printf("Subscribing to topics:\r\n");
hudakz 12:3af0ae09bd01 96 printf("outdoor/temperature\r\n");
hudakz 12:3af0ae09bd01 97 printf("boiler/outlet/temperature\r\n");
hudakz 12:3af0ae09bd01 98 printf("\r\n");
hudakz 8:7b4e0d15249f 99 mqttClient.subscribe("outdoor/temperature");
hudakz 8:7b4e0d15249f 100 mqttClient.subscribe("boiler/outlet/temperature");
hudakz 2:6cd2390302ac 101 }
hudakz 2:6cd2390302ac 102 else {
hudakz 12:3af0ae09bd01 103 printf("Failed to connect to the MQTT broker.\r\n");
hudakz 12:3af0ae09bd01 104 return -1;
hudakz 0:158b106f3cfc 105 }
hudakz 0:158b106f3cfc 106
hudakz 15:da8fef967438 107 // Attach ISR (callback) handling new MQTT message from the broker
hudakz 15:da8fef967438 108 mqttClient.attach(onMqttMessage);
hudakz 15:da8fef967438 109
hudakz 12:3af0ae09bd01 110 while (1) {
hudakz 15:da8fef967438 111 mqttClient.poll(); // poll new MQTT messages from the broker
hudakz 15:da8fef967438 112
hudakz 15:da8fef967438 113 // publish MQTT messages
hudakz 0:158b106f3cfc 114 t = time(NULL);
hudakz 12:3af0ae09bd01 115 if (t > (lastTime + INTERVAL)) {
hudakz 0:158b106f3cfc 116 lastTime = t;
hudakz 12:3af0ae09bd01 117 if (connected) {
hudakz 12:3af0ae09bd01 118 printf("Publishing: example/hello\r\n");
hudakz 0:158b106f3cfc 119 mqttClient.publish("example/hello", payload);
hudakz 0:158b106f3cfc 120 }
hudakz 2:6cd2390302ac 121 }
hudakz 0:158b106f3cfc 122 }
hudakz 0:158b106f3cfc 123 }