
Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.
Dependencies: C12832 MQTT LM75B MMA7660
Dependents: MFT_IoT_demo_USB400 IBM_RFID
main.cpp
00001 /******************************************************************************* 00002 * Copyright (c) 2014, 2017 IBM Corp. 00003 * 00004 * All rights reserved. This program and the accompanying materials 00005 * are made available under the terms of the Eclipse Public License v1.0 00006 * and Eclipse Distribution License v1.0 which accompany this distribution. 00007 * 00008 * The Eclipse Public License is available at 00009 * http://www.eclipse.org/legal/epl-v10.html 00010 * and the Eclipse Distribution License is available at 00011 * http://www.eclipse.org/org/documents/edl-v10.php. 00012 * 00013 * Contributors: 00014 * Sam Danbury - initial implementation 00015 * Ian Craggs - refactoring to remove STL and other changes 00016 * Sam Grove - added check for Ethernet cable. 00017 * Chris Styles - Added additional menu screen for software revision 00018 * James Sutton - Mac fix and extra debug 00019 * Ian Craggs - add not authorized messages 00020 * 00021 * To do : 00022 * Add magnetometer sensor output to IoT data stream 00023 * 00024 *******************************************************************************/ 00025 00026 #include "LM75B.h" 00027 #include "MMA7660.h" 00028 #include "MQTTClient.h" 00029 #include "MQTTEthernet.h" 00030 #include "C12832.h" 00031 #include "Arial12x12.h" 00032 #include "rtos.h" 00033 00034 // Update this to the next number *before* a commit 00035 #define __APP_SW_REVISION__ "19" 00036 00037 // Configuration values needed to connect to IBM IoT Cloud 00038 #define ORG "quickstart" // For a registered connection, replace with your org 00039 #define ID "" // For a registered connection, replace with your id 00040 #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token 00041 #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type 00042 00043 #define MQTT_PORT 1883 00044 #define MQTT_TLS_PORT 8883 00045 #define IBM_IOT_PORT MQTT_PORT 00046 00047 #define MQTT_MAX_PACKET_SIZE 250 00048 00049 #if defined(TARGET_UBLOX_C027) 00050 #warning "Compiling for mbed C027" 00051 #include "C027.h" 00052 #elif defined(TARGET_LPC1768) 00053 #warning "Compiling for mbed LPC1768" 00054 #include "LPC1768.h" 00055 #elif defined(TARGET_K64F) 00056 #warning "Compiling for mbed K64F" 00057 #include "K64F.h" 00058 #endif 00059 00060 00061 bool quickstartMode = true; 00062 char org[11] = ORG; 00063 char type[30] = TYPE; 00064 char id[30] = ID; // mac without colons 00065 char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode 00066 00067 bool connected = false; 00068 bool mqttConnecting = false; 00069 bool netConnected = false; 00070 bool netConnecting = false; 00071 bool ethernetInitialising = true; 00072 int connack_rc = 0; // MQTT connack return code 00073 int retryAttempt = 0; 00074 int menuItem = 0; 00075 00076 char* joystickPos = "CENTRE"; 00077 int blink_interval = 0; 00078 00079 const char* ip_addr = ""; 00080 const char* gateway_addr = "n/a"; 00081 const char* host_addr = ""; 00082 int connectTimeout = 1000; 00083 00084 // If we wanted to manually set the MAC address, 00085 // this is how to do it. In this example, we take 00086 // the original Mbed Set MAC address and combine it 00087 // with a prefix of our choosing. 00088 /* 00089 extern "C" void $Super$$mbed_mac_address(char *s); 00090 extern "C" void $Sub$$mbed_mac_address(char *s) 00091 { 00092 char originalMAC[6] = ""; 00093 $Super$$mbed_mac_address(originalMAC); 00094 00095 char mac[6]; 00096 mac[0] = 0x00; 00097 mac[1] = 0x08; 00098 mac[2] = 0xdc; 00099 mac[3] = originalMAC[3]; 00100 mac[4] = originalMAC[4]; 00101 mac[5] = originalMAC[5]; 00102 memcpy(s, mac, 6); 00103 } 00104 */ 00105 00106 00107 void off() 00108 { 00109 r = g = b = 1.0; // 1 is off, 0 is full brightness 00110 } 00111 00112 void red() 00113 { 00114 r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness 00115 } 00116 00117 void yellow() 00118 { 00119 r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness 00120 } 00121 00122 void green() 00123 { 00124 r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness 00125 } 00126 00127 00128 void flashing_yellow(void const *args) 00129 { 00130 bool on = false; 00131 while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD) // flashing yellow only while connecting 00132 { 00133 on = !on; 00134 if (on) 00135 yellow(); 00136 else 00137 off(); 00138 wait(0.5); 00139 } 00140 } 00141 00142 00143 void flashing_red(void const *args) // to be used when the connection is lost 00144 { 00145 bool on = false; 00146 while (!connected) 00147 { 00148 on = !on; 00149 if (on) 00150 red(); 00151 else 00152 off(); 00153 wait(2.0); 00154 } 00155 } 00156 00157 00158 void printMenu(int menuItem) 00159 { 00160 static char last_line1[30] = "", last_line2[30] = ""; 00161 char line1[30] = "", line2[30] = ""; 00162 00163 switch (menuItem) 00164 { 00165 case 0: 00166 sprintf(line1, "IBM IoT Cloud"); 00167 sprintf(line2, "Scroll with joystick"); 00168 break; 00169 case 1: 00170 sprintf(line1, "Go to:"); 00171 sprintf(line2, "http://ibm.biz/iotqstart"); 00172 break; 00173 case 2: 00174 sprintf(line1, "Device Identity:"); 00175 sprintf(line2, "%s", id); 00176 break; 00177 case 3: 00178 sprintf(line1, "MQTT Status:"); 00179 if (mqttConnecting) 00180 sprintf(line2, "Connecting... %d/5", retryAttempt); 00181 else 00182 { 00183 if (connected) 00184 sprintf(line2, "Connected"); 00185 else 00186 { 00187 switch (connack_rc) 00188 { 00189 case MQTT_CLIENTID_REJECTED: 00190 sprintf(line2, "Clientid rejected"); 00191 break; 00192 case MQTT_BAD_USERNAME_OR_PASSWORD: 00193 sprintf(line2, "Invalid username or password"); 00194 break; 00195 case MQTT_NOT_AUTHORIZED: 00196 sprintf(line2, "Not authorized"); 00197 break; 00198 default: 00199 sprintf(line2, "Disconnected"); 00200 } 00201 } 00202 } 00203 break; 00204 case 4: 00205 sprintf(line1, "Ethernet State:"); 00206 sprintf(line2, ethernetInitialising ? "Initializing..." : "Initialized"); 00207 break; 00208 case 5: 00209 sprintf(line1, "Socket State:"); 00210 if (netConnecting) 00211 sprintf(line2, "Connecting... %d/5", retryAttempt); 00212 else 00213 sprintf(line2, netConnected ? "Connected" : "Disconnected"); 00214 break; 00215 case 6: 00216 sprintf(line1, "IP Address:"); 00217 sprintf(line2, "%s", ip_addr); 00218 break; 00219 case 7: 00220 sprintf(line1, "Gateway:"); 00221 sprintf(line2, "%s", gateway_addr); 00222 break; 00223 case 8: 00224 sprintf(line1, "App version:"); 00225 sprintf(line2, "%s", __APP_SW_REVISION__); 00226 break; 00227 case 9: 00228 sprintf(line1, "Current Timeout:"); 00229 sprintf(line2, "%d ms", connectTimeout); 00230 break; 00231 } 00232 00233 if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0) 00234 { 00235 lcd.cls(); 00236 lcd.locate(0, 0); 00237 lcd.printf(line1); 00238 strncpy(last_line1, line1, sizeof(last_line1)); 00239 00240 lcd.locate(0,16); 00241 lcd.printf(line2); 00242 strncpy(last_line2, line2, sizeof(last_line2)); 00243 } 00244 } 00245 00246 00247 void setMenu() 00248 { 00249 00250 if (Down) 00251 { 00252 joystickPos = "DOWN"; 00253 if (menuItem >= 0 && menuItem < 9) 00254 printMenu(++menuItem); 00255 } 00256 else if (Left) 00257 joystickPos = "LEFT"; 00258 else if (Click) 00259 joystickPos = "CLICK"; 00260 else if (Up) 00261 { 00262 joystickPos = "UP"; 00263 if (menuItem <= 9 && menuItem > 0) 00264 printMenu(--menuItem); 00265 } 00266 else if (Right) 00267 joystickPos = "RIGHT"; 00268 else 00269 joystickPos = "CENTRE"; 00270 } 00271 00272 void menu_loop(void const *args) 00273 { 00274 int count = 0; 00275 while(true) 00276 { 00277 setMenu(); 00278 if (++count % 10 == 0) 00279 printMenu(menuItem); 00280 Thread::wait(100); 00281 } 00282 } 00283 00284 00285 /** 00286 * Display a message on the LCD screen prefixed with IBM IoT Cloud 00287 */ 00288 void displayMessage(char* message) 00289 { 00290 lcd.cls(); 00291 lcd.locate(0,0); 00292 lcd.printf("IBM IoT Cloud"); 00293 lcd.locate(0,16); 00294 lcd.printf(message); 00295 } 00296 00297 00298 int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) 00299 { 00300 const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; 00301 00302 char hostname[strlen(org) + strlen(iot_ibm) + 1]; 00303 sprintf(hostname, "%s%s", org, iot_ibm); 00304 EthernetInterface& eth = ipstack->getEth(); 00305 ip_addr = eth.get_ip_address(); 00306 //gateway_addr = EthernetInterface::getGateway(); 00307 00308 // Construct clientId - d:org:type:id 00309 char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; 00310 sprintf(clientId, "d:%s:%s:%s", org, type, id); 00311 00312 // Network debug statements 00313 LOG("=====================================\r\n"); 00314 LOG("Connecting Ethernet.\r\n"); 00315 LOG("IP ADDRESS: %s\r\n", ip_addr); 00316 LOG("MAC ADDRESS: %s\r\n", eth.get_mac_address()); 00317 //LOG("Gateway: %s\r\n", eth.getGateway()); 00318 //LOG("Network Mask: %s\r\n", eth.getNetworkMask()); 00319 LOG("Server Hostname: %s\r\n", hostname); 00320 LOG("Client ID: %s\r\n", clientId); 00321 LOG("=====================================\r\n"); 00322 00323 netConnecting = true; 00324 int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout); 00325 if (rc != 0) 00326 { 00327 WARN("IP Stack connect returned: %d\r\n", rc); 00328 return rc; 00329 } 00330 netConnected = true; 00331 netConnecting = false; 00332 00333 // MQTT Connect 00334 mqttConnecting = true; 00335 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00336 data.MQTTVersion = 3; 00337 data.clientID.cstring = clientId; 00338 00339 if (!quickstartMode) 00340 { 00341 data.username.cstring = "use-token-auth"; 00342 data.password.cstring = auth_token; 00343 } 00344 00345 if ((rc = client->connect(data)) == 0) 00346 { 00347 connected = true; 00348 green(); 00349 displayMessage("Connected"); 00350 wait(1); 00351 displayMessage("Scroll with joystick"); 00352 } 00353 else 00354 WARN("MQTT connect returned %d\r\n", rc); 00355 if (rc >= 0) 00356 connack_rc = rc; 00357 mqttConnecting = false; 00358 return rc; 00359 } 00360 00361 00362 int getConnTimeout(int attemptNumber) 00363 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute 00364 // after 20 attempts, retry every 10 minutes 00365 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; 00366 } 00367 00368 00369 void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) 00370 { 00371 connected = false; 00372 00373 // make sure a cable is connected before starting to connect 00374 while (!linkStatus()) 00375 { 00376 wait(1.0f); 00377 WARN("Ethernet link not present. Check cable connection\r\n"); 00378 } 00379 00380 while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) 00381 { 00382 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) 00383 return; // don't reattempt to connect if credentials are wrong 00384 00385 Thread red_thread(flashing_red); 00386 00387 int timeout = getConnTimeout(++retryAttempt); 00388 WARN("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout); 00389 00390 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed 00391 // or maybe just add the proper members to do this disconnect and call attemptConnect(...) 00392 00393 // this works - reset the system when the retry count gets to a threshold 00394 if (retryAttempt == 5) 00395 NVIC_SystemReset(); 00396 else 00397 wait(timeout); 00398 } 00399 } 00400 00401 00402 int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) 00403 { 00404 MQTT::Message message; 00405 char* pubTopic = "iot-2/evt/status/fmt/json"; 00406 00407 char buf[250]; 00408 sprintf(buf, 00409 "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}", 00410 MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read()); 00411 message.qos = MQTT::QOS0; 00412 message.retained = false; 00413 message.dup = false; 00414 message.payload = (void*)buf; 00415 message.payloadlen = strlen(buf); 00416 00417 LOG("Publishing %s\r\n", buf); 00418 return client->publish(pubTopic, message); 00419 } 00420 00421 00422 char* getMac(EthernetInterface& eth, char* buf, int buflen) // Obtain MAC address 00423 { 00424 strncpy(buf, eth.get_mac_address(), buflen); 00425 00426 char* pos; // Remove colons from mac address 00427 while ((pos = strchr(buf, ':')) != NULL) 00428 memmove(pos, pos + 1, strlen(pos) + 1); 00429 return buf; 00430 } 00431 00432 00433 void messageArrived(MQTT::MessageData& md) 00434 { 00435 MQTT::Message &message = md.message; 00436 char topic[md.topicName.lenstring.len + 1]; 00437 00438 sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); 00439 00440 LOG("Message arrived on topic %s: %.*s\r\n", topic, message.payloadlen, message.payload); 00441 00442 // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ 00443 char* start = strstr(topic, "/cmd/") + 5; 00444 int len = strstr(topic, "/fmt/") - start; 00445 00446 if (memcmp(start, "blink", len) == 0) 00447 { 00448 char payload[message.payloadlen + 1]; 00449 sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); 00450 00451 char* pos = strchr(payload, '}'); 00452 if (pos != NULL) 00453 { 00454 *pos = '\0'; 00455 if ((pos = strchr(payload, ':')) != NULL) 00456 { 00457 int blink_rate = atoi(pos + 1); 00458 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate); 00459 } 00460 } 00461 } 00462 else 00463 WARN("Unsupported command: %.*s\r\n", len, start); 00464 } 00465 00466 00467 int main() 00468 { 00469 quickstartMode = (strcmp(org, "quickstart") == 0); 00470 00471 lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen 00472 00473 led2 = LED2_OFF; // K64F: turn off the main board LED 00474 00475 displayMessage("Connecting"); 00476 Thread yellow_thread(flashing_yellow); 00477 Thread menu_thread(menu_loop); 00478 00479 LOG("***** IBM IoT Client Ethernet Example *****\r\n"); 00480 MQTTEthernet ipstack; 00481 ethernetInitialising = false; 00482 MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); 00483 LOG("Ethernet Initialized\r\n"); 00484 00485 if (quickstartMode) 00486 getMac(ipstack.getEth(), id, sizeof(id)); 00487 00488 attemptConnect(&client, &ipstack); 00489 00490 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) 00491 { 00492 red(); 00493 while (true) 00494 wait(1.0); // Permanent failures - don't retry 00495 } 00496 00497 if (!quickstartMode) 00498 { 00499 int rc = 0; 00500 if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) 00501 WARN("rc from MQTT subscribe is %d\r\n", rc); 00502 } 00503 00504 blink_interval = 0; 00505 int count = 0; 00506 while (true) 00507 { 00508 if (++count == 100) 00509 { // Publish a message every second 00510 if (publish(&client, &ipstack) != 0) 00511 attemptConnect(&client, &ipstack); // if we have lost the connection 00512 count = 0; 00513 } 00514 00515 if (blink_interval == 0) 00516 led2 = LED2_OFF; 00517 else if (count % blink_interval == 0) 00518 led2 = !led2; 00519 client.yield(10); // allow the MQTT client to receive messages 00520 } 00521 }
Generated on Thu Jul 14 2022 03:22:23 by
