working

Dependencies:   C027_Support IBMIoTClientCellularExample MQTT mbed

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

Committer:
mazgch
Date:
Fri Jul 03 11:46:09 2015 +0000
Revision:
17:b08722ede282
Parent:
16:a931c0a6d987
Child:
18:e9e87362018b
added some additional modem sensor data

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
mazgch 16:a931c0a6d987 34 #define __APP_SW_REVISION__ "11"
mazgch 16:a931c0a6d987 35 #define BOARD_NAME "IoT u-blox"
chris 10:0b5e0dfee08e 36
samdanbury 6:37b6d0d56190 37 // Configuration values needed to connect to IBM IoT Cloud
icraggs 8:80d49dd91542 38 #define ORG "quickstart" // For a registered connection, replace with your org
icraggs 8:80d49dd91542 39 #define ID "" // For a registered connection, replace with your id
icraggs 8:80d49dd91542 40 #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token
mazgch 12:d6b94e8051b0 41 #define TYPE_NAME DEFAULT_TYPE_NAME // For a registered connection, replace with your type
samdanbury 6:37b6d0d56190 42
samdanbury 6:37b6d0d56190 43 #define MQTT_PORT 1883
samdanbury 6:37b6d0d56190 44 #define MQTT_TLS_PORT 8883
samdanbury 6:37b6d0d56190 45 #define IBM_IOT_PORT MQTT_PORT
samdanbury 6:37b6d0d56190 46
samdanbury 6:37b6d0d56190 47 #define MQTT_MAX_PACKET_SIZE 250
samdanbury 6:37b6d0d56190 48
mazgch 14:6f36d75b3917 49 //------------------------------------------------------------------------------------
mazgch 13:6a0af5deb2db 50 // You need to configure these cellular modem / SIM parameters.
mazgch 13:6a0af5deb2db 51 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 13:6a0af5deb2db 52 //------------------------------------------------------------------------------------
mazgch 13:6a0af5deb2db 53 # include "MDM.h"
mazgch 13:6a0af5deb2db 54 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 13:6a0af5deb2db 55 # define SIMPIN NULL
mazgch 13:6a0af5deb2db 56 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 13:6a0af5deb2db 57 contract with the network operator. You can also try to look-up your settings in
mazgch 13:6a0af5deb2db 58 google: https://www.google.de/search?q=APN+list */
mazgch 13:6a0af5deb2db 59 # define APN NULL
mazgch 13:6a0af5deb2db 60 //! Set the user name for your APN, or NULL if not needed
mazgch 13:6a0af5deb2db 61 # define USERNAME NULL
mazgch 13:6a0af5deb2db 62 //! Set the password for your APN, or NULL if not needed
mazgch 13:6a0af5deb2db 63 # define PASSWORD NULL
mazgch 13:6a0af5deb2db 64 //------------------------------------------------------------------------------------
mazgch 15:7c3279ac3c52 65 # include "GPS.h"
mazgch 15:7c3279ac3c52 66 //------------------------------------------------------------------------------------
mazgch 13:6a0af5deb2db 67
mazgch 14:6f36d75b3917 68 C12832 lcd(D11, D13, D12, D7, D10);
mazgch 14:6f36d75b3917 69 PwmOut r(D5);
mazgch 14:6f36d75b3917 70 PwmOut g(D9);
mazgch 14:6f36d75b3917 71 PwmOut b(D8);
mazgch 14:6f36d75b3917 72 DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3);
mazgch 14:6f36d75b3917 73 MMA7660 MMA(D14, D15);
mazgch 14:6f36d75b3917 74 LM75B sensor(D14,D15);
mazgch 14:6f36d75b3917 75 DigitalIn Up(A2); DigitalIn Down(A3); DigitalIn Left(A4); DigitalIn Right(A5); DigitalIn Click(D4);
mazgch 14:6f36d75b3917 76 AnalogIn ain1 (A0); AnalogIn ain2 (A1);
mazgch 14:6f36d75b3917 77
mazgch 14:6f36d75b3917 78 #define LED2_OFF 0
mazgch 14:6f36d75b3917 79 #define LED2_ON 1
mazgch 14:6f36d75b3917 80
mazgch 14:6f36d75b3917 81 static uint32_t linkStatus(void) { return true; }
mazgch 14:6f36d75b3917 82
mazgch 14:6f36d75b3917 83 #define DEFAULT_TYPE_NAME "iotsample-mbed-c027"
mazgch 14:6f36d75b3917 84
mazgch 13:6a0af5deb2db 85 #define MQTT_CLIENT_TYPE MQTTSocket
mazgch 13:6a0af5deb2db 86 #include "MQTTSocket.h"
samdanbury 6:37b6d0d56190 87
icraggs 8:80d49dd91542 88 bool quickstartMode = true;
samdanbury 6:37b6d0d56190 89 char org[11] = ORG;
mazgch 12:d6b94e8051b0 90 char type[30] = TYPE_NAME;
mazgch 15:7c3279ac3c52 91 char id[30] = ID; // device ID
samdanbury 6:37b6d0d56190 92 char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
samdanbury 6:37b6d0d56190 93
samdanbury 6:37b6d0d56190 94 bool connected = false;
mazgch 15:7c3279ac3c52 95 bool shieldConnected = false;
samdanbury 6:37b6d0d56190 96 char* joystickPos = "CENTRE";
samdanbury 6:37b6d0d56190 97 int blink_interval = 0;
samdanbury 6:37b6d0d56190 98
samdanbury 6:37b6d0d56190 99
samdanbury 6:37b6d0d56190 100 void off()
samdanbury 6:37b6d0d56190 101 {
samdanbury 6:37b6d0d56190 102 r = g = b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 103 }
samdanbury 6:37b6d0d56190 104
samdanbury 6:37b6d0d56190 105 void red()
samdanbury 6:37b6d0d56190 106 {
samdanbury 6:37b6d0d56190 107 r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 108 }
samdanbury 6:37b6d0d56190 109
samdanbury 6:37b6d0d56190 110 void yellow()
samdanbury 6:37b6d0d56190 111 {
samdanbury 6:37b6d0d56190 112 r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 113 }
samdanbury 6:37b6d0d56190 114
samdanbury 6:37b6d0d56190 115 void green()
samdanbury 6:37b6d0d56190 116 {
samdanbury 6:37b6d0d56190 117 r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 118 }
samdanbury 6:37b6d0d56190 119
samdanbury 6:37b6d0d56190 120
samdanbury 6:37b6d0d56190 121 void flashing_yellow(void const *args)
samdanbury 6:37b6d0d56190 122 {
samdanbury 6:37b6d0d56190 123 bool on = false;
samdanbury 6:37b6d0d56190 124 while (!connected) // flashing yellow only while connecting
samdanbury 6:37b6d0d56190 125 {
samdanbury 6:37b6d0d56190 126 on = !on;
samdanbury 6:37b6d0d56190 127 if (on)
samdanbury 6:37b6d0d56190 128 yellow();
samdanbury 6:37b6d0d56190 129 else
samdanbury 6:37b6d0d56190 130 off();
samdanbury 6:37b6d0d56190 131 wait(0.5);
samdanbury 6:37b6d0d56190 132 }
samdanbury 6:37b6d0d56190 133 }
samdanbury 6:37b6d0d56190 134
samdanbury 6:37b6d0d56190 135
samdanbury 6:37b6d0d56190 136 void flashing_red(void const *args) // to be used when the connection is lost
samdanbury 6:37b6d0d56190 137 {
samdanbury 6:37b6d0d56190 138 bool on = false;
samdanbury 6:37b6d0d56190 139 while (!connected)
samdanbury 6:37b6d0d56190 140 {
samdanbury 6:37b6d0d56190 141 on = !on;
samdanbury 6:37b6d0d56190 142 if (on)
samdanbury 6:37b6d0d56190 143 red();
samdanbury 6:37b6d0d56190 144 else
samdanbury 6:37b6d0d56190 145 off();
samdanbury 6:37b6d0d56190 146 wait(2.0);
samdanbury 6:37b6d0d56190 147 }
samdanbury 6:37b6d0d56190 148 }
samdanbury 6:37b6d0d56190 149
samdanbury 6:37b6d0d56190 150
samdanbury 6:37b6d0d56190 151 void printMenu(int menuItem)
samdanbury 6:37b6d0d56190 152 {
samdanbury 6:37b6d0d56190 153 lcd.cls();
samdanbury 6:37b6d0d56190 154 lcd.locate(0,0);
samdanbury 6:37b6d0d56190 155 switch (menuItem)
samdanbury 6:37b6d0d56190 156 {
samdanbury 6:37b6d0d56190 157 case 0:
mazgch 16:a931c0a6d987 158 lcd.printf("IBM/u-blox IoT Cloud");
samdanbury 6:37b6d0d56190 159 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 160 lcd.printf("Scroll with joystick");
samdanbury 6:37b6d0d56190 161 break;
samdanbury 6:37b6d0d56190 162 case 1:
samdanbury 6:37b6d0d56190 163 lcd.printf("Go to:");
samdanbury 6:37b6d0d56190 164 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 165 lcd.printf("http://ibm.biz/iotqstart");
samdanbury 6:37b6d0d56190 166 break;
samdanbury 6:37b6d0d56190 167 case 2:
samdanbury 6:37b6d0d56190 168 lcd.printf("Device Identity:");
samdanbury 6:37b6d0d56190 169 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 170 lcd.printf("%s", id);
samdanbury 6:37b6d0d56190 171 break;
samdanbury 6:37b6d0d56190 172 case 3:
samdanbury 6:37b6d0d56190 173 lcd.printf("Status:");
samdanbury 6:37b6d0d56190 174 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 175 lcd.printf(connected ? "Connected" : "Disconnected");
samdanbury 6:37b6d0d56190 176 break;
chris 10:0b5e0dfee08e 177 case 4:
chris 10:0b5e0dfee08e 178 lcd.printf("App version:");
chris 10:0b5e0dfee08e 179 lcd.locate(0,16);
chris 10:0b5e0dfee08e 180 lcd.printf("%s",__APP_SW_REVISION__);
chris 10:0b5e0dfee08e 181 break;
samdanbury 6:37b6d0d56190 182 }
samdanbury 6:37b6d0d56190 183 }
samdanbury 6:37b6d0d56190 184
samdanbury 6:37b6d0d56190 185
samdanbury 6:37b6d0d56190 186 void setMenu()
samdanbury 6:37b6d0d56190 187 {
samdanbury 6:37b6d0d56190 188 static int menuItem = 0;
samdanbury 6:37b6d0d56190 189 if (Down)
samdanbury 6:37b6d0d56190 190 {
samdanbury 6:37b6d0d56190 191 joystickPos = "DOWN";
chris 10:0b5e0dfee08e 192 if (menuItem >= 0 && menuItem < 4)
samdanbury 6:37b6d0d56190 193 printMenu(++menuItem);
samdanbury 6:37b6d0d56190 194 }
samdanbury 6:37b6d0d56190 195 else if (Left)
samdanbury 6:37b6d0d56190 196 joystickPos = "LEFT";
samdanbury 6:37b6d0d56190 197 else if (Click)
samdanbury 6:37b6d0d56190 198 joystickPos = "CLICK";
samdanbury 6:37b6d0d56190 199 else if (Up)
samdanbury 6:37b6d0d56190 200 {
samdanbury 6:37b6d0d56190 201 joystickPos = "UP";
chris 10:0b5e0dfee08e 202 if (menuItem <= 4 && menuItem > 0)
samdanbury 6:37b6d0d56190 203 printMenu(--menuItem);
samdanbury 6:37b6d0d56190 204 }
samdanbury 6:37b6d0d56190 205 else if (Right)
samdanbury 6:37b6d0d56190 206 joystickPos = "RIGHT";
samdanbury 6:37b6d0d56190 207 else
samdanbury 6:37b6d0d56190 208 joystickPos = "CENTRE";
samdanbury 6:37b6d0d56190 209 }
samdanbury 6:37b6d0d56190 210
samdanbury 6:37b6d0d56190 211
samdanbury 6:37b6d0d56190 212 /**
samdanbury 6:37b6d0d56190 213 * Display a message on the LCD screen prefixed with IBM IoT Cloud
samdanbury 6:37b6d0d56190 214 */
samdanbury 6:37b6d0d56190 215 void displayMessage(char* message)
samdanbury 6:37b6d0d56190 216 {
samdanbury 6:37b6d0d56190 217 lcd.cls();
samdanbury 6:37b6d0d56190 218 lcd.locate(0,0);
mazgch 16:a931c0a6d987 219 lcd.printf("IBM/u-blox IoT Cloud");
samdanbury 6:37b6d0d56190 220 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 221 lcd.printf(message);
samdanbury 6:37b6d0d56190 222 }
samdanbury 6:37b6d0d56190 223
samdanbury 6:37b6d0d56190 224
mazgch 12:d6b94e8051b0 225 int connect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack)
samdanbury 6:37b6d0d56190 226 {
samdanbury 6:37b6d0d56190 227 const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com";
samdanbury 6:37b6d0d56190 228
samdanbury 6:37b6d0d56190 229 char hostname[strlen(org) + strlen(iot_ibm) + 1];
samdanbury 6:37b6d0d56190 230 sprintf(hostname, "%s%s", org, iot_ibm);
mazgch 11:7a6df9a2dcdc 231 DEBUG("hostname is %s\n", hostname);
samdanbury 6:37b6d0d56190 232 int rc = ipstack->connect(hostname, IBM_IOT_PORT);
samdanbury 6:37b6d0d56190 233 if (rc != 0)
samdanbury 6:37b6d0d56190 234 return rc;
samdanbury 6:37b6d0d56190 235
samdanbury 6:37b6d0d56190 236 // Construct clientId - d:org:type:id
samdanbury 6:37b6d0d56190 237 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
samdanbury 6:37b6d0d56190 238 sprintf(clientId, "d:%s:%s:%s", org, type, id);
samdanbury 6:37b6d0d56190 239 DEBUG("clientid is %s\n", clientId);
samdanbury 6:37b6d0d56190 240
samdanbury 6:37b6d0d56190 241 // MQTT Connect
samdanbury 6:37b6d0d56190 242 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
samdanbury 6:37b6d0d56190 243 data.MQTTVersion = 3;
samdanbury 6:37b6d0d56190 244 data.clientID.cstring = clientId;
samdanbury 6:37b6d0d56190 245
samdanbury 6:37b6d0d56190 246 if (!quickstartMode)
samdanbury 6:37b6d0d56190 247 {
samdanbury 6:37b6d0d56190 248 data.username.cstring = "use-token-auth";
samdanbury 6:37b6d0d56190 249 data.password.cstring = auth_token;
samdanbury 6:37b6d0d56190 250 }
samdanbury 6:37b6d0d56190 251
icraggs 8:80d49dd91542 252 if ((rc = client->connect(data)) == 0)
samdanbury 6:37b6d0d56190 253 {
samdanbury 6:37b6d0d56190 254 connected = true;
samdanbury 6:37b6d0d56190 255 green();
samdanbury 6:37b6d0d56190 256 displayMessage("Connected");
samdanbury 6:37b6d0d56190 257 wait(2);
samdanbury 6:37b6d0d56190 258 displayMessage("Scroll with joystick");
samdanbury 6:37b6d0d56190 259 }
samdanbury 6:37b6d0d56190 260 return rc;
samdanbury 6:37b6d0d56190 261 }
samdanbury 6:37b6d0d56190 262
samdanbury 6:37b6d0d56190 263
samdanbury 6:37b6d0d56190 264 int getConnTimeout(int attemptNumber)
samdanbury 6:37b6d0d56190 265 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
samdanbury 6:37b6d0d56190 266 // after 20 attempts, retry every 10 minutes
samdanbury 6:37b6d0d56190 267 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
samdanbury 6:37b6d0d56190 268 }
samdanbury 6:37b6d0d56190 269
samdanbury 6:37b6d0d56190 270
mazgch 12:d6b94e8051b0 271 void attemptConnect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack)
samdanbury 6:37b6d0d56190 272 {
samdanbury 6:37b6d0d56190 273 int retryAttempt = 0;
samdanbury 6:37b6d0d56190 274 connected = false;
icraggs 8:80d49dd91542 275
icraggs 8:80d49dd91542 276 // make sure a cable is connected before starting to connect
icraggs 8:80d49dd91542 277 while (!linkStatus()) {
icraggs 8:80d49dd91542 278 wait(1.0f);
mazgch 12:d6b94e8051b0 279 WARN("Internet link not present. Check cable connection\n");
icraggs 8:80d49dd91542 280 }
samdanbury 6:37b6d0d56190 281
samdanbury 6:37b6d0d56190 282 while (connect(client, ipstack) != 0)
samdanbury 6:37b6d0d56190 283 {
samdanbury 6:37b6d0d56190 284 #if defined(TARGET_K64F)
samdanbury 6:37b6d0d56190 285 red();
samdanbury 6:37b6d0d56190 286 #else
samdanbury 6:37b6d0d56190 287 Thread red_thread(flashing_red);
samdanbury 6:37b6d0d56190 288 #endif
samdanbury 6:37b6d0d56190 289 int timeout = getConnTimeout(++retryAttempt);
samdanbury 6:37b6d0d56190 290 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
icraggs 8:80d49dd91542 291
icraggs 8:80d49dd91542 292 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
icraggs 8:80d49dd91542 293 // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
icraggs 8:80d49dd91542 294
icraggs 8:80d49dd91542 295 // this works - reset the system when the retry count gets to a threshold
icraggs 8:80d49dd91542 296 if (retryAttempt == 5)
icraggs 8:80d49dd91542 297 NVIC_SystemReset();
icraggs 8:80d49dd91542 298 else
icraggs 8:80d49dd91542 299 wait(timeout);
samdanbury 6:37b6d0d56190 300 }
samdanbury 6:37b6d0d56190 301 }
samdanbury 6:37b6d0d56190 302
samdanbury 6:37b6d0d56190 303
mazgch 12:d6b94e8051b0 304 int publish(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack)
samdanbury 6:37b6d0d56190 305 {
samdanbury 6:37b6d0d56190 306 MQTT::Message message;
samdanbury 6:37b6d0d56190 307 char* pubTopic = "iot-2/evt/status/fmt/json";
samdanbury 6:37b6d0d56190 308
samdanbury 6:37b6d0d56190 309 char buf[250];
mazgch 15:7c3279ac3c52 310 int l = 0;
mazgch 16:a931c0a6d987 311 l += sprintf(buf+l,"{\"d\":{\"myName\":\"%s\"", BOARD_NAME);
mazgch 15:7c3279ac3c52 312 if (shieldConnected) {
mazgch 15:7c3279ac3c52 313 l += sprintf(buf+l,",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f", MMA.x(), MMA.y(), MMA.z());
mazgch 15:7c3279ac3c52 314 l += sprintf(buf+l,",\"temp\":%0.4f", sensor.temp());
mazgch 15:7c3279ac3c52 315 l += sprintf(buf+l,",\"joystick\":\"%s\"", joystickPos);
mazgch 15:7c3279ac3c52 316 l += sprintf(buf+l,",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f", ain1.read(), ain2.read());
mazgch 15:7c3279ac3c52 317 }
mazgch 15:7c3279ac3c52 318 l += sprintf(buf+l,"}}");
mazgch 15:7c3279ac3c52 319
samdanbury 6:37b6d0d56190 320 message.qos = MQTT::QOS0;
samdanbury 6:37b6d0d56190 321 message.retained = false;
samdanbury 6:37b6d0d56190 322 message.dup = false;
samdanbury 6:37b6d0d56190 323 message.payload = (void*)buf;
samdanbury 6:37b6d0d56190 324 message.payloadlen = strlen(buf);
samdanbury 6:37b6d0d56190 325
samdanbury 6:37b6d0d56190 326 LOG("Publishing %s\n", buf);
icraggs 8:80d49dd91542 327 return client->publish(pubTopic, message);
samdanbury 6:37b6d0d56190 328 }
samdanbury 6:37b6d0d56190 329
mazgch 17:b08722ede282 330 int publishMdm(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, MDMParser* mdm)
mazgch 17:b08722ede282 331 {
mazgch 17:b08722ede282 332 MQTT::Message message;
mazgch 17:b08722ede282 333 char* pubTopic = "iot-2/evt/modem/fmt/json";
mazgch 17:b08722ede282 334 char buf[250];
mazgch 17:b08722ede282 335 int l = 0;
mazgch 17:b08722ede282 336 MDMParser::IP ip = mdm->getIpAddress();
mazgch 17:b08722ede282 337 l += sprintf(buf+l,"{\"d\":{\"ip\":\"" IPSTR "\"", IPNUM(ip));
mazgch 17:b08722ede282 338 MDMParser::NetStatus sta;
mazgch 17:b08722ede282 339 mdm->checkNetStatus(&sta);
mazgch 17:b08722ede282 340 //const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
mazgch 17:b08722ede282 341 //if (sta.act < sizeof(txtAct)/sizeof(*txtAct) && (sta.act != MDMParser::ACT_UNKNOWN))
mazgch 17:b08722ede282 342 // l += sprintf(buf+l,",\"act\":\"%s\"",txtAct[sta.act]);
mazgch 17:b08722ede282 343 //if (*sta.num) l += sprintf(buf+l,",\"num\":\"%s\"",sta.num);
mazgch 17:b08722ede282 344 //if (sta.lac != 0xFFFF) l += sprintf(buf+l,",\"lac\":\"%04X\"",sta.lac);
mazgch 17:b08722ede282 345 //if (sta.ci != 0xFFFFFFFF) l += sprintf(buf+l,",\"ci\":\"%06X\"",sta.ci);
mazgch 17:b08722ede282 346 if (sta.rssi) l += sprintf(buf+l,",\"rssi\":%d",sta.rssi);
mazgch 17:b08722ede282 347 if (sta.ber) l += sprintf(buf+l,",\"ber\":%d",sta.ber);
mazgch 17:b08722ede282 348 if (*sta.opr) l += sprintf(buf+l,",\"operator\":\"%s\"",sta.opr);
mazgch 17:b08722ede282 349 l += sprintf(buf+l,"}}");
mazgch 17:b08722ede282 350
mazgch 17:b08722ede282 351 message.qos = MQTT::QOS0;
mazgch 17:b08722ede282 352 message.retained = false;
mazgch 17:b08722ede282 353 message.dup = false;
mazgch 17:b08722ede282 354 message.payload = (void*)buf;
mazgch 17:b08722ede282 355 message.payloadlen = strlen(buf);
mazgch 17:b08722ede282 356
mazgch 17:b08722ede282 357 LOG("Publishing %s\n", buf);
mazgch 17:b08722ede282 358 return client->publish(pubTopic, message);
mazgch 17:b08722ede282 359 }
mazgch 17:b08722ede282 360
mazgch 15:7c3279ac3c52 361 int publishGps(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, double lat, double lon)
mazgch 15:7c3279ac3c52 362 {
mazgch 15:7c3279ac3c52 363 MQTT::Message message;
mazgch 15:7c3279ac3c52 364 char* pubTopic = "iot-2/evt/gps/fmt/json";
mazgch 15:7c3279ac3c52 365 char buf[250];
mazgch 15:7c3279ac3c52 366 sprintf(buf,"{\"d\":{\"lat\":\"%.7f\",\"long\":\"%.7f\"}}", lat, lon);
mazgch 15:7c3279ac3c52 367 message.qos = MQTT::QOS0;
mazgch 15:7c3279ac3c52 368 message.retained = false;
mazgch 15:7c3279ac3c52 369 message.dup = false;
mazgch 15:7c3279ac3c52 370 message.payload = (void*)buf;
mazgch 15:7c3279ac3c52 371 message.payloadlen = strlen(buf);
mazgch 15:7c3279ac3c52 372 LOG("Publishing GPS %s\n", buf);
mazgch 15:7c3279ac3c52 373 return client->publish(pubTopic, message);
mazgch 15:7c3279ac3c52 374 }
mazgch 15:7c3279ac3c52 375
samdanbury 6:37b6d0d56190 376 void messageArrived(MQTT::MessageData& md)
samdanbury 6:37b6d0d56190 377 {
samdanbury 6:37b6d0d56190 378 MQTT::Message &message = md.message;
samdanbury 6:37b6d0d56190 379 char topic[md.topicName.lenstring.len + 1];
samdanbury 6:37b6d0d56190 380
samdanbury 6:37b6d0d56190 381 sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
samdanbury 6:37b6d0d56190 382
samdanbury 6:37b6d0d56190 383 LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload);
samdanbury 6:37b6d0d56190 384
samdanbury 6:37b6d0d56190 385 // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/
samdanbury 6:37b6d0d56190 386 char* start = strstr(topic, "/cmd/") + 5;
samdanbury 6:37b6d0d56190 387 int len = strstr(topic, "/fmt/") - start;
samdanbury 6:37b6d0d56190 388
samdanbury 6:37b6d0d56190 389 if (memcmp(start, "blink", len) == 0)
samdanbury 6:37b6d0d56190 390 {
samdanbury 6:37b6d0d56190 391 char payload[message.payloadlen + 1];
samdanbury 6:37b6d0d56190 392 sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload);
samdanbury 6:37b6d0d56190 393
samdanbury 6:37b6d0d56190 394 char* pos = strchr(payload, '}');
samdanbury 6:37b6d0d56190 395 if (pos != NULL)
samdanbury 6:37b6d0d56190 396 {
samdanbury 6:37b6d0d56190 397 *pos = '\0';
samdanbury 6:37b6d0d56190 398 if ((pos = strchr(payload, ':')) != NULL)
samdanbury 6:37b6d0d56190 399 {
samdanbury 6:37b6d0d56190 400 int blink_rate = atoi(pos + 1);
samdanbury 6:37b6d0d56190 401 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
samdanbury 6:37b6d0d56190 402 }
samdanbury 6:37b6d0d56190 403 }
samdanbury 6:37b6d0d56190 404 }
samdanbury 6:37b6d0d56190 405 else
samdanbury 6:37b6d0d56190 406 WARN("Unsupported command: %.*s\n", len, start);
samdanbury 6:37b6d0d56190 407 }
samdanbury 6:37b6d0d56190 408
samdanbury 6:37b6d0d56190 409 int main()
samdanbury 6:37b6d0d56190 410 {
icraggs 8:80d49dd91542 411 quickstartMode = (strcmp(org, "quickstart") == 0);
mazgch 15:7c3279ac3c52 412 shieldConnected = MMA.testConnection() && sensor.open();
icraggs 8:80d49dd91542 413
samdanbury 6:37b6d0d56190 414 lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen
samdanbury 6:37b6d0d56190 415
samdanbury 6:37b6d0d56190 416 led2 = LED2_OFF; // K64F: turn off the main board LED
samdanbury 6:37b6d0d56190 417
samdanbury 6:37b6d0d56190 418 displayMessage("Connecting");
samdanbury 6:37b6d0d56190 419 #if defined(TARGET_K64F)
samdanbury 6:37b6d0d56190 420 yellow(); // Don't flash on the K64F, because starting a thread causes the EthernetInterface init call to hang
samdanbury 6:37b6d0d56190 421 #else
samdanbury 6:37b6d0d56190 422 Thread yellow_thread(flashing_yellow);
samdanbury 6:37b6d0d56190 423 #endif
samdanbury 6:37b6d0d56190 424
mazgch 12:d6b94e8051b0 425 MDMSerial mdm;
mazgch 17:b08722ede282 426 //mdm.setDebug(3); // enable this for debugging issues
mazgch 12:d6b94e8051b0 427 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
mazgch 12:d6b94e8051b0 428 return -1;
mazgch 13:6a0af5deb2db 429
mazgch 15:7c3279ac3c52 430 GPSI2C gps;
mazgch 15:7c3279ac3c52 431
mazgch 12:d6b94e8051b0 432 MQTT_CLIENT_TYPE ipstack;
mazgch 12:d6b94e8051b0 433 MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
samdanbury 6:37b6d0d56190 434 if (quickstartMode)
samdanbury 6:37b6d0d56190 435 {
mazgch 15:7c3279ac3c52 436 MDMParser::DevStatus dev;
mazgch 15:7c3279ac3c52 437 mdm.getDevStatus(&dev);
mazgch 15:7c3279ac3c52 438 if (*dev.imei) strncpy(id, dev.imei, sizeof(id)-1);
mazgch 15:7c3279ac3c52 439 else if (*dev.meid) strncpy(id, dev.meid, sizeof(id)-1);
samdanbury 6:37b6d0d56190 440 }
samdanbury 6:37b6d0d56190 441
mazgch 16:a931c0a6d987 442 LOG("IBM Quickstart: https://quickstart.internetofthings.ibmcloud.com/#/device/%s/sensor/\n", id);
mazgch 16:a931c0a6d987 443
samdanbury 6:37b6d0d56190 444 attemptConnect(&client, &ipstack);
samdanbury 6:37b6d0d56190 445
samdanbury 6:37b6d0d56190 446 if (!quickstartMode)
samdanbury 6:37b6d0d56190 447 {
samdanbury 6:37b6d0d56190 448 int rc = 0;
samdanbury 6:37b6d0d56190 449 if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0)
samdanbury 6:37b6d0d56190 450 WARN("rc from MQTT subscribe is %d\n", rc);
samdanbury 6:37b6d0d56190 451 }
samdanbury 6:37b6d0d56190 452
samdanbury 6:37b6d0d56190 453 blink_interval = 0;
samdanbury 6:37b6d0d56190 454 int count = 0;
mazgch 15:7c3279ac3c52 455 int count2 = 0;
samdanbury 6:37b6d0d56190 456 while (true)
samdanbury 6:37b6d0d56190 457 {
samdanbury 6:37b6d0d56190 458 if (++count == 100)
samdanbury 6:37b6d0d56190 459 { // Publish a message every second
mazgch 17:b08722ede282 460 if ((publish(&client, &ipstack) != 0) ||
mazgch 17:b08722ede282 461 (publishMdm(&client, &ipstack, &mdm) != 0))
samdanbury 6:37b6d0d56190 462 attemptConnect(&client, &ipstack); // if we have lost the connection
samdanbury 6:37b6d0d56190 463 count = 0;
samdanbury 6:37b6d0d56190 464 }
mazgch 15:7c3279ac3c52 465 if (++count2 == 10)
mazgch 15:7c3279ac3c52 466 { // Publish a message every second
mazgch 15:7c3279ac3c52 467 count2 = 0;
mazgch 15:7c3279ac3c52 468 while (1) {
mazgch 15:7c3279ac3c52 469 char buf[256];
mazgch 15:7c3279ac3c52 470 int ret = gps.getMessage(buf, sizeof(buf));
mazgch 15:7c3279ac3c52 471 if (ret <= 0) break;
mazgch 15:7c3279ac3c52 472 int len = LENGTH(ret);
mazgch 15:7c3279ac3c52 473 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) {
mazgch 15:7c3279ac3c52 474 // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS
mazgch 15:7c3279ac3c52 475 if ((buf[0] == '$') || buf[1] == 'G') {
mazgch 15:7c3279ac3c52 476 #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2]))
mazgch 15:7c3279ac3c52 477 if (_CHECK_TALKER("GGA")) {
mazgch 15:7c3279ac3c52 478 //printf("%.*s\n", len, buf);
mazgch 15:7c3279ac3c52 479 char ch;
mazgch 15:7c3279ac3c52 480 double latitude, longitude, elevation;
mazgch 15:7c3279ac3c52 481 static double lastLat = 0, lastLon = 0;
mazgch 15:7c3279ac3c52 482 if (gps.getNmeaAngle(2,buf,len,latitude) &&
mazgch 15:7c3279ac3c52 483 gps.getNmeaAngle(4,buf,len,longitude) &&
mazgch 15:7c3279ac3c52 484 gps.getNmeaItem(6,buf,len,ch) &&
mazgch 15:7c3279ac3c52 485 gps.getNmeaItem(9,buf,len,elevation)) {
mazgch 15:7c3279ac3c52 486 //printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch);
mazgch 16:a931c0a6d987 487 if ((ch == '1' || ch == '2' || ch == '6') &&
mazgch 16:a931c0a6d987 488 (fabs(lastLat - latitude) < 0.0000001) &&
mazgch 16:a931c0a6d987 489 (fabs(lastLon - longitude) < 0.0000001)) {
mazgch 15:7c3279ac3c52 490 publishGps(&client, &ipstack, latitude, longitude);
mazgch 15:7c3279ac3c52 491 lastLat = latitude;
mazgch 15:7c3279ac3c52 492 lastLon = longitude;
mazgch 15:7c3279ac3c52 493 }
mazgch 15:7c3279ac3c52 494 }
mazgch 15:7c3279ac3c52 495 }
mazgch 15:7c3279ac3c52 496 }
mazgch 15:7c3279ac3c52 497 }
mazgch 15:7c3279ac3c52 498 }
mazgch 15:7c3279ac3c52 499 }
samdanbury 6:37b6d0d56190 500
samdanbury 6:37b6d0d56190 501 if (blink_interval == 0)
samdanbury 6:37b6d0d56190 502 led2 = LED2_OFF;
samdanbury 6:37b6d0d56190 503 else if (count % blink_interval == 0)
samdanbury 6:37b6d0d56190 504 led2 = !led2;
samdanbury 6:37b6d0d56190 505 if (count % 20 == 0)
samdanbury 6:37b6d0d56190 506 setMenu();
samdanbury 6:37b6d0d56190 507 client.yield(10); // allow the MQTT client to receive messages
samdanbury 6:37b6d0d56190 508 }
samdanbury 6:37b6d0d56190 509 }