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

Dependencies:   UIPEthernet MQTTClient

Committer:
hudakz
Date:
Mon Sep 15 12:51:58 2014 +0000
Revision:
0:158b106f3cfc
Child:
1:49fb5754df16
rev. 00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:158b106f3cfc 1 // In this example an MQTT client is created using an ENC28J60 chip/board
hudakz 0:158b106f3cfc 2 // It is publishing a simple 'example/hello' message 'Hello World.'
hudakz 0:158b106f3cfc 3 // and subscribes to 'outdoor/temperature' messages
hudakz 0:158b106f3cfc 4 // UIPEthernet library is used to drive the ENC28J60
hudakz 0:158b106f3cfc 5 #include "mbed.h"
hudakz 0:158b106f3cfc 6 #include <UIPEthernet.h>
hudakz 0:158b106f3cfc 7 #include <UIPClient.h>
hudakz 0:158b106f3cfc 8 #include <MQTTClient.h>
hudakz 0:158b106f3cfc 9 #include <string>
hudakz 0:158b106f3cfc 10
hudakz 0:158b106f3cfc 11 using namespace std;
hudakz 0:158b106f3cfc 12
hudakz 0:158b106f3cfc 13 // UIPEthernet is the name of a global instance of UIPEthernetClass.
hudakz 0:158b106f3cfc 14 // Do not change the name! It is used within the UIPEthernet library.
hudakz 0:158b106f3cfc 15 // Adapt the SPI pin names to your mbed platform/board if not present yet.
hudakz 0:158b106f3cfc 16 #if defined(TARGET_LPC1768)
hudakz 0:158b106f3cfc 17 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // mosi, miso, sck, cs
hudakz 0:158b106f3cfc 18 #elif defined(TARGET_LPC1114)
hudakz 0:158b106f3cfc 19 UIPEthernetClass UIPEthernet(dp2, dp1, dp6, dp25); // mosi, miso, sck, cs
hudakz 0:158b106f3cfc 20 #elif defined(TARGET_LPC11U68)
hudakz 0:158b106f3cfc 21 UIPEthernetClass UIPEthernet(P0_9, P0_8, P1_29, P0_2); // mosi, miso, sck, cs
hudakz 0:158b106f3cfc 22 #elif defined (TARGET_NUCLEO_F103RB)
hudakz 0:158b106f3cfc 23 UIPEthernetClass UIPEthernet(PB_5, PB_4, PB_3, PB_6); // mosi, miso, sck, cs
hudakz 0:158b106f3cfc 24 #endif
hudakz 0:158b106f3cfc 25
hudakz 0:158b106f3cfc 26 Serial pc(PA_9, PA_10);
hudakz 0:158b106f3cfc 27
hudakz 0:158b106f3cfc 28 // Make sure that the MAC number is unique within the connected network.
hudakz 0:158b106f3cfc 29 const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
hudakz 0:158b106f3cfc 30 // IP address must be unique and compatible with your network too. Change as appropriate.
hudakz 0:158b106f3cfc 31 const IPAddress MY_IP(192,168,1,181);
hudakz 0:158b106f3cfc 32 char message_buff[100];
hudakz 0:158b106f3cfc 33 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
hudakz 0:158b106f3cfc 34 IPAddress serverIP(192,168,1,30); // MQTT server (e.g. 'mosquitto' running on a Ubuntu PC or Raspberry Pi)
hudakz 0:158b106f3cfc 35 EthernetClient ethernetClient;
hudakz 0:158b106f3cfc 36 MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
hudakz 0:158b106f3cfc 37
hudakz 0:158b106f3cfc 38 int main()
hudakz 0:158b106f3cfc 39 {
hudakz 0:158b106f3cfc 40 const int MAX_COUNT = 5;
hudakz 0:158b106f3cfc 41 int i = 0;
hudakz 0:158b106f3cfc 42 bool connected = false;
hudakz 0:158b106f3cfc 43 char* payload = "Hello World.";
hudakz 0:158b106f3cfc 44 time_t t = 0;
hudakz 0:158b106f3cfc 45 time_t lastTime = t;
hudakz 0:158b106f3cfc 46
hudakz 0:158b106f3cfc 47 // initialize the ethernet device
hudakz 0:158b106f3cfc 48 Ethernet.begin(MY_MAC, MY_IP);
hudakz 0:158b106f3cfc 49 // IPAddress localIP = Ethernet.localIP();
hudakz 0:158b106f3cfc 50 // pc.printf("IP = ");
hudakz 0:158b106f3cfc 51 // for(uint8_t i = 0; i < 3; i++)
hudakz 0:158b106f3cfc 52 // pc.printf("%d.", localIP[i]);
hudakz 0:158b106f3cfc 53 // pc.printf("%d\r\n", localIP[3]);
hudakz 0:158b106f3cfc 54 pc.printf("Connecting to MQTT server ..\r\n");
hudakz 0:158b106f3cfc 55 do {
hudakz 0:158b106f3cfc 56 wait(1.0);
hudakz 0:158b106f3cfc 57 connected = mqttClient.connect("myMQTTHelloClient");
hudakz 0:158b106f3cfc 58 } while(!connected && (i < MAX_COUNT));
hudakz 0:158b106f3cfc 59
hudakz 0:158b106f3cfc 60 if(connected) {
hudakz 0:158b106f3cfc 61 pc.printf("MQTT Server connected.\r\n");
hudakz 0:158b106f3cfc 62 mqttClient.subscribe("outdoor/temperature");
hudakz 0:158b106f3cfc 63 pc.printf("Subscribed to: outdoor/temperature\r\n");
hudakz 0:158b106f3cfc 64 } else {
hudakz 0:158b106f3cfc 65 pc.printf("Failed to connect to MQTT server.\r\n");
hudakz 0:158b106f3cfc 66 }
hudakz 0:158b106f3cfc 67
hudakz 0:158b106f3cfc 68 while(1) {
hudakz 0:158b106f3cfc 69 t = time(NULL);
hudakz 0:158b106f3cfc 70 if(t > (lastTime + 5)) {
hudakz 0:158b106f3cfc 71 lastTime = t;
hudakz 0:158b106f3cfc 72 if(connected) {
hudakz 0:158b106f3cfc 73 mqttClient.publish("example/hello", payload);
hudakz 0:158b106f3cfc 74 pc.printf("Published: example/hello\r\n");
hudakz 0:158b106f3cfc 75 }
hudakz 0:158b106f3cfc 76
hudakz 0:158b106f3cfc 77 }
hudakz 0:158b106f3cfc 78 mqttClient.loop(); // MQTT client loop processing
hudakz 0:158b106f3cfc 79 }
hudakz 0:158b106f3cfc 80 }
hudakz 0:158b106f3cfc 81
hudakz 0:158b106f3cfc 82 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length)
hudakz 0:158b106f3cfc 83 {
hudakz 0:158b106f3cfc 84 int i = 0;
hudakz 0:158b106f3cfc 85
hudakz 0:158b106f3cfc 86 pc.printf("Message arrived:\r\n");
hudakz 0:158b106f3cfc 87 pc.printf(" Topic: %s\r\n", topic);
hudakz 0:158b106f3cfc 88 pc.printf(" Length: %d\r\n", length);
hudakz 0:158b106f3cfc 89
hudakz 0:158b106f3cfc 90 // create character buffer with ending null terminator (string)
hudakz 0:158b106f3cfc 91 for(i = 0; i < length; i++) {
hudakz 0:158b106f3cfc 92 message_buff[i] = payload[i];
hudakz 0:158b106f3cfc 93 }
hudakz 0:158b106f3cfc 94
hudakz 0:158b106f3cfc 95 message_buff[i] = '\0';
hudakz 0:158b106f3cfc 96 pc.printf(" Payload: %s\r\n", message_buff);
hudakz 0:158b106f3cfc 97 }