Test of MQTT client running on an Outrageous Circuit mBuino with an ENC28J60 ethernet adapter. Derived from Zoltan Hudak's MQTT_Hello_ENC28J60 program as well as his MQTTClient and UIPEthernet libraries.

Dependencies:   MQTTClient UIPEthernet mbed

This program is a quick test of the Outrageous Circuits mBuino using an ENC28J60 Ethernet chip. This is modified from Zoltan Hudak's MQTT_Hello_ENC28J60 program. https://developer.mbed.org/users/hudakz/code/MQTT_Hello_ENC28J60/

UIPEthernet and MQTTClient are from Zoltan Hudak as well.

This program connects to an MQTT broker. Subscribes to two topics and publishes to one of those topics every 5 seconds. The payload for the published topic are the elapsed milliseconds from a free running timer. The round trip time is calculated for the published topic to be received.

It uses 27.8kB of 32kB of the LPC11U24's flash so there is not much left but it works. Debugging was done using the UART pins.

Committer:
skeller
Date:
Sun Aug 23 00:15:53 2015 +0000
Revision:
0:acb3694163ca
Child:
1:b5084016cae1
First test.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
skeller 0:acb3694163ca 1 /*
skeller 0:acb3694163ca 2 This program is a quick test of the Outrageous Circuits mBuino using an ENC28J60 Ethernet chip.
skeller 0:acb3694163ca 3 This is modified from Zoltan Hudak's MQTT_Hello_ENC28J60 program.
skeller 0:acb3694163ca 4 https://developer.mbed.org/users/hudakz/code/MQTT_Hello_ENC28J60/
skeller 0:acb3694163ca 5
skeller 0:acb3694163ca 6 UIPEthernet and MQTTClient are from Zoltan Hudak as well.
skeller 0:acb3694163ca 7
skeller 0:acb3694163ca 8 This program connects to an MQTT broker. Subscribes to two topics and publishes to one of those topics every 5 seconds.
skeller 0:acb3694163ca 9 The payload for the published topic are the elapsed milliseconds from a free running timer. The round trip time is calculated for the published topic to be received.
skeller 0:acb3694163ca 10
skeller 0:acb3694163ca 11 */
skeller 0:acb3694163ca 12
skeller 0:acb3694163ca 13
skeller 0:acb3694163ca 14
skeller 0:acb3694163ca 15 #include "mbed.h"
skeller 0:acb3694163ca 16 #include <UIPEthernet.h>
skeller 0:acb3694163ca 17 #include <MQTTClient.h>
skeller 0:acb3694163ca 18 #include <UIPClient.h>
skeller 0:acb3694163ca 19
skeller 0:acb3694163ca 20 DigitalOut myled(LED1);
skeller 0:acb3694163ca 21 DigitalOut led2(LED2);
skeller 0:acb3694163ca 22 Serial pc(P0_19,P0_18 );
skeller 0:acb3694163ca 23
skeller 0:acb3694163ca 24 UIPEthernetClass UIPEthernet(P0_21, P0_22, P1_15, P0_11); // mosi, miso, sck, cs
skeller 0:acb3694163ca 25
skeller 0:acb3694163ca 26 // MAC number must be unique within the connected network. Modify as appropriate.
skeller 0:acb3694163ca 27 const uint8_t MY_MAC[6] = {0x00,0x01,0x02,0x03,0x04,0x06};
skeller 0:acb3694163ca 28 // IP address must be also unique and compatible with your network. Change as appropriate.
skeller 0:acb3694163ca 29 const IPAddress MY_IP(192,168,1,181);
skeller 0:acb3694163ca 30 const IPAddress MY_DNS(192,168,1,1);
skeller 0:acb3694163ca 31 const IPAddress MY_GATEWAY(192,168,1,1);
skeller 0:acb3694163ca 32 const IPAddress MY_MASK(255,255,255,0);
skeller 0:acb3694163ca 33
skeller 0:acb3694163ca 34 char message_buff[100];
skeller 0:acb3694163ca 35 IPAddress serverIP(192,168,1,1); // MQTT broker (e.g. 'Mosquitto' running on a Raspberry Pi or Linux machine)
skeller 0:acb3694163ca 36 EthernetClient ethernetClient;
skeller 0:acb3694163ca 37 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length);
skeller 0:acb3694163ca 38 MQTTClient mqttClient(serverIP, 1883, onMqttMessage, ethernetClient);
skeller 0:acb3694163ca 39 Timer timer;
skeller 0:acb3694163ca 40
skeller 0:acb3694163ca 41
skeller 0:acb3694163ca 42 int main(void)
skeller 0:acb3694163ca 43 {
skeller 0:acb3694163ca 44 const int MAX_COUNT = 5;
skeller 0:acb3694163ca 45 int i = 0;
skeller 0:acb3694163ca 46 bool connected = false;
skeller 0:acb3694163ca 47 char payload[100];
skeller 0:acb3694163ca 48 int nextTimer = 0;
skeller 0:acb3694163ca 49 bool led2Status = false;
skeller 0:acb3694163ca 50 pc.baud(19200);
skeller 0:acb3694163ca 51 // initialize the ethernet device
skeller 0:acb3694163ca 52
skeller 0:acb3694163ca 53 UIPEthernet.begin(MY_MAC, MY_IP, MY_DNS, MY_GATEWAY, MY_MASK); //uint8_t* mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet
skeller 0:acb3694163ca 54
skeller 0:acb3694163ca 55 // The MQTT broker is like a post office distributing messages published by clients to all subscribers (clients).
skeller 0:acb3694163ca 56 // So the 'example/hello' messages published by this client will be sent via the broker to all clients which subscribed to such messages.
skeller 0:acb3694163ca 57 // This way also this client will receive all subscribed messages (in this case 'outdoor/temperature' messages)
skeller 0:acb3694163ca 58 // published by other clients (however not directly but via the broker).
skeller 0:acb3694163ca 59 pc.printf("Connecting to MQTT broker ..\r\n");
skeller 0:acb3694163ca 60 do {
skeller 0:acb3694163ca 61 wait(1.0);
skeller 0:acb3694163ca 62 connected = mqttClient.connect("myMQTTHelloClient");
skeller 0:acb3694163ca 63 } while(!connected && (i < MAX_COUNT));
skeller 0:acb3694163ca 64
skeller 0:acb3694163ca 65 if(connected) {
skeller 0:acb3694163ca 66 pc.printf("MQTT broker connected.\r\n");
skeller 0:acb3694163ca 67 mqttClient.subscribe("outdoor/temperature");
skeller 0:acb3694163ca 68 pc.printf("Subscribed to: outdoor/temperature\r\n");
skeller 0:acb3694163ca 69 mqttClient.subscribe("example/hello");
skeller 0:acb3694163ca 70 pc.printf("Subscribed to: example/hello\r\n");
skeller 0:acb3694163ca 71 } else {
skeller 0:acb3694163ca 72 pc.printf("Failed to connect to MQTT broker.\r\n");
skeller 0:acb3694163ca 73 }
skeller 0:acb3694163ca 74
skeller 0:acb3694163ca 75 //start global timer
skeller 0:acb3694163ca 76 timer.start();
skeller 0:acb3694163ca 77
skeller 0:acb3694163ca 78 while(1) {
skeller 0:acb3694163ca 79
skeller 0:acb3694163ca 80
skeller 0:acb3694163ca 81 if(timer.read_ms() > nextTimer) {
skeller 0:acb3694163ca 82 nextTimer = timer.read_ms() + 5000;
skeller 0:acb3694163ca 83
skeller 0:acb3694163ca 84 if(led2Status == true) {
skeller 0:acb3694163ca 85 led2 = 0;
skeller 0:acb3694163ca 86 led2Status = false;
skeller 0:acb3694163ca 87 } else {
skeller 0:acb3694163ca 88 led2 = 1;
skeller 0:acb3694163ca 89 led2Status = true;
skeller 0:acb3694163ca 90 }
skeller 0:acb3694163ca 91 //lastTime = t;
skeller 0:acb3694163ca 92 if(connected) {
skeller 0:acb3694163ca 93 pc.printf("Publish. . .\r\n");
skeller 0:acb3694163ca 94 sprintf(payload, "%d", timer.read_ms());
skeller 0:acb3694163ca 95 mqttClient.publish("example/hello", payload);
skeller 0:acb3694163ca 96 pc.printf("Published: example/hello\r\n");
skeller 0:acb3694163ca 97 } else {
skeller 0:acb3694163ca 98 pc.printf("Not connected\r\n");
skeller 0:acb3694163ca 99 }
skeller 0:acb3694163ca 100 }
skeller 0:acb3694163ca 101
skeller 0:acb3694163ca 102 mqttClient.loop(); // MQTT client loop processing
skeller 0:acb3694163ca 103 }
skeller 0:acb3694163ca 104 }
skeller 0:acb3694163ca 105
skeller 0:acb3694163ca 106 /**
skeller 0:acb3694163ca 107 * @brief Called on new MQTT message arrival
skeller 0:acb3694163ca 108 * @note
skeller 0:acb3694163ca 109 * @param topic: The topic of the new message
skeller 0:acb3694163ca 110 * payload: The payload of the new message
skeller 0:acb3694163ca 111 * length: Payload's length
skeller 0:acb3694163ca 112 * @retval
skeller 0:acb3694163ca 113 */
skeller 0:acb3694163ca 114 void onMqttMessage(char* topic, uint8_t* payload, unsigned int length)
skeller 0:acb3694163ca 115 {
skeller 0:acb3694163ca 116 int i = 0;
skeller 0:acb3694163ca 117 int elapsed = 0;
skeller 0:acb3694163ca 118 int value = 0;
skeller 0:acb3694163ca 119 myled = 1;
skeller 0:acb3694163ca 120 //pc.printf("Message arrived:\r\n");
skeller 0:acb3694163ca 121 //pc.printf(" Topic: %s\r\n", topic);
skeller 0:acb3694163ca 122 //pc.printf(" Length: %d\r\n", length);
skeller 0:acb3694163ca 123
skeller 0:acb3694163ca 124 // create character buffer with ending null terminator (string)
skeller 0:acb3694163ca 125 for(i = 0; i < length; i++) {
skeller 0:acb3694163ca 126 message_buff[i] = payload[i];
skeller 0:acb3694163ca 127 }
skeller 0:acb3694163ca 128
skeller 0:acb3694163ca 129 message_buff[i] = '\0';
skeller 0:acb3694163ca 130 value = atoi(message_buff);
skeller 0:acb3694163ca 131 elapsed = timer.read_ms() - value;
skeller 0:acb3694163ca 132 pc.printf("%d - Payload: %s\r\n", elapsed, message_buff);
skeller 0:acb3694163ca 133 myled = 0;
skeller 0:acb3694163ca 134 }
skeller 0:acb3694163ca 135 /*int main()
skeller 0:acb3694163ca 136 {
skeller 0:acb3694163ca 137 uart.baud(19200);
skeller 0:acb3694163ca 138 uart.printf("Hello World");
skeller 0:acb3694163ca 139
skeller 0:acb3694163ca 140 while(1) {
skeller 0:acb3694163ca 141 myled = 1;
skeller 0:acb3694163ca 142 wait(0.2);
skeller 0:acb3694163ca 143 myled = 0;
skeller 0:acb3694163ca 144 wait(1);
skeller 0:acb3694163ca 145 uart.printf("Hello World\r\n");
skeller 0:acb3694163ca 146 }
skeller 0:acb3694163ca 147 }*/