Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface MQTT mbed-rtos mbed HC_SR04_Ultrasonic_Library
Fork of K64F-RTOS-MQTT-Example by
main.cpp
00001 #include "mbed.h" 00002 #include "MQTTClient.h" 00003 #include "MQTTEthernet.h" 00004 #include "rtos.h" 00005 #include "k64f.h" 00006 #include "ultrasonic.h" 00007 00008 // connect options for MQTT broker 00009 #define BROKER "192.168.1.133"//"broker.mqttdashboard.com" // MQTT broker URL 00010 #define PORT 1883 // MQTT broker port number 00011 #define CLIENTID "cd23787" // use K64F MAC address without colons 00012 #define USERNAME "c1" // not required for MQTT Dashboard public broker 00013 #define PASSWORD "" // not required for MQTT Dashboard public broker 00014 #define TOPIC "3435" // MQTT topic 00015 00016 Queue<uint32_t, 6> messageQ; 00017 00018 Timer timer; 00019 00020 DigitalOut trigger1(PTC5); 00021 DigitalIn echo1(PTC7); 00022 00023 DigitalOut trigger2(PTC9); 00024 DigitalIn echo2(PTC8); 00025 00026 DigitalOut trigger3(PTC0); 00027 DigitalIn echo3(PTC1); 00028 00029 DigitalOut trigger4(PTB19); 00030 DigitalIn echo4(PTB18); 00031 00032 // LED color control function 00033 void controlLED(color_t led_color) { 00034 switch(led_color) { 00035 case red : 00036 greenLED = blueLED = 1; 00037 redLED = 0.7; 00038 break; 00039 case green : 00040 redLED = blueLED = 1; 00041 greenLED = 0.7; 00042 break; 00043 case blue : 00044 redLED = greenLED = 1; 00045 blueLED = 0.7; 00046 break; 00047 case off : 00048 redLED = greenLED = blueLED = 1; 00049 break; 00050 } 00051 } 00052 00053 // Switch 2 interrupt handler 00054 void sw2_ISR(void) { 00055 messageQ.put((uint32_t*)22); 00056 } 00057 00058 // Switch3 interrupt handler 00059 void sw3_ISR(void) { 00060 messageQ.put((uint32_t*)33); 00061 } 00062 00063 // MQTT message arrived callback function 00064 void messageArrived(MQTT::MessageData& md) { 00065 MQTT::Message &message = md.message; 00066 00067 pc.printf("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); 00068 00069 if (message.payloadlen == 3) { 00070 if (strncmp((char*)message.payload, "red", 3) == 0) 00071 controlLED(red); 00072 00073 else if(strncmp((char*)message.payload, "grn", 3) == 0) 00074 controlLED(green); 00075 00076 else if(strncmp((char*)message.payload, "blu", 3) == 0) 00077 controlLED(blue); 00078 00079 else if(strncmp((char*)message.payload, "off", 3) == 0) 00080 controlLED(off); 00081 } 00082 } 00083 void dist1(int distance){ 00084 pc.printf("| %d mm\t",distance); 00085 } 00086 void dist2(int distance){ 00087 pc.printf("| %d mm\t",distance); 00088 } 00089 void dist3(int distance){ 00090 pc.printf("| %d mm\t",distance); 00091 } 00092 void dist4(int distance){ 00093 pc.printf("| %d mm\t |\n",distance); 00094 } 00095 //ultrasonic mu1(PTC5,PTC7,0.1,0.5,&dist1); 00096 //ultrasonic mu2(PTC9,PTC8,0.1,.5,&dist1); 00097 //ultrasonic mu3(PTC0,PTC1,0.1,.5,&dist1); 00098 //ultrasonic mu4(PTC19,PTC18,0.1,.5,&dist1); 00099 int main() { 00100 int distance; 00101 int corr,res; 00102 00103 // turn off LED 00104 controlLED(off); 00105 // mu1.startUpdates(); 00106 // mu2.startUpdates(); 00107 // mu3.startUpdates(); 00108 // mu4.startUpdates(); 00109 00110 timer.reset(); 00111 timer.start(); 00112 while(echo1==2){}; 00113 timer.stop(); 00114 corr =timer.read_us(); 00115 00116 00117 00118 // set SW2 and SW3 to generate interrupt on falling edge 00119 switch2.fall(&sw2_ISR); 00120 switch3.fall(&sw3_ISR); 00121 pc.baud(115200); 00122 pc.printf("\r\n\r\nWelcome to the K64F MQTT Demo!\r\n"); 00123 pc.printf("\r\nAttempting connect to local network...\r\n"); 00124 00125 // initialize ethernet interface 00126 MQTTEthernet ipstack = MQTTEthernet(); 00127 00128 // get and display client network info 00129 EthernetInterface& eth = ipstack.getEth(); 00130 pc.printf("IP address is %s\r\n", eth.getIPAddress()); 00131 pc.printf("MAC address is %s\r\n", eth.getMACAddress()); 00132 pc.printf("Gateway address is %s\r\n", eth.getGateway()); 00133 00134 // construct the MQTT client 00135 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); 00136 00137 char* hostname = BROKER; 00138 int port = PORT; 00139 int rc; 00140 00141 pc.printf("\r\nAttempting TCP connect to %s:%d: ", hostname, port); 00142 00143 // connect to TCP socket and check return code 00144 if ((rc = ipstack.connect(hostname, port)) != 0) 00145 pc.printf("failed: rc= %d\r\n", rc); 00146 00147 else 00148 pc.printf("success\r\n"); 00149 00150 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00151 data.MQTTVersion = 3; 00152 data.clientID.cstring = CLIENTID; 00153 // data.username.cstring = USERNAME; 00154 // data.password.cstring = PASSWORD; 00155 00156 // send MQTT connect packet and check return code 00157 pc.printf("Attempting MQTT connect to %s:%d: ", hostname, port); 00158 if ((rc = client.connect(data)) != 0) 00159 pc.printf("failed: rc= %d\r\n", rc); 00160 00161 else 00162 pc.printf("success\r\n"); 00163 00164 char* topic = TOPIC; 00165 int lenth1; 00166 00167 // subscribe to MQTT topic 00168 pc.printf("Subscribing to MQTT topic %s: ", topic); 00169 if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0) 00170 pc.printf("failed: rc= %d\r\n", rc); 00171 00172 else 00173 pc.printf("success\r\n"); 00174 00175 MQTT::Message message; 00176 char buf[100]; 00177 message.qos = MQTT::QOS1; 00178 message.retained = false; 00179 message.dup = false; 00180 message.payload = (void*)buf; 00181 message.payloadlen = strlen(buf)+1; 00182 00183 while(true) { 00184 // mu1.checkDistance(); 00185 // mu2.checkDistance(); 00186 // mu3.checkDistance(); 00187 // mu4.checkDistance(); 00188 00189 trigger1=1; 00190 timer.reset(); 00191 wait_us(10); 00192 trigger1=0; 00193 while(echo1==0){}; 00194 timer.start(); 00195 while(echo1==1){}; 00196 timer.stop(); 00197 res=timer.read_us(); 00198 distance =(res-corr)/58.0; 00199 pc.printf("| %d\t",distance); 00200 00201 trigger2=1; 00202 timer.reset(); 00203 wait_us(10); 00204 trigger2=0; 00205 while(echo2==0){}; 00206 timer.start(); 00207 while(echo2==1){}; 00208 timer.stop(); 00209 res=timer.read_us(); 00210 distance =(res-corr)/58.0; 00211 pc.printf("| %d\t",distance); 00212 00213 trigger3=1; 00214 timer.reset(); 00215 wait_us(10); 00216 trigger3=0; 00217 while(echo3==0){}; 00218 timer.start(); 00219 while(echo3==1){}; 00220 timer.stop(); 00221 res=timer.read_us(); 00222 distance =(res-corr)/58.0; 00223 pc.printf("| %d\t",distance); 00224 00225 trigger4=1; 00226 timer.reset(); 00227 wait_us(10); 00228 trigger4=0; 00229 while(echo4==0){}; 00230 timer.start(); 00231 while(echo4==1){}; 00232 timer.stop(); 00233 res=timer.read_us(); 00234 distance =(res-corr)/58.0; 00235 pc.printf("| %d |\n",distance); 00236 00237 00238 00239 osEvent switchEvent = messageQ.get(100); 00240 00241 if (switchEvent.value.v == 22 || switchEvent.value.v == 33) { 00242 switch(switchEvent.value.v) { 00243 case 22 : 00244 sprintf(buf, "sw2"); 00245 break; 00246 case 33 : 00247 sprintf(buf, "sw3"); 00248 break; 00249 } 00250 pc.printf("Publishing MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); 00251 rc = client.publish(topic, message); 00252 client.yield(100); 00253 } 00254 00255 else { 00256 client.yield(100); 00257 } 00258 } 00259 }
Generated on Tue Jul 12 2022 18:59:27 by
1.7.2
