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

Dependencies:   UIPEthernet MQTTClient

Committer:
hudakz
Date:
Sat Dec 20 12:04:36 2014 +0000
Revision:
1:49fb5754df16
Parent:
0:158b106f3cfc
Child:
2:6cd2390302ac
rev 01

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 1:49fb5754df16 1 // In this example an MQTT client is created using an ENC28J60
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 1:49fb5754df16 26 //Serial pc(PA_9, PA_10);
hudakz 1:49fb5754df16 27 Serial pc(USBTX, USBRX);
hudakz 1:49fb5754df16 28
hudakz 0:158b106f3cfc 29
hudakz 0:158b106f3cfc 30 // Make sure that the MAC number is unique within the connected network.
hudakz 0:158b106f3cfc 31 const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
hudakz 0:158b106f3cfc 32 // IP address must be unique and compatible with your network too. Change as appropriate.
hudakz 0:158b106f3cfc 33 const IPAddress MY_IP(192,168,1,181);
hudakz 0:158b106f3cfc 34 char message_buff[100];
hudakz 0:158b106f3cfc 35 IPAddress serverIP(192,168,1,30); // MQTT server (e.g. 'mosquitto' running on a Ubuntu PC or Raspberry Pi)
hudakz 0:158b106f3cfc 36 EthernetClient ethernetClient;
hudakz 1:49fb5754df16 37 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
hudakz 0:158b106f3cfc 38 MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
hudakz 0:158b106f3cfc 39
hudakz 0:158b106f3cfc 40 int main()
hudakz 0:158b106f3cfc 41 {
hudakz 0:158b106f3cfc 42 const int MAX_COUNT = 5;
hudakz 0:158b106f3cfc 43 int i = 0;
hudakz 0:158b106f3cfc 44 bool connected = false;
hudakz 0:158b106f3cfc 45 char* payload = "Hello World.";
hudakz 0:158b106f3cfc 46 time_t t = 0;
hudakz 0:158b106f3cfc 47 time_t lastTime = t;
hudakz 0:158b106f3cfc 48
hudakz 0:158b106f3cfc 49 // initialize the ethernet device
hudakz 1:49fb5754df16 50 UIPEthernet.begin(MY_MAC, MY_IP);
hudakz 1:49fb5754df16 51 // IPAddress localIP = UIPEthernet.localIP();
hudakz 0:158b106f3cfc 52 // pc.printf("IP = ");
hudakz 0:158b106f3cfc 53 // for(uint8_t i = 0; i < 3; i++)
hudakz 0:158b106f3cfc 54 // pc.printf("%d.", localIP[i]);
hudakz 0:158b106f3cfc 55 // pc.printf("%d\r\n", localIP[3]);
hudakz 0:158b106f3cfc 56 pc.printf("Connecting to MQTT server ..\r\n");
hudakz 0:158b106f3cfc 57 do {
hudakz 0:158b106f3cfc 58 wait(1.0);
hudakz 0:158b106f3cfc 59 connected = mqttClient.connect("myMQTTHelloClient");
hudakz 0:158b106f3cfc 60 } while(!connected && (i < MAX_COUNT));
hudakz 0:158b106f3cfc 61
hudakz 0:158b106f3cfc 62 if(connected) {
hudakz 0:158b106f3cfc 63 pc.printf("MQTT Server connected.\r\n");
hudakz 0:158b106f3cfc 64 mqttClient.subscribe("outdoor/temperature");
hudakz 0:158b106f3cfc 65 pc.printf("Subscribed to: outdoor/temperature\r\n");
hudakz 0:158b106f3cfc 66 } else {
hudakz 0:158b106f3cfc 67 pc.printf("Failed to connect to MQTT server.\r\n");
hudakz 0:158b106f3cfc 68 }
hudakz 0:158b106f3cfc 69
hudakz 0:158b106f3cfc 70 while(1) {
hudakz 0:158b106f3cfc 71 t = time(NULL);
hudakz 0:158b106f3cfc 72 if(t > (lastTime + 5)) {
hudakz 0:158b106f3cfc 73 lastTime = t;
hudakz 0:158b106f3cfc 74 if(connected) {
hudakz 0:158b106f3cfc 75 mqttClient.publish("example/hello", payload);
hudakz 0:158b106f3cfc 76 pc.printf("Published: example/hello\r\n");
hudakz 0:158b106f3cfc 77 }
hudakz 0:158b106f3cfc 78
hudakz 0:158b106f3cfc 79 }
hudakz 0:158b106f3cfc 80 mqttClient.loop(); // MQTT client loop processing
hudakz 0:158b106f3cfc 81 }
hudakz 0:158b106f3cfc 82 }
hudakz 0:158b106f3cfc 83
hudakz 0:158b106f3cfc 84 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length)
hudakz 0:158b106f3cfc 85 {
hudakz 0:158b106f3cfc 86 int i = 0;
hudakz 0:158b106f3cfc 87
hudakz 0:158b106f3cfc 88 pc.printf("Message arrived:\r\n");
hudakz 0:158b106f3cfc 89 pc.printf(" Topic: %s\r\n", topic);
hudakz 0:158b106f3cfc 90 pc.printf(" Length: %d\r\n", length);
hudakz 0:158b106f3cfc 91
hudakz 0:158b106f3cfc 92 // create character buffer with ending null terminator (string)
hudakz 0:158b106f3cfc 93 for(i = 0; i < length; i++) {
hudakz 0:158b106f3cfc 94 message_buff[i] = payload[i];
hudakz 0:158b106f3cfc 95 }
hudakz 0:158b106f3cfc 96
hudakz 0:158b106f3cfc 97 message_buff[i] = '\0';
hudakz 0:158b106f3cfc 98 pc.printf(" Payload: %s\r\n", message_buff);
hudakz 0:158b106f3cfc 99 }