Wireless Networks tester code
Dependencies: BME280 C027_Support MQTT mbed-rtos mbed
main.cpp@0:04eef28d3932, 2016-11-30 (annotated)
- Committer:
- maximusismax
- Date:
- Wed Nov 30 15:27:36 2016 +0000
- Revision:
- 0:04eef28d3932
took lots out, doesnt work yet though
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maximusismax | 0:04eef28d3932 | 1 | /* |
maximusismax | 0:04eef28d3932 | 2 | #include "mbed.h" |
maximusismax | 0:04eef28d3932 | 3 | #include "BME280.h" |
maximusismax | 0:04eef28d3932 | 4 | #ifdef TARGET_UBLOX_C027 |
maximusismax | 0:04eef28d3932 | 5 | #include "C027_api.h" |
maximusismax | 0:04eef28d3932 | 6 | #else |
maximusismax | 0:04eef28d3932 | 7 | #error "This example is targeted for the C027 platform" |
maximusismax | 0:04eef28d3932 | 8 | #endif |
maximusismax | 0:04eef28d3932 | 9 | |
maximusismax | 0:04eef28d3932 | 10 | DigitalOut myled(LED1); |
maximusismax | 0:04eef28d3932 | 11 | Serial pc(USBTX, USBRX); |
maximusismax | 0:04eef28d3932 | 12 | I2C i2c(P0_0, P0_1); |
maximusismax | 0:04eef28d3932 | 13 | BME280 sensor(i2c); |
maximusismax | 0:04eef28d3932 | 14 | |
maximusismax | 0:04eef28d3932 | 15 | int main() { |
maximusismax | 0:04eef28d3932 | 16 | float temp = 0; |
maximusismax | 0:04eef28d3932 | 17 | float pres = 0; |
maximusismax | 0:04eef28d3932 | 18 | float humid = 0; |
maximusismax | 0:04eef28d3932 | 19 | pc.printf("Starting simple test program"); |
maximusismax | 0:04eef28d3932 | 20 | |
maximusismax | 0:04eef28d3932 | 21 | sensor.initialize(); |
maximusismax | 0:04eef28d3932 | 22 | while(1) { |
maximusismax | 0:04eef28d3932 | 23 | myled = !myled; |
maximusismax | 0:04eef28d3932 | 24 | wait(0.2); |
maximusismax | 0:04eef28d3932 | 25 | temp = sensor.getTemperature(); |
maximusismax | 0:04eef28d3932 | 26 | pres = sensor.getPressure(); |
maximusismax | 0:04eef28d3932 | 27 | humid = sensor.getHumidity(); |
maximusismax | 0:04eef28d3932 | 28 | pc.printf("temp: %f, pres: %f, humid: %f\n", temp, pres, humid); |
maximusismax | 0:04eef28d3932 | 29 | } |
maximusismax | 0:04eef28d3932 | 30 | } |
maximusismax | 0:04eef28d3932 | 31 | */ |
maximusismax | 0:04eef28d3932 | 32 | |
maximusismax | 0:04eef28d3932 | 33 | /******************************************************************************* |
maximusismax | 0:04eef28d3932 | 34 | * Copyright (c) 2014 IBM Corp. |
maximusismax | 0:04eef28d3932 | 35 | * |
maximusismax | 0:04eef28d3932 | 36 | * All rights reserved. This program and the accompanying materials |
maximusismax | 0:04eef28d3932 | 37 | * are made available under the terms of the Eclipse Public License v1.0 |
maximusismax | 0:04eef28d3932 | 38 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
maximusismax | 0:04eef28d3932 | 39 | * |
maximusismax | 0:04eef28d3932 | 40 | * The Eclipse Public License is available at |
maximusismax | 0:04eef28d3932 | 41 | * http://www.eclipse.org/legal/epl-v10.html |
maximusismax | 0:04eef28d3932 | 42 | * and the Eclipse Distribution License is available at |
maximusismax | 0:04eef28d3932 | 43 | * http://www.eclipse.org/org/documents/edl-v10.php. |
maximusismax | 0:04eef28d3932 | 44 | * |
maximusismax | 0:04eef28d3932 | 45 | * Contributors: |
maximusismax | 0:04eef28d3932 | 46 | * Sam Danbury - initial implementation |
maximusismax | 0:04eef28d3932 | 47 | * Ian Craggs - refactoring to remove STL and other changes |
maximusismax | 0:04eef28d3932 | 48 | * Sam Grove - added check for Ethernet cable. |
maximusismax | 0:04eef28d3932 | 49 | * Chris Styles - Added additional menu screen for software revision |
maximusismax | 0:04eef28d3932 | 50 | * |
maximusismax | 0:04eef28d3932 | 51 | * To do : |
maximusismax | 0:04eef28d3932 | 52 | * Add magnetometer sensor output to IoT data stream |
maximusismax | 0:04eef28d3932 | 53 | * |
maximusismax | 0:04eef28d3932 | 54 | *******************************************************************************/ |
maximusismax | 0:04eef28d3932 | 55 | |
maximusismax | 0:04eef28d3932 | 56 | #define USE_CELLULAR // Enable this switch on the C027 to use cellular |
maximusismax | 0:04eef28d3932 | 57 | |
maximusismax | 0:04eef28d3932 | 58 | |
maximusismax | 0:04eef28d3932 | 59 | #include "MQTTClient.h" |
maximusismax | 0:04eef28d3932 | 60 | |
maximusismax | 0:04eef28d3932 | 61 | #include "rtos.h" |
maximusismax | 0:04eef28d3932 | 62 | |
maximusismax | 0:04eef28d3932 | 63 | // Update this to the next number *before* a commit |
maximusismax | 0:04eef28d3932 | 64 | #define __APP_SW_REVISION__ "11" |
maximusismax | 0:04eef28d3932 | 65 | #define BOARD_NAME "IoT u-blox" |
maximusismax | 0:04eef28d3932 | 66 | |
maximusismax | 0:04eef28d3932 | 67 | // Configuration values needed to connect to IBM IoT Cloud |
maximusismax | 0:04eef28d3932 | 68 | #define ORG "quickstart" // For a registered connection, replace with your org |
maximusismax | 0:04eef28d3932 | 69 | #define ID "" // For a registered connection, replace with your id |
maximusismax | 0:04eef28d3932 | 70 | #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token |
maximusismax | 0:04eef28d3932 | 71 | #define TYPE_NAME DEFAULT_TYPE_NAME // For a registered connection, replace with your type |
maximusismax | 0:04eef28d3932 | 72 | |
maximusismax | 0:04eef28d3932 | 73 | #define MQTT_PORT 1883 |
maximusismax | 0:04eef28d3932 | 74 | #define MQTT_TLS_PORT 8883 |
maximusismax | 0:04eef28d3932 | 75 | #define IBM_IOT_PORT MQTT_PORT |
maximusismax | 0:04eef28d3932 | 76 | |
maximusismax | 0:04eef28d3932 | 77 | #define MQTT_MAX_PACKET_SIZE 250 |
maximusismax | 0:04eef28d3932 | 78 | |
maximusismax | 0:04eef28d3932 | 79 | //------------------------------------------------------------------------------------ |
maximusismax | 0:04eef28d3932 | 80 | // You need to configure these cellular modem / SIM parameters. |
maximusismax | 0:04eef28d3932 | 81 | // These parameters are ignored for LISA-C200 variants and can be left NULL. |
maximusismax | 0:04eef28d3932 | 82 | //------------------------------------------------------------------------------------ |
maximusismax | 0:04eef28d3932 | 83 | # include "MDM.h" |
maximusismax | 0:04eef28d3932 | 84 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
maximusismax | 0:04eef28d3932 | 85 | # define SIMPIN "1111" //default EE sim pin |
maximusismax | 0:04eef28d3932 | 86 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
maximusismax | 0:04eef28d3932 | 87 | contract with the network operator. You can also try to look-up your settings in |
maximusismax | 0:04eef28d3932 | 88 | google: https://www.google.de/search?q=APN+list */ |
maximusismax | 0:04eef28d3932 | 89 | # define APN "everywhere" //EE default |
maximusismax | 0:04eef28d3932 | 90 | //! Set the user name for your APN, or NULL if not needed |
maximusismax | 0:04eef28d3932 | 91 | # define USERNAME NULL |
maximusismax | 0:04eef28d3932 | 92 | //! Set the password for your APN, or NULL if not needed |
maximusismax | 0:04eef28d3932 | 93 | # define PASSWORD NULL |
maximusismax | 0:04eef28d3932 | 94 | //------------------------------------------------------------------------------------ |
maximusismax | 0:04eef28d3932 | 95 | # include "GPS.h" |
maximusismax | 0:04eef28d3932 | 96 | //------------------------------------------------------------------------------------ |
maximusismax | 0:04eef28d3932 | 97 | |
maximusismax | 0:04eef28d3932 | 98 | |
maximusismax | 0:04eef28d3932 | 99 | static uint32_t linkStatus(void) { return true; } |
maximusismax | 0:04eef28d3932 | 100 | |
maximusismax | 0:04eef28d3932 | 101 | #define DEFAULT_TYPE_NAME "iotsample-mbed-c027" |
maximusismax | 0:04eef28d3932 | 102 | |
maximusismax | 0:04eef28d3932 | 103 | #define MQTT_CLIENT_TYPE MQTTSocket |
maximusismax | 0:04eef28d3932 | 104 | #include "MQTTSocket.h" |
maximusismax | 0:04eef28d3932 | 105 | |
maximusismax | 0:04eef28d3932 | 106 | bool quickstartMode = true; |
maximusismax | 0:04eef28d3932 | 107 | char org[11] = ORG; |
maximusismax | 0:04eef28d3932 | 108 | char type[30] = TYPE_NAME; |
maximusismax | 0:04eef28d3932 | 109 | char id[30] = ID; // device ID |
maximusismax | 0:04eef28d3932 | 110 | char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode |
maximusismax | 0:04eef28d3932 | 111 | |
maximusismax | 0:04eef28d3932 | 112 | bool connected = false; |
maximusismax | 0:04eef28d3932 | 113 | |
maximusismax | 0:04eef28d3932 | 114 | int connect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) |
maximusismax | 0:04eef28d3932 | 115 | { |
maximusismax | 0:04eef28d3932 | 116 | const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; |
maximusismax | 0:04eef28d3932 | 117 | |
maximusismax | 0:04eef28d3932 | 118 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
maximusismax | 0:04eef28d3932 | 119 | sprintf(hostname, "%s%s", org, iot_ibm); |
maximusismax | 0:04eef28d3932 | 120 | DEBUG("hostname is %s\n", hostname); |
maximusismax | 0:04eef28d3932 | 121 | int rc = ipstack->connect(hostname, IBM_IOT_PORT); |
maximusismax | 0:04eef28d3932 | 122 | if (rc != 0) |
maximusismax | 0:04eef28d3932 | 123 | return rc; |
maximusismax | 0:04eef28d3932 | 124 | |
maximusismax | 0:04eef28d3932 | 125 | // Construct clientId - d:org:type:id |
maximusismax | 0:04eef28d3932 | 126 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
maximusismax | 0:04eef28d3932 | 127 | sprintf(clientId, "d:%s:%s:%s", org, type, id); |
maximusismax | 0:04eef28d3932 | 128 | DEBUG("clientid is %s\n", clientId); |
maximusismax | 0:04eef28d3932 | 129 | |
maximusismax | 0:04eef28d3932 | 130 | // MQTT Connect |
maximusismax | 0:04eef28d3932 | 131 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
maximusismax | 0:04eef28d3932 | 132 | data.MQTTVersion = 3; |
maximusismax | 0:04eef28d3932 | 133 | data.clientID.cstring = clientId; |
maximusismax | 0:04eef28d3932 | 134 | |
maximusismax | 0:04eef28d3932 | 135 | if (!quickstartMode) |
maximusismax | 0:04eef28d3932 | 136 | { |
maximusismax | 0:04eef28d3932 | 137 | data.username.cstring = "use-token-auth"; |
maximusismax | 0:04eef28d3932 | 138 | data.password.cstring = auth_token; |
maximusismax | 0:04eef28d3932 | 139 | } |
maximusismax | 0:04eef28d3932 | 140 | |
maximusismax | 0:04eef28d3932 | 141 | if ((rc = client->connect(data)) == 0) |
maximusismax | 0:04eef28d3932 | 142 | { |
maximusismax | 0:04eef28d3932 | 143 | connected = true; |
maximusismax | 0:04eef28d3932 | 144 | } |
maximusismax | 0:04eef28d3932 | 145 | return rc; |
maximusismax | 0:04eef28d3932 | 146 | } |
maximusismax | 0:04eef28d3932 | 147 | |
maximusismax | 0:04eef28d3932 | 148 | |
maximusismax | 0:04eef28d3932 | 149 | int getConnTimeout(int attemptNumber) |
maximusismax | 0:04eef28d3932 | 150 | { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
maximusismax | 0:04eef28d3932 | 151 | // after 20 attempts, retry every 10 minutes |
maximusismax | 0:04eef28d3932 | 152 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
maximusismax | 0:04eef28d3932 | 153 | } |
maximusismax | 0:04eef28d3932 | 154 | |
maximusismax | 0:04eef28d3932 | 155 | |
maximusismax | 0:04eef28d3932 | 156 | void attemptConnect(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) |
maximusismax | 0:04eef28d3932 | 157 | { |
maximusismax | 0:04eef28d3932 | 158 | int retryAttempt = 0; |
maximusismax | 0:04eef28d3932 | 159 | connected = false; |
maximusismax | 0:04eef28d3932 | 160 | |
maximusismax | 0:04eef28d3932 | 161 | // make sure a cable is connected before starting to connect |
maximusismax | 0:04eef28d3932 | 162 | while (!linkStatus()) { |
maximusismax | 0:04eef28d3932 | 163 | wait(1.0f); |
maximusismax | 0:04eef28d3932 | 164 | WARN("Internet link not present. Check cable connection\n"); |
maximusismax | 0:04eef28d3932 | 165 | } |
maximusismax | 0:04eef28d3932 | 166 | |
maximusismax | 0:04eef28d3932 | 167 | while (connect(client, ipstack) != 0) |
maximusismax | 0:04eef28d3932 | 168 | { |
maximusismax | 0:04eef28d3932 | 169 | int timeout = getConnTimeout(++retryAttempt); |
maximusismax | 0:04eef28d3932 | 170 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
maximusismax | 0:04eef28d3932 | 171 | |
maximusismax | 0:04eef28d3932 | 172 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
maximusismax | 0:04eef28d3932 | 173 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
maximusismax | 0:04eef28d3932 | 174 | |
maximusismax | 0:04eef28d3932 | 175 | // this works - reset the system when the retry count gets to a threshold |
maximusismax | 0:04eef28d3932 | 176 | if (retryAttempt == 5) |
maximusismax | 0:04eef28d3932 | 177 | NVIC_SystemReset(); |
maximusismax | 0:04eef28d3932 | 178 | else |
maximusismax | 0:04eef28d3932 | 179 | wait(timeout); |
maximusismax | 0:04eef28d3932 | 180 | } |
maximusismax | 0:04eef28d3932 | 181 | } |
maximusismax | 0:04eef28d3932 | 182 | |
maximusismax | 0:04eef28d3932 | 183 | |
maximusismax | 0:04eef28d3932 | 184 | int publish(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack) |
maximusismax | 0:04eef28d3932 | 185 | { |
maximusismax | 0:04eef28d3932 | 186 | MQTT::Message message; |
maximusismax | 0:04eef28d3932 | 187 | char* pubTopic = "iot-2/evt/status/fmt/json"; |
maximusismax | 0:04eef28d3932 | 188 | |
maximusismax | 0:04eef28d3932 | 189 | char buf[250]; |
maximusismax | 0:04eef28d3932 | 190 | int l = 0; |
maximusismax | 0:04eef28d3932 | 191 | l += sprintf(buf+l,"{\"d\":{\"myName\":\"%s\"", BOARD_NAME); |
maximusismax | 0:04eef28d3932 | 192 | //if (shieldConnected) { |
maximusismax | 0:04eef28d3932 | 193 | /* |
maximusismax | 0:04eef28d3932 | 194 | l += sprintf(buf+l,",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f", MMA.x(), MMA.y(), MMA.z()); |
maximusismax | 0:04eef28d3932 | 195 | l += sprintf(buf+l,",\"temp\":%0.4f", sensor.temp()); |
maximusismax | 0:04eef28d3932 | 196 | l += sprintf(buf+l,",\"joystick\":\"%s\"", joystickPos); |
maximusismax | 0:04eef28d3932 | 197 | l += sprintf(buf+l,",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f", ain1.read(), ain2.read()); |
maximusismax | 0:04eef28d3932 | 198 | */ |
maximusismax | 0:04eef28d3932 | 199 | //l += sprintf(buf+l,",\"temp\":%0.4f", sensor.getTemperature()); |
maximusismax | 0:04eef28d3932 | 200 | //} |
maximusismax | 0:04eef28d3932 | 201 | l += sprintf(buf+l,"}}"); |
maximusismax | 0:04eef28d3932 | 202 | |
maximusismax | 0:04eef28d3932 | 203 | message.qos = MQTT::QOS0; |
maximusismax | 0:04eef28d3932 | 204 | message.retained = false; |
maximusismax | 0:04eef28d3932 | 205 | message.dup = false; |
maximusismax | 0:04eef28d3932 | 206 | message.payload = (void*)buf; |
maximusismax | 0:04eef28d3932 | 207 | message.payloadlen = strlen(buf); |
maximusismax | 0:04eef28d3932 | 208 | |
maximusismax | 0:04eef28d3932 | 209 | LOG("Publishing %s\n", buf); |
maximusismax | 0:04eef28d3932 | 210 | return client->publish(pubTopic, message); |
maximusismax | 0:04eef28d3932 | 211 | } |
maximusismax | 0:04eef28d3932 | 212 | |
maximusismax | 0:04eef28d3932 | 213 | int publishMdm(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, MDMParser* mdm) |
maximusismax | 0:04eef28d3932 | 214 | { |
maximusismax | 0:04eef28d3932 | 215 | MQTT::Message message; |
maximusismax | 0:04eef28d3932 | 216 | char* pubTopic = "iot-2/evt/modem/fmt/json"; |
maximusismax | 0:04eef28d3932 | 217 | char buf[250]; |
maximusismax | 0:04eef28d3932 | 218 | int l = 0; |
maximusismax | 0:04eef28d3932 | 219 | MDMParser::IP ip = mdm->getIpAddress(); |
maximusismax | 0:04eef28d3932 | 220 | l += sprintf(buf+l,"{\"d\":{\"ip\":\"" IPSTR "\"", IPNUM(ip)); |
maximusismax | 0:04eef28d3932 | 221 | MDMParser::NetStatus sta; |
maximusismax | 0:04eef28d3932 | 222 | mdm->checkNetStatus(&sta); |
maximusismax | 0:04eef28d3932 | 223 | //const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" }; |
maximusismax | 0:04eef28d3932 | 224 | //if (sta.act < sizeof(txtAct)/sizeof(*txtAct) && (sta.act != MDMParser::ACT_UNKNOWN)) |
maximusismax | 0:04eef28d3932 | 225 | // l += sprintf(buf+l,",\"act\":\"%s\"",txtAct[sta.act]); |
maximusismax | 0:04eef28d3932 | 226 | //if (*sta.num) l += sprintf(buf+l,",\"num\":\"%s\"",sta.num); |
maximusismax | 0:04eef28d3932 | 227 | //if (sta.lac != 0xFFFF) l += sprintf(buf+l,",\"lac\":\"%04X\"",sta.lac); |
maximusismax | 0:04eef28d3932 | 228 | //if (sta.ci != 0xFFFFFFFF) l += sprintf(buf+l,",\"ci\":\"%06X\"",sta.ci); |
maximusismax | 0:04eef28d3932 | 229 | if (sta.rssi) l += sprintf(buf+l,",\"rssi\":%d",sta.rssi); |
maximusismax | 0:04eef28d3932 | 230 | if (sta.ber) l += sprintf(buf+l,",\"ber\":%d",sta.ber); |
maximusismax | 0:04eef28d3932 | 231 | if (*sta.opr) l += sprintf(buf+l,",\"operator\":\"%s\"",sta.opr); |
maximusismax | 0:04eef28d3932 | 232 | l += sprintf(buf+l,"}}"); |
maximusismax | 0:04eef28d3932 | 233 | |
maximusismax | 0:04eef28d3932 | 234 | message.qos = MQTT::QOS0; |
maximusismax | 0:04eef28d3932 | 235 | message.retained = false; |
maximusismax | 0:04eef28d3932 | 236 | message.dup = false; |
maximusismax | 0:04eef28d3932 | 237 | message.payload = (void*)buf; |
maximusismax | 0:04eef28d3932 | 238 | message.payloadlen = strlen(buf); |
maximusismax | 0:04eef28d3932 | 239 | |
maximusismax | 0:04eef28d3932 | 240 | LOG("Publishing %s\n", buf); |
maximusismax | 0:04eef28d3932 | 241 | return client->publish(pubTopic, message); |
maximusismax | 0:04eef28d3932 | 242 | } |
maximusismax | 0:04eef28d3932 | 243 | |
maximusismax | 0:04eef28d3932 | 244 | int publishGps(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, double lat, double lon) |
maximusismax | 0:04eef28d3932 | 245 | { |
maximusismax | 0:04eef28d3932 | 246 | MQTT::Message message; |
maximusismax | 0:04eef28d3932 | 247 | char* pubTopic = "iot-2/evt/gps/fmt/json"; |
maximusismax | 0:04eef28d3932 | 248 | char buf[250]; |
maximusismax | 0:04eef28d3932 | 249 | sprintf(buf,"{\"d\":{\"lat\":\"%.7f\",\"long\":\"%.7f\"}}", lat, lon); |
maximusismax | 0:04eef28d3932 | 250 | message.qos = MQTT::QOS0; |
maximusismax | 0:04eef28d3932 | 251 | message.retained = false; |
maximusismax | 0:04eef28d3932 | 252 | message.dup = false; |
maximusismax | 0:04eef28d3932 | 253 | message.payload = (void*)buf; |
maximusismax | 0:04eef28d3932 | 254 | message.payloadlen = strlen(buf); |
maximusismax | 0:04eef28d3932 | 255 | LOG("Publishing GPS %s\n", buf); |
maximusismax | 0:04eef28d3932 | 256 | return client->publish(pubTopic, message); |
maximusismax | 0:04eef28d3932 | 257 | } |
maximusismax | 0:04eef28d3932 | 258 | |
maximusismax | 0:04eef28d3932 | 259 | void messageArrived(MQTT::MessageData& md) |
maximusismax | 0:04eef28d3932 | 260 | { |
maximusismax | 0:04eef28d3932 | 261 | MQTT::Message &message = md.message; |
maximusismax | 0:04eef28d3932 | 262 | char topic[md.topicName.lenstring.len + 1]; |
maximusismax | 0:04eef28d3932 | 263 | |
maximusismax | 0:04eef28d3932 | 264 | sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); |
maximusismax | 0:04eef28d3932 | 265 | |
maximusismax | 0:04eef28d3932 | 266 | LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload); |
maximusismax | 0:04eef28d3932 | 267 | |
maximusismax | 0:04eef28d3932 | 268 | // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ |
maximusismax | 0:04eef28d3932 | 269 | char* start = strstr(topic, "/cmd/") + 5; |
maximusismax | 0:04eef28d3932 | 270 | int len = strstr(topic, "/fmt/") - start; |
maximusismax | 0:04eef28d3932 | 271 | |
maximusismax | 0:04eef28d3932 | 272 | if (memcmp(start, "blink", len) == 0) |
maximusismax | 0:04eef28d3932 | 273 | { |
maximusismax | 0:04eef28d3932 | 274 | char payload[message.payloadlen + 1]; |
maximusismax | 0:04eef28d3932 | 275 | sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); |
maximusismax | 0:04eef28d3932 | 276 | |
maximusismax | 0:04eef28d3932 | 277 | char* pos = strchr(payload, '}'); |
maximusismax | 0:04eef28d3932 | 278 | |
maximusismax | 0:04eef28d3932 | 279 | |
maximusismax | 0:04eef28d3932 | 280 | |
maximusismax | 0:04eef28d3932 | 281 | |
maximusismax | 0:04eef28d3932 | 282 | if (pos != NULL) |
maximusismax | 0:04eef28d3932 | 283 | { |
maximusismax | 0:04eef28d3932 | 284 | *pos = '\0'; |
maximusismax | 0:04eef28d3932 | 285 | if ((pos = strchr(payload, ':')) != NULL) |
maximusismax | 0:04eef28d3932 | 286 | { |
maximusismax | 0:04eef28d3932 | 287 | //int blink_rate = atoi(pos + 1); |
maximusismax | 0:04eef28d3932 | 288 | //blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate); |
maximusismax | 0:04eef28d3932 | 289 | } |
maximusismax | 0:04eef28d3932 | 290 | } |
maximusismax | 0:04eef28d3932 | 291 | } |
maximusismax | 0:04eef28d3932 | 292 | else |
maximusismax | 0:04eef28d3932 | 293 | WARN("Unsupported command: %.*s\n", len, start); |
maximusismax | 0:04eef28d3932 | 294 | } |
maximusismax | 0:04eef28d3932 | 295 | |
maximusismax | 0:04eef28d3932 | 296 | int main() |
maximusismax | 0:04eef28d3932 | 297 | { |
maximusismax | 0:04eef28d3932 | 298 | printf("Starting program\n"); |
maximusismax | 0:04eef28d3932 | 299 | |
maximusismax | 0:04eef28d3932 | 300 | printf("Connecting"); |
maximusismax | 0:04eef28d3932 | 301 | |
maximusismax | 0:04eef28d3932 | 302 | MDMSerial mdm; |
maximusismax | 0:04eef28d3932 | 303 | //mdm.setDebug(3); // enable this for debugging issues |
maximusismax | 0:04eef28d3932 | 304 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
maximusismax | 0:04eef28d3932 | 305 | return -1; |
maximusismax | 0:04eef28d3932 | 306 | |
maximusismax | 0:04eef28d3932 | 307 | GPSI2C gps; |
maximusismax | 0:04eef28d3932 | 308 | |
maximusismax | 0:04eef28d3932 | 309 | MQTT_CLIENT_TYPE ipstack; |
maximusismax | 0:04eef28d3932 | 310 | MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
maximusismax | 0:04eef28d3932 | 311 | if (quickstartMode) |
maximusismax | 0:04eef28d3932 | 312 | { |
maximusismax | 0:04eef28d3932 | 313 | MDMParser::DevStatus dev; |
maximusismax | 0:04eef28d3932 | 314 | mdm.getDevStatus(&dev); |
maximusismax | 0:04eef28d3932 | 315 | if (*dev.imei) strncpy(id, dev.imei, sizeof(id)-1); |
maximusismax | 0:04eef28d3932 | 316 | else if (*dev.meid) strncpy(id, dev.meid, sizeof(id)-1); |
maximusismax | 0:04eef28d3932 | 317 | } |
maximusismax | 0:04eef28d3932 | 318 | |
maximusismax | 0:04eef28d3932 | 319 | LOG("IBM Quickstart: https://quickstart.internetofthings.ibmcloud.com/#/device/%s/sensor/\n", id); |
maximusismax | 0:04eef28d3932 | 320 | |
maximusismax | 0:04eef28d3932 | 321 | attemptConnect(&client, &ipstack); |
maximusismax | 0:04eef28d3932 | 322 | |
maximusismax | 0:04eef28d3932 | 323 | if (!quickstartMode) |
maximusismax | 0:04eef28d3932 | 324 | { |
maximusismax | 0:04eef28d3932 | 325 | int rc = 0; |
maximusismax | 0:04eef28d3932 | 326 | if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) |
maximusismax | 0:04eef28d3932 | 327 | WARN("rc from MQTT subscribe is %d\n", rc); |
maximusismax | 0:04eef28d3932 | 328 | } |
maximusismax | 0:04eef28d3932 | 329 | |
maximusismax | 0:04eef28d3932 | 330 | //blink_interval = 0; |
maximusismax | 0:04eef28d3932 | 331 | int count = 0; |
maximusismax | 0:04eef28d3932 | 332 | int count2 = 0; |
maximusismax | 0:04eef28d3932 | 333 | while (true) |
maximusismax | 0:04eef28d3932 | 334 | { |
maximusismax | 0:04eef28d3932 | 335 | if (++count == 100) |
maximusismax | 0:04eef28d3932 | 336 | { // Publish a message every second |
maximusismax | 0:04eef28d3932 | 337 | if ((publish(&client, &ipstack) != 0) || |
maximusismax | 0:04eef28d3932 | 338 | (publishMdm(&client, &ipstack, &mdm) != 0)) |
maximusismax | 0:04eef28d3932 | 339 | attemptConnect(&client, &ipstack); // if we have lost the connection |
maximusismax | 0:04eef28d3932 | 340 | count = 0; |
maximusismax | 0:04eef28d3932 | 341 | } |
maximusismax | 0:04eef28d3932 | 342 | if (++count2 == 10) |
maximusismax | 0:04eef28d3932 | 343 | { // Publish a message every second |
maximusismax | 0:04eef28d3932 | 344 | count2 = 0; |
maximusismax | 0:04eef28d3932 | 345 | while (1) { |
maximusismax | 0:04eef28d3932 | 346 | char buf[256]; |
maximusismax | 0:04eef28d3932 | 347 | int ret = gps.getMessage(buf, sizeof(buf)); |
maximusismax | 0:04eef28d3932 | 348 | if (ret <= 0) break; |
maximusismax | 0:04eef28d3932 | 349 | int len = LENGTH(ret); |
maximusismax | 0:04eef28d3932 | 350 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) { |
maximusismax | 0:04eef28d3932 | 351 | // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS |
maximusismax | 0:04eef28d3932 | 352 | if ((buf[0] == '$') || buf[1] == 'G') { |
maximusismax | 0:04eef28d3932 | 353 | #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) |
maximusismax | 0:04eef28d3932 | 354 | if (_CHECK_TALKER("GGA")) { |
maximusismax | 0:04eef28d3932 | 355 | //printf("%.*s\n", len, buf); |
maximusismax | 0:04eef28d3932 | 356 | char ch; |
maximusismax | 0:04eef28d3932 | 357 | double latitude, longitude, elevation; |
maximusismax | 0:04eef28d3932 | 358 | static double lastLat = 0, lastLon = 0; |
maximusismax | 0:04eef28d3932 | 359 | if (gps.getNmeaAngle(2,buf,len,latitude) && |
maximusismax | 0:04eef28d3932 | 360 | gps.getNmeaAngle(4,buf,len,longitude) && |
maximusismax | 0:04eef28d3932 | 361 | gps.getNmeaItem(6,buf,len,ch) && |
maximusismax | 0:04eef28d3932 | 362 | gps.getNmeaItem(9,buf,len,elevation)) { |
maximusismax | 0:04eef28d3932 | 363 | //printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); |
maximusismax | 0:04eef28d3932 | 364 | if ((ch == '1' || ch == '2' || ch == '6') && |
maximusismax | 0:04eef28d3932 | 365 | (fabs(lastLat - latitude) < 0.0000001) && |
maximusismax | 0:04eef28d3932 | 366 | (fabs(lastLon - longitude) < 0.0000001)) { |
maximusismax | 0:04eef28d3932 | 367 | publishGps(&client, &ipstack, latitude, longitude); |
maximusismax | 0:04eef28d3932 | 368 | lastLat = latitude; |
maximusismax | 0:04eef28d3932 | 369 | lastLon = longitude; |
maximusismax | 0:04eef28d3932 | 370 | } |
maximusismax | 0:04eef28d3932 | 371 | } |
maximusismax | 0:04eef28d3932 | 372 | } |
maximusismax | 0:04eef28d3932 | 373 | } |
maximusismax | 0:04eef28d3932 | 374 | } |
maximusismax | 0:04eef28d3932 | 375 | } |
maximusismax | 0:04eef28d3932 | 376 | } |
maximusismax | 0:04eef28d3932 | 377 | |
maximusismax | 0:04eef28d3932 | 378 | |
maximusismax | 0:04eef28d3932 | 379 | client.yield(10); // allow the MQTT client to receive messages |
maximusismax | 0:04eef28d3932 | 380 | } |
maximusismax | 0:04eef28d3932 | 381 | } |