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
main.cpp@3:e33c80165703, 2017-08-09 (annotated)
- Committer:
- sim2egor
- Date:
- Wed Aug 09 18:39:52 2017 +0300
- Revision:
- 3:e33c80165703
- Parent:
- 2:24b2b10d3bc2
??????? ?? ????????? ?? ???????
Who changed what in which revision?
User | Revision | Line number | New 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" |
sim2egor |
3:e33c80165703 | 6 | #include "ultrasonic.h" |
msbaylis | 0:9796509d718e | 7 | |
msbaylis | 0:9796509d718e | 8 | // connect options for MQTT broker |
sim2egor | 1:9caf46f4ed3e | 9 | #define BROKER "192.168.1.133"//"broker.mqttdashboard.com" // MQTT broker URL |
msbaylis | 0:9796509d718e | 10 | #define PORT 1883 // MQTT broker port number |
sim2egor | 1:9caf46f4ed3e | 11 | #define CLIENTID "cd23787" // use K64F MAC address without colons |
sim2egor | 1:9caf46f4ed3e | 12 | #define USERNAME "c1" // not required for MQTT Dashboard public broker |
msbaylis | 0:9796509d718e | 13 | #define PASSWORD "" // not required for MQTT Dashboard public broker |
sim2egor | 1:9caf46f4ed3e | 14 | #define TOPIC "3435" // MQTT topic |
msbaylis | 0:9796509d718e | 15 | |
msbaylis | 0:9796509d718e | 16 | Queue<uint32_t, 6> messageQ; |
msbaylis | 0:9796509d718e | 17 | |
sim2egor |
3:e33c80165703 | 18 | Timer timer; |
sim2egor |
3:e33c80165703 | 19 | |
sim2egor |
3:e33c80165703 | 20 | DigitalOut trigger1(PTC5); |
sim2egor |
3:e33c80165703 | 21 | DigitalIn echo1(PTC7); |
sim2egor |
3:e33c80165703 | 22 | |
sim2egor |
3:e33c80165703 | 23 | DigitalOut trigger2(PTC9); |
sim2egor |
3:e33c80165703 | 24 | DigitalIn echo2(PTC8); |
sim2egor |
3:e33c80165703 | 25 | |
sim2egor |
3:e33c80165703 | 26 | DigitalOut trigger3(PTC0); |
sim2egor |
3:e33c80165703 | 27 | DigitalIn echo3(PTC1); |
sim2egor |
3:e33c80165703 | 28 | |
sim2egor |
3:e33c80165703 | 29 | DigitalOut trigger4(PTB19); |
sim2egor |
3:e33c80165703 | 30 | DigitalIn echo4(PTB18); |
sim2egor |
3:e33c80165703 | 31 | |
msbaylis | 0:9796509d718e | 32 | // LED color control function |
msbaylis | 0:9796509d718e | 33 | void controlLED(color_t led_color) { |
msbaylis | 0:9796509d718e | 34 | switch(led_color) { |
msbaylis | 0:9796509d718e | 35 | case red : |
msbaylis | 0:9796509d718e | 36 | greenLED = blueLED = 1; |
msbaylis | 0:9796509d718e | 37 | redLED = 0.7; |
msbaylis | 0:9796509d718e | 38 | break; |
msbaylis | 0:9796509d718e | 39 | case green : |
msbaylis | 0:9796509d718e | 40 | redLED = blueLED = 1; |
msbaylis | 0:9796509d718e | 41 | greenLED = 0.7; |
msbaylis | 0:9796509d718e | 42 | break; |
msbaylis | 0:9796509d718e | 43 | case blue : |
msbaylis | 0:9796509d718e | 44 | redLED = greenLED = 1; |
msbaylis | 0:9796509d718e | 45 | blueLED = 0.7; |
msbaylis | 0:9796509d718e | 46 | break; |
msbaylis | 0:9796509d718e | 47 | case off : |
msbaylis | 0:9796509d718e | 48 | redLED = greenLED = blueLED = 1; |
msbaylis | 0:9796509d718e | 49 | break; |
msbaylis | 0:9796509d718e | 50 | } |
msbaylis | 0:9796509d718e | 51 | } |
msbaylis | 0:9796509d718e | 52 | |
msbaylis | 0:9796509d718e | 53 | // Switch 2 interrupt handler |
msbaylis | 0:9796509d718e | 54 | void sw2_ISR(void) { |
msbaylis | 0:9796509d718e | 55 | messageQ.put((uint32_t*)22); |
msbaylis | 0:9796509d718e | 56 | } |
msbaylis | 0:9796509d718e | 57 | |
msbaylis | 0:9796509d718e | 58 | // Switch3 interrupt handler |
msbaylis | 0:9796509d718e | 59 | void sw3_ISR(void) { |
msbaylis | 0:9796509d718e | 60 | messageQ.put((uint32_t*)33); |
msbaylis | 0:9796509d718e | 61 | } |
msbaylis | 0:9796509d718e | 62 | |
msbaylis | 0:9796509d718e | 63 | // MQTT message arrived callback function |
msbaylis | 0:9796509d718e | 64 | void messageArrived(MQTT::MessageData& md) { |
msbaylis | 0:9796509d718e | 65 | MQTT::Message &message = md.message; |
sim2egor |
2:24b2b10d3bc2 | 66 | |
msbaylis | 0:9796509d718e | 67 | pc.printf("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); |
msbaylis | 0:9796509d718e | 68 | |
msbaylis | 0:9796509d718e | 69 | if (message.payloadlen == 3) { |
msbaylis | 0:9796509d718e | 70 | if (strncmp((char*)message.payload, "red", 3) == 0) |
msbaylis | 0:9796509d718e | 71 | controlLED(red); |
msbaylis | 0:9796509d718e | 72 | |
msbaylis | 0:9796509d718e | 73 | else if(strncmp((char*)message.payload, "grn", 3) == 0) |
msbaylis | 0:9796509d718e | 74 | controlLED(green); |
msbaylis | 0:9796509d718e | 75 | |
msbaylis | 0:9796509d718e | 76 | else if(strncmp((char*)message.payload, "blu", 3) == 0) |
msbaylis | 0:9796509d718e | 77 | controlLED(blue); |
msbaylis | 0:9796509d718e | 78 | |
msbaylis | 0:9796509d718e | 79 | else if(strncmp((char*)message.payload, "off", 3) == 0) |
msbaylis | 0:9796509d718e | 80 | controlLED(off); |
msbaylis | 0:9796509d718e | 81 | } |
msbaylis | 0:9796509d718e | 82 | } |
sim2egor |
3:e33c80165703 | 83 | void dist1(int distance){ |
sim2egor |
3:e33c80165703 | 84 | pc.printf("| %d mm\t",distance); |
sim2egor |
3:e33c80165703 | 85 | } |
sim2egor |
3:e33c80165703 | 86 | void dist2(int distance){ |
sim2egor |
3:e33c80165703 | 87 | pc.printf("| %d mm\t",distance); |
sim2egor |
3:e33c80165703 | 88 | } |
sim2egor |
3:e33c80165703 | 89 | void dist3(int distance){ |
sim2egor |
3:e33c80165703 | 90 | pc.printf("| %d mm\t",distance); |
sim2egor |
3:e33c80165703 | 91 | } |
sim2egor |
3:e33c80165703 | 92 | void dist4(int distance){ |
sim2egor |
3:e33c80165703 | 93 | pc.printf("| %d mm\t |\n",distance); |
sim2egor |
3:e33c80165703 | 94 | } |
sim2egor |
3:e33c80165703 | 95 | //ultrasonic mu1(PTC5,PTC7,0.1,0.5,&dist1); |
sim2egor |
3:e33c80165703 | 96 | //ultrasonic mu2(PTC9,PTC8,0.1,.5,&dist1); |
sim2egor |
3:e33c80165703 | 97 | //ultrasonic mu3(PTC0,PTC1,0.1,.5,&dist1); |
sim2egor |
3:e33c80165703 | 98 | //ultrasonic mu4(PTC19,PTC18,0.1,.5,&dist1); |
sim2egor |
3:e33c80165703 | 99 | int main() { |
sim2egor |
3:e33c80165703 | 100 | int distance; |
sim2egor |
3:e33c80165703 | 101 | int corr,res; |
msbaylis | 0:9796509d718e | 102 | |
msbaylis | 0:9796509d718e | 103 | // turn off LED |
msbaylis | 0:9796509d718e | 104 | controlLED(off); |
sim2egor |
3:e33c80165703 | 105 | // mu1.startUpdates(); |
sim2egor |
3:e33c80165703 | 106 | // mu2.startUpdates(); |
sim2egor |
3:e33c80165703 | 107 | // mu3.startUpdates(); |
sim2egor |
3:e33c80165703 | 108 | // mu4.startUpdates(); |
sim2egor |
3:e33c80165703 | 109 | |
sim2egor |
3:e33c80165703 | 110 | timer.reset(); |
sim2egor |
3:e33c80165703 | 111 | timer.start(); |
sim2egor |
3:e33c80165703 | 112 | while(echo1==2){}; |
sim2egor |
3:e33c80165703 | 113 | timer.stop(); |
sim2egor |
3:e33c80165703 | 114 | corr =timer.read_us(); |
sim2egor |
3:e33c80165703 | 115 | |
sim2egor |
3:e33c80165703 | 116 | |
sim2egor |
3:e33c80165703 | 117 | |
msbaylis | 0:9796509d718e | 118 | // set SW2 and SW3 to generate interrupt on falling edge |
msbaylis | 0:9796509d718e | 119 | switch2.fall(&sw2_ISR); |
msbaylis | 0:9796509d718e | 120 | switch3.fall(&sw3_ISR); |
sim2egor |
2:24b2b10d3bc2 | 121 | pc.baud(115200); |
msbaylis | 0:9796509d718e | 122 | pc.printf("\r\n\r\nWelcome to the K64F MQTT Demo!\r\n"); |
msbaylis | 0:9796509d718e | 123 | pc.printf("\r\nAttempting connect to local network...\r\n"); |
msbaylis | 0:9796509d718e | 124 | |
msbaylis | 0:9796509d718e | 125 | // initialize ethernet interface |
msbaylis | 0:9796509d718e | 126 | MQTTEthernet ipstack = MQTTEthernet(); |
msbaylis | 0:9796509d718e | 127 | |
msbaylis | 0:9796509d718e | 128 | // get and display client network info |
msbaylis | 0:9796509d718e | 129 | EthernetInterface& eth = ipstack.getEth(); |
msbaylis | 0:9796509d718e | 130 | pc.printf("IP address is %s\r\n", eth.getIPAddress()); |
msbaylis | 0:9796509d718e | 131 | pc.printf("MAC address is %s\r\n", eth.getMACAddress()); |
msbaylis | 0:9796509d718e | 132 | pc.printf("Gateway address is %s\r\n", eth.getGateway()); |
msbaylis | 0:9796509d718e | 133 | |
msbaylis | 0:9796509d718e | 134 | // construct the MQTT client |
msbaylis | 0:9796509d718e | 135 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
msbaylis | 0:9796509d718e | 136 | |
msbaylis | 0:9796509d718e | 137 | char* hostname = BROKER; |
msbaylis | 0:9796509d718e | 138 | int port = PORT; |
msbaylis | 0:9796509d718e | 139 | int rc; |
msbaylis | 0:9796509d718e | 140 | |
msbaylis | 0:9796509d718e | 141 | pc.printf("\r\nAttempting TCP connect to %s:%d: ", hostname, port); |
msbaylis | 0:9796509d718e | 142 | |
msbaylis | 0:9796509d718e | 143 | // connect to TCP socket and check return code |
msbaylis | 0:9796509d718e | 144 | if ((rc = ipstack.connect(hostname, port)) != 0) |
msbaylis | 0:9796509d718e | 145 | pc.printf("failed: rc= %d\r\n", rc); |
msbaylis | 0:9796509d718e | 146 | |
msbaylis | 0:9796509d718e | 147 | else |
msbaylis | 0:9796509d718e | 148 | pc.printf("success\r\n"); |
msbaylis | 0:9796509d718e | 149 | |
msbaylis | 0:9796509d718e | 150 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
msbaylis | 0:9796509d718e | 151 | data.MQTTVersion = 3; |
msbaylis | 0:9796509d718e | 152 | data.clientID.cstring = CLIENTID; |
msbaylis | 0:9796509d718e | 153 | // data.username.cstring = USERNAME; |
msbaylis | 0:9796509d718e | 154 | // data.password.cstring = PASSWORD; |
msbaylis | 0:9796509d718e | 155 | |
msbaylis | 0:9796509d718e | 156 | // send MQTT connect packet and check return code |
msbaylis | 0:9796509d718e | 157 | pc.printf("Attempting MQTT connect to %s:%d: ", hostname, port); |
msbaylis | 0:9796509d718e | 158 | if ((rc = client.connect(data)) != 0) |
msbaylis | 0:9796509d718e | 159 | pc.printf("failed: rc= %d\r\n", rc); |
msbaylis | 0:9796509d718e | 160 | |
msbaylis | 0:9796509d718e | 161 | else |
msbaylis | 0:9796509d718e | 162 | pc.printf("success\r\n"); |
msbaylis | 0:9796509d718e | 163 | |
msbaylis | 0:9796509d718e | 164 | char* topic = TOPIC; |
sim2egor |
3:e33c80165703 | 165 | int lenth1; |
msbaylis | 0:9796509d718e | 166 | |
msbaylis | 0:9796509d718e | 167 | // subscribe to MQTT topic |
msbaylis | 0:9796509d718e | 168 | pc.printf("Subscribing to MQTT topic %s: ", topic); |
msbaylis | 0:9796509d718e | 169 | if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0) |
msbaylis | 0:9796509d718e | 170 | pc.printf("failed: rc= %d\r\n", rc); |
msbaylis | 0:9796509d718e | 171 | |
msbaylis | 0:9796509d718e | 172 | else |
msbaylis | 0:9796509d718e | 173 | pc.printf("success\r\n"); |
msbaylis | 0:9796509d718e | 174 | |
msbaylis | 0:9796509d718e | 175 | MQTT::Message message; |
msbaylis | 0:9796509d718e | 176 | char buf[100]; |
sim2egor |
3:e33c80165703 | 177 | message.qos = MQTT::QOS1; |
msbaylis | 0:9796509d718e | 178 | message.retained = false; |
msbaylis | 0:9796509d718e | 179 | message.dup = false; |
msbaylis | 0:9796509d718e | 180 | message.payload = (void*)buf; |
msbaylis | 0:9796509d718e | 181 | message.payloadlen = strlen(buf)+1; |
msbaylis | 0:9796509d718e | 182 | |
msbaylis | 0:9796509d718e | 183 | while(true) { |
sim2egor |
3:e33c80165703 | 184 | // mu1.checkDistance(); |
sim2egor |
3:e33c80165703 | 185 | // mu2.checkDistance(); |
sim2egor |
3:e33c80165703 | 186 | // mu3.checkDistance(); |
sim2egor |
3:e33c80165703 | 187 | // mu4.checkDistance(); |
sim2egor |
3:e33c80165703 | 188 | |
sim2egor |
3:e33c80165703 | 189 | trigger1=1; |
sim2egor |
3:e33c80165703 | 190 | timer.reset(); |
sim2egor |
3:e33c80165703 | 191 | wait_us(10); |
sim2egor |
3:e33c80165703 | 192 | trigger1=0; |
sim2egor |
3:e33c80165703 | 193 | while(echo1==0){}; |
sim2egor |
3:e33c80165703 | 194 | timer.start(); |
sim2egor |
3:e33c80165703 | 195 | while(echo1==1){}; |
sim2egor |
3:e33c80165703 | 196 | timer.stop(); |
sim2egor |
3:e33c80165703 | 197 | res=timer.read_us(); |
sim2egor |
3:e33c80165703 | 198 | distance =(res-corr)/58.0; |
sim2egor |
3:e33c80165703 | 199 | pc.printf("| %d\t",distance); |
sim2egor |
3:e33c80165703 | 200 | |
sim2egor |
3:e33c80165703 | 201 | trigger2=1; |
sim2egor |
3:e33c80165703 | 202 | timer.reset(); |
sim2egor |
3:e33c80165703 | 203 | wait_us(10); |
sim2egor |
3:e33c80165703 | 204 | trigger2=0; |
sim2egor |
3:e33c80165703 | 205 | while(echo2==0){}; |
sim2egor |
3:e33c80165703 | 206 | timer.start(); |
sim2egor |
3:e33c80165703 | 207 | while(echo2==1){}; |
sim2egor |
3:e33c80165703 | 208 | timer.stop(); |
sim2egor |
3:e33c80165703 | 209 | res=timer.read_us(); |
sim2egor |
3:e33c80165703 | 210 | distance =(res-corr)/58.0; |
sim2egor |
3:e33c80165703 | 211 | pc.printf("| %d\t",distance); |
sim2egor |
3:e33c80165703 | 212 | |
sim2egor |
3:e33c80165703 | 213 | trigger3=1; |
sim2egor |
3:e33c80165703 | 214 | timer.reset(); |
sim2egor |
3:e33c80165703 | 215 | wait_us(10); |
sim2egor |
3:e33c80165703 | 216 | trigger3=0; |
sim2egor |
3:e33c80165703 | 217 | while(echo3==0){}; |
sim2egor |
3:e33c80165703 | 218 | timer.start(); |
sim2egor |
3:e33c80165703 | 219 | while(echo3==1){}; |
sim2egor |
3:e33c80165703 | 220 | timer.stop(); |
sim2egor |
3:e33c80165703 | 221 | res=timer.read_us(); |
sim2egor |
3:e33c80165703 | 222 | distance =(res-corr)/58.0; |
sim2egor |
3:e33c80165703 | 223 | pc.printf("| %d\t",distance); |
sim2egor |
3:e33c80165703 | 224 | |
sim2egor |
3:e33c80165703 | 225 | trigger4=1; |
sim2egor |
3:e33c80165703 | 226 | timer.reset(); |
sim2egor |
3:e33c80165703 | 227 | wait_us(10); |
sim2egor |
3:e33c80165703 | 228 | trigger4=0; |
sim2egor |
3:e33c80165703 | 229 | while(echo4==0){}; |
sim2egor |
3:e33c80165703 | 230 | timer.start(); |
sim2egor |
3:e33c80165703 | 231 | while(echo4==1){}; |
sim2egor |
3:e33c80165703 | 232 | timer.stop(); |
sim2egor |
3:e33c80165703 | 233 | res=timer.read_us(); |
sim2egor |
3:e33c80165703 | 234 | distance =(res-corr)/58.0; |
sim2egor |
3:e33c80165703 | 235 | pc.printf("| %d |\n",distance); |
sim2egor |
3:e33c80165703 | 236 | |
sim2egor |
3:e33c80165703 | 237 | |
sim2egor |
3:e33c80165703 | 238 | |
msbaylis | 0:9796509d718e | 239 | osEvent switchEvent = messageQ.get(100); |
msbaylis | 0:9796509d718e | 240 | |
msbaylis | 0:9796509d718e | 241 | if (switchEvent.value.v == 22 || switchEvent.value.v == 33) { |
msbaylis | 0:9796509d718e | 242 | switch(switchEvent.value.v) { |
msbaylis | 0:9796509d718e | 243 | case 22 : |
msbaylis | 0:9796509d718e | 244 | sprintf(buf, "sw2"); |
msbaylis | 0:9796509d718e | 245 | break; |
msbaylis | 0:9796509d718e | 246 | case 33 : |
msbaylis | 0:9796509d718e | 247 | sprintf(buf, "sw3"); |
msbaylis | 0:9796509d718e | 248 | break; |
msbaylis | 0:9796509d718e | 249 | } |
msbaylis | 0:9796509d718e | 250 | pc.printf("Publishing MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); |
msbaylis | 0:9796509d718e | 251 | rc = client.publish(topic, message); |
msbaylis | 0:9796509d718e | 252 | client.yield(100); |
msbaylis | 0:9796509d718e | 253 | } |
msbaylis | 0:9796509d718e | 254 | |
msbaylis | 0:9796509d718e | 255 | else { |
msbaylis | 0:9796509d718e | 256 | client.yield(100); |
msbaylis | 0:9796509d718e | 257 | } |
msbaylis | 0:9796509d718e | 258 | } |
msbaylis | 0:9796509d718e | 259 | } |