Bluemix Demo program

Dependencies:   WNCInterface mbed-rtos mbed

Committer:
root@developer-sjc-cyan-compiler.local.mbed.org
Date:
Thu Nov 17 18:26:24 2016 +0000
Revision:
3:4b91766a3817
Parent:
1:b3de2b183932
Added tag att_cellular_K64_wnc_14A2A_20161117 for changeset 5fc069c70129

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:6a929f0d0e58 1 //#define MQTT_DEBUG
JMF 0:6a929f0d0e58 2
JMF 1:b3de2b183932 3
JMF 0:6a929f0d0e58 4 #include "mbed.h"
JMF 0:6a929f0d0e58 5 #include "MQTTClient.h"
JMF 0:6a929f0d0e58 6 #include "MQTTFormat.h"
JMF 0:6a929f0d0e58 7
JMF 0:6a929f0d0e58 8 #include "MQTTWNCInterface.h"
JMF 0:6a929f0d0e58 9 #include "rtos.h"
JMF 0:6a929f0d0e58 10 #include "k64f.h"
JMF 0:6a929f0d0e58 11 #include "HTS221.h"
JMF 0:6a929f0d0e58 12
JMF 0:6a929f0d0e58 13 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used
JMF 0:6a929f0d0e58 14 MODSERIAL pc(USBTX,USBRX,256,256);
JMF 0:6a929f0d0e58 15
JMF 0:6a929f0d0e58 16 #include "hardware.h"
JMF 1:b3de2b183932 17 //
JMF 1:b3de2b183932 18 // When the Bluemix device is created, the following information is provided:
JMF 1:b3de2b183932 19 //
JMF 1:b3de2b183932 20 // Organization ID 9k09br
JMF 1:b3de2b183932 21 // Device Type ATTIOTK
JMF 1:b3de2b183932 22 // Device ID Mz-11027214-7010
JMF 1:b3de2b183932 23 // Authentication Method token
JMF 1:b3de2b183932 24 // Authentication Token SecurityToken99
JMF 1:b3de2b183932 25 // Use this information for the following defines:
JMF 0:6a929f0d0e58 26
JMF 1:b3de2b183932 27 #define ORG_ID "k1hdoc"
JMF 1:b3de2b183932 28 #define DEVICE_TYPE "Avnet-WNC_card"
JMF 1:b3de2b183932 29 #define DEVICE_NAME "IoT-11027214-2016"
JMF 1:b3de2b183932 30 #define USERNAME "use-token-auth" // not required for demo app
JMF 1:b3de2b183932 31 #define PASSWORD "SecurityToken99" // not required for demo app
JMF 1:b3de2b183932 32
JMF 1:b3de2b183932 33 #define URL ORG_ID ".messaging.internetofthings.ibmcloud.com"
JMF 1:b3de2b183932 34 #define CLIENTSTR "d:" ORG_ID ":" DEVICE_TYPE ":%s"
JMF 0:6a929f0d0e58 35
JMF 0:6a929f0d0e58 36 #define PORT 1883 // MQTT broker port number
JMF 0:6a929f0d0e58 37 #define PUBLISH_TOPIC "iot-2/evt/status/fmt/json" // MQTT topic
JMF 0:6a929f0d0e58 38 #define SUBSCRIBTOPIC "iot-2/cmd/+/fmt/+"
JMF 0:6a929f0d0e58 39
JMF 0:6a929f0d0e58 40
JMF 0:6a929f0d0e58 41 Queue<uint32_t, 6> messageQ;
JMF 0:6a929f0d0e58 42
JMF 0:6a929f0d0e58 43 // LED color control function
JMF 0:6a929f0d0e58 44 void controlLED(color_t led_color) {
JMF 0:6a929f0d0e58 45 switch(led_color) {
JMF 0:6a929f0d0e58 46 case red :
JMF 0:6a929f0d0e58 47 greenLED = blueLED = 1;
JMF 0:6a929f0d0e58 48 redLED = 0.7;
JMF 0:6a929f0d0e58 49 break;
JMF 0:6a929f0d0e58 50 case green :
JMF 0:6a929f0d0e58 51 redLED = blueLED = 1;
JMF 0:6a929f0d0e58 52 greenLED = 0.7;
JMF 0:6a929f0d0e58 53 break;
JMF 0:6a929f0d0e58 54 case blue :
JMF 0:6a929f0d0e58 55 redLED = greenLED = 1;
JMF 0:6a929f0d0e58 56 blueLED = 0.7;
JMF 0:6a929f0d0e58 57 break;
JMF 0:6a929f0d0e58 58 case off :
JMF 0:6a929f0d0e58 59 redLED = greenLED = blueLED = 1;
JMF 0:6a929f0d0e58 60 break;
JMF 0:6a929f0d0e58 61 }
JMF 0:6a929f0d0e58 62 }
JMF 0:6a929f0d0e58 63
JMF 0:6a929f0d0e58 64 // Switch 2 interrupt handler
JMF 0:6a929f0d0e58 65 void sw2_ISR(void) {
JMF 0:6a929f0d0e58 66 messageQ.put((uint32_t*)22);
JMF 0:6a929f0d0e58 67 }
JMF 0:6a929f0d0e58 68
JMF 0:6a929f0d0e58 69 // Switch3 interrupt handler
JMF 0:6a929f0d0e58 70 void sw3_ISR(void) {
JMF 0:6a929f0d0e58 71 messageQ.put((uint32_t*)33);
JMF 0:6a929f0d0e58 72 }
JMF 0:6a929f0d0e58 73
JMF 0:6a929f0d0e58 74 // MQTT message arrived callback function
JMF 0:6a929f0d0e58 75 void messageArrived(MQTT::MessageData& md) {
JMF 0:6a929f0d0e58 76 MQTT::Message &message = md.message;
JMF 1:b3de2b183932 77 PRINTF("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload);
JMF 0:6a929f0d0e58 78
JMF 0:6a929f0d0e58 79 if (message.payloadlen == 3) {
JMF 0:6a929f0d0e58 80 if (strncmp((char*)message.payload, "red", 3) == 0)
JMF 0:6a929f0d0e58 81 controlLED(red);
JMF 0:6a929f0d0e58 82
JMF 0:6a929f0d0e58 83 else if(strncmp((char*)message.payload, "grn", 3) == 0)
JMF 0:6a929f0d0e58 84 controlLED(green);
JMF 0:6a929f0d0e58 85
JMF 0:6a929f0d0e58 86 else if(strncmp((char*)message.payload, "blu", 3) == 0)
JMF 0:6a929f0d0e58 87 controlLED(blue);
JMF 0:6a929f0d0e58 88
JMF 0:6a929f0d0e58 89 else if(strncmp((char*)message.payload, "off", 3) == 0)
JMF 0:6a929f0d0e58 90 controlLED(off);
JMF 0:6a929f0d0e58 91 }
JMF 0:6a929f0d0e58 92 }
JMF 0:6a929f0d0e58 93
JMF 0:6a929f0d0e58 94 int main() {
JMF 1:b3de2b183932 95 int rc, pSW2=0, txSel=0, good = 0;
JMF 0:6a929f0d0e58 96 Timer tmr;
JMF 0:6a929f0d0e58 97 char* topic = PUBLISH_TOPIC;
JMF 1:b3de2b183932 98 char clientID[100], buf[100];
JMF 1:b3de2b183932 99 string st, uniqueID;
JMF 0:6a929f0d0e58 100
JMF 0:6a929f0d0e58 101 HTS221 hts221;
JMF 0:6a929f0d0e58 102
JMF 0:6a929f0d0e58 103 pc.baud(115200);
JMF 0:6a929f0d0e58 104 rc = hts221.init();
JMF 0:6a929f0d0e58 105 if ( rc ) {
JMF 1:b3de2b183932 106 PRINTF(BLU "HTS221 Detected (0x%02X)\n\r",rc);
JMF 1:b3de2b183932 107 PRINTF(" Temp is: %0.2f F \r\n Huumid is: %02d %%\r\n\r\n",
JMF 1:b3de2b183932 108 CTOF(hts221.readTemperature()), hts221.readHumidity()/10);
JMF 0:6a929f0d0e58 109 }
JMF 0:6a929f0d0e58 110 else {
JMF 1:b3de2b183932 111 PRINTF(RED "HTS221 NOT DETECTED!\n\r");
JMF 0:6a929f0d0e58 112 }
JMF 0:6a929f0d0e58 113
JMF 1:b3de2b183932 114 controlLED(green);
JMF 0:6a929f0d0e58 115
JMF 0:6a929f0d0e58 116 // set SW2 and SW3 to generate interrupt on falling edge
JMF 0:6a929f0d0e58 117 switch2.fall(&sw2_ISR);
JMF 0:6a929f0d0e58 118 switch3.fall(&sw3_ISR);
JMF 0:6a929f0d0e58 119
JMF 0:6a929f0d0e58 120 // initialize ethernet interface
JMF 0:6a929f0d0e58 121 MQTTwnc ipstack = MQTTwnc();
JMF 0:6a929f0d0e58 122
JMF 0:6a929f0d0e58 123 // get and display client network info
JMF 0:6a929f0d0e58 124 WNCInterface& eth = ipstack.getEth();
JMF 0:6a929f0d0e58 125
JMF 0:6a929f0d0e58 126 // construct the MQTT client
JMF 0:6a929f0d0e58 127 MQTT::Client<MQTTwnc, Countdown> client = MQTT::Client<MQTTwnc, Countdown>(ipstack);
JMF 1:b3de2b183932 128
JMF 1:b3de2b183932 129 controlLED(blue);
JMF 0:6a929f0d0e58 130
JMF 0:6a929f0d0e58 131 char* hostname = URL;
JMF 0:6a929f0d0e58 132 int port = PORT;
JMF 1:b3de2b183932 133 st = eth.getMACAddress();
JMF 1:b3de2b183932 134 uniqueID="IoT-";
JMF 1:b3de2b183932 135 uniqueID += st[0];
JMF 1:b3de2b183932 136 uniqueID += st[1];
JMF 1:b3de2b183932 137 uniqueID += st[3];
JMF 1:b3de2b183932 138 uniqueID += st[4];
JMF 1:b3de2b183932 139 uniqueID += st[6];
JMF 1:b3de2b183932 140 uniqueID += st[7];
JMF 1:b3de2b183932 141 uniqueID += st[9];
JMF 1:b3de2b183932 142 uniqueID += st[10];
JMF 1:b3de2b183932 143 uniqueID += "-2016";
JMF 1:b3de2b183932 144
JMF 1:b3de2b183932 145 sprintf(clientID, CLIENTSTR, uniqueID.c_str());
JMF 0:6a929f0d0e58 146
JMF 1:b3de2b183932 147 PRINTF("\r\n\r\n");
JMF 1:b3de2b183932 148 PRINTF(" _____\r\n");
JMF 1:b3de2b183932 149 PRINTF(" * *\r\n");
JMF 1:b3de2b183932 150 PRINTF(" *____ *____ Bluemix IIoT Demo using\r\n");
JMF 1:b3de2b183932 151 PRINTF(" * *===* *==* the AT&T IoT Starter Kit\r\n");
JMF 1:b3de2b183932 152 PRINTF(" *___*===*___** AVNET\r\n");
JMF 1:b3de2b183932 153 PRINTF(" *======*\r\n");
JMF 1:b3de2b183932 154 PRINTF(" *====*\r\n");
JMF 1:b3de2b183932 155 PRINTF("\r\n");
JMF 1:b3de2b183932 156 PRINTF("This demonstration program operates the same as the original \r\n");
JMF 1:b3de2b183932 157 PRINTF("MicroZed IIoT Starter Kit except it only reads from the HTS221 \r\n");
JMF 1:b3de2b183932 158 PRINTF("temp sensor (no 31855 currently present and no generated data).\r\n");
JMF 1:b3de2b183932 159 PRINTF("\r\n");
JMF 1:b3de2b183932 160 PRINTF("Local network info...\r\n");
JMF 1:b3de2b183932 161 PRINTF("IP address is %s\r\n", eth.getIPAddress());
JMF 1:b3de2b183932 162 PRINTF("MAC address is %s\r\n", eth.getMACAddress());
JMF 1:b3de2b183932 163 PRINTF("Gateway address is %s\r\n", eth.getGateway());
JMF 1:b3de2b183932 164 PRINTF("Your <uniqueID> is: %s\r\n", uniqueID.c_str());
JMF 1:b3de2b183932 165 PRINTF("---------------------------------------------------------------\r\n");
JMF 0:6a929f0d0e58 166
JMF 0:6a929f0d0e58 167 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
JMF 0:6a929f0d0e58 168
JMF 0:6a929f0d0e58 169 int tries;
JMF 0:6a929f0d0e58 170
JMF 0:6a929f0d0e58 171 while( !good ) {
JMF 0:6a929f0d0e58 172 tries=0;
JMF 0:6a929f0d0e58 173 // connect to TCP socket and check return code
JMF 0:6a929f0d0e58 174 tmr.start();
JMF 0:6a929f0d0e58 175 rc = 1;
JMF 0:6a929f0d0e58 176 while( rc && tries < 3) {
JMF 1:b3de2b183932 177 PRINTF("\r\n\r\n(%d) Attempting TCP connect to %s:%d: ", tries++, hostname, port);
JMF 0:6a929f0d0e58 178 rc = ipstack.connect(hostname, port);
JMF 0:6a929f0d0e58 179 if( rc ) {
JMF 1:b3de2b183932 180 PRINTF("Failed (%d)!\r\n",rc);
JMF 0:6a929f0d0e58 181 while( tmr.read_ms() < 5000 ) ;
JMF 0:6a929f0d0e58 182 tmr.reset();
JMF 0:6a929f0d0e58 183 }
JMF 0:6a929f0d0e58 184 else {
JMF 1:b3de2b183932 185 PRINTF("Success!\r\n");
JMF 0:6a929f0d0e58 186 rc = 0;
JMF 0:6a929f0d0e58 187 }
JMF 0:6a929f0d0e58 188 }
JMF 0:6a929f0d0e58 189 if( tries < 3 )
JMF 0:6a929f0d0e58 190 tries = 0;
JMF 0:6a929f0d0e58 191 else
JMF 0:6a929f0d0e58 192 continue;
JMF 0:6a929f0d0e58 193
JMF 0:6a929f0d0e58 194 data.willFlag = 0;
JMF 0:6a929f0d0e58 195 data.MQTTVersion = 3;
JMF 0:6a929f0d0e58 196
JMF 0:6a929f0d0e58 197 data.clientID.cstring = clientID;
JMF 1:b3de2b183932 198 data.username.cstring = USERNAME;
JMF 1:b3de2b183932 199 data.password.cstring = PASSWORD;
JMF 0:6a929f0d0e58 200 data.keepAliveInterval = 10;
JMF 0:6a929f0d0e58 201 data.cleansession = 1;
JMF 0:6a929f0d0e58 202
JMF 0:6a929f0d0e58 203 rc = 1;
JMF 0:6a929f0d0e58 204 tmr.reset();
JMF 0:6a929f0d0e58 205 while( !client.isConnected() && rc && tries < 3) {
JMF 1:b3de2b183932 206 PRINTF("(%d) Attempting MQTT connect to '%s': ", tries++, clientID);
JMF 0:6a929f0d0e58 207 rc = client.connect(data);
JMF 0:6a929f0d0e58 208 if( rc ) {
JMF 1:b3de2b183932 209 PRINTF("Failed (%d)!\r\n",rc);
JMF 0:6a929f0d0e58 210 while( tmr.read_ms() < 5000 );
JMF 0:6a929f0d0e58 211 tmr.reset();
JMF 0:6a929f0d0e58 212 }
JMF 0:6a929f0d0e58 213 else
JMF 1:b3de2b183932 214 PRINTF("Success!\r\n");
JMF 0:6a929f0d0e58 215 }
JMF 0:6a929f0d0e58 216
JMF 0:6a929f0d0e58 217 if( tries < 3 )
JMF 0:6a929f0d0e58 218 tries = 0;
JMF 0:6a929f0d0e58 219 else
JMF 0:6a929f0d0e58 220 continue;
JMF 0:6a929f0d0e58 221
JMF 0:6a929f0d0e58 222 // subscribe to MQTT topic
JMF 0:6a929f0d0e58 223 tmr.reset();
JMF 0:6a929f0d0e58 224 rc = 1;
JMF 0:6a929f0d0e58 225 while( rc && client.isConnected() && tries < 3) {
JMF 1:b3de2b183932 226 PRINTF("(%d) Attempting to subscribing to MQTT topic '%s': ", tries, SUBSCRIBTOPIC);
JMF 0:6a929f0d0e58 227 rc = client.subscribe(SUBSCRIBTOPIC, MQTT::QOS0, messageArrived);
JMF 0:6a929f0d0e58 228 if( rc ) {
JMF 1:b3de2b183932 229 PRINTF("Failed (%d)!\r\n", rc);
JMF 0:6a929f0d0e58 230 while( tmr.read_ms() < 5000 );
JMF 0:6a929f0d0e58 231 tries++;
JMF 0:6a929f0d0e58 232 tmr.reset();
JMF 0:6a929f0d0e58 233 }
JMF 0:6a929f0d0e58 234 else {
JMF 0:6a929f0d0e58 235 good=1;
JMF 1:b3de2b183932 236 PRINTF("Subscribe successful!\r\n");
JMF 0:6a929f0d0e58 237 }
JMF 0:6a929f0d0e58 238 }
JMF 0:6a929f0d0e58 239 while (!good);
JMF 0:6a929f0d0e58 240 }
JMF 0:6a929f0d0e58 241
JMF 0:6a929f0d0e58 242 MQTT::Message message;
JMF 0:6a929f0d0e58 243 message.qos = MQTT::QOS0;
JMF 0:6a929f0d0e58 244 message.retained = false;
JMF 0:6a929f0d0e58 245 message.dup = false;
JMF 0:6a929f0d0e58 246 message.payload = (void*)buf;
JMF 0:6a929f0d0e58 247
JMF 0:6a929f0d0e58 248 while(true) {
JMF 0:6a929f0d0e58 249 osEvent switchEvent = messageQ.get(100);
JMF 0:6a929f0d0e58 250
JMF 0:6a929f0d0e58 251 if( switchEvent.value.v == 22 ) //switch between sending humidity & temp
JMF 0:6a929f0d0e58 252 txSel = !txSel;
JMF 0:6a929f0d0e58 253
JMF 0:6a929f0d0e58 254 if( switchEvent.value.v == 33) //user wants to run in Quickstart of Demo mode
JMF 1:b3de2b183932 255 pSW2 = !pSW2;
JMF 0:6a929f0d0e58 256
JMF 0:6a929f0d0e58 257 memset(buf,0x00,sizeof(buf));
JMF 0:6a929f0d0e58 258 if( txSel ) {
JMF 0:6a929f0d0e58 259 rc = hts221.readHumidity();
JMF 0:6a929f0d0e58 260 sprintf(buf, "{\"d\" : {\"humd\" : \"%2d.%d\" }}", rc/10, rc-((rc/10)*10));
JMF 1:b3de2b183932 261 PRINTF("Publishing MQTT message '%s' ", (char*)message.payload);
JMF 0:6a929f0d0e58 262 }
JMF 0:6a929f0d0e58 263 else {
JMF 1:b3de2b183932 264 sprintf(buf, "{\"d\" : {\"temp\" : %5d }}", (int)CTOF(hts221.readTemperature())*10);
JMF 1:b3de2b183932 265 PRINTF("Publishing MQTT message '%s' ", (char*)message.payload);
JMF 0:6a929f0d0e58 266 }
JMF 0:6a929f0d0e58 267 message.payloadlen = strlen(buf);
JMF 1:b3de2b183932 268 PRINTF("(%d)\r\n",message.payloadlen);
JMF 0:6a929f0d0e58 269 rc = client.publish(topic, message);
JMF 0:6a929f0d0e58 270 if( rc ) {
JMF 1:b3de2b183932 271 PRINTF("Publish request failed! (%d)\r\n",rc);
JMF 0:6a929f0d0e58 272 FATAL_WNC_ERROR(resume);
JMF 0:6a929f0d0e58 273 }
JMF 0:6a929f0d0e58 274
JMF 0:6a929f0d0e58 275 client.yield(6000);
JMF 0:6a929f0d0e58 276 }
JMF 0:6a929f0d0e58 277 }