MQTT example using the Freescale FRDM-K64F platform without additional hardware.

Dependencies:   EthernetInterface MQTT mbed-rtos mbed HC_SR04_Ultrasonic_Library

Fork of K64F-RTOS-MQTT-Example by Mike Baylis

Committer:
sim2egor
Date:
Thu Jul 27 16:08:25 2017 +0000
Revision:
1:9caf46f4ed3e
Parent:
0:9796509d718e
Child:
2:24b2b10d3bc2
Eth error;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
msbaylis 0:9796509d718e 1 #include "mbed.h"
msbaylis 0:9796509d718e 2 #include "MQTTClient.h"
msbaylis 0:9796509d718e 3 #include "MQTTEthernet.h"
msbaylis 0:9796509d718e 4 #include "rtos.h"
msbaylis 0:9796509d718e 5 #include "k64f.h"
msbaylis 0:9796509d718e 6
msbaylis 0:9796509d718e 7 // connect options for MQTT broker
sim2egor 1:9caf46f4ed3e 8 #define BROKER "192.168.1.133"//"broker.mqttdashboard.com" // MQTT broker URL
msbaylis 0:9796509d718e 9 #define PORT 1883 // MQTT broker port number
sim2egor 1:9caf46f4ed3e 10 #define CLIENTID "cd23787" // use K64F MAC address without colons
sim2egor 1:9caf46f4ed3e 11 #define USERNAME "c1" // not required for MQTT Dashboard public broker
msbaylis 0:9796509d718e 12 #define PASSWORD "" // not required for MQTT Dashboard public broker
sim2egor 1:9caf46f4ed3e 13 #define TOPIC "3435" // MQTT topic
msbaylis 0:9796509d718e 14
msbaylis 0:9796509d718e 15 Queue<uint32_t, 6> messageQ;
msbaylis 0:9796509d718e 16
msbaylis 0:9796509d718e 17 // LED color control function
msbaylis 0:9796509d718e 18 void controlLED(color_t led_color) {
msbaylis 0:9796509d718e 19 switch(led_color) {
msbaylis 0:9796509d718e 20 case red :
msbaylis 0:9796509d718e 21 greenLED = blueLED = 1;
msbaylis 0:9796509d718e 22 redLED = 0.7;
msbaylis 0:9796509d718e 23 break;
msbaylis 0:9796509d718e 24 case green :
msbaylis 0:9796509d718e 25 redLED = blueLED = 1;
msbaylis 0:9796509d718e 26 greenLED = 0.7;
msbaylis 0:9796509d718e 27 break;
msbaylis 0:9796509d718e 28 case blue :
msbaylis 0:9796509d718e 29 redLED = greenLED = 1;
msbaylis 0:9796509d718e 30 blueLED = 0.7;
msbaylis 0:9796509d718e 31 break;
msbaylis 0:9796509d718e 32 case off :
msbaylis 0:9796509d718e 33 redLED = greenLED = blueLED = 1;
msbaylis 0:9796509d718e 34 break;
msbaylis 0:9796509d718e 35 }
msbaylis 0:9796509d718e 36 }
msbaylis 0:9796509d718e 37
msbaylis 0:9796509d718e 38 // Switch 2 interrupt handler
msbaylis 0:9796509d718e 39 void sw2_ISR(void) {
msbaylis 0:9796509d718e 40 messageQ.put((uint32_t*)22);
msbaylis 0:9796509d718e 41 }
msbaylis 0:9796509d718e 42
msbaylis 0:9796509d718e 43 // Switch3 interrupt handler
msbaylis 0:9796509d718e 44 void sw3_ISR(void) {
msbaylis 0:9796509d718e 45 messageQ.put((uint32_t*)33);
msbaylis 0:9796509d718e 46 }
msbaylis 0:9796509d718e 47
msbaylis 0:9796509d718e 48 // MQTT message arrived callback function
msbaylis 0:9796509d718e 49 void messageArrived(MQTT::MessageData& md) {
msbaylis 0:9796509d718e 50 MQTT::Message &message = md.message;
msbaylis 0:9796509d718e 51 pc.printf("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload);
msbaylis 0:9796509d718e 52
msbaylis 0:9796509d718e 53 if (message.payloadlen == 3) {
msbaylis 0:9796509d718e 54 if (strncmp((char*)message.payload, "red", 3) == 0)
msbaylis 0:9796509d718e 55 controlLED(red);
msbaylis 0:9796509d718e 56
msbaylis 0:9796509d718e 57 else if(strncmp((char*)message.payload, "grn", 3) == 0)
msbaylis 0:9796509d718e 58 controlLED(green);
msbaylis 0:9796509d718e 59
msbaylis 0:9796509d718e 60 else if(strncmp((char*)message.payload, "blu", 3) == 0)
msbaylis 0:9796509d718e 61 controlLED(blue);
msbaylis 0:9796509d718e 62
msbaylis 0:9796509d718e 63 else if(strncmp((char*)message.payload, "off", 3) == 0)
msbaylis 0:9796509d718e 64 controlLED(off);
msbaylis 0:9796509d718e 65 }
msbaylis 0:9796509d718e 66 }
msbaylis 0:9796509d718e 67
msbaylis 0:9796509d718e 68 int main() {
msbaylis 0:9796509d718e 69 // turn off LED
msbaylis 0:9796509d718e 70 controlLED(off);
msbaylis 0:9796509d718e 71
msbaylis 0:9796509d718e 72 // set SW2 and SW3 to generate interrupt on falling edge
msbaylis 0:9796509d718e 73 switch2.fall(&sw2_ISR);
msbaylis 0:9796509d718e 74 switch3.fall(&sw3_ISR);
msbaylis 0:9796509d718e 75
msbaylis 0:9796509d718e 76 pc.printf("\r\n\r\nWelcome to the K64F MQTT Demo!\r\n");
msbaylis 0:9796509d718e 77 pc.printf("\r\nAttempting connect to local network...\r\n");
msbaylis 0:9796509d718e 78
msbaylis 0:9796509d718e 79 // initialize ethernet interface
msbaylis 0:9796509d718e 80 MQTTEthernet ipstack = MQTTEthernet();
msbaylis 0:9796509d718e 81
msbaylis 0:9796509d718e 82 // get and display client network info
msbaylis 0:9796509d718e 83 EthernetInterface& eth = ipstack.getEth();
msbaylis 0:9796509d718e 84 pc.printf("IP address is %s\r\n", eth.getIPAddress());
msbaylis 0:9796509d718e 85 pc.printf("MAC address is %s\r\n", eth.getMACAddress());
msbaylis 0:9796509d718e 86 pc.printf("Gateway address is %s\r\n", eth.getGateway());
msbaylis 0:9796509d718e 87
msbaylis 0:9796509d718e 88 // construct the MQTT client
msbaylis 0:9796509d718e 89 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
msbaylis 0:9796509d718e 90
msbaylis 0:9796509d718e 91 char* hostname = BROKER;
msbaylis 0:9796509d718e 92 int port = PORT;
msbaylis 0:9796509d718e 93 int rc;
msbaylis 0:9796509d718e 94
msbaylis 0:9796509d718e 95 pc.printf("\r\nAttempting TCP connect to %s:%d: ", hostname, port);
msbaylis 0:9796509d718e 96
msbaylis 0:9796509d718e 97 // connect to TCP socket and check return code
msbaylis 0:9796509d718e 98 if ((rc = ipstack.connect(hostname, port)) != 0)
msbaylis 0:9796509d718e 99 pc.printf("failed: rc= %d\r\n", rc);
msbaylis 0:9796509d718e 100
msbaylis 0:9796509d718e 101 else
msbaylis 0:9796509d718e 102 pc.printf("success\r\n");
msbaylis 0:9796509d718e 103
msbaylis 0:9796509d718e 104 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
msbaylis 0:9796509d718e 105 data.MQTTVersion = 3;
msbaylis 0:9796509d718e 106 data.clientID.cstring = CLIENTID;
msbaylis 0:9796509d718e 107 // data.username.cstring = USERNAME;
msbaylis 0:9796509d718e 108 // data.password.cstring = PASSWORD;
msbaylis 0:9796509d718e 109
msbaylis 0:9796509d718e 110 // send MQTT connect packet and check return code
msbaylis 0:9796509d718e 111 pc.printf("Attempting MQTT connect to %s:%d: ", hostname, port);
msbaylis 0:9796509d718e 112 if ((rc = client.connect(data)) != 0)
msbaylis 0:9796509d718e 113 pc.printf("failed: rc= %d\r\n", rc);
msbaylis 0:9796509d718e 114
msbaylis 0:9796509d718e 115 else
msbaylis 0:9796509d718e 116 pc.printf("success\r\n");
msbaylis 0:9796509d718e 117
msbaylis 0:9796509d718e 118 char* topic = TOPIC;
msbaylis 0:9796509d718e 119
msbaylis 0:9796509d718e 120 // subscribe to MQTT topic
msbaylis 0:9796509d718e 121 pc.printf("Subscribing to MQTT topic %s: ", topic);
msbaylis 0:9796509d718e 122 if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0)
msbaylis 0:9796509d718e 123 pc.printf("failed: rc= %d\r\n", rc);
msbaylis 0:9796509d718e 124
msbaylis 0:9796509d718e 125 else
msbaylis 0:9796509d718e 126 pc.printf("success\r\n");
msbaylis 0:9796509d718e 127
msbaylis 0:9796509d718e 128 MQTT::Message message;
msbaylis 0:9796509d718e 129 char buf[100];
msbaylis 0:9796509d718e 130 message.qos = MQTT::QOS0;
msbaylis 0:9796509d718e 131 message.retained = false;
msbaylis 0:9796509d718e 132 message.dup = false;
msbaylis 0:9796509d718e 133 message.payload = (void*)buf;
msbaylis 0:9796509d718e 134 message.payloadlen = strlen(buf)+1;
msbaylis 0:9796509d718e 135
msbaylis 0:9796509d718e 136 while(true) {
msbaylis 0:9796509d718e 137 osEvent switchEvent = messageQ.get(100);
msbaylis 0:9796509d718e 138
msbaylis 0:9796509d718e 139 if (switchEvent.value.v == 22 || switchEvent.value.v == 33) {
msbaylis 0:9796509d718e 140 switch(switchEvent.value.v) {
msbaylis 0:9796509d718e 141 case 22 :
msbaylis 0:9796509d718e 142 sprintf(buf, "sw2");
msbaylis 0:9796509d718e 143 break;
msbaylis 0:9796509d718e 144 case 33 :
msbaylis 0:9796509d718e 145 sprintf(buf, "sw3");
msbaylis 0:9796509d718e 146 break;
msbaylis 0:9796509d718e 147 }
msbaylis 0:9796509d718e 148 pc.printf("Publishing MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload);
msbaylis 0:9796509d718e 149 rc = client.publish(topic, message);
msbaylis 0:9796509d718e 150 client.yield(100);
msbaylis 0:9796509d718e 151 }
msbaylis 0:9796509d718e 152
msbaylis 0:9796509d718e 153 else {
msbaylis 0:9796509d718e 154 client.yield(100);
msbaylis 0:9796509d718e 155 }
msbaylis 0:9796509d718e 156 }
msbaylis 0:9796509d718e 157 }