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

Dependencies:   UIPEthernet MQTTClient

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