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: C12832 LM75B MMA7660 MQTT
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 00093 // define your own MAC Address, starting with 0x, e.g., 0x54... 00094 s[0] = 0x68; 00095 s[1] = 0xf7; 00096 s[2] = 0x28; 00097 s[3] = 0x06; 00098 s[4] = 0x02; 00099 s[5] = 0x63; 00100 00101 } 00102 */ 00103 void off() 00104 { 00105 r = g = b = 1.0; // 1 is off, 0 is full brightness 00106 } 00107 00108 void red() 00109 { 00110 r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness 00111 } 00112 00113 void yellow() 00114 { 00115 r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness 00116 } 00117 00118 void green() 00119 { 00120 r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness 00121 } 00122 00123 00124 void flashing_yellow(void const *args) 00125 { 00126 bool on = false; 00127 while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD) // flashing yellow only while connecting 00128 { 00129 on = !on; 00130 if (on) 00131 yellow(); 00132 else 00133 off(); 00134 wait(0.5); 00135 } 00136 } 00137 00138 00139 void flashing_red(void const *args) // to be used when the connection is lost 00140 { 00141 bool on = false; 00142 while (!connected) 00143 { 00144 on = !on; 00145 if (on) 00146 red(); 00147 else 00148 off(); 00149 wait(2.0); 00150 } 00151 } 00152 00153 00154 void printMenu(int menuItem) 00155 { 00156 static char last_line1[30] = "", last_line2[30] = ""; 00157 char line1[30] = "", line2[30] = ""; 00158 00159 switch (menuItem) 00160 { 00161 case 0: 00162 sprintf(line1, "IBM IoT Cloud"); 00163 sprintf(line2, "Scroll with joystick"); 00164 break; 00165 case 1: 00166 sprintf(line1, "Go to:"); 00167 sprintf(line2, "http://ibm.biz/iotqstart"); 00168 break; 00169 case 2: 00170 sprintf(line1, "Device Identity:"); 00171 sprintf(line2, "%s", id); 00172 break; 00173 case 3: 00174 sprintf(line1, "MQTT Status:"); 00175 if (mqttConnecting) 00176 sprintf(line2, "Connecting... %d/5", retryAttempt); 00177 else 00178 { 00179 if (connected) 00180 sprintf(line2, "Connected"); 00181 else 00182 { 00183 switch (connack_rc) 00184 { 00185 case MQTT_CLIENTID_REJECTED: 00186 sprintf(line2, "Clientid rejected"); 00187 break; 00188 case MQTT_BAD_USERNAME_OR_PASSWORD: 00189 sprintf(line2, "Invalid username or password"); 00190 break; 00191 case MQTT_NOT_AUTHORIZED: 00192 sprintf(line2, "Not authorized"); 00193 break; 00194 default: 00195 sprintf(line2, "Disconnected"); 00196 } 00197 } 00198 } 00199 break; 00200 case 4: 00201 sprintf(line1, "Ethernet State:"); 00202 sprintf(line2, ethernetInitialising ? "Initializing..." : "Initialized"); 00203 break; 00204 case 5: 00205 sprintf(line1, "Socket State:"); 00206 if (netConnecting) 00207 sprintf(line2, "Connecting... %d/5", retryAttempt); 00208 else 00209 sprintf(line2, netConnected ? "Connected" : "Disconnected"); 00210 break; 00211 case 6: 00212 sprintf(line1, "IP Address:"); 00213 sprintf(line2, "%s", ip_addr); 00214 break; 00215 case 7: 00216 sprintf(line1, "Gateway:"); 00217 sprintf(line2, "%s", gateway_addr); 00218 break; 00219 case 8: 00220 sprintf(line1, "App version:"); 00221 sprintf(line2, "%s", __APP_SW_REVISION__); 00222 break; 00223 case 9: 00224 sprintf(line1, "Current Timeout:"); 00225 sprintf(line2, "%d ms", connectTimeout); 00226 break; 00227 } 00228 00229 if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0) 00230 { 00231 lcd.cls(); 00232 lcd.locate(0, 0); 00233 lcd.printf(line1); 00234 strncpy(last_line1, line1, sizeof(last_line1)); 00235 00236 lcd.locate(0,16); 00237 lcd.printf(line2); 00238 strncpy(last_line2, line2, sizeof(last_line2)); 00239 } 00240 } 00241 00242 00243 void setMenu() 00244 { 00245 00246 if (Down) 00247 { 00248 joystickPos = "DOWN"; 00249 if (menuItem >= 0 && menuItem < 9) 00250 printMenu(++menuItem); 00251 } 00252 else if (Left) 00253 joystickPos = "LEFT"; 00254 else if (Click) 00255 joystickPos = "CLICK"; 00256 else if (Up) 00257 { 00258 joystickPos = "UP"; 00259 if (menuItem <= 9 && menuItem > 0) 00260 printMenu(--menuItem); 00261 } 00262 else if (Right) 00263 joystickPos = "RIGHT"; 00264 else 00265 joystickPos = "CENTRE"; 00266 } 00267 00268 void menu_loop(void const *args) 00269 { 00270 int count = 0; 00271 while(true) 00272 { 00273 setMenu(); 00274 if (++count % 10 == 0) 00275 printMenu(menuItem); 00276 Thread::wait(100); 00277 } 00278 } 00279 00280 00281 /** 00282 * Display a message on the LCD screen prefixed with IBM IoT Cloud 00283 */ 00284 void displayMessage(char* message) 00285 { 00286 lcd.cls(); 00287 lcd.locate(0,0); 00288 lcd.printf("IBM IoT Cloud"); 00289 lcd.locate(0,16); 00290 lcd.printf(message); 00291 } 00292 00293 00294 int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) 00295 { 00296 const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; 00297 00298 char hostname[strlen(org) + strlen(iot_ibm) + 1]; 00299 sprintf(hostname, "%s%s", org, iot_ibm); 00300 EthernetInterface& eth = ipstack->getEth(); 00301 ip_addr = eth.get_ip_address(); 00302 //gateway_addr = EthernetInterface::getGateway(); 00303 00304 // Construct clientId - d:org:type:id 00305 char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; 00306 sprintf(clientId, "d:%s:%s:%s", org, type, id); 00307 00308 // Network debug statements 00309 LOG("=====================================\r\n"); 00310 LOG("Connecting Ethernet.\r\n"); 00311 LOG("IP ADDRESS: %s\r\n", ip_addr); 00312 LOG("MAC ADDRESS: %s\r\n", eth.get_mac_address()); 00313 //LOG("Gateway: %s\r\n", eth.getGateway()); 00314 //LOG("Network Mask: %s\r\n", eth.getNetworkMask()); 00315 LOG("Server Hostname: %s\r\n", hostname); 00316 LOG("Client ID: %s\r\n", clientId); 00317 LOG("=====================================\r\n"); 00318 00319 netConnecting = true; 00320 int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout); 00321 if (rc != 0) 00322 { 00323 WARN("IP Stack connect returned: %d\r\n", rc); 00324 return rc; 00325 } 00326 netConnected = true; 00327 netConnecting = false; 00328 00329 // MQTT Connect 00330 mqttConnecting = true; 00331 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00332 data.MQTTVersion = 3; 00333 data.clientID.cstring = clientId; 00334 00335 if (!quickstartMode) 00336 { 00337 data.username.cstring = "use-token-auth"; 00338 data.password.cstring = auth_token; 00339 } 00340 00341 if ((rc = client->connect(data)) == 0) 00342 { 00343 connected = true; 00344 green(); 00345 displayMessage("Connected"); 00346 wait(1); 00347 displayMessage("Scroll with joystick"); 00348 } 00349 else 00350 WARN("MQTT connect returned %d\r\n", rc); 00351 if (rc >= 0) 00352 connack_rc = rc; 00353 mqttConnecting = false; 00354 return rc; 00355 } 00356 00357 00358 int getConnTimeout(int attemptNumber) 00359 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute 00360 // after 20 attempts, retry every 10 minutes 00361 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; 00362 } 00363 00364 00365 void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) 00366 { 00367 connected = false; 00368 00369 // make sure a cable is connected before starting to connect 00370 while (!linkStatus()) 00371 { 00372 wait(1.0f); 00373 WARN("Ethernet link not present. Check cable connection\r\n"); 00374 } 00375 00376 while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) 00377 { 00378 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) 00379 return; // don't reattempt to connect if credentials are wrong 00380 00381 Thread red_thread(flashing_red); 00382 00383 int timeout = getConnTimeout(++retryAttempt); 00384 WARN("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout); 00385 00386 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed 00387 // or maybe just add the proper members to do this disconnect and call attemptConnect(...) 00388 00389 // this works - reset the system when the retry count gets to a threshold 00390 if (retryAttempt == 5) 00391 NVIC_SystemReset(); 00392 else 00393 wait(timeout); 00394 } 00395 } 00396 00397 00398 int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) 00399 { 00400 MQTT::Message message; 00401 char* pubTopic = "iot-2/evt/status/fmt/json"; 00402 00403 char buf[250]; 00404 sprintf(buf, 00405 "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}", 00406 MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read()); 00407 message.qos = MQTT::QOS0; 00408 message.retained = false; 00409 message.dup = false; 00410 message.payload = (void*)buf; 00411 message.payloadlen = strlen(buf); 00412 00413 LOG("Publishing %s\r\n", buf); 00414 return client->publish(pubTopic, message); 00415 } 00416 00417 00418 char* getMac(EthernetInterface& eth, char* buf, int buflen) // Obtain MAC address 00419 { 00420 strncpy(buf, eth.get_mac_address(), buflen); 00421 00422 char* pos; // Remove colons from mac address 00423 while ((pos = strchr(buf, ':')) != NULL) 00424 memmove(pos, pos + 1, strlen(pos) + 1); 00425 return buf; 00426 } 00427 00428 00429 void messageArrived(MQTT::MessageData& md) 00430 { 00431 MQTT::Message &message = md.message; 00432 char topic[md.topicName.lenstring.len + 1]; 00433 00434 sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); 00435 00436 LOG("Message arrived on topic %s: %.*s\r\n", topic, message.payloadlen, message.payload); 00437 00438 // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ 00439 char* start = strstr(topic, "/cmd/") + 5; 00440 int len = strstr(topic, "/fmt/") - start; 00441 00442 if (memcmp(start, "blink", len) == 0) 00443 { 00444 char payload[message.payloadlen + 1]; 00445 sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); 00446 00447 char* pos = strchr(payload, '}'); 00448 if (pos != NULL) 00449 { 00450 *pos = '\0'; 00451 if ((pos = strchr(payload, ':')) != NULL) 00452 { 00453 int blink_rate = atoi(pos + 1); 00454 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate); 00455 } 00456 } 00457 } 00458 else 00459 WARN("Unsupported command: %.*s\r\n", len, start); 00460 } 00461 00462 00463 int main() 00464 { 00465 quickstartMode = (strcmp(org, "quickstart") == 0); 00466 00467 lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen 00468 00469 led2 = LED2_OFF; // K64F: turn off the main board LED 00470 00471 displayMessage("Connecting"); 00472 Thread yellow_thread(flashing_yellow); 00473 Thread menu_thread(menu_loop); 00474 00475 LOG("***** IBM IoT Client Ethernet Example *****\r\n"); 00476 MQTTEthernet ipstack; 00477 ethernetInitialising = false; 00478 MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); 00479 LOG("Ethernet Initialized\r\n"); 00480 00481 if (quickstartMode) 00482 getMac(ipstack.getEth(), id, sizeof(id)); 00483 00484 attemptConnect(&client, &ipstack); 00485 00486 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) 00487 { 00488 red(); 00489 while (true) 00490 wait(1.0); // Permanent failures - don't retry 00491 } 00492 00493 if (!quickstartMode) 00494 { 00495 int rc = 0; 00496 if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) 00497 WARN("rc from MQTT subscribe is %d\r\n", rc); 00498 } 00499 00500 blink_interval = 0; 00501 int count = 0; 00502 while (true) 00503 { 00504 if (++count == 100) 00505 { // Publish a message every second 00506 if (publish(&client, &ipstack) != 0) 00507 attemptConnect(&client, &ipstack); // if we have lost the connection 00508 count = 0; 00509 } 00510 00511 if (blink_interval == 0) 00512 led2 = LED2_OFF; 00513 else if (count % blink_interval == 0) 00514 led2 = !led2; 00515 client.yield(10); // allow the MQTT client to receive messages 00516 } 00517 }
Generated on Thu Jul 28 2022 16:42:17 by
1.7.2