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

Dependencies:   UIPEthernet MQTTClient

Committer:
hudakz
Date:
Thu Sep 12 20:27:21 2019 +0000
Revision:
14:b2c96d7b4335
Parent:
13:6b2f125cc9b8
Child:
15:da8fef967438
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 14:b2c96d7b4335 26 void onMqttMessage(char* topic, uint8_t* payload, uint16_t length); // ISR (callback) to handle received MQTT messages.
hudakz 14:b2c96d7b4335 27 MQTTClient mqttClient(BROKER_IP, MQTT_PORT, onMqttMessage);
hudakz 14:b2c96d7b4335 28 char msgBuf[MQTT_MAX_PACKET_SIZE]; // buffer to store received MQTT messages
hudakz 14:b2c96d7b4335 29
hudakz 14:b2c96d7b4335 30 /**
hudakz 14:b2c96d7b4335 31 * @brief ISR to handle received MQTT messages
hudakz 14:b2c96d7b4335 32 * @note Called on arrival of new MQTT message
hudakz 14:b2c96d7b4335 33 * @param topic: The topic of received message
hudakz 14:b2c96d7b4335 34 * payload: The payload of received message
hudakz 14:b2c96d7b4335 35 * length: Payload's length
hudakz 14:b2c96d7b4335 36 * @retval
hudakz 14:b2c96d7b4335 37 */
hudakz 14:b2c96d7b4335 38 void onMqttMessage(char* topic, uint8_t* payload, uint16_t length)
hudakz 14:b2c96d7b4335 39 {
hudakz 14:b2c96d7b4335 40 int i = 0;
hudakz 14:b2c96d7b4335 41
hudakz 14:b2c96d7b4335 42 printf("Message arrived:\r\n");
hudakz 14:b2c96d7b4335 43 printf(" Topic: %s\r\n", topic);
hudakz 14:b2c96d7b4335 44 printf(" Length: %d\r\n", length);
hudakz 14:b2c96d7b4335 45
hudakz 14:b2c96d7b4335 46 // create character buffer with ending null terminator (string)
hudakz 14:b2c96d7b4335 47 for (i = 0; i < length; i++) {
hudakz 14:b2c96d7b4335 48 msgBuf[i] = payload[i];
hudakz 14:b2c96d7b4335 49 }
hudakz 14:b2c96d7b4335 50
hudakz 14:b2c96d7b4335 51 msgBuf[i] = '\0';
hudakz 14:b2c96d7b4335 52 printf(" Payload: %s\r\n\r\n", msgBuf);
hudakz 14:b2c96d7b4335 53 }
hudakz 9:18b414a8c5f7 54
hudakz 2:6cd2390302ac 55 /**
hudakz 2:6cd2390302ac 56 * @brief
hudakz 2:6cd2390302ac 57 * @note
hudakz 2:6cd2390302ac 58 * @param
hudakz 2:6cd2390302ac 59 * @retval
hudakz 2:6cd2390302ac 60 */
hudakz 12:3af0ae09bd01 61 int main(void)
hudakz 12:3af0ae09bd01 62 {
hudakz 12:3af0ae09bd01 63 const int MAX_TRIES = 5;
hudakz 12:3af0ae09bd01 64 const time_t INTERVAL = 5;
hudakz 12:3af0ae09bd01 65 int i = 0;
hudakz 12:3af0ae09bd01 66 bool connected = false;
hudakz 12:3af0ae09bd01 67 char payload[] = "Hello World!";
hudakz 12:3af0ae09bd01 68 time_t t = 0;
hudakz 12:3af0ae09bd01 69 time_t lastTime = t;
hudakz 8:7b4e0d15249f 70
hudakz 12:3af0ae09bd01 71 // Bring up the ethernet interface
hudakz 12:3af0ae09bd01 72 //net.set_network(IP, NETMASK, GATEWAY); // include this to use static IP address
hudakz 12:3af0ae09bd01 73 printf("MQTT client example\n");
hudakz 12:3af0ae09bd01 74 if (net.connect() != 0) {
hudakz 12:3af0ae09bd01 75 printf("Error connecting\n");
hudakz 12:3af0ae09bd01 76 return -1;
hudakz 3:3e5f4d503a66 77 }
hudakz 12:3af0ae09bd01 78
hudakz 12:3af0ae09bd01 79 // Show the network address
hudakz 12:3af0ae09bd01 80 const char* ip = net.get_ip_address();
hudakz 12:3af0ae09bd01 81 const char* netmask = net.get_netmask();
hudakz 12:3af0ae09bd01 82 const char* gateway = net.get_gateway();
hudakz 12:3af0ae09bd01 83 printf("IP address: %s\n", ip ? ip : "None");
hudakz 12:3af0ae09bd01 84 printf("Netmask: %s\n", netmask ? netmask : "None");
hudakz 12:3af0ae09bd01 85 printf("Gateway: %s\n", gateway ? gateway : "None");
hudakz 12:3af0ae09bd01 86
hudakz 12:3af0ae09bd01 87 printf("Connecting to the MQTT broker ... ");
hudakz 11:a049ad4b63c0 88 do {
hudakz 0:158b106f3cfc 89 wait(1.0);
hudakz 0:158b106f3cfc 90 connected = mqttClient.connect("myMQTTHelloClient");
hudakz 12:3af0ae09bd01 91 } while (!connected && (i++ < MAX_TRIES));
hudakz 0:158b106f3cfc 92
hudakz 12:3af0ae09bd01 93 if (connected) {
hudakz 12:3af0ae09bd01 94 printf("Connected.\r\n\r\n");
hudakz 9:18b414a8c5f7 95 // We can subscribe to various topics published by other clients.
hudakz 12:3af0ae09bd01 96 printf("Subscribing to topics:\r\n");
hudakz 12:3af0ae09bd01 97 printf("outdoor/temperature\r\n");
hudakz 12:3af0ae09bd01 98 printf("boiler/outlet/temperature\r\n");
hudakz 12:3af0ae09bd01 99 printf("\r\n");
hudakz 8:7b4e0d15249f 100 mqttClient.subscribe("outdoor/temperature");
hudakz 8:7b4e0d15249f 101 mqttClient.subscribe("boiler/outlet/temperature");
hudakz 2:6cd2390302ac 102 }
hudakz 2:6cd2390302ac 103 else {
hudakz 12:3af0ae09bd01 104 printf("Failed to connect to the MQTT broker.\r\n");
hudakz 12:3af0ae09bd01 105 return -1;
hudakz 0:158b106f3cfc 106 }
hudakz 0:158b106f3cfc 107
hudakz 12:3af0ae09bd01 108 while (1) {
hudakz 0:158b106f3cfc 109 t = time(NULL);
hudakz 12:3af0ae09bd01 110 if (t > (lastTime + INTERVAL)) {
hudakz 0:158b106f3cfc 111 lastTime = t;
hudakz 12:3af0ae09bd01 112 if (connected) {
hudakz 12:3af0ae09bd01 113 printf("Publishing: example/hello\r\n");
hudakz 0:158b106f3cfc 114 mqttClient.publish("example/hello", payload);
hudakz 0:158b106f3cfc 115 }
hudakz 2:6cd2390302ac 116 }
hudakz 12:3af0ae09bd01 117
hudakz 14:b2c96d7b4335 118 mqttClient.process(); // MQTT client loop processing (receiving messages)
hudakz 0:158b106f3cfc 119 }
hudakz 0:158b106f3cfc 120 }