Code sample to be used with Bluemix for education purpose
Dependencies: C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
Diff: main.cpp
- Revision:
- 13:85801e3b83d3
- Parent:
- 12:8b480eb8a496
- Child:
- 14:1f961d19f3cf
--- a/main.cpp Thu Jul 09 14:35:57 2015 +0000 +++ b/main.cpp Wed Jul 22 13:51:22 2015 +0000 @@ -28,6 +28,8 @@ #include "C12832.h" #include "Arial12x12.h" #include "rtos.h" +#include <string> +#include <iostream> // Update this to the next number *before* a commit #define __APP_SW_REVISION__ "11" @@ -65,6 +67,10 @@ char* joystickPos = "CENTRE"; int blink_interval = 0; +char* ip_addr = ""; +char* gateway_addr = ""; +bool netConnected = false; + void off() { @@ -117,6 +123,8 @@ } + + void printMenu(int menuItem) { lcd.cls(); @@ -139,11 +147,26 @@ lcd.printf("%s", id); break; case 3: - lcd.printf("Status:"); + lcd.printf("MQTT Status:"); lcd.locate(0,16); lcd.printf(connected ? "Connected" : "Disconnected"); break; case 4: + lcd.printf("Socket State:"); + lcd.locate(0,16); + lcd.printf(netConnected ? "Connected" : "Disconnected"); + break; + case 5: + lcd.printf("IP Address:"); + lcd.locate(0,16); + lcd.printf("%s", ip_addr); + break; + case 6: + lcd.printf("Gateway:"); + lcd.locate(0,16); + lcd.printf("%s",gateway_addr); + break; + case 7: lcd.printf("App version:"); lcd.locate(0,16); lcd.printf("%s",__APP_SW_REVISION__); @@ -158,7 +181,7 @@ if (Down) { joystickPos = "DOWN"; - if (menuItem >= 0 && menuItem < 4) + if (menuItem >= 0 && menuItem < 7) printMenu(++menuItem); } else if (Left) @@ -168,7 +191,7 @@ else if (Up) { joystickPos = "UP"; - if (menuItem <= 4 && menuItem > 0) + if (menuItem <= 7 && menuItem > 0) printMenu(--menuItem); } else if (Right) @@ -177,6 +200,14 @@ joystickPos = "CENTRE"; } +void menu_loop(void const *args) +{ + while(true){ + setMenu(); + Thread::wait(100); + } +} + /** * Display a message on the LCD screen prefixed with IBM IoT Cloud @@ -198,9 +229,17 @@ char hostname[strlen(org) + strlen(iot_ibm) + 1]; sprintf(hostname, "%s%s", org, iot_ibm); DEBUG("hostname is %s\n", hostname); + EthernetInterface& eth = ipstack->getEth(); + ip_addr = eth.getIPAddress(); + gateway_addr = eth.getGateway(); int rc = ipstack->connect(hostname, IBM_IOT_PORT); - if (rc != 0) + if (rc != 0){ + WARN("IP Stack returned: %d\n", rc); return rc; + } + netConnected = true; + + // Construct clientId - d:org:type:id char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; @@ -223,8 +262,9 @@ connected = true; green(); displayMessage("Connected"); - wait(2); + wait(1); displayMessage("Scroll with joystick"); + } return rc; } @@ -343,11 +383,15 @@ led2 = LED2_OFF; // K64F: turn off the main board LED displayMessage("Connecting"); - Thread yellow_thread(flashing_yellow); + Thread yellow_thread(flashing_yellow); + Thread menu_thread(menu_loop); + printf("connecting..."); MQTTEthernet ipstack; + MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); + if (quickstartMode) { getMac(ipstack.getEth(), id, sizeof(id)); @@ -377,8 +421,8 @@ led2 = LED2_OFF; else if (count % blink_interval == 0) led2 = !led2; - if (count % 20 == 0) - setMenu(); + //if (count % 20 == 0) + // setMenu(); client.yield(10); // allow the MQTT client to receive messages } }