working

Dependencies:   C027_Support IBMIoTClientCellularExample MQTT mbed

Fork of MQTT-GSM-GPS-CAN-SMS by Babu Alikapati

Committer:
mazgch
Date:
Fri Mar 13 07:11:56 2015 +0000
Revision:
14:6f36d75b3917
Parent:
13:6a0af5deb2db
Child:
15:7c3279ac3c52
moved config

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 6:37b6d0d56190 1 /*******************************************************************************
samdanbury 6:37b6d0d56190 2 * Copyright (c) 2014 IBM Corp.
samdanbury 6:37b6d0d56190 3 *
samdanbury 6:37b6d0d56190 4 * All rights reserved. This program and the accompanying materials
samdanbury 6:37b6d0d56190 5 * are made available under the terms of the Eclipse Public License v1.0
samdanbury 6:37b6d0d56190 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
samdanbury 6:37b6d0d56190 7 *
samdanbury 6:37b6d0d56190 8 * The Eclipse Public License is available at
samdanbury 6:37b6d0d56190 9 * http://www.eclipse.org/legal/epl-v10.html
samdanbury 6:37b6d0d56190 10 * and the Eclipse Distribution License is available at
samdanbury 6:37b6d0d56190 11 * http://www.eclipse.org/org/documents/edl-v10.php.
samdanbury 6:37b6d0d56190 12 *
samdanbury 6:37b6d0d56190 13 * Contributors:
samdanbury 6:37b6d0d56190 14 * Sam Danbury - initial implementation
samdanbury 6:37b6d0d56190 15 * Ian Craggs - refactoring to remove STL and other changes
icraggs 8:80d49dd91542 16 * Sam Grove - added check for Ethernet cable.
chris 10:0b5e0dfee08e 17 * Chris Styles - Added additional menu screen for software revision
chris 10:0b5e0dfee08e 18 *
chris 10:0b5e0dfee08e 19 * To do :
chris 10:0b5e0dfee08e 20 * Add magnetometer sensor output to IoT data stream
chris 10:0b5e0dfee08e 21 *
samdanbury 6:37b6d0d56190 22 *******************************************************************************/
samdanbury 6:37b6d0d56190 23
mazgch 12:d6b94e8051b0 24 #define USE_CELLULAR // Enable this switch on the C027 to use cellular
mazgch 12:d6b94e8051b0 25
samdanbury 6:37b6d0d56190 26 #include "LM75B.h"
samdanbury 6:37b6d0d56190 27 #include "MMA7660.h"
samdanbury 6:37b6d0d56190 28 #include "MQTTClient.h"
samdanbury 6:37b6d0d56190 29 #include "C12832.h"
samdanbury 6:37b6d0d56190 30 #include "Arial12x12.h"
samdanbury 6:37b6d0d56190 31 #include "rtos.h"
samdanbury 6:37b6d0d56190 32
chris 10:0b5e0dfee08e 33 // Update this to the next number *before* a commit
chris 10:0b5e0dfee08e 34 #define __APP_SW_REVISION__ "10"
chris 10:0b5e0dfee08e 35
samdanbury 6:37b6d0d56190 36 // Configuration values needed to connect to IBM IoT Cloud
icraggs 8:80d49dd91542 37 #define ORG "quickstart" // For a registered connection, replace with your org
icraggs 8:80d49dd91542 38 #define ID "" // For a registered connection, replace with your id
icraggs 8:80d49dd91542 39 #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token
mazgch 12:d6b94e8051b0 40 #define TYPE_NAME DEFAULT_TYPE_NAME // For a registered connection, replace with your type
samdanbury 6:37b6d0d56190 41
samdanbury 6:37b6d0d56190 42 #define MQTT_PORT 1883
samdanbury 6:37b6d0d56190 43 #define MQTT_TLS_PORT 8883
samdanbury 6:37b6d0d56190 44 #define IBM_IOT_PORT MQTT_PORT
samdanbury 6:37b6d0d56190 45
samdanbury 6:37b6d0d56190 46 #define MQTT_MAX_PACKET_SIZE 250
samdanbury 6:37b6d0d56190 47
mazgch 14:6f36d75b3917 48 //------------------------------------------------------------------------------------
mazgch 13:6a0af5deb2db 49 // You need to configure these cellular modem / SIM parameters.
mazgch 13:6a0af5deb2db 50 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 13:6a0af5deb2db 51 //------------------------------------------------------------------------------------
mazgch 13:6a0af5deb2db 52 # include "MDM.h"
mazgch 13:6a0af5deb2db 53 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 13:6a0af5deb2db 54 # define SIMPIN NULL
mazgch 13:6a0af5deb2db 55 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 13:6a0af5deb2db 56 contract with the network operator. You can also try to look-up your settings in
mazgch 13:6a0af5deb2db 57 google: https://www.google.de/search?q=APN+list */
mazgch 13:6a0af5deb2db 58 # define APN NULL
mazgch 13:6a0af5deb2db 59 //! Set the user name for your APN, or NULL if not needed
mazgch 13:6a0af5deb2db 60 # define USERNAME NULL
mazgch 13:6a0af5deb2db 61 //! Set the password for your APN, or NULL if not needed
mazgch 13:6a0af5deb2db 62 # define PASSWORD NULL
mazgch 13:6a0af5deb2db 63 //------------------------------------------------------------------------------------
mazgch 13:6a0af5deb2db 64
mazgch 14:6f36d75b3917 65 C12832 lcd(D11, D13, D12, D7, D10);
mazgch 14:6f36d75b3917 66 PwmOut r(D5);
mazgch 14:6f36d75b3917 67 PwmOut g(D9);
mazgch 14:6f36d75b3917 68 PwmOut b(D8);
mazgch 14:6f36d75b3917 69 DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3);
mazgch 14:6f36d75b3917 70 MMA7660 MMA(D14, D15);
mazgch 14:6f36d75b3917 71 LM75B sensor(D14,D15);
mazgch 14:6f36d75b3917 72 DigitalIn Up(A2); DigitalIn Down(A3); DigitalIn Left(A4); DigitalIn Right(A5); DigitalIn Click(D4);
mazgch 14:6f36d75b3917 73 AnalogIn ain1 (A0); AnalogIn ain2 (A1);
mazgch 14:6f36d75b3917 74
mazgch 14:6f36d75b3917 75 #define LED2_OFF 0
mazgch 14:6f36d75b3917 76 #define LED2_ON 1
mazgch 14:6f36d75b3917 77
mazgch 14:6f36d75b3917 78 static uint32_t linkStatus(void) { return true; }
mazgch 14:6f36d75b3917 79
mazgch 14:6f36d75b3917 80 #define DEFAULT_TYPE_NAME "iotsample-mbed-c027"
mazgch 14:6f36d75b3917 81
mazgch 13:6a0af5deb2db 82 #define MQTT_CLIENT_TYPE MQTTSocket
mazgch 13:6a0af5deb2db 83 #include "MQTTSocket.h"
samdanbury 6:37b6d0d56190 84
icraggs 8:80d49dd91542 85 bool quickstartMode = true;
samdanbury 6:37b6d0d56190 86 char org[11] = ORG;
mazgch 12:d6b94e8051b0 87 char type[30] = TYPE_NAME;
samdanbury 6:37b6d0d56190 88 char id[30] = ID; // mac without colons
samdanbury 6:37b6d0d56190 89 char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
samdanbury 6:37b6d0d56190 90
samdanbury 6:37b6d0d56190 91 bool connected = false;
samdanbury 6:37b6d0d56190 92 char* joystickPos = "CENTRE";
samdanbury 6:37b6d0d56190 93 int blink_interval = 0;
samdanbury 6:37b6d0d56190 94
samdanbury 6:37b6d0d56190 95
samdanbury 6:37b6d0d56190 96 void off()
samdanbury 6:37b6d0d56190 97 {
samdanbury 6:37b6d0d56190 98 r = g = b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 99 }
samdanbury 6:37b6d0d56190 100
samdanbury 6:37b6d0d56190 101 void red()
samdanbury 6:37b6d0d56190 102 {
samdanbury 6:37b6d0d56190 103 r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 104 }
samdanbury 6:37b6d0d56190 105
samdanbury 6:37b6d0d56190 106 void yellow()
samdanbury 6:37b6d0d56190 107 {
samdanbury 6:37b6d0d56190 108 r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 109 }
samdanbury 6:37b6d0d56190 110
samdanbury 6:37b6d0d56190 111 void green()
samdanbury 6:37b6d0d56190 112 {
samdanbury 6:37b6d0d56190 113 r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 114 }
samdanbury 6:37b6d0d56190 115
samdanbury 6:37b6d0d56190 116
samdanbury 6:37b6d0d56190 117 void flashing_yellow(void const *args)
samdanbury 6:37b6d0d56190 118 {
samdanbury 6:37b6d0d56190 119 bool on = false;
samdanbury 6:37b6d0d56190 120 while (!connected) // flashing yellow only while connecting
samdanbury 6:37b6d0d56190 121 {
samdanbury 6:37b6d0d56190 122 on = !on;
samdanbury 6:37b6d0d56190 123 if (on)
samdanbury 6:37b6d0d56190 124 yellow();
samdanbury 6:37b6d0d56190 125 else
samdanbury 6:37b6d0d56190 126 off();
samdanbury 6:37b6d0d56190 127 wait(0.5);
samdanbury 6:37b6d0d56190 128 }
samdanbury 6:37b6d0d56190 129 }
samdanbury 6:37b6d0d56190 130
samdanbury 6:37b6d0d56190 131
samdanbury 6:37b6d0d56190 132 void flashing_red(void const *args) // to be used when the connection is lost
samdanbury 6:37b6d0d56190 133 {
samdanbury 6:37b6d0d56190 134 bool on = false;
samdanbury 6:37b6d0d56190 135 while (!connected)
samdanbury 6:37b6d0d56190 136 {
samdanbury 6:37b6d0d56190 137 on = !on;
samdanbury 6:37b6d0d56190 138 if (on)
samdanbury 6:37b6d0d56190 139 red();
samdanbury 6:37b6d0d56190 140 else
samdanbury 6:37b6d0d56190 141 off();
samdanbury 6:37b6d0d56190 142 wait(2.0);
samdanbury 6:37b6d0d56190 143 }
samdanbury 6:37b6d0d56190 144 }
samdanbury 6:37b6d0d56190 145
samdanbury 6:37b6d0d56190 146
samdanbury 6:37b6d0d56190 147 void printMenu(int menuItem)
samdanbury 6:37b6d0d56190 148 {
samdanbury 6:37b6d0d56190 149 lcd.cls();
samdanbury 6:37b6d0d56190 150 lcd.locate(0,0);
samdanbury 6:37b6d0d56190 151 switch (menuItem)
samdanbury 6:37b6d0d56190 152 {
samdanbury 6:37b6d0d56190 153 case 0:
samdanbury 6:37b6d0d56190 154 lcd.printf("IBM IoT Cloud");
samdanbury 6:37b6d0d56190 155 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 156 lcd.printf("Scroll with joystick");
samdanbury 6:37b6d0d56190 157 break;
samdanbury 6:37b6d0d56190 158 case 1:
samdanbury 6:37b6d0d56190 159 lcd.printf("Go to:");
samdanbury 6:37b6d0d56190 160 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 161 lcd.printf("http://ibm.biz/iotqstart");
samdanbury 6:37b6d0d56190 162 break;
samdanbury 6:37b6d0d56190 163 case 2:
samdanbury 6:37b6d0d56190 164 lcd.printf("Device Identity:");
samdanbury 6:37b6d0d56190 165 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 166 lcd.printf("%s", id);
samdanbury 6:37b6d0d56190 167 break;
samdanbury 6:37b6d0d56190 168 case 3:
samdanbury 6:37b6d0d56190 169 lcd.printf("Status:");
samdanbury 6:37b6d0d56190 170 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 171 lcd.printf(connected ? "Connected" : "Disconnected");
samdanbury 6:37b6d0d56190 172 break;
chris 10:0b5e0dfee08e 173 case 4:
chris 10:0b5e0dfee08e 174 lcd.printf("App version:");
chris 10:0b5e0dfee08e 175 lcd.locate(0,16);
chris 10:0b5e0dfee08e 176 lcd.printf("%s",__APP_SW_REVISION__);
chris 10:0b5e0dfee08e 177 break;
samdanbury 6:37b6d0d56190 178 }
samdanbury 6:37b6d0d56190 179 }
samdanbury 6:37b6d0d56190 180
samdanbury 6:37b6d0d56190 181
samdanbury 6:37b6d0d56190 182 void setMenu()
samdanbury 6:37b6d0d56190 183 {
samdanbury 6:37b6d0d56190 184 static int menuItem = 0;
samdanbury 6:37b6d0d56190 185 if (Down)
samdanbury 6:37b6d0d56190 186 {
samdanbury 6:37b6d0d56190 187 joystickPos = "DOWN";
chris 10:0b5e0dfee08e 188 if (menuItem >= 0 && menuItem < 4)
samdanbury 6:37b6d0d56190 189 printMenu(++menuItem);
samdanbury 6:37b6d0d56190 190 }
samdanbury 6:37b6d0d56190 191 else if (Left)
samdanbury 6:37b6d0d56190 192 joystickPos = "LEFT";
samdanbury 6:37b6d0d56190 193 else if (Click)
samdanbury 6:37b6d0d56190 194 joystickPos = "CLICK";
samdanbury 6:37b6d0d56190 195 else if (Up)
samdanbury 6:37b6d0d56190 196 {
samdanbury 6:37b6d0d56190 197 joystickPos = "UP";
chris 10:0b5e0dfee08e 198 if (menuItem <= 4 && menuItem > 0)
samdanbury 6:37b6d0d56190 199 printMenu(--menuItem);
samdanbury 6:37b6d0d56190 200 }
samdanbury 6:37b6d0d56190 201 else if (Right)
samdanbury 6:37b6d0d56190 202 joystickPos = "RIGHT";
samdanbury 6:37b6d0d56190 203 else
samdanbury 6:37b6d0d56190 204 joystickPos = "CENTRE";
samdanbury 6:37b6d0d56190 205 }
samdanbury 6:37b6d0d56190 206
samdanbury 6:37b6d0d56190 207
samdanbury 6:37b6d0d56190 208 /**
samdanbury 6:37b6d0d56190 209 * Display a message on the LCD screen prefixed with IBM IoT Cloud
samdanbury 6:37b6d0d56190 210 */
samdanbury 6:37b6d0d56190 211 void displayMessage(char* message)
samdanbury 6:37b6d0d56190 212 {
samdanbury 6:37b6d0d56190 213 lcd.cls();
samdanbury 6:37b6d0d56190 214 lcd.locate(0,0);
samdanbury 6:37b6d0d56190 215 lcd.printf("IBM IoT Cloud");
samdanbury 6:37b6d0d56190 216 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 217 lcd.printf(message);
samdanbury 6:37b6d0d56190 218 }
samdanbury 6:37b6d0d56190 219
samdanbury 6:37b6d0d56190 220
mazgch 12:d6b94e8051b0 221 int connect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack)
samdanbury 6:37b6d0d56190 222 {
samdanbury 6:37b6d0d56190 223 const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com";
samdanbury 6:37b6d0d56190 224
samdanbury 6:37b6d0d56190 225 char hostname[strlen(org) + strlen(iot_ibm) + 1];
samdanbury 6:37b6d0d56190 226 sprintf(hostname, "%s%s", org, iot_ibm);
mazgch 11:7a6df9a2dcdc 227 DEBUG("hostname is %s\n", hostname);
samdanbury 6:37b6d0d56190 228 int rc = ipstack->connect(hostname, IBM_IOT_PORT);
samdanbury 6:37b6d0d56190 229 if (rc != 0)
samdanbury 6:37b6d0d56190 230 return rc;
samdanbury 6:37b6d0d56190 231
samdanbury 6:37b6d0d56190 232 // Construct clientId - d:org:type:id
samdanbury 6:37b6d0d56190 233 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
samdanbury 6:37b6d0d56190 234 sprintf(clientId, "d:%s:%s:%s", org, type, id);
samdanbury 6:37b6d0d56190 235 DEBUG("clientid is %s\n", clientId);
samdanbury 6:37b6d0d56190 236
samdanbury 6:37b6d0d56190 237 // MQTT Connect
samdanbury 6:37b6d0d56190 238 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
samdanbury 6:37b6d0d56190 239 data.MQTTVersion = 3;
samdanbury 6:37b6d0d56190 240 data.clientID.cstring = clientId;
samdanbury 6:37b6d0d56190 241
samdanbury 6:37b6d0d56190 242 if (!quickstartMode)
samdanbury 6:37b6d0d56190 243 {
samdanbury 6:37b6d0d56190 244 data.username.cstring = "use-token-auth";
samdanbury 6:37b6d0d56190 245 data.password.cstring = auth_token;
samdanbury 6:37b6d0d56190 246 }
samdanbury 6:37b6d0d56190 247
icraggs 8:80d49dd91542 248 if ((rc = client->connect(data)) == 0)
samdanbury 6:37b6d0d56190 249 {
samdanbury 6:37b6d0d56190 250 connected = true;
samdanbury 6:37b6d0d56190 251 green();
samdanbury 6:37b6d0d56190 252 displayMessage("Connected");
samdanbury 6:37b6d0d56190 253 wait(2);
samdanbury 6:37b6d0d56190 254 displayMessage("Scroll with joystick");
samdanbury 6:37b6d0d56190 255 }
samdanbury 6:37b6d0d56190 256 return rc;
samdanbury 6:37b6d0d56190 257 }
samdanbury 6:37b6d0d56190 258
samdanbury 6:37b6d0d56190 259
samdanbury 6:37b6d0d56190 260 int getConnTimeout(int attemptNumber)
samdanbury 6:37b6d0d56190 261 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
samdanbury 6:37b6d0d56190 262 // after 20 attempts, retry every 10 minutes
samdanbury 6:37b6d0d56190 263 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
samdanbury 6:37b6d0d56190 264 }
samdanbury 6:37b6d0d56190 265
samdanbury 6:37b6d0d56190 266
mazgch 12:d6b94e8051b0 267 void attemptConnect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack)
samdanbury 6:37b6d0d56190 268 {
samdanbury 6:37b6d0d56190 269 int retryAttempt = 0;
samdanbury 6:37b6d0d56190 270 connected = false;
icraggs 8:80d49dd91542 271
icraggs 8:80d49dd91542 272 // make sure a cable is connected before starting to connect
icraggs 8:80d49dd91542 273 while (!linkStatus()) {
icraggs 8:80d49dd91542 274 wait(1.0f);
mazgch 12:d6b94e8051b0 275 WARN("Internet link not present. Check cable connection\n");
icraggs 8:80d49dd91542 276 }
samdanbury 6:37b6d0d56190 277
samdanbury 6:37b6d0d56190 278 while (connect(client, ipstack) != 0)
samdanbury 6:37b6d0d56190 279 {
samdanbury 6:37b6d0d56190 280 #if defined(TARGET_K64F)
samdanbury 6:37b6d0d56190 281 red();
samdanbury 6:37b6d0d56190 282 #else
samdanbury 6:37b6d0d56190 283 Thread red_thread(flashing_red);
samdanbury 6:37b6d0d56190 284 #endif
samdanbury 6:37b6d0d56190 285 int timeout = getConnTimeout(++retryAttempt);
samdanbury 6:37b6d0d56190 286 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
icraggs 8:80d49dd91542 287
icraggs 8:80d49dd91542 288 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
icraggs 8:80d49dd91542 289 // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
icraggs 8:80d49dd91542 290
icraggs 8:80d49dd91542 291 // this works - reset the system when the retry count gets to a threshold
icraggs 8:80d49dd91542 292 if (retryAttempt == 5)
icraggs 8:80d49dd91542 293 NVIC_SystemReset();
icraggs 8:80d49dd91542 294 else
icraggs 8:80d49dd91542 295 wait(timeout);
samdanbury 6:37b6d0d56190 296 }
samdanbury 6:37b6d0d56190 297 }
samdanbury 6:37b6d0d56190 298
samdanbury 6:37b6d0d56190 299
mazgch 12:d6b94e8051b0 300 int publish(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack)
samdanbury 6:37b6d0d56190 301 {
samdanbury 6:37b6d0d56190 302 MQTT::Message message;
samdanbury 6:37b6d0d56190 303 char* pubTopic = "iot-2/evt/status/fmt/json";
samdanbury 6:37b6d0d56190 304
samdanbury 6:37b6d0d56190 305 char buf[250];
samdanbury 6:37b6d0d56190 306 sprintf(buf,
samdanbury 6:37b6d0d56190 307 "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}",
samdanbury 6:37b6d0d56190 308 MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
samdanbury 6:37b6d0d56190 309 message.qos = MQTT::QOS0;
samdanbury 6:37b6d0d56190 310 message.retained = false;
samdanbury 6:37b6d0d56190 311 message.dup = false;
samdanbury 6:37b6d0d56190 312 message.payload = (void*)buf;
samdanbury 6:37b6d0d56190 313 message.payloadlen = strlen(buf);
samdanbury 6:37b6d0d56190 314
samdanbury 6:37b6d0d56190 315 LOG("Publishing %s\n", buf);
icraggs 8:80d49dd91542 316 return client->publish(pubTopic, message);
samdanbury 6:37b6d0d56190 317 }
samdanbury 6:37b6d0d56190 318
samdanbury 6:37b6d0d56190 319 void messageArrived(MQTT::MessageData& md)
samdanbury 6:37b6d0d56190 320 {
samdanbury 6:37b6d0d56190 321 MQTT::Message &message = md.message;
samdanbury 6:37b6d0d56190 322 char topic[md.topicName.lenstring.len + 1];
samdanbury 6:37b6d0d56190 323
samdanbury 6:37b6d0d56190 324 sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
samdanbury 6:37b6d0d56190 325
samdanbury 6:37b6d0d56190 326 LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload);
samdanbury 6:37b6d0d56190 327
samdanbury 6:37b6d0d56190 328 // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/
samdanbury 6:37b6d0d56190 329 char* start = strstr(topic, "/cmd/") + 5;
samdanbury 6:37b6d0d56190 330 int len = strstr(topic, "/fmt/") - start;
samdanbury 6:37b6d0d56190 331
samdanbury 6:37b6d0d56190 332 if (memcmp(start, "blink", len) == 0)
samdanbury 6:37b6d0d56190 333 {
samdanbury 6:37b6d0d56190 334 char payload[message.payloadlen + 1];
samdanbury 6:37b6d0d56190 335 sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload);
samdanbury 6:37b6d0d56190 336
samdanbury 6:37b6d0d56190 337 char* pos = strchr(payload, '}');
samdanbury 6:37b6d0d56190 338 if (pos != NULL)
samdanbury 6:37b6d0d56190 339 {
samdanbury 6:37b6d0d56190 340 *pos = '\0';
samdanbury 6:37b6d0d56190 341 if ((pos = strchr(payload, ':')) != NULL)
samdanbury 6:37b6d0d56190 342 {
samdanbury 6:37b6d0d56190 343 int blink_rate = atoi(pos + 1);
samdanbury 6:37b6d0d56190 344 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
samdanbury 6:37b6d0d56190 345 }
samdanbury 6:37b6d0d56190 346 }
samdanbury 6:37b6d0d56190 347 }
samdanbury 6:37b6d0d56190 348 else
samdanbury 6:37b6d0d56190 349 WARN("Unsupported command: %.*s\n", len, start);
samdanbury 6:37b6d0d56190 350 }
samdanbury 6:37b6d0d56190 351
samdanbury 6:37b6d0d56190 352
samdanbury 6:37b6d0d56190 353 int main()
samdanbury 6:37b6d0d56190 354 {
icraggs 8:80d49dd91542 355 quickstartMode = (strcmp(org, "quickstart") == 0);
icraggs 8:80d49dd91542 356
samdanbury 6:37b6d0d56190 357 lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen
samdanbury 6:37b6d0d56190 358
samdanbury 6:37b6d0d56190 359 led2 = LED2_OFF; // K64F: turn off the main board LED
samdanbury 6:37b6d0d56190 360
samdanbury 6:37b6d0d56190 361 displayMessage("Connecting");
samdanbury 6:37b6d0d56190 362 #if defined(TARGET_K64F)
samdanbury 6:37b6d0d56190 363 yellow(); // Don't flash on the K64F, because starting a thread causes the EthernetInterface init call to hang
samdanbury 6:37b6d0d56190 364 #else
samdanbury 6:37b6d0d56190 365 Thread yellow_thread(flashing_yellow);
samdanbury 6:37b6d0d56190 366 #endif
samdanbury 6:37b6d0d56190 367
mazgch 12:d6b94e8051b0 368 MDMSerial mdm;
mazgch 12:d6b94e8051b0 369 //mdm.setDebug(4); // enable this for debugging issues
mazgch 12:d6b94e8051b0 370 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
mazgch 12:d6b94e8051b0 371 return -1;
mazgch 13:6a0af5deb2db 372
mazgch 12:d6b94e8051b0 373 MQTT_CLIENT_TYPE ipstack;
mazgch 12:d6b94e8051b0 374 MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
samdanbury 6:37b6d0d56190 375 if (quickstartMode)
samdanbury 6:37b6d0d56190 376 {
mazgch 12:d6b94e8051b0 377 MDMParser::NetStatus status;
mazgch 12:d6b94e8051b0 378 if (mdm.checkNetStatus(&status) && *status.num) {
mazgch 12:d6b94e8051b0 379 int l = strlen(status.num);
mazgch 12:d6b94e8051b0 380 strcpy(id, "000000000000");
mazgch 12:d6b94e8051b0 381 strcpy(id+12-l, status.num);
mazgch 12:d6b94e8051b0 382 }
samdanbury 6:37b6d0d56190 383 }
samdanbury 6:37b6d0d56190 384
samdanbury 6:37b6d0d56190 385 attemptConnect(&client, &ipstack);
samdanbury 6:37b6d0d56190 386
samdanbury 6:37b6d0d56190 387 if (!quickstartMode)
samdanbury 6:37b6d0d56190 388 {
samdanbury 6:37b6d0d56190 389 int rc = 0;
samdanbury 6:37b6d0d56190 390 if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0)
samdanbury 6:37b6d0d56190 391 WARN("rc from MQTT subscribe is %d\n", rc);
samdanbury 6:37b6d0d56190 392 }
samdanbury 6:37b6d0d56190 393
samdanbury 6:37b6d0d56190 394 blink_interval = 0;
samdanbury 6:37b6d0d56190 395 int count = 0;
samdanbury 6:37b6d0d56190 396 while (true)
samdanbury 6:37b6d0d56190 397 {
samdanbury 6:37b6d0d56190 398 if (++count == 100)
samdanbury 6:37b6d0d56190 399 { // Publish a message every second
samdanbury 6:37b6d0d56190 400 if (publish(&client, &ipstack) != 0)
samdanbury 6:37b6d0d56190 401 attemptConnect(&client, &ipstack); // if we have lost the connection
samdanbury 6:37b6d0d56190 402 count = 0;
samdanbury 6:37b6d0d56190 403 }
samdanbury 6:37b6d0d56190 404
samdanbury 6:37b6d0d56190 405 if (blink_interval == 0)
samdanbury 6:37b6d0d56190 406 led2 = LED2_OFF;
samdanbury 6:37b6d0d56190 407 else if (count % blink_interval == 0)
samdanbury 6:37b6d0d56190 408 led2 = !led2;
samdanbury 6:37b6d0d56190 409 if (count % 20 == 0)
samdanbury 6:37b6d0d56190 410 setMenu();
samdanbury 6:37b6d0d56190 411 client.yield(10); // allow the MQTT client to receive messages
samdanbury 6:37b6d0d56190 412 }
samdanbury 6:37b6d0d56190 413 }