IBMIoTClientEthernetExample for WIZwiki-W7500 platform
Dependencies: MQTT WIZnetInterface mbed-src
Diff: main.cpp
- Revision:
- 2:b1dcfd885d7f
- Parent:
- 0:7488a229e6fc
- Child:
- 4:167fd0256a06
--- a/main.cpp Sun Sep 06 23:54:30 2015 +0000 +++ b/main.cpp Tue Sep 15 00:43:05 2015 +0000 @@ -3,14 +3,30 @@ #include "MQTTClient.h" #include "MQTTEthernet.h" +Serial pc(USBTX, USBRX); + // Update this to the next number *before* a commit #define __APP_SW_REVISION__ "1" // Configuration values needed to connect to IBM IoT Cloud -#define ORG "pya466" // For a registered connection, replace with your org -#define ID "0008DC1D69F3" // For a registered connection, replace with your id -#define AUTH_TOKEN "RS-NqB7J72H8rwIA7Q" // For a registered connection, replace with your auth-token -#define TYPE "WIZwiki-W7500_Eric" // For a registered connection, replace with your type +#define QUICKSTARTMODE 1 +#if (QUICKSTARTMODE) + #define ORG "quickstart" // For a registered connection, replace with your org + #define ID "" // For a registered connection, replace with your id + #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token + #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type +#else + #define ORG "l24fbu" // For a registered connection, replace with your org + #define ID "0008DC1D69F3" // For a registered connection, replace with your id + #define AUTH_TOKEN "4uvlg2&uQRO-WVf_14" // For a registered connection, replace with your auth-token + #define TYPE "WIZwiki-W7500" // For a registered connection, replace with your type +#endif + + +#if defined(TARGET_WIZwiki_W7500) +//#warning "Compiling for mbed WIZwiki-W7500" +#include "WIZwiki-W7500.h" +#endif #define MQTT_PORT 1883 #define MQTT_TLS_PORT 8883 @@ -19,11 +35,15 @@ #define MQTT_MAX_PACKET_SIZE 250 #if defined(TARGET_WIZwiki_W7500) -#warning "Compiling for mbed WIZwiki-W7500" -#include "WIZwiki-W7500.h" +// PWM: RGB LED control +void off(); +void red(); +void green(); +void blue(); +//void yellow(); #endif -bool quickstartMode = true; +bool quickstartMode = QUICKSTARTMODE; char org[11] = ORG; char type[30] = TYPE; char id[30] = ID; // mac without colons @@ -32,39 +52,6 @@ bool connected = false; int blink_interval = 0; -void baud(int baudrate) -{ - Serial s(USBTX, USBRX); - s.baud(baudrate); -} - -// PWM: RGB LED control -void off() -{ - r = g = b = 1.0; // 1 is off, 0 is full brightness -} - -void red() -{ - r = 0.7; - g = 1.0; - b = 1.0; // 1 is off, 0 is full brightness -} - -void yellow() -{ - r = 0.7; - g = 0.7; - b = 1.0; // 1 is off, 0 is full brightness -} - -void green() -{ - r = 1.0; - g = 0.7; - b = 1.0; // 1 is off, 0 is full brightness -} - int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) { const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; @@ -74,8 +61,7 @@ //const char* iot_ibm = "23.246.232.210"; //char hostname[strlen(iot_ibm) + 1]; //sprintf(hostname, "%s", iot_ibm); - - //DEBUG("hostname is %s\n", hostname); + DEBUG("hostname is %s\r\n", hostname); int rc = ipstack->connect(hostname, IBM_IOT_PORT); if (rc != 0) @@ -83,8 +69,7 @@ // Construct clientId - d:org:type:id char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; - sprintf(clientId, "d:%s:%s:%s", org, type, id); - //DEBUG("clientid is %s\n", clientId); + sprintf(clientId, "d:%s:%s:%s", org, type, id); DEBUG("clientid is %s\r\n", clientId); // MQTT Connect @@ -99,8 +84,9 @@ if ((rc = client->connect(data)) == 0) { connected = true; +#if defined(TARGET_WIZwiki_W7500) green(); - //displayMessage("Connected"); +#endif printf("Connected\r\n"); wait(2); } @@ -133,8 +119,7 @@ red(); #endif int timeout = getConnTimeout(++retryAttempt); - WARN("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout); - //WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); + WARN("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout); // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed // or maybe just add the proper members to do this disconnect and call attemptConnect(...) @@ -153,20 +138,8 @@ char* pubTopic = "iot-2/evt/status/fmt/json"; char buf[250]; - /* - sprintf(buf, - "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}", - MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read()); - */ - /* - sprintf(buf, - "{\"d\":{\"myName\":\"IoT mbed\",\"temp\":%0.4f,\"light\":%0.4f}}", - ain0.read(), ain1.read()); - */ - - sprintf(buf, - "{\"d\":{\"myName\":\"IoT mbed\",\"temp\":%0.4f,\"light\":%0.4f}}", - ain0.read(), ain1.read()); + + sprintf(buf, "{\"d\":{\"myName\":\"IoT mbed\",\"potentiometer\":%0.4f}}", ain0.read()); message.qos = MQTT::QOS0; message.retained = false; @@ -214,31 +187,31 @@ } } } else - WARN("Unsupported command: %.*s\r\n", len, start); - //WARN("Unsupported command: %.*s\n", len, start); + WARN("Unsupported command: %.*s\r\n", len, start); } int main (void) { - baud(115200); - - // Board init indicator : Init success + pc.baud(115200); + +#if defined(TARGET_WIZwiki_W7500) + // Board init indicator : Init success red(); wait(0.2); - yellow(); + blue(); wait(0.2); green(); wait(0.2); off(); wait(0.2); // end of board init indicator +#endif - quickstartMode = (strcmp(org, "quickstart") == 0); - //displayMessage("Connecting"); + quickstartMode = (strcmp(org, "quickstart") == 0); printf("Connecting...\r\n"); #if defined(TARGET_WIZwiki_W7500) - yellow(); + blue(); #endif MQTTEthernet ipstack; @@ -246,7 +219,7 @@ if (quickstartMode) { getMac(ipstack.getEth(), id, sizeof(id)); - //printf("%s\r\n", id); //?? + //printf("getMac: %s\r\n", id); } attemptConnect(&client, &ipstack); @@ -255,8 +228,7 @@ { int rc = 0; if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) - WARN("rc from MQTT subscribe is %d\r\n", rc); - //WARN("rc from MQTT subscribe is %d\n", rc); + WARN("rc from MQTT subscribe is %d\r\n", rc); } blink_interval = 0; @@ -264,18 +236,58 @@ while(true) { - //if (++count == 100) - if (++count == 500) - { // Publish a message every second - - //printf("A0: %0.4f, A1: %0.4f\r\n", ain0.read(), ain1.read()); + if (++count == 100) + { // Publish a message every second + + //printf("A0: %0.4f\r\n", ain0.read()); if (publish(&client, &ipstack) != 0) attemptConnect(&client, &ipstack); // if we have lost the connection count = 0; } + + // LED Blink: By Bluemix + + if (blink_interval == 0) + off(); + else if (count % blink_interval == 0) + b = !b; + client.yield(10); // allow the MQTT client to receive messages } } +#if defined(TARGET_WIZwiki_W7500) +// PWM: RGB LED control -> DigitalOut +void off() +{ + //r = g = b = 1.0; // 1 is off, 0 is full brightness + r = g = b = 1; +} + +void red() +{ + //r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness + r = 0; g = 1; b = 1; +} + +void green() +{ + //r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness + r = 1; g = 0; b = 1; +} + +void blue() +{ + //r = 1.0; g = 1.0; b = 0.7; // 1 is off, 0 is full brightness + r = 1; g = 1; b = 0; +} + + +//void yellow() +//{ + //r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness +//} +#endif +