Bluemix Demo program
Dependencies: WNCInterface mbed-rtos mbed
Diff: main.cpp
- Revision:
- 1:b3de2b183932
- Parent:
- 0:6a929f0d0e58
--- a/main.cpp Wed Sep 28 00:56:47 2016 +0000 +++ b/main.cpp Fri Sep 30 19:37:09 2016 +0000 @@ -1,10 +1,10 @@ //#define MQTT_DEBUG + #include "mbed.h" #include "MQTTClient.h" #include "MQTTFormat.h" -//#include "MQTTEthernet.h" #include "MQTTWNCInterface.h" #include "rtos.h" #include "k64f.h" @@ -14,57 +14,32 @@ MODSERIAL pc(USBTX,USBRX,256,256); #include "hardware.h" - +// +// When the Bluemix device is created, the following information is provided: +// +// Organization ID 9k09br +// Device Type ATTIOTK +// Device ID Mz-11027214-7010 +// Authentication Method token +// Authentication Token SecurityToken99 +// Use this information for the following defines: -// connect options for MQTT broker -#define CLIENT "quickstart" -#define URL CLIENT ".messaging.internetofthings.ibmcloud.com" -#define CONFIGTYPE "d:" CLIENT ":MicroZed:%s" +#define ORG_ID "k1hdoc" +#define DEVICE_TYPE "Avnet-WNC_card" +#define DEVICE_NAME "IoT-11027214-2016" +#define USERNAME "use-token-auth" // not required for demo app +#define PASSWORD "SecurityToken99" // not required for demo app + +#define URL ORG_ID ".messaging.internetofthings.ibmcloud.com" +#define CLIENTSTR "d:" ORG_ID ":" DEVICE_TYPE ":%s" #define PORT 1883 // MQTT broker port number -#define CLIENTID "96430312d8f7" // use MAC address without colons -#define USERNAME "" // not required for demo app -#define PASSWORD "" // not required for demo app #define PUBLISH_TOPIC "iot-2/evt/status/fmt/json" // MQTT topic #define SUBSCRIBTOPIC "iot-2/cmd/+/fmt/+" -#define HTS221_STR " Temp is: %0.2f F \n\r Humid is: %02d %%\n\r" Queue<uint32_t, 6> messageQ; -struct rcvd_errs{ - int err; - char *er; - }; - -rcvd_errs response[] = { - 200, "200 OK - Request has succeeded.", - 201, "201 Created - Request has been fulfilled and a new resource created.", - 202, "202 Accepted - The request has been accepted for processing, but the processing will be completed asynchronously", - 204, "204 No Content - The server has fulfilled the request but does not need to return an entity-body.", - 400, "400 Bad Request - Bad request (e.g. sending an array when the API is expecting a hash.)", - 401, "401 Unauthorized - No valid API key provided.", - 403, "403 Forbidden - You tried to access a disabled device, or your API key is not allowed to access that resource, etc.", - 404, "404 Not Found - The requested item could not be found.", - 405, "405 Method Not Allowed - The HTTP method specified is not allowed.", - 415, "415 Unsupported Media Type - The requested media type is currently not supported.", - 422, "422 Unprocessable Entity - Can result from sending invalid fields.", - 429, "429 Too Many Requests - The user has sent too many requests in a given period of time.", - 500, "500 Server errors - Something went wrong in the M2X server", - 502, "502 Server errors - Something went wrong in the M2X server", - 503, "503 Server errors - Something went wrong in the M2X server", - 504, "504 Server errors - Something went wrong in the M2X server", - }; -#define RCMAX sizeof(response)/sizeof(rcvd_errs) - -char * response_str(int rc) { - static char *unkown = "Unknown error code..."; - int i=0; - while( response[i].err != rc && i < RCMAX) - i++; - return (i<RCMAX? response[i].er : unkown); -} - // LED color control function void controlLED(color_t led_color) { switch(led_color) { @@ -99,7 +74,7 @@ // MQTT message arrived callback function void messageArrived(MQTT::MessageData& md) { MQTT::Message &message = md.message; - printf("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); + PRINTF("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); if (message.payloadlen == 3) { if (strncmp((char*)message.payload, "red", 3) == 0) @@ -117,26 +92,26 @@ } int main() { - int rc, qStart=0, txSel=0, good = 0; + int rc, pSW2=0, txSel=0, good = 0; Timer tmr; char* topic = PUBLISH_TOPIC; - char clientID[100], buf[100], uniqueID[50]; + char clientID[100], buf[100]; + string st, uniqueID; HTS221 hts221; pc.baud(115200); rc = hts221.init(); if ( rc ) { - pc.printf(BLU "HTS221 Detected (0x%02X)\n\r",rc); - pc.printf(HTS221_STR, CTOF(hts221.readTemperature()), hts221.readHumidity()/10); + PRINTF(BLU "HTS221 Detected (0x%02X)\n\r",rc); + PRINTF(" Temp is: %0.2f F \r\n Huumid is: %02d %%\r\n\r\n", + CTOF(hts221.readTemperature()), hts221.readHumidity()/10); } else { - pc.printf(RED "HTS221 NOT DETECTED!\n\r"); + PRINTF(RED "HTS221 NOT DETECTED!\n\r"); } - - // turn off LED - controlLED(blue); + controlLED(green); // set SW2 and SW3 to generate interrupt on falling edge switch2.fall(&sw2_ISR); @@ -150,37 +125,44 @@ // construct the MQTT client MQTT::Client<MQTTwnc, Countdown> client = MQTT::Client<MQTTwnc, Countdown>(ipstack); + + controlLED(blue); char* hostname = URL; int port = PORT; - char *tptr = eth.getMACAddress(); - strncpy(buf,tptr,strlen(tptr)); - for( int x=3, i=2; i<strlen(tptr)-2; x+=3,i++ ){ - buf[i] = buf[x]; - buf[x]=buf[x+1]; - } - sprintf(uniqueID, "Mz-%s-7010", buf); - sprintf(clientID, CONFIGTYPE, uniqueID); - printf("ClientID='%s'\r\n",clientID); + st = eth.getMACAddress(); + uniqueID="IoT-"; + uniqueID += st[0]; + uniqueID += st[1]; + uniqueID += st[3]; + uniqueID += st[4]; + uniqueID += st[6]; + uniqueID += st[7]; + uniqueID += st[9]; + uniqueID += st[10]; + uniqueID += "-2016"; + + sprintf(clientID, CLIENTSTR, uniqueID.c_str()); - printf(" _____\r\n"); - printf(" * *\r\n"); - printf(" *____ *____ Bluemix IIoT Demo using\r\n"); - printf(" * *===* *==* the AT&T IoT Starter Kit\r\n"); - printf(" *___*===*___** AVNET\r\n"); - printf(" *======*\r\n"); - printf(" *====*\r\n"); - printf("\r\n"); - printf("This demonstration program operates the same as the original \r\n"); - printf("MicroZed IIoT Starter Kit except it only reads from the HTS221 \r\n"); - printf("temp sensor (no 31855 currently present and no generated data).\r\n"); - printf("\r\n"); - printf("Local network info...\r\n"); - printf("IP address is %s\r\n", eth.getIPAddress()); - printf("MAC address is %s\r\n", eth.getMACAddress()); - printf("Gateway address is %s\r\n", eth.getGateway()); - printf("Your <uniqueID> is: %s\r\n", uniqueID); - printf("---------------------------------------------------------------\r\n"); + PRINTF("\r\n\r\n"); + PRINTF(" _____\r\n"); + PRINTF(" * *\r\n"); + PRINTF(" *____ *____ Bluemix IIoT Demo using\r\n"); + PRINTF(" * *===* *==* the AT&T IoT Starter Kit\r\n"); + PRINTF(" *___*===*___** AVNET\r\n"); + PRINTF(" *======*\r\n"); + PRINTF(" *====*\r\n"); + PRINTF("\r\n"); + PRINTF("This demonstration program operates the same as the original \r\n"); + PRINTF("MicroZed IIoT Starter Kit except it only reads from the HTS221 \r\n"); + PRINTF("temp sensor (no 31855 currently present and no generated data).\r\n"); + PRINTF("\r\n"); + PRINTF("Local network info...\r\n"); + PRINTF("IP address is %s\r\n", eth.getIPAddress()); + PRINTF("MAC address is %s\r\n", eth.getMACAddress()); + PRINTF("Gateway address is %s\r\n", eth.getGateway()); + PRINTF("Your <uniqueID> is: %s\r\n", uniqueID.c_str()); + PRINTF("---------------------------------------------------------------\r\n"); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; @@ -192,15 +174,15 @@ tmr.start(); rc = 1; while( rc && tries < 3) { - printf("\r\n\r\n(%d) Attempting TCP connect to %s:%d: ", tries++, hostname, port); + PRINTF("\r\n\r\n(%d) Attempting TCP connect to %s:%d: ", tries++, hostname, port); rc = ipstack.connect(hostname, port); if( rc ) { - printf("Failed (%d)!\r\n",rc); + PRINTF("Failed (%d)!\r\n",rc); while( tmr.read_ms() < 5000 ) ; tmr.reset(); } else { - printf("Success!\r\n"); + PRINTF("Success!\r\n"); rc = 0; } } @@ -213,23 +195,23 @@ data.MQTTVersion = 3; data.clientID.cstring = clientID; - // data.username.cstring = USERNAME; - // data.password.cstring = PASSWORD; + data.username.cstring = USERNAME; + data.password.cstring = PASSWORD; data.keepAliveInterval = 10; data.cleansession = 1; rc = 1; tmr.reset(); while( !client.isConnected() && rc && tries < 3) { - printf("(%d) Attempting MQTT connect to %s:%d: ", tries++, hostname, port); + PRINTF("(%d) Attempting MQTT connect to '%s': ", tries++, clientID); rc = client.connect(data); if( rc ) { - printf("Failed (%d)!\r\n",rc); + PRINTF("Failed (%d)!\r\n",rc); while( tmr.read_ms() < 5000 ); tmr.reset(); } else - printf("Success!\r\n"); + PRINTF("Success!\r\n"); } if( tries < 3 ) @@ -237,28 +219,23 @@ else continue; -#if 0 -//Only need to subscribe if we are not running quickstart // subscribe to MQTT topic tmr.reset(); rc = 1; while( rc && client.isConnected() && tries < 3) { - printf("(%d) Attempting to subscribing to MQTT topic %s: ", tries, SUBSCRIBTOPIC); + PRINTF("(%d) Attempting to subscribing to MQTT topic '%s': ", tries, SUBSCRIBTOPIC); rc = client.subscribe(SUBSCRIBTOPIC, MQTT::QOS0, messageArrived); if( rc ) { - printf("Failed (%d)!\r\n", rc); + PRINTF("Failed (%d)!\r\n", rc); while( tmr.read_ms() < 5000 ); tries++; tmr.reset(); } else { good=1; - printf("Subscribe successful!\r\n"); + PRINTF("Subscribe successful!\r\n"); } } -#else - good = 1; -#endif while (!good); } @@ -275,23 +252,23 @@ txSel = !txSel; if( switchEvent.value.v == 33) //user wants to run in Quickstart of Demo mode - qStart = !qStart; + pSW2 = !pSW2; memset(buf,0x00,sizeof(buf)); if( txSel ) { rc = hts221.readHumidity(); sprintf(buf, "{\"d\" : {\"humd\" : \"%2d.%d\" }}", rc/10, rc-((rc/10)*10)); - printf("Publishing MQTT message '%s' ", (char*)message.payload); + PRINTF("Publishing MQTT message '%s' ", (char*)message.payload); } else { - sprintf(buf, "{\"d\" : {\"temp\" : \"%5.1f\" }}", CTOF(hts221.readTemperature())); - printf("Publishing MQTT message '%s' ", (char*)message.payload); + sprintf(buf, "{\"d\" : {\"temp\" : %5d }}", (int)CTOF(hts221.readTemperature())*10); + PRINTF("Publishing MQTT message '%s' ", (char*)message.payload); } message.payloadlen = strlen(buf); - printf("(%d)\r\n",message.payloadlen); + PRINTF("(%d)\r\n",message.payloadlen); rc = client.publish(topic, message); if( rc ) { - printf("Publish request failed! (%d)\r\n",rc); + PRINTF("Publish request failed! (%d)\r\n",rc); FATAL_WNC_ERROR(resume); }