working
Dependencies: C027_Support IBMIoTClientCellularExample MQTT mbed
Fork of MQTT-GSM-GPS-CAN-SMS by
main.cpp@19:689b00477c81, 2017-02-22 (annotated)
- Committer:
- alikapatibabu
- Date:
- Wed Feb 22 10:27:35 2017 +0000
- Revision:
- 19:689b00477c81
- Parent:
- 18:e9e87362018b
updated
Who changed what in which revision?
User | Revision | Line number | New 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 "MQTTClient.h" |
samdanbury | 6:37b6d0d56190 | 27 | #include "Arial12x12.h" |
alikapatibabu | 18:e9e87362018b | 28 | //#include "rtos.h" |
samdanbury | 6:37b6d0d56190 | 29 | |
chris | 10:0b5e0dfee08e | 30 | // Update this to the next number *before* a commit |
mazgch | 16:a931c0a6d987 | 31 | #define __APP_SW_REVISION__ "11" |
mazgch | 16:a931c0a6d987 | 32 | #define BOARD_NAME "IoT u-blox" |
chris | 10:0b5e0dfee08e | 33 | |
samdanbury | 6:37b6d0d56190 | 34 | // Configuration values needed to connect to IBM IoT Cloud |
icraggs | 8:80d49dd91542 | 35 | #define ORG "quickstart" // For a registered connection, replace with your org |
icraggs | 8:80d49dd91542 | 36 | #define ID "" // For a registered connection, replace with your id |
icraggs | 8:80d49dd91542 | 37 | #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token |
mazgch | 12:d6b94e8051b0 | 38 | #define TYPE_NAME DEFAULT_TYPE_NAME // For a registered connection, replace with your type |
samdanbury | 6:37b6d0d56190 | 39 | |
samdanbury | 6:37b6d0d56190 | 40 | #define MQTT_PORT 1883 |
samdanbury | 6:37b6d0d56190 | 41 | #define MQTT_TLS_PORT 8883 |
samdanbury | 6:37b6d0d56190 | 42 | #define IBM_IOT_PORT MQTT_PORT |
samdanbury | 6:37b6d0d56190 | 43 | |
samdanbury | 6:37b6d0d56190 | 44 | #define MQTT_MAX_PACKET_SIZE 250 |
samdanbury | 6:37b6d0d56190 | 45 | |
alikapatibabu | 18:e9e87362018b | 46 | #define MAX_SMS_LEN 160 |
alikapatibabu | 19:689b00477c81 | 47 | char strMobilenumber[32] = "xxxxxxxxxxx"; |
alikapatibabu | 18:e9e87362018b | 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 |
alikapatibabu | 18:e9e87362018b | 56 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
alikapatibabu | 18:e9e87362018b | 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 |
alikapatibabu | 18:e9e87362018b | 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 | PwmOut r(D5); |
mazgch | 14:6f36d75b3917 | 69 | PwmOut g(D9); |
mazgch | 14:6f36d75b3917 | 70 | PwmOut b(D8); |
alikapatibabu | 18:e9e87362018b | 71 | |
alikapatibabu | 18:e9e87362018b | 72 | DigitalOut led2(LED2); |
alikapatibabu | 18:e9e87362018b | 73 | |
mazgch | 14:6f36d75b3917 | 74 | |
mazgch | 14:6f36d75b3917 | 75 | #define LED2_OFF 0 |
mazgch | 14:6f36d75b3917 | 76 | #define LED2_ON 1 |
mazgch | 14:6f36d75b3917 | 77 | |
alikapatibabu | 18:e9e87362018b | 78 | static uint32_t linkStatus(void) |
alikapatibabu | 18:e9e87362018b | 79 | { |
alikapatibabu | 18:e9e87362018b | 80 | return true; |
alikapatibabu | 18:e9e87362018b | 81 | } |
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; |
alikapatibabu | 18:e9e87362018b | 89 | char org[11] = "jne5sm"; |
alikapatibabu | 18:e9e87362018b | 90 | char type[30] = "hud"; |
alikapatibabu | 18:e9e87362018b | 91 | char id[30] = "emb001"; // device ID |
alikapatibabu | 18:e9e87362018b | 92 | char auth_token[30] = "Embitel123"; // 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 | |
alikapatibabu | 18:e9e87362018b | 99 | CAN can1(CANRD, CANTD); |
alikapatibabu | 18:e9e87362018b | 100 | |
alikapatibabu | 18:e9e87362018b | 101 | struct vehicleParams { |
alikapatibabu | 18:e9e87362018b | 102 | |
alikapatibabu | 18:e9e87362018b | 103 | unsigned int speed; |
alikapatibabu | 18:e9e87362018b | 104 | unsigned int EngRpm; |
alikapatibabu | 18:e9e87362018b | 105 | unsigned int FuelLevel; |
alikapatibabu | 18:e9e87362018b | 106 | unsigned int EngCoolantTemp; |
alikapatibabu | 18:e9e87362018b | 107 | unsigned int A_BCall; |
alikapatibabu | 18:e9e87362018b | 108 | unsigned int A_ECall; |
alikapatibabu | 18:e9e87362018b | 109 | bool ABSIndcnON; |
alikapatibabu | 18:e9e87362018b | 110 | |
alikapatibabu | 18:e9e87362018b | 111 | |
alikapatibabu | 18:e9e87362018b | 112 | }; |
alikapatibabu | 18:e9e87362018b | 113 | |
alikapatibabu | 18:e9e87362018b | 114 | // Endzeit |
alikapatibabu | 18:e9e87362018b | 115 | time_t timeStop; |
alikapatibabu | 18:e9e87362018b | 116 | |
alikapatibabu | 18:e9e87362018b | 117 | unsigned char readBit(char strByte, char intRow) // Is a bit of a byte from , counting from the right , intRow = 0 - 7. |
alikapatibabu | 18:e9e87362018b | 118 | { |
alikapatibabu | 18:e9e87362018b | 119 | return strByte >> intRow & 1; |
alikapatibabu | 18:e9e87362018b | 120 | } |
samdanbury | 6:37b6d0d56190 | 121 | |
samdanbury | 6:37b6d0d56190 | 122 | void off() |
samdanbury | 6:37b6d0d56190 | 123 | { |
samdanbury | 6:37b6d0d56190 | 124 | r = g = b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 125 | } |
samdanbury | 6:37b6d0d56190 | 126 | |
samdanbury | 6:37b6d0d56190 | 127 | void red() |
samdanbury | 6:37b6d0d56190 | 128 | { |
alikapatibabu | 18:e9e87362018b | 129 | r = 0.7; |
alikapatibabu | 18:e9e87362018b | 130 | g = 1.0; |
alikapatibabu | 18:e9e87362018b | 131 | b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 132 | } |
samdanbury | 6:37b6d0d56190 | 133 | |
samdanbury | 6:37b6d0d56190 | 134 | void yellow() |
samdanbury | 6:37b6d0d56190 | 135 | { |
alikapatibabu | 18:e9e87362018b | 136 | r = 0.7; |
alikapatibabu | 18:e9e87362018b | 137 | g = 0.7; |
alikapatibabu | 18:e9e87362018b | 138 | b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 139 | } |
samdanbury | 6:37b6d0d56190 | 140 | |
samdanbury | 6:37b6d0d56190 | 141 | void green() |
samdanbury | 6:37b6d0d56190 | 142 | { |
alikapatibabu | 18:e9e87362018b | 143 | r = 1.0; |
alikapatibabu | 18:e9e87362018b | 144 | g = 0.7; |
alikapatibabu | 18:e9e87362018b | 145 | b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 146 | } |
samdanbury | 6:37b6d0d56190 | 147 | |
samdanbury | 6:37b6d0d56190 | 148 | |
samdanbury | 6:37b6d0d56190 | 149 | void flashing_yellow(void const *args) |
samdanbury | 6:37b6d0d56190 | 150 | { |
samdanbury | 6:37b6d0d56190 | 151 | bool on = false; |
alikapatibabu | 18:e9e87362018b | 152 | while (!connected) { // flashing yellow only while connecting |
alikapatibabu | 18:e9e87362018b | 153 | on = !on; |
samdanbury | 6:37b6d0d56190 | 154 | if (on) |
samdanbury | 6:37b6d0d56190 | 155 | yellow(); |
samdanbury | 6:37b6d0d56190 | 156 | else |
alikapatibabu | 18:e9e87362018b | 157 | off(); |
samdanbury | 6:37b6d0d56190 | 158 | wait(0.5); |
samdanbury | 6:37b6d0d56190 | 159 | } |
samdanbury | 6:37b6d0d56190 | 160 | } |
samdanbury | 6:37b6d0d56190 | 161 | |
samdanbury | 6:37b6d0d56190 | 162 | |
samdanbury | 6:37b6d0d56190 | 163 | void flashing_red(void const *args) // to be used when the connection is lost |
samdanbury | 6:37b6d0d56190 | 164 | { |
samdanbury | 6:37b6d0d56190 | 165 | bool on = false; |
alikapatibabu | 18:e9e87362018b | 166 | while (!connected) { |
samdanbury | 6:37b6d0d56190 | 167 | on = !on; |
samdanbury | 6:37b6d0d56190 | 168 | if (on) |
samdanbury | 6:37b6d0d56190 | 169 | red(); |
samdanbury | 6:37b6d0d56190 | 170 | else |
samdanbury | 6:37b6d0d56190 | 171 | off(); |
samdanbury | 6:37b6d0d56190 | 172 | wait(2.0); |
samdanbury | 6:37b6d0d56190 | 173 | } |
samdanbury | 6:37b6d0d56190 | 174 | } |
samdanbury | 6:37b6d0d56190 | 175 | |
alikapatibabu | 18:e9e87362018b | 176 | int connect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) |
samdanbury | 6:37b6d0d56190 | 177 | { |
alikapatibabu | 18:e9e87362018b | 178 | const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; |
samdanbury | 6:37b6d0d56190 | 179 | |
samdanbury | 6:37b6d0d56190 | 180 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
samdanbury | 6:37b6d0d56190 | 181 | sprintf(hostname, "%s%s", org, iot_ibm); |
mazgch | 11:7a6df9a2dcdc | 182 | DEBUG("hostname is %s\n", hostname); |
samdanbury | 6:37b6d0d56190 | 183 | int rc = ipstack->connect(hostname, IBM_IOT_PORT); |
alikapatibabu | 18:e9e87362018b | 184 | if (rc != 0){ |
alikapatibabu | 18:e9e87362018b | 185 | DEBUG("connect Error: %d \n", rc); |
samdanbury | 6:37b6d0d56190 | 186 | return rc; |
alikapatibabu | 18:e9e87362018b | 187 | } |
alikapatibabu | 18:e9e87362018b | 188 | |
samdanbury | 6:37b6d0d56190 | 189 | // Construct clientId - d:org:type:id |
samdanbury | 6:37b6d0d56190 | 190 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
samdanbury | 6:37b6d0d56190 | 191 | sprintf(clientId, "d:%s:%s:%s", org, type, id); |
samdanbury | 6:37b6d0d56190 | 192 | DEBUG("clientid is %s\n", clientId); |
alikapatibabu | 18:e9e87362018b | 193 | |
samdanbury | 6:37b6d0d56190 | 194 | // MQTT Connect |
samdanbury | 6:37b6d0d56190 | 195 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
samdanbury | 6:37b6d0d56190 | 196 | data.MQTTVersion = 3; |
samdanbury | 6:37b6d0d56190 | 197 | data.clientID.cstring = clientId; |
alikapatibabu | 18:e9e87362018b | 198 | |
alikapatibabu | 18:e9e87362018b | 199 | if (!quickstartMode) { |
samdanbury | 6:37b6d0d56190 | 200 | data.username.cstring = "use-token-auth"; |
samdanbury | 6:37b6d0d56190 | 201 | data.password.cstring = auth_token; |
samdanbury | 6:37b6d0d56190 | 202 | } |
alikapatibabu | 18:e9e87362018b | 203 | if ((rc = client->connect(data)) == 0) { |
samdanbury | 6:37b6d0d56190 | 204 | connected = true; |
alikapatibabu | 18:e9e87362018b | 205 | green(); |
samdanbury | 6:37b6d0d56190 | 206 | wait(2); |
alikapatibabu | 18:e9e87362018b | 207 | } |
samdanbury | 6:37b6d0d56190 | 208 | return rc; |
samdanbury | 6:37b6d0d56190 | 209 | } |
samdanbury | 6:37b6d0d56190 | 210 | |
samdanbury | 6:37b6d0d56190 | 211 | |
samdanbury | 6:37b6d0d56190 | 212 | int getConnTimeout(int attemptNumber) |
alikapatibabu | 18:e9e87362018b | 213 | { |
alikapatibabu | 18:e9e87362018b | 214 | // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
alikapatibabu | 18:e9e87362018b | 215 | // after 20 attempts, retry every 10 minutes |
samdanbury | 6:37b6d0d56190 | 216 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
samdanbury | 6:37b6d0d56190 | 217 | } |
samdanbury | 6:37b6d0d56190 | 218 | |
mazgch | 12:d6b94e8051b0 | 219 | void attemptConnect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) |
samdanbury | 6:37b6d0d56190 | 220 | { |
samdanbury | 6:37b6d0d56190 | 221 | int retryAttempt = 0; |
samdanbury | 6:37b6d0d56190 | 222 | connected = false; |
alikapatibabu | 18:e9e87362018b | 223 | |
icraggs | 8:80d49dd91542 | 224 | // make sure a cable is connected before starting to connect |
icraggs | 8:80d49dd91542 | 225 | while (!linkStatus()) { |
icraggs | 8:80d49dd91542 | 226 | wait(1.0f); |
mazgch | 12:d6b94e8051b0 | 227 | WARN("Internet link not present. Check cable connection\n"); |
icraggs | 8:80d49dd91542 | 228 | } |
alikapatibabu | 18:e9e87362018b | 229 | while (connect(client, ipstack) != 0) { |
alikapatibabu | 18:e9e87362018b | 230 | //Thread red_thread(flashing_red); |
alikapatibabu | 18:e9e87362018b | 231 | |
samdanbury | 6:37b6d0d56190 | 232 | int timeout = getConnTimeout(++retryAttempt); |
samdanbury | 6:37b6d0d56190 | 233 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
alikapatibabu | 18:e9e87362018b | 234 | |
icraggs | 8:80d49dd91542 | 235 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
icraggs | 8:80d49dd91542 | 236 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
alikapatibabu | 18:e9e87362018b | 237 | |
icraggs | 8:80d49dd91542 | 238 | // this works - reset the system when the retry count gets to a threshold |
icraggs | 8:80d49dd91542 | 239 | if (retryAttempt == 5) |
icraggs | 8:80d49dd91542 | 240 | NVIC_SystemReset(); |
icraggs | 8:80d49dd91542 | 241 | else |
icraggs | 8:80d49dd91542 | 242 | wait(timeout); |
samdanbury | 6:37b6d0d56190 | 243 | } |
samdanbury | 6:37b6d0d56190 | 244 | } |
samdanbury | 6:37b6d0d56190 | 245 | |
samdanbury | 6:37b6d0d56190 | 246 | |
mazgch | 12:d6b94e8051b0 | 247 | int publish(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) |
samdanbury | 6:37b6d0d56190 | 248 | { |
samdanbury | 6:37b6d0d56190 | 249 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 250 | char* pubTopic = "iot-2/cmd/gps/fmt/json"; |
alikapatibabu | 18:e9e87362018b | 251 | |
samdanbury | 6:37b6d0d56190 | 252 | char buf[250]; |
mazgch | 15:7c3279ac3c52 | 253 | int l = 0; |
alikapatibabu | 18:e9e87362018b | 254 | l += sprintf(buf+l,"{\"lat\":100,\"long\":50}"); |
mazgch | 15:7c3279ac3c52 | 255 | |
samdanbury | 6:37b6d0d56190 | 256 | message.qos = MQTT::QOS0; |
samdanbury | 6:37b6d0d56190 | 257 | message.retained = false; |
samdanbury | 6:37b6d0d56190 | 258 | message.dup = false; |
samdanbury | 6:37b6d0d56190 | 259 | message.payload = (void*)buf; |
samdanbury | 6:37b6d0d56190 | 260 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 261 | |
samdanbury | 6:37b6d0d56190 | 262 | LOG("Publishing %s\n", buf); |
alikapatibabu | 18:e9e87362018b | 263 | LOG("Publiching Topic: %s\n",pubTopic); |
icraggs | 8:80d49dd91542 | 264 | return client->publish(pubTopic, message); |
alikapatibabu | 18:e9e87362018b | 265 | |
samdanbury | 6:37b6d0d56190 | 266 | } |
samdanbury | 6:37b6d0d56190 | 267 | |
mazgch | 17:b08722ede282 | 268 | int publishMdm(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, MDMParser* mdm) |
mazgch | 17:b08722ede282 | 269 | { |
mazgch | 17:b08722ede282 | 270 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 271 | char* pubTopic = "iot-2/cmd/modem/fmt/json"; |
mazgch | 17:b08722ede282 | 272 | char buf[250]; |
mazgch | 17:b08722ede282 | 273 | int l = 0; |
mazgch | 17:b08722ede282 | 274 | MDMParser::IP ip = mdm->getIpAddress(); |
mazgch | 17:b08722ede282 | 275 | l += sprintf(buf+l,"{\"d\":{\"ip\":\"" IPSTR "\"", IPNUM(ip)); |
mazgch | 17:b08722ede282 | 276 | MDMParser::NetStatus sta; |
mazgch | 17:b08722ede282 | 277 | mdm->checkNetStatus(&sta); |
mazgch | 17:b08722ede282 | 278 | if (sta.rssi) l += sprintf(buf+l,",\"rssi\":%d",sta.rssi); |
mazgch | 17:b08722ede282 | 279 | if (sta.ber) l += sprintf(buf+l,",\"ber\":%d",sta.ber); |
mazgch | 17:b08722ede282 | 280 | if (*sta.opr) l += sprintf(buf+l,",\"operator\":\"%s\"",sta.opr); |
mazgch | 17:b08722ede282 | 281 | l += sprintf(buf+l,"}}"); |
mazgch | 17:b08722ede282 | 282 | |
mazgch | 17:b08722ede282 | 283 | message.qos = MQTT::QOS0; |
mazgch | 17:b08722ede282 | 284 | message.retained = false; |
mazgch | 17:b08722ede282 | 285 | message.dup = false; |
mazgch | 17:b08722ede282 | 286 | message.payload = (void*)buf; |
mazgch | 17:b08722ede282 | 287 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 288 | |
mazgch | 17:b08722ede282 | 289 | LOG("Publishing %s\n", buf); |
mazgch | 17:b08722ede282 | 290 | return client->publish(pubTopic, message); |
mazgch | 17:b08722ede282 | 291 | } |
mazgch | 17:b08722ede282 | 292 | |
mazgch | 15:7c3279ac3c52 | 293 | int publishGps(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, double lat, double lon) |
mazgch | 15:7c3279ac3c52 | 294 | { |
mazgch | 15:7c3279ac3c52 | 295 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 296 | |
mazgch | 15:7c3279ac3c52 | 297 | char* pubTopic = "iot-2/evt/gps/fmt/json"; |
mazgch | 15:7c3279ac3c52 | 298 | char buf[250]; |
alikapatibabu | 18:e9e87362018b | 299 | //lat = 500.2323; |
alikapatibabu | 18:e9e87362018b | 300 | //lon = 700.2323; |
alikapatibabu | 18:e9e87362018b | 301 | sprintf(buf,"{\"lat\":\"%.6f\",\"long\":\"%.6f\"}", lat, lon); |
mazgch | 15:7c3279ac3c52 | 302 | message.qos = MQTT::QOS0; |
mazgch | 15:7c3279ac3c52 | 303 | message.retained = false; |
mazgch | 15:7c3279ac3c52 | 304 | message.dup = false; |
mazgch | 15:7c3279ac3c52 | 305 | message.payload = (void*)buf; |
mazgch | 15:7c3279ac3c52 | 306 | message.payloadlen = strlen(buf); |
mazgch | 15:7c3279ac3c52 | 307 | LOG("Publishing GPS %s\n", buf); |
mazgch | 15:7c3279ac3c52 | 308 | return client->publish(pubTopic, message); |
mazgch | 15:7c3279ac3c52 | 309 | } |
mazgch | 15:7c3279ac3c52 | 310 | |
alikapatibabu | 18:e9e87362018b | 311 | int publishSpeed(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, int speed) |
alikapatibabu | 18:e9e87362018b | 312 | { |
alikapatibabu | 18:e9e87362018b | 313 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 314 | char* pubTopic = "iot-2/evt/speed/fmt/json"; |
alikapatibabu | 18:e9e87362018b | 315 | char buf[250]; |
alikapatibabu | 18:e9e87362018b | 316 | //speed = 0x11; |
alikapatibabu | 18:e9e87362018b | 317 | sprintf(buf,"{\"value\":\"%1d\"}", speed); |
alikapatibabu | 18:e9e87362018b | 318 | message.qos = MQTT::QOS0; |
alikapatibabu | 18:e9e87362018b | 319 | message.retained = false; |
alikapatibabu | 18:e9e87362018b | 320 | message.dup = false; |
alikapatibabu | 18:e9e87362018b | 321 | message.payload = (void*)buf; |
alikapatibabu | 18:e9e87362018b | 322 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 323 | LOG("Publishing Speed %s \n", buf); |
alikapatibabu | 18:e9e87362018b | 324 | return client->publish(pubTopic, message); |
alikapatibabu | 18:e9e87362018b | 325 | } |
alikapatibabu | 18:e9e87362018b | 326 | |
alikapatibabu | 18:e9e87362018b | 327 | int publishFuelLevel(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, int FuelLevel) |
alikapatibabu | 18:e9e87362018b | 328 | { |
alikapatibabu | 18:e9e87362018b | 329 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 330 | char* pubTopic = "iot-2/evt/FuelLevel/fmt/json"; |
alikapatibabu | 18:e9e87362018b | 331 | char buf[250]; |
alikapatibabu | 18:e9e87362018b | 332 | //speed = 0x11; |
alikapatibabu | 18:e9e87362018b | 333 | sprintf(buf,"{\"value\":\"%1d\"}", FuelLevel); |
alikapatibabu | 18:e9e87362018b | 334 | message.qos = MQTT::QOS0; |
alikapatibabu | 18:e9e87362018b | 335 | message.retained = false; |
alikapatibabu | 18:e9e87362018b | 336 | message.dup = false; |
alikapatibabu | 18:e9e87362018b | 337 | message.payload = (void*)buf; |
alikapatibabu | 18:e9e87362018b | 338 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 339 | LOG("Publishing FuelLevel %s \n", buf); |
alikapatibabu | 18:e9e87362018b | 340 | return client->publish(pubTopic, message); |
alikapatibabu | 18:e9e87362018b | 341 | } |
alikapatibabu | 18:e9e87362018b | 342 | |
alikapatibabu | 18:e9e87362018b | 343 | int publishEngCoolantTemp(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, int EngOilTemp) |
alikapatibabu | 18:e9e87362018b | 344 | { |
alikapatibabu | 18:e9e87362018b | 345 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 346 | char* pubTopic = "iot-2/evt/EngOilTemp/fmt/json"; |
alikapatibabu | 18:e9e87362018b | 347 | char buf[250]; |
alikapatibabu | 18:e9e87362018b | 348 | //speed = 0x11; |
alikapatibabu | 18:e9e87362018b | 349 | sprintf(buf,"{\"value\":\"%1d\"}", EngOilTemp); |
alikapatibabu | 18:e9e87362018b | 350 | message.qos = MQTT::QOS0; |
alikapatibabu | 18:e9e87362018b | 351 | message.retained = false; |
alikapatibabu | 18:e9e87362018b | 352 | message.dup = false; |
alikapatibabu | 18:e9e87362018b | 353 | message.payload = (void*)buf; |
alikapatibabu | 18:e9e87362018b | 354 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 355 | LOG("Publishing EngOilTemp %s: %d \n", buf); |
alikapatibabu | 18:e9e87362018b | 356 | return client->publish(pubTopic, message); |
alikapatibabu | 18:e9e87362018b | 357 | } |
alikapatibabu | 18:e9e87362018b | 358 | |
alikapatibabu | 18:e9e87362018b | 359 | int publishA_BCall(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, int A_BCall) |
alikapatibabu | 18:e9e87362018b | 360 | { |
alikapatibabu | 18:e9e87362018b | 361 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 362 | char* pubTopic = "iot-2/evt/FuelLevel/fmt/json"; |
alikapatibabu | 18:e9e87362018b | 363 | char buf[250]; |
alikapatibabu | 18:e9e87362018b | 364 | //speed = 0x11; |
alikapatibabu | 18:e9e87362018b | 365 | sprintf(buf,"{\"value\":\"%1d\"}", A_BCall); |
alikapatibabu | 18:e9e87362018b | 366 | message.qos = MQTT::QOS0; |
alikapatibabu | 18:e9e87362018b | 367 | message.retained = false; |
alikapatibabu | 18:e9e87362018b | 368 | message.dup = false; |
alikapatibabu | 18:e9e87362018b | 369 | message.payload = (void*)buf; |
alikapatibabu | 18:e9e87362018b | 370 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 371 | LOG("Publishing A_BCall %s \n", buf); |
alikapatibabu | 18:e9e87362018b | 372 | return client->publish(pubTopic, message); |
alikapatibabu | 18:e9e87362018b | 373 | } |
alikapatibabu | 18:e9e87362018b | 374 | |
alikapatibabu | 18:e9e87362018b | 375 | int publishA_ECall(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, int A_ECall) |
alikapatibabu | 18:e9e87362018b | 376 | { |
alikapatibabu | 18:e9e87362018b | 377 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 378 | char* pubTopic = "iot-2/evt/FuelLevel/fmt/json"; |
alikapatibabu | 18:e9e87362018b | 379 | char buf[250]; |
alikapatibabu | 18:e9e87362018b | 380 | //speed = 0x11; |
alikapatibabu | 18:e9e87362018b | 381 | sprintf(buf,"{\"value\":\"%1d\"}", A_ECall); |
alikapatibabu | 18:e9e87362018b | 382 | message.qos = MQTT::QOS0; |
alikapatibabu | 18:e9e87362018b | 383 | message.retained = false; |
alikapatibabu | 18:e9e87362018b | 384 | message.dup = false; |
alikapatibabu | 18:e9e87362018b | 385 | message.payload = (void*)buf; |
alikapatibabu | 18:e9e87362018b | 386 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 387 | LOG("Publishing A_ECall %s \n", buf); |
alikapatibabu | 18:e9e87362018b | 388 | return client->publish(pubTopic, message); |
alikapatibabu | 18:e9e87362018b | 389 | } |
alikapatibabu | 18:e9e87362018b | 390 | |
alikapatibabu | 18:e9e87362018b | 391 | int publishEngRpm(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, int EngSpd) |
alikapatibabu | 18:e9e87362018b | 392 | { |
alikapatibabu | 18:e9e87362018b | 393 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 394 | char* pubTopic = "iot-2/evt/FuelLevel/fmt/json"; |
alikapatibabu | 18:e9e87362018b | 395 | char buf[250]; |
alikapatibabu | 18:e9e87362018b | 396 | sprintf(buf,"{\"value\":\"%1d\"}", 100); |
alikapatibabu | 18:e9e87362018b | 397 | message.qos = MQTT::QOS0; |
alikapatibabu | 18:e9e87362018b | 398 | message.retained = false; |
alikapatibabu | 18:e9e87362018b | 399 | message.dup = false; |
alikapatibabu | 18:e9e87362018b | 400 | message.payload = (void*)buf; |
alikapatibabu | 18:e9e87362018b | 401 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 402 | LOG("Publishing EngRpm %s \n", buf); |
alikapatibabu | 18:e9e87362018b | 403 | return client->publish(pubTopic, message); |
alikapatibabu | 18:e9e87362018b | 404 | } |
alikapatibabu | 18:e9e87362018b | 405 | |
alikapatibabu | 18:e9e87362018b | 406 | int publishABS(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, bool ABSIndcnON) |
alikapatibabu | 18:e9e87362018b | 407 | { |
alikapatibabu | 18:e9e87362018b | 408 | MQTT::Message message; |
alikapatibabu | 18:e9e87362018b | 409 | char* pubTopic = "iot-2/evt/ABSIndcnON/fmt/json"; |
alikapatibabu | 18:e9e87362018b | 410 | char buf[250]; |
alikapatibabu | 18:e9e87362018b | 411 | sprintf(buf,"{\"value\":\"%1d\"}", ABSIndcnON); |
alikapatibabu | 18:e9e87362018b | 412 | message.qos = MQTT::QOS0; |
alikapatibabu | 18:e9e87362018b | 413 | message.retained = false; |
alikapatibabu | 18:e9e87362018b | 414 | message.dup = false; |
alikapatibabu | 18:e9e87362018b | 415 | message.payload = (void*)buf; |
alikapatibabu | 18:e9e87362018b | 416 | message.payloadlen = strlen(buf); |
alikapatibabu | 18:e9e87362018b | 417 | LOG("Publishing ABS %s \n", buf); |
alikapatibabu | 18:e9e87362018b | 418 | return client->publish(pubTopic, message); |
alikapatibabu | 18:e9e87362018b | 419 | } |
alikapatibabu | 18:e9e87362018b | 420 | |
samdanbury | 6:37b6d0d56190 | 421 | void messageArrived(MQTT::MessageData& md) |
samdanbury | 6:37b6d0d56190 | 422 | { |
samdanbury | 6:37b6d0d56190 | 423 | MQTT::Message &message = md.message; |
samdanbury | 6:37b6d0d56190 | 424 | char topic[md.topicName.lenstring.len + 1]; |
alikapatibabu | 18:e9e87362018b | 425 | |
samdanbury | 6:37b6d0d56190 | 426 | sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); |
alikapatibabu | 18:e9e87362018b | 427 | |
samdanbury | 6:37b6d0d56190 | 428 | LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload); |
alikapatibabu | 18:e9e87362018b | 429 | |
samdanbury | 6:37b6d0d56190 | 430 | // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ |
samdanbury | 6:37b6d0d56190 | 431 | char* start = strstr(topic, "/cmd/") + 5; |
samdanbury | 6:37b6d0d56190 | 432 | int len = strstr(topic, "/fmt/") - start; |
alikapatibabu | 18:e9e87362018b | 433 | |
alikapatibabu | 18:e9e87362018b | 434 | if (memcmp(start, "blink", len) == 0) { |
samdanbury | 6:37b6d0d56190 | 435 | char payload[message.payloadlen + 1]; |
samdanbury | 6:37b6d0d56190 | 436 | sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); |
alikapatibabu | 18:e9e87362018b | 437 | |
samdanbury | 6:37b6d0d56190 | 438 | char* pos = strchr(payload, '}'); |
alikapatibabu | 18:e9e87362018b | 439 | if (pos != NULL) { |
samdanbury | 6:37b6d0d56190 | 440 | *pos = '\0'; |
alikapatibabu | 18:e9e87362018b | 441 | if ((pos = strchr(payload, ':')) != NULL) { |
alikapatibabu | 18:e9e87362018b | 442 | int blink_rate = atoi(pos + 1); |
samdanbury | 6:37b6d0d56190 | 443 | blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate); |
samdanbury | 6:37b6d0d56190 | 444 | } |
samdanbury | 6:37b6d0d56190 | 445 | } |
alikapatibabu | 18:e9e87362018b | 446 | } else |
samdanbury | 6:37b6d0d56190 | 447 | WARN("Unsupported command: %.*s\n", len, start); |
samdanbury | 6:37b6d0d56190 | 448 | } |
samdanbury | 6:37b6d0d56190 | 449 | |
alikapatibabu | 18:e9e87362018b | 450 | void requestOBD2Data() |
alikapatibabu | 18:e9e87362018b | 451 | { |
alikapatibabu | 18:e9e87362018b | 452 | |
alikapatibabu | 18:e9e87362018b | 453 | CANMessage canSndMsg; |
alikapatibabu | 18:e9e87362018b | 454 | canSndMsg.id = 0x7e0; |
alikapatibabu | 18:e9e87362018b | 455 | canSndMsg.len=8; |
alikapatibabu | 18:e9e87362018b | 456 | /* Fill All the data with 0x00 by default */ |
alikapatibabu | 18:e9e87362018b | 457 | canSndMsg.data[0] = 0x00; |
alikapatibabu | 18:e9e87362018b | 458 | canSndMsg.data[1] = 0x00; |
alikapatibabu | 18:e9e87362018b | 459 | canSndMsg.data[2] = 0x00; |
alikapatibabu | 18:e9e87362018b | 460 | canSndMsg.data[3] = 0x00; |
alikapatibabu | 18:e9e87362018b | 461 | canSndMsg.data[4] = 0x00; |
alikapatibabu | 18:e9e87362018b | 462 | canSndMsg.data[5] = 0x00; |
alikapatibabu | 18:e9e87362018b | 463 | canSndMsg.data[6] = 0x00; |
alikapatibabu | 18:e9e87362018b | 464 | canSndMsg.data[7] = 0x00; |
alikapatibabu | 18:e9e87362018b | 465 | //printf("Sending CAN Data...\n"); |
alikapatibabu | 18:e9e87362018b | 466 | can1.write(canSndMsg); |
alikapatibabu | 18:e9e87362018b | 467 | wait_ms(100); |
alikapatibabu | 18:e9e87362018b | 468 | |
alikapatibabu | 18:e9e87362018b | 469 | /* request for Speed OBD-II 0x0d */ |
alikapatibabu | 18:e9e87362018b | 470 | canSndMsg.data[0] = 0x02; |
alikapatibabu | 18:e9e87362018b | 471 | canSndMsg.data[1] = 0x01; |
alikapatibabu | 18:e9e87362018b | 472 | canSndMsg.data[2] = 0x0d; |
alikapatibabu | 18:e9e87362018b | 473 | can1.write(canSndMsg); |
alikapatibabu | 18:e9e87362018b | 474 | /* Wait 100ms for next request */ |
alikapatibabu | 18:e9e87362018b | 475 | wait_ms(100); |
alikapatibabu | 18:e9e87362018b | 476 | |
alikapatibabu | 18:e9e87362018b | 477 | /* request for Fuel Level OBD-II 0x0d */ |
alikapatibabu | 18:e9e87362018b | 478 | canSndMsg.data[0] = 0x02; |
alikapatibabu | 18:e9e87362018b | 479 | canSndMsg.data[1] = 0x01; |
alikapatibabu | 18:e9e87362018b | 480 | canSndMsg.data[2] = 0x2f; |
alikapatibabu | 18:e9e87362018b | 481 | can1.write(canSndMsg); |
alikapatibabu | 18:e9e87362018b | 482 | /* Wait 100ms for next request */ |
alikapatibabu | 18:e9e87362018b | 483 | wait_ms(100); |
alikapatibabu | 18:e9e87362018b | 484 | |
alikapatibabu | 18:e9e87362018b | 485 | /* request for Engine RPM OBD-II 0x0d */ |
alikapatibabu | 18:e9e87362018b | 486 | canSndMsg.data[0] = 0x02; |
alikapatibabu | 18:e9e87362018b | 487 | canSndMsg.data[1] = 0x01; |
alikapatibabu | 18:e9e87362018b | 488 | canSndMsg.data[2] = 0x0c; |
alikapatibabu | 18:e9e87362018b | 489 | can1.write(canSndMsg); |
alikapatibabu | 18:e9e87362018b | 490 | /* Wait 100ms for next request */ |
alikapatibabu | 18:e9e87362018b | 491 | wait_ms(100); |
alikapatibabu | 18:e9e87362018b | 492 | |
alikapatibabu | 18:e9e87362018b | 493 | /* request for Engine Coolant Temperature OBD-II 0x0d */ |
alikapatibabu | 18:e9e87362018b | 494 | canSndMsg.data[0] = 0x02; |
alikapatibabu | 18:e9e87362018b | 495 | canSndMsg.data[1] = 0x01; |
alikapatibabu | 18:e9e87362018b | 496 | canSndMsg.data[2] = 0x05; |
alikapatibabu | 18:e9e87362018b | 497 | can1.write(canSndMsg); |
alikapatibabu | 18:e9e87362018b | 498 | /* Wait 100ms for next request */ |
alikapatibabu | 18:e9e87362018b | 499 | wait_ms(100); |
alikapatibabu | 18:e9e87362018b | 500 | } |
alikapatibabu | 18:e9e87362018b | 501 | |
samdanbury | 6:37b6d0d56190 | 502 | int main() |
alikapatibabu | 18:e9e87362018b | 503 | { |
alikapatibabu | 18:e9e87362018b | 504 | |
icraggs | 8:80d49dd91542 | 505 | quickstartMode = (strcmp(org, "quickstart") == 0); |
icraggs | 8:80d49dd91542 | 506 | |
alikapatibabu | 18:e9e87362018b | 507 | led2 = LED2_OFF; // K64F: turn off the main board LED |
alikapatibabu | 18:e9e87362018b | 508 | //Thread yellow_thread(flashing_yellow); |
mazgch | 12:d6b94e8051b0 | 509 | MDMSerial mdm; |
alikapatibabu | 18:e9e87362018b | 510 | mdm.setDebug(3); // enable this for debugging issues |
mazgch | 12:d6b94e8051b0 | 511 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
mazgch | 12:d6b94e8051b0 | 512 | return -1; |
mazgch | 13:6a0af5deb2db | 513 | |
mazgch | 15:7c3279ac3c52 | 514 | GPSI2C gps; |
alikapatibabu | 18:e9e87362018b | 515 | |
mazgch | 12:d6b94e8051b0 | 516 | MQTT_CLIENT_TYPE ipstack; |
mazgch | 12:d6b94e8051b0 | 517 | MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
alikapatibabu | 18:e9e87362018b | 518 | if (quickstartMode) { |
mazgch | 15:7c3279ac3c52 | 519 | MDMParser::DevStatus dev; |
mazgch | 15:7c3279ac3c52 | 520 | mdm.getDevStatus(&dev); |
mazgch | 15:7c3279ac3c52 | 521 | if (*dev.imei) strncpy(id, dev.imei, sizeof(id)-1); |
mazgch | 15:7c3279ac3c52 | 522 | else if (*dev.meid) strncpy(id, dev.meid, sizeof(id)-1); |
samdanbury | 6:37b6d0d56190 | 523 | } |
alikapatibabu | 18:e9e87362018b | 524 | |
alikapatibabu | 18:e9e87362018b | 525 | LOG("Trying to connect to MQTT Cloud\n", id); |
samdanbury | 6:37b6d0d56190 | 526 | attemptConnect(&client, &ipstack); |
alikapatibabu | 18:e9e87362018b | 527 | publishEngRpm(&client, &ipstack,100); |
alikapatibabu | 18:e9e87362018b | 528 | LOG("GSM modem ready Create CAN \r\n"); |
alikapatibabu | 18:e9e87362018b | 529 | DigitalOut can_standby(CANS); |
alikapatibabu | 18:e9e87362018b | 530 | can1.frequency(500000); |
alikapatibabu | 18:e9e87362018b | 531 | can1.mode(CAN::Normal); |
alikapatibabu | 18:e9e87362018b | 532 | can_standby = 0; |
samdanbury | 6:37b6d0d56190 | 533 | |
alikapatibabu | 18:e9e87362018b | 534 | char strReply[160]; |
alikapatibabu | 18:e9e87362018b | 535 | snprintf(strReply, MAX_SMS_LEN, "Sample SMS "); |
alikapatibabu | 18:e9e87362018b | 536 | LOG("Sending Sample SMS\n \n"); |
alikapatibabu | 18:e9e87362018b | 537 | mdm.smsSend(strMobilenumber, strReply); |
alikapatibabu | 18:e9e87362018b | 538 | |
samdanbury | 6:37b6d0d56190 | 539 | blink_interval = 0; |
samdanbury | 6:37b6d0d56190 | 540 | int count = 0; |
mazgch | 15:7c3279ac3c52 | 541 | int count2 = 0; |
alikapatibabu | 18:e9e87362018b | 542 | while (true) { |
alikapatibabu | 18:e9e87362018b | 543 | if (++count == 100) { |
alikapatibabu | 18:e9e87362018b | 544 | // Publish a message every second |
alikapatibabu | 18:e9e87362018b | 545 | if ((publish(&client, &ipstack) != 0)) |
samdanbury | 6:37b6d0d56190 | 546 | attemptConnect(&client, &ipstack); // if we have lost the connection |
samdanbury | 6:37b6d0d56190 | 547 | count = 0; |
samdanbury | 6:37b6d0d56190 | 548 | } |
alikapatibabu | 18:e9e87362018b | 549 | |
alikapatibabu | 18:e9e87362018b | 550 | if (++count2 == 10) { |
alikapatibabu | 18:e9e87362018b | 551 | //Publish a message every second |
mazgch | 15:7c3279ac3c52 | 552 | count2 = 0; |
mazgch | 15:7c3279ac3c52 | 553 | while (1) { |
mazgch | 15:7c3279ac3c52 | 554 | char buf[256]; |
alikapatibabu | 18:e9e87362018b | 555 | CANMessage canMsg; |
alikapatibabu | 18:e9e87362018b | 556 | requestOBD2Data(); |
alikapatibabu | 18:e9e87362018b | 557 | vehicleParams Param; |
alikapatibabu | 18:e9e87362018b | 558 | if(can1.read(canMsg)) { |
alikapatibabu | 18:e9e87362018b | 559 | switch(canMsg.data[2]) { |
alikapatibabu | 18:e9e87362018b | 560 | case 0x0d: |
alikapatibabu | 18:e9e87362018b | 561 | Param.speed = canMsg.data[3]; |
alikapatibabu | 18:e9e87362018b | 562 | publishSpeed(&client, &ipstack,Param.speed); |
alikapatibabu | 18:e9e87362018b | 563 | break; |
alikapatibabu | 18:e9e87362018b | 564 | case 0x0c: |
alikapatibabu | 18:e9e87362018b | 565 | Param.EngRpm = canMsg.data[3]; |
alikapatibabu | 18:e9e87362018b | 566 | publishEngRpm(&client, &ipstack,Param.EngRpm); |
alikapatibabu | 18:e9e87362018b | 567 | break; |
alikapatibabu | 18:e9e87362018b | 568 | case 0x2f: |
alikapatibabu | 18:e9e87362018b | 569 | Param.FuelLevel = canMsg.data[3]; |
alikapatibabu | 18:e9e87362018b | 570 | publishFuelLevel(&client, &ipstack,Param.FuelLevel); |
alikapatibabu | 18:e9e87362018b | 571 | break; |
alikapatibabu | 18:e9e87362018b | 572 | case 0x05: |
alikapatibabu | 18:e9e87362018b | 573 | Param.EngCoolantTemp = canMsg.data[3]; |
alikapatibabu | 18:e9e87362018b | 574 | publishEngCoolantTemp(&client, &ipstack,Param.EngCoolantTemp); |
alikapatibabu | 18:e9e87362018b | 575 | break; |
alikapatibabu | 18:e9e87362018b | 576 | case 0x22: |
alikapatibabu | 18:e9e87362018b | 577 | Param.ABSIndcnON = canMsg.data[3]; |
alikapatibabu | 18:e9e87362018b | 578 | publishABS(&client, &ipstack,Param.ABSIndcnON); |
alikapatibabu | 18:e9e87362018b | 579 | case 0x08: |
alikapatibabu | 18:e9e87362018b | 580 | Param.A_BCall = canMsg.data[3]; |
alikapatibabu | 18:e9e87362018b | 581 | publishA_BCall(&client, &ipstack,Param.A_BCall); |
alikapatibabu | 18:e9e87362018b | 582 | case 0x09: |
alikapatibabu | 18:e9e87362018b | 583 | Param.A_ECall = canMsg.data[3]; |
alikapatibabu | 18:e9e87362018b | 584 | publishA_ECall(&client, &ipstack,Param.A_ECall); |
alikapatibabu | 18:e9e87362018b | 585 | default: |
alikapatibabu | 18:e9e87362018b | 586 | LOG("Not valid param..\n"); |
alikapatibabu | 18:e9e87362018b | 587 | break; |
alikapatibabu | 18:e9e87362018b | 588 | } |
alikapatibabu | 18:e9e87362018b | 589 | } |
mazgch | 15:7c3279ac3c52 | 590 | int ret = gps.getMessage(buf, sizeof(buf)); |
alikapatibabu | 18:e9e87362018b | 591 | // LOG("GPS: Return: %d \n", ret); |
mazgch | 15:7c3279ac3c52 | 592 | if (ret <= 0) break; |
mazgch | 15:7c3279ac3c52 | 593 | int len = LENGTH(ret); |
mazgch | 15:7c3279ac3c52 | 594 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) { |
mazgch | 15:7c3279ac3c52 | 595 | // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS |
mazgch | 15:7c3279ac3c52 | 596 | if ((buf[0] == '$') || buf[1] == 'G') { |
alikapatibabu | 18:e9e87362018b | 597 | #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) |
mazgch | 15:7c3279ac3c52 | 598 | if (_CHECK_TALKER("GGA")) { |
alikapatibabu | 18:e9e87362018b | 599 | LOG("%.*s\n", len, buf); |
mazgch | 15:7c3279ac3c52 | 600 | char ch; |
mazgch | 15:7c3279ac3c52 | 601 | double latitude, longitude, elevation; |
mazgch | 15:7c3279ac3c52 | 602 | static double lastLat = 0, lastLon = 0; |
alikapatibabu | 18:e9e87362018b | 603 | if (gps.getNmeaAngle(2,buf,len,latitude) && gps.getNmeaAngle(4,buf,len,longitude) && |
alikapatibabu | 18:e9e87362018b | 604 | gps.getNmeaItem(6,buf,len,ch) && gps.getNmeaItem(9,buf,len,elevation)) { |
alikapatibabu | 18:e9e87362018b | 605 | |
alikapatibabu | 18:e9e87362018b | 606 | LOG("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); |
alikapatibabu | 18:e9e87362018b | 607 | publishGps(&client, &ipstack, latitude, longitude); |
alikapatibabu | 18:e9e87362018b | 608 | lastLat = latitude; |
alikapatibabu | 18:e9e87362018b | 609 | lastLon = longitude; |
alikapatibabu | 18:e9e87362018b | 610 | } |
mazgch | 15:7c3279ac3c52 | 611 | } |
mazgch | 15:7c3279ac3c52 | 612 | } |
mazgch | 15:7c3279ac3c52 | 613 | } |
mazgch | 15:7c3279ac3c52 | 614 | } |
mazgch | 15:7c3279ac3c52 | 615 | } |
samdanbury | 6:37b6d0d56190 | 616 | } |
samdanbury | 6:37b6d0d56190 | 617 | } |