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: MQTT-JMF WNCInterface mbed-rtos mbed
Fork of WNCInterface_MQTT_hivemq by
main.cpp
00001 //#define MQTT_DEBUG 00002 00003 #include "mbed.h" 00004 #include "MQTTClient.h" 00005 #include "MQTTFormat.h" 00006 00007 //#include "MQTTEthernet.h" 00008 #include "MQTTWNCInterface.h" 00009 #include "rtos.h" 00010 #include "k64f.h" 00011 #include "Proximity.h" 00012 00013 00014 // connect options for MQTT broker 00015 #define BROKER "iot.foundry.att.com" // MQTT broker URL 00016 #define PORT 1880 // MQTT broker port number 00017 #define CLIENTID "96430312d8f7" // use K64F MAC address without colons 00018 #define USERNAME "" // not required for MQTT Dashboard public broker 00019 #define PASSWORD "" // not required for MQTT Dashboard public broker 00020 #define TOPIC "proximity" // MQTT topic 00021 00022 Proximity proximityStrip; 00023 Queue<uint32_t, 6> messageQ; 00024 00025 struct rcvd_errs{ 00026 int err; 00027 char *er; 00028 }; 00029 00030 rcvd_errs response[] = { 00031 200, "200 OK - Request has succeeded.", 00032 201, "201 Created - Request has been fulfilled and a new resource created.", 00033 202, "202 Accepted - The request has been accepted for processing, but the processing will be completed asynchronously", 00034 204, "204 No Content - The server has fulfilled the request but does not need to return an entity-body.", 00035 400, "400 Bad Request - Bad request (e.g. sending an array when the API is expecting a hash.)", 00036 401, "401 Unauthorized - No valid API key provided.", 00037 403, "403 Forbidden - You tried to access a disabled device, or your API key is not allowed to access that resource, etc.", 00038 404, "404 Not Found - The requested item could not be found.", 00039 405, "405 Method Not Allowed - The HTTP method specified is not allowed.", 00040 415, "415 Unsupported Media Type - The requested media type is currently not supported.", 00041 422, "422 Unprocessable Entity - Can result from sending invalid fields.", 00042 429, "429 Too Many Requests - The user has sent too many requests in a given period of time.", 00043 500, "500 Server errors - Something went wrong in the M2X server", 00044 502, "502 Server errors - Something went wrong in the M2X server", 00045 503, "503 Server errors - Something went wrong in the M2X server", 00046 504, "504 Server errors - Something went wrong in the M2X server", 00047 }; 00048 #define RCMAX sizeof(response)/sizeof(rcvd_errs) 00049 00050 char * response_str(int rc) { 00051 static char *unkown = "Unknown error code..."; 00052 int i=0; 00053 while( response[i].err != rc && i < RCMAX) 00054 i++; 00055 return (i<RCMAX? response[i].er : unkown); 00056 } 00057 00058 // LED color control function 00059 void controlLED(color_t led_color) { 00060 switch(led_color) { 00061 case red : 00062 greenLED = blueLED = 1; 00063 redLED = 0.7; 00064 break; 00065 case green : 00066 redLED = blueLED = 1; 00067 greenLED = 0.7; 00068 break; 00069 case blue : 00070 redLED = greenLED = 1; 00071 blueLED = 0.7; 00072 break; 00073 case off : 00074 redLED = greenLED = blueLED = 1; 00075 break; 00076 } 00077 } 00078 00079 // Switch 2 interrupt handler 00080 void sw2_ISR(void) { 00081 messageQ.put((uint32_t*)22); 00082 } 00083 00084 // Switch3 interrupt handler 00085 void sw3_ISR(void) { 00086 messageQ.put((uint32_t*)33); 00087 } 00088 00089 // MQTT message arrived callback function 00090 void messageArrived(MQTT::MessageData& md) { 00091 MQTT::Message &message = md.message; 00092 printf("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); 00093 00094 if (message.payloadlen == 3) { 00095 if (strncmp((char*)message.payload, "red", 3) == 0) 00096 controlLED(red); 00097 00098 else if(strncmp((char*)message.payload, "grn", 3) == 0) 00099 controlLED(green); 00100 00101 else if(strncmp((char*)message.payload, "blu", 3) == 0) 00102 controlLED(blue); 00103 00104 else if(strncmp((char*)message.payload, "off", 3) == 0) 00105 controlLED(off); 00106 } 00107 } 00108 00109 int main() { 00110 int rc, good = 0; 00111 Timer tmr; 00112 char* topic = TOPIC; 00113 int count = 0; 00114 // turn off LED 00115 controlLED(red); 00116 00117 // set SW2 and SW3 to generate interrupt on falling edge 00118 switch2.fall(&sw2_ISR); 00119 switch3.fall(&sw3_ISR); 00120 00121 // initialize ethernet interface 00122 MQTTwnc ipstack = MQTTwnc(); 00123 00124 // get and display client network info 00125 WNCInterface& eth = ipstack.getEth(); 00126 00127 // construct the MQTT client 00128 MQTT::Client<MQTTwnc, Countdown> client = MQTT::Client<MQTTwnc, Countdown>(ipstack); 00129 00130 char* hostname = BROKER; 00131 int port = PORT; 00132 00133 printf("\r\n\r\nWelcome to the K64F MQTT Demo!\r\n"); 00134 printf("\r\nConnected to local network...\r\n"); 00135 printf("IP address is %s\r\n", eth.getIPAddress()); 00136 printf("MAC address is %s\r\n", eth.getMACAddress()); 00137 printf("Gateway address is %s\r\n", eth.getGateway()); 00138 00139 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00140 int tries; 00141 00142 while( !good ) { 00143 tries=0; 00144 // connect to TCP socket and check return code 00145 tmr.start(); 00146 rc = 1; 00147 while( rc && tries < 3) { 00148 printf("\r\n\r\nAttempting TCP connect to %s:%d: ", hostname, port); 00149 rc = ipstack.connect(hostname, port); 00150 if( rc ) { 00151 printf("Failed!!\r\n"); 00152 while( tmr.read_ms() < 5000 ) ; 00153 tries++; 00154 tmr.reset(); 00155 } 00156 else { 00157 printf("Success!\r\n"); 00158 rc = 0; 00159 } 00160 } 00161 00162 data.MQTTVersion = 3; 00163 data.clientID.cstring = eth.getMACAddress(); 00164 // data.username.cstring = USERNAME; 00165 // data.password.cstring = PASSWORD; 00166 00167 // send MQTT connect packet and check return code 00168 rc = 1; 00169 tmr.reset(); 00170 while( !client.isConnected() && rc && tries < 3) { 00171 printf("Attempting (%d) MQTT connect to %s:%d: ", tries, hostname, port); 00172 rc = client.connect(data); 00173 if( rc ) { 00174 printf("Connection Failed!\r\n"); 00175 while( tmr.read_ms() < 5000 ); 00176 tmr.reset(); 00177 tries++; 00178 } 00179 else 00180 printf("Connected!\r\n"); 00181 } 00182 00183 // subscribe to MQTT topic 00184 tmr.reset(); 00185 rc = 1; 00186 while( rc && client.isConnected() && tries < 3) { 00187 printf("We are %s, Subscribing to MQTT topic %s (%d): ", client.isConnected()?"connected":"NOT CONNECTED",topic,tries); 00188 rc = client.subscribe(topic, MQTT::QOS0, messageArrived); 00189 if( rc ) { 00190 printf("Subscribe request failed!\r\n"); 00191 while( tmr.read_ms() < 5000 ); 00192 tries++; 00193 tmr.reset(); 00194 } 00195 else { 00196 good=1; 00197 printf("Subscribe successful!\r\n"); 00198 } 00199 } 00200 } 00201 controlLED(blue); 00202 MQTT::Message message; 00203 char buf[512]; 00204 message.qos = MQTT::QOS0; 00205 message.retained = false; 00206 message.dup = false; 00207 message.payload = (void*)buf; 00208 message.payloadlen = strlen(buf)+1; 00209 00210 sprintf(buf, "[\"Hi\"]"); 00211 message.payloadlen = strlen(buf)+1; 00212 rc = client.publish(topic, message); 00213 if( rc ) { 00214 printf("Publish request failed! (%d)\r\n",rc); 00215 } 00216 else { 00217 printf("Publish successful!\r\n"); 00218 client.yield(100); 00219 } 00220 00221 proximityStrip.init(); 00222 proximityStrip.on(); 00223 00224 while(true) { 00225 count++; 00226 if(count >= 30) 00227 { 00228 count = 0; 00229 sprintf(buf, "wake"); 00230 message.payloadlen = strlen(buf)+1; 00231 controlLED(red); 00232 rc = client.publish(topic, message); 00233 controlLED(off); 00234 if( rc ) { 00235 printf("Publish request failed! (%d)\r\n",rc); 00236 } 00237 else { 00238 printf("Publish successful!\r\n"); 00239 client.yield(100); 00240 } 00241 } 00242 00243 osEvent switchEvent = messageQ.get(100); 00244 controlLED(green); 00245 proximityStrip.scan(); 00246 controlLED(off); 00247 if(proximityStrip.changed(50)) 00248 { 00249 controlLED(blue); 00250 char* data = proximityStrip.getDataStr(); 00251 printf("data:%s",data); 00252 sprintf(buf, data); 00253 //sprintf(buf, "[{\"s\":\"1\"},{\"s\":\"2\"}]"); 00254 message.payloadlen = strlen(buf)+1; 00255 rc = client.publish(topic, message); 00256 if( rc ) { 00257 printf("Publish request failed! (%d)\r\n",rc); 00258 } 00259 else { 00260 printf("Publish successful!\r\n"); 00261 count=0; 00262 client.yield(100); 00263 } 00264 } 00265 if (switchEvent.value.v == 22 || switchEvent.value.v == 33) { 00266 switch(switchEvent.value.v) { 00267 case 22 : 00268 sprintf(buf, "sw2"); 00269 message.payloadlen = strlen(buf)+1; 00270 break; 00271 case 33 : 00272 sprintf(buf, "sw3"); 00273 message.payloadlen = strlen(buf)+1; 00274 break; 00275 } 00276 printf("Publishing MQTT message: %s (%d)\r\n", (char*)message.payload, 00277 message.payloadlen); 00278 rc = client.publish(topic, message); 00279 if( rc ) { 00280 printf("Publish request failed! (%d)\r\n",rc); 00281 } 00282 else { 00283 printf("Publish successful!\r\n"); 00284 count=0; 00285 client.yield(100); 00286 } 00287 } 00288 else { 00289 client.yield(100); 00290 } 00291 } 00292 } 00293
Generated on Thu Jul 14 2022 09:17:06 by
1.7.2
