Working example code TCP 1883.
Dependencies: EthernetInterface MQTT mbed-rtos mbed
Fork of K64F-RTOS-MQTT-Example by
Diff: main.cpp
- Revision:
- 1:9e3ca45e51b5
- Parent:
- 0:9796509d718e
- Child:
- 2:005cdf4e8fb3
--- a/main.cpp Wed Feb 10 18:39:26 2016 +0000 +++ b/main.cpp Sat Mar 18 19:54:58 2017 +0000 @@ -5,12 +5,12 @@ #include "k64f.h" // connect options for MQTT broker -#define BROKER "broker.mqttdashboard.com" // MQTT broker URL +#define BROKER "broker.hivemq.com" // MQTT broker URL #define PORT 1883 // MQTT broker port number -#define CLIENTID "" // use K64F MAC address without colons +#define CLIENTID "0002F7F000" // use K64F MAC address without colons #define USERNAME "" // not required for MQTT Dashboard public broker #define PASSWORD "" // not required for MQTT Dashboard public broker -#define TOPIC "" // MQTT topic +#define TOPIC "testeroo" // MQTT topic Queue<uint32_t, 6> messageQ; @@ -67,7 +67,7 @@ int main() { // turn off LED - controlLED(off); + controlLED(red); // set SW2 and SW3 to generate interrupt on falling edge switch2.fall(&sw2_ISR); @@ -85,6 +85,7 @@ pc.printf("MAC address is %s\r\n", eth.getMACAddress()); pc.printf("Gateway address is %s\r\n", eth.getGateway()); + // construct the MQTT client MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); @@ -95,8 +96,9 @@ pc.printf("\r\nAttempting TCP connect to %s:%d: ", hostname, port); // connect to TCP socket and check return code - if ((rc = ipstack.connect(hostname, port)) != 0) + if ((rc = ipstack.connect(hostname, port)) != 0){ pc.printf("failed: rc= %d\r\n", rc); + controlLED(blue);} else pc.printf("success\r\n"); @@ -109,11 +111,12 @@ // send MQTT connect packet and check return code pc.printf("Attempting MQTT connect to %s:%d: ", hostname, port); - if ((rc = client.connect(data)) != 0) + if ((rc = client.connect(data)) != 0){ pc.printf("failed: rc= %d\r\n", rc); - - else + controlLED(green);} + else{ pc.printf("success\r\n"); + controlLED(off);} char* topic = TOPIC;