Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip
main.cpp@5:3d814faa2b2e, 2017-03-24 (annotated)
- Committer:
- markrad
- Date:
- Fri Mar 24 23:05:08 2017 +0000
- Revision:
- 5:3d814faa2b2e
- Parent:
- 4:9b3da9969b1b
- Child:
- 6:0bffe8529f60
Add time stamp and dump ten seconds of data.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| markrad | 0:6c46c366f500 | 1 | // Copyright (c) Microsoft. All rights reserved. |
| markrad | 0:6c46c366f500 | 2 | // Licensed under the MIT license. See LICENSE file at https://github.com/Azure/azure-iot-sdks/blob/master/LICENSE for full license information. |
| markrad | 0:6c46c366f500 | 3 | |
| markrad | 0:6c46c366f500 | 4 | /* -------------------------------------------------------------------------- *\ |
| markrad | 0:6c46c366f500 | 5 | |
| markrad | 0:6c46c366f500 | 6 | Simple progam to demonstrate reading the FRDM-K64F FXOS8700CQ |
| markrad | 0:6c46c366f500 | 7 | accelerometer, convert the data to JSON and send to an Azure IoT Hub. You |
| markrad | 0:6c46c366f500 | 8 | must provide your hub's connection string in the variable |
| markrad | 0:6c46c366f500 | 9 | 'connectionString'. |
| markrad | 0:6c46c366f500 | 10 | |
| markrad | 0:6c46c366f500 | 11 | markrad |
| markrad | 0:6c46c366f500 | 12 | |
| markrad | 0:6c46c366f500 | 13 | \* -------------------------------------------------------------------------- */ |
| markrad | 0:6c46c366f500 | 14 | |
| markrad | 0:6c46c366f500 | 15 | #include <string.h> |
| markrad | 4:9b3da9969b1b | 16 | #include <limits.h> |
| markrad | 0:6c46c366f500 | 17 | |
| markrad | 0:6c46c366f500 | 18 | #include "SingletonFXOS8700CQ.h" |
| markrad | 0:6c46c366f500 | 19 | |
| markrad | 0:6c46c366f500 | 20 | #include "iothub_client.h" |
| markrad | 0:6c46c366f500 | 21 | #include "iothub_message.h" |
| markrad | 0:6c46c366f500 | 22 | #include "azure_c_shared_utility/threadapi.h" |
| markrad | 0:6c46c366f500 | 23 | #include "azure_c_shared_utility/crt_abstractions.h" |
| markrad | 0:6c46c366f500 | 24 | #include "azure_c_shared_utility/platform.h" |
| markrad | 0:6c46c366f500 | 25 | #include "iothubtransportmqtt.h" |
| markrad | 2:2b9acda15ef0 | 26 | #include "lock.h" |
| markrad | 0:6c46c366f500 | 27 | |
| markrad | 0:6c46c366f500 | 28 | #include "certs.h" |
| markrad | 5:3d814faa2b2e | 29 | #include "NTPClient.h" |
| markrad | 5:3d814faa2b2e | 30 | |
| markrad | 5:3d814faa2b2e | 31 | NTPClient ntp; |
| markrad | 0:6c46c366f500 | 32 | |
| markrad | 4:9b3da9969b1b | 33 | /* |
| markrad | 0:6c46c366f500 | 34 | int readingToJSON(char *buffer, int bufferlen, READING &reading) |
| markrad | 0:6c46c366f500 | 35 | { |
| markrad | 0:6c46c366f500 | 36 | static const char READING[] = "\"reading\""; |
| markrad | 0:6c46c366f500 | 37 | static const char ACCELEROMETER[] = "\"accelerometer\""; |
| markrad | 0:6c46c366f500 | 38 | static const char MAGNOMETER[] = "\"magnometer\""; |
| markrad | 0:6c46c366f500 | 39 | static const char X[] = "\"X\""; |
| markrad | 0:6c46c366f500 | 40 | static const char Y[] = "\"Y\""; |
| markrad | 0:6c46c366f500 | 41 | static const char Z[] = "\"Z\""; |
| markrad | 0:6c46c366f500 | 42 | static const char STARTOBJ[] = " : {\n"; |
| markrad | 0:6c46c366f500 | 43 | static const char ENDOBJ[] = "}\n"; |
| markrad | 0:6c46c366f500 | 44 | static const char PREPEND[] = "{\n"; |
| markrad | 0:6c46c366f500 | 45 | static const int MINBUFFERLEN = |
| markrad | 0:6c46c366f500 | 46 | sizeof(READING) + |
| markrad | 0:6c46c366f500 | 47 | sizeof(ACCELEROMETER) + |
| markrad | 0:6c46c366f500 | 48 | sizeof(MAGNOMETER) + |
| markrad | 0:6c46c366f500 | 49 | 2 * (sizeof(X) + sizeof(Y) + sizeof(Z)) + |
| markrad | 0:6c46c366f500 | 50 | 3 * sizeof(STARTOBJ) + |
| markrad | 0:6c46c366f500 | 51 | 4 * sizeof(ENDOBJ) + |
| markrad | 0:6c46c366f500 | 52 | sizeof(PREPEND) + |
| markrad | 0:6c46c366f500 | 53 | 6 * 9; |
| markrad | 0:6c46c366f500 | 54 | static const char numConvert[] = "%d"; |
| markrad | 0:6c46c366f500 | 55 | |
| markrad | 0:6c46c366f500 | 56 | char toNum[10]; |
| markrad | 0:6c46c366f500 | 57 | char work[MINBUFFERLEN + 1]; |
| markrad | 0:6c46c366f500 | 58 | |
| markrad | 0:6c46c366f500 | 59 | if (buffer == NULL) |
| markrad | 0:6c46c366f500 | 60 | return 0; |
| markrad | 0:6c46c366f500 | 61 | |
| markrad | 0:6c46c366f500 | 62 | buffer[0] = '\0'; |
| markrad | 0:6c46c366f500 | 63 | |
| markrad | 0:6c46c366f500 | 64 | strcpy(work, PREPEND); |
| markrad | 0:6c46c366f500 | 65 | strcat(work, READING); |
| markrad | 0:6c46c366f500 | 66 | strcat(work, STARTOBJ); |
| markrad | 0:6c46c366f500 | 67 | strcat(work, ACCELEROMETER); |
| markrad | 0:6c46c366f500 | 68 | strcat(work, STARTOBJ); |
| markrad | 0:6c46c366f500 | 69 | strcat(work, X); |
| markrad | 0:6c46c366f500 | 70 | strcat(work, " : "); |
| markrad | 0:6c46c366f500 | 71 | sprintf(toNum, numConvert, reading.accelerometer.x); |
| markrad | 0:6c46c366f500 | 72 | strcat(work, toNum); |
| markrad | 0:6c46c366f500 | 73 | strcat(work, ",\n"); |
| markrad | 0:6c46c366f500 | 74 | strcat(work, Y); |
| markrad | 0:6c46c366f500 | 75 | strcat(work, " : "); |
| markrad | 0:6c46c366f500 | 76 | sprintf(toNum, numConvert, reading.accelerometer.y); |
| markrad | 0:6c46c366f500 | 77 | strcat(work, toNum); |
| markrad | 0:6c46c366f500 | 78 | strcat(work, ",\n"); |
| markrad | 0:6c46c366f500 | 79 | strcat(work, Z); |
| markrad | 0:6c46c366f500 | 80 | strcat(work, " : "); |
| markrad | 0:6c46c366f500 | 81 | sprintf(toNum, numConvert, reading.accelerometer.z); |
| markrad | 0:6c46c366f500 | 82 | strcat(work, toNum); |
| markrad | 0:6c46c366f500 | 83 | strcat(work, "\n"); |
| markrad | 0:6c46c366f500 | 84 | strcat(work, ENDOBJ); |
| markrad | 0:6c46c366f500 | 85 | strcat(work, MAGNOMETER); |
| markrad | 0:6c46c366f500 | 86 | strcat(work, STARTOBJ); |
| markrad | 0:6c46c366f500 | 87 | strcat(work, X); |
| markrad | 0:6c46c366f500 | 88 | strcat(work, " : "); |
| markrad | 0:6c46c366f500 | 89 | sprintf(toNum, numConvert, reading.magnometer.x); |
| markrad | 0:6c46c366f500 | 90 | strcat(work, toNum); |
| markrad | 0:6c46c366f500 | 91 | strcat(work, ",\n"); |
| markrad | 0:6c46c366f500 | 92 | strcat(work, Y); |
| markrad | 0:6c46c366f500 | 93 | strcat(work, " : "); |
| markrad | 0:6c46c366f500 | 94 | sprintf(toNum, numConvert, reading.magnometer.y); |
| markrad | 0:6c46c366f500 | 95 | strcat(work, toNum); |
| markrad | 0:6c46c366f500 | 96 | strcat(work, ",\n"); |
| markrad | 0:6c46c366f500 | 97 | strcat(work, Z); |
| markrad | 0:6c46c366f500 | 98 | strcat(work, " : "); |
| markrad | 0:6c46c366f500 | 99 | sprintf(toNum, numConvert, reading.magnometer.z); |
| markrad | 0:6c46c366f500 | 100 | strcat(work, toNum); |
| markrad | 0:6c46c366f500 | 101 | strcat(work, "\n"); |
| markrad | 0:6c46c366f500 | 102 | strcat(work, ENDOBJ); |
| markrad | 0:6c46c366f500 | 103 | strcat(work, ENDOBJ); |
| markrad | 0:6c46c366f500 | 104 | strcat(work, ENDOBJ); |
| markrad | 0:6c46c366f500 | 105 | |
| markrad | 0:6c46c366f500 | 106 | if (strlen(work) + 1 < bufferlen) |
| markrad | 0:6c46c366f500 | 107 | strcpy(buffer, work); |
| markrad | 0:6c46c366f500 | 108 | |
| markrad | 0:6c46c366f500 | 109 | return strlen(work); |
| markrad | 0:6c46c366f500 | 110 | } |
| markrad | 4:9b3da9969b1b | 111 | */ |
| markrad | 4:9b3da9969b1b | 112 | |
| markrad | 4:9b3da9969b1b | 113 | static int JSONifyData(char *buffer, int bufferlen, int reading) |
| markrad | 4:9b3da9969b1b | 114 | { |
| markrad | 5:3d814faa2b2e | 115 | static const char *format = "{ \"device\": \"%s\", \"timestamp\": \"%s\", \"reading\": %f }"; |
| markrad | 5:3d814faa2b2e | 116 | static const char *timeFormat = "%FT%X"; |
| markrad | 5:3d814faa2b2e | 117 | char timeOut[80]; |
| markrad | 4:9b3da9969b1b | 118 | double work; |
| markrad | 4:9b3da9969b1b | 119 | int rc; |
| markrad | 5:3d814faa2b2e | 120 | time_t rawtime = 0; |
| markrad | 5:3d814faa2b2e | 121 | struct tm *ptm; |
| markrad | 4:9b3da9969b1b | 122 | |
| markrad | 5:3d814faa2b2e | 123 | // gmtime() does not work on the FRDM K64F - set RTC to UTC |
| markrad | 5:3d814faa2b2e | 124 | time(&rawtime); |
| markrad | 5:3d814faa2b2e | 125 | printf("%x\r\n", ptm); |
| markrad | 5:3d814faa2b2e | 126 | ptm = localtime(&rawtime); |
| markrad | 5:3d814faa2b2e | 127 | printf("%x\r\n", ptm); |
| markrad | 5:3d814faa2b2e | 128 | |
| markrad | 5:3d814faa2b2e | 129 | strftime(timeOut, sizeof(timeOut), timeFormat, ptm); |
| markrad | 5:3d814faa2b2e | 130 | printf("rawtime >>%d<<\r\n", rawtime); |
| markrad | 5:3d814faa2b2e | 131 | printf("timeOut >>%s<<\r\n", timeOut); |
| markrad | 5:3d814faa2b2e | 132 | printf("month=%d\r\n", ptm->tm_mon); |
| markrad | 4:9b3da9969b1b | 133 | work = sqrt((double)reading); |
| markrad | 5:3d814faa2b2e | 134 | rc = snprintf(buffer, bufferlen, format, "mydevice", timeOut, work); |
| markrad | 4:9b3da9969b1b | 135 | |
| markrad | 4:9b3da9969b1b | 136 | if (rc < 0) |
| markrad | 4:9b3da9969b1b | 137 | printf("*** ERROR *** out of buffer space\r\n"); |
| markrad | 4:9b3da9969b1b | 138 | |
| markrad | 4:9b3da9969b1b | 139 | return rc; |
| markrad | 4:9b3da9969b1b | 140 | } |
| markrad | 0:6c46c366f500 | 141 | |
| markrad | 2:2b9acda15ef0 | 142 | static LOCK_HANDLE msgLock; |
| markrad | 2:2b9acda15ef0 | 143 | static int msgCount = 0; |
| markrad | 3:c0556ff7b8e3 | 144 | static Timer t; |
| markrad | 3:c0556ff7b8e3 | 145 | static int CONNECTIONTIMEOUT = (20 * 1000); |
| markrad | 2:2b9acda15ef0 | 146 | |
| markrad | 0:6c46c366f500 | 147 | static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) |
| markrad | 0:6c46c366f500 | 148 | { |
| markrad | 0:6c46c366f500 | 149 | int* counter = (int*)userContextCallback; |
| markrad | 0:6c46c366f500 | 150 | const char* buffer; |
| markrad | 0:6c46c366f500 | 151 | size_t size; |
| markrad | 0:6c46c366f500 | 152 | |
| markrad | 0:6c46c366f500 | 153 | if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK) |
| markrad | 0:6c46c366f500 | 154 | { |
| markrad | 0:6c46c366f500 | 155 | (void)printf("unable to retrieve the message data\r\n"); |
| markrad | 0:6c46c366f500 | 156 | } |
| markrad | 0:6c46c366f500 | 157 | else |
| markrad | 0:6c46c366f500 | 158 | { |
| markrad | 0:6c46c366f500 | 159 | (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size); |
| markrad | 0:6c46c366f500 | 160 | } |
| markrad | 0:6c46c366f500 | 161 | |
| markrad | 0:6c46c366f500 | 162 | // Some device specific action code goes here... |
| markrad | 0:6c46c366f500 | 163 | (*counter)++; |
| markrad | 0:6c46c366f500 | 164 | |
| markrad | 0:6c46c366f500 | 165 | return IOTHUBMESSAGE_ACCEPTED; |
| markrad | 0:6c46c366f500 | 166 | } |
| markrad | 0:6c46c366f500 | 167 | |
| markrad | 0:6c46c366f500 | 168 | static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) |
| markrad | 0:6c46c366f500 | 169 | { |
| markrad | 2:2b9acda15ef0 | 170 | int* messageTrackingId = (int*)userContextCallback; |
| markrad | 0:6c46c366f500 | 171 | |
| markrad | 0:6c46c366f500 | 172 | (void)printf("Confirmation received for message tracking id = %d with result = %s\r\n", |
| markrad | 2:2b9acda15ef0 | 173 | *messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); |
| markrad | 0:6c46c366f500 | 174 | |
| markrad | 2:2b9acda15ef0 | 175 | free(userContextCallback); |
| markrad | 2:2b9acda15ef0 | 176 | Lock(msgLock); |
| markrad | 2:2b9acda15ef0 | 177 | msgCount--; |
| markrad | 3:c0556ff7b8e3 | 178 | |
| markrad | 3:c0556ff7b8e3 | 179 | if (result == IOTHUB_CLIENT_CONFIRMATION_OK) |
| markrad | 3:c0556ff7b8e3 | 180 | { |
| markrad | 3:c0556ff7b8e3 | 181 | t.stop(); |
| markrad | 3:c0556ff7b8e3 | 182 | t.reset(); |
| markrad | 3:c0556ff7b8e3 | 183 | } |
| markrad | 3:c0556ff7b8e3 | 184 | |
| markrad | 2:2b9acda15ef0 | 185 | Unlock(msgLock); |
| markrad | 2:2b9acda15ef0 | 186 | |
| markrad | 0:6c46c366f500 | 187 | } |
| markrad | 0:6c46c366f500 | 188 | |
| markrad | 0:6c46c366f500 | 189 | void stall(Serial &pc, char *message) |
| markrad | 0:6c46c366f500 | 190 | { |
| markrad | 0:6c46c366f500 | 191 | printf(message); |
| markrad | 0:6c46c366f500 | 192 | printf("stalled "); |
| markrad | 0:6c46c366f500 | 193 | |
| markrad | 4:9b3da9969b1b | 194 | while(true) |
| markrad | 4:9b3da9969b1b | 195 | { |
| markrad | 0:6c46c366f500 | 196 | pc.putc('.'); // idle dots |
| markrad | 0:6c46c366f500 | 197 | wait(1.0); |
| markrad | 0:6c46c366f500 | 198 | } |
| markrad | 0:6c46c366f500 | 199 | } |
| markrad | 0:6c46c366f500 | 200 | |
| markrad | 3:c0556ff7b8e3 | 201 | IOTHUB_CLIENT_HANDLE setupConnection(Serial &pc, const char *connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol, void *receiveContext) |
| markrad | 3:c0556ff7b8e3 | 202 | { |
| markrad | 3:c0556ff7b8e3 | 203 | IOTHUB_CLIENT_HANDLE iotHubClientHandle = NULL; |
| markrad | 3:c0556ff7b8e3 | 204 | |
| markrad | 3:c0556ff7b8e3 | 205 | printf("Calling platform_init\r\n"); |
| markrad | 3:c0556ff7b8e3 | 206 | |
| markrad | 3:c0556ff7b8e3 | 207 | while (platform_init()) |
| markrad | 3:c0556ff7b8e3 | 208 | { |
| markrad | 3:c0556ff7b8e3 | 209 | pc.putc('P'); |
| markrad | 3:c0556ff7b8e3 | 210 | wait(1.0); |
| markrad | 3:c0556ff7b8e3 | 211 | platform_deinit(); |
| markrad | 3:c0556ff7b8e3 | 212 | } |
| markrad | 3:c0556ff7b8e3 | 213 | |
| markrad | 3:c0556ff7b8e3 | 214 | // if (platform_init() != 0) |
| markrad | 3:c0556ff7b8e3 | 215 | // stall(pc, "Failed to initialize platform\n"); |
| markrad | 3:c0556ff7b8e3 | 216 | |
| markrad | 3:c0556ff7b8e3 | 217 | printf("Calling IoTHubClient_CreateFromConnectionString\r\n"); |
| markrad | 3:c0556ff7b8e3 | 218 | if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, protocol)) == NULL) |
| markrad | 3:c0556ff7b8e3 | 219 | stall(pc, "ERROR: Could not create iotHubClientHandle\n"); |
| markrad | 3:c0556ff7b8e3 | 220 | |
| markrad | 3:c0556ff7b8e3 | 221 | bool traceOn = false; |
| markrad | 3:c0556ff7b8e3 | 222 | //bool traceOn = true; |
| markrad | 3:c0556ff7b8e3 | 223 | |
| markrad | 3:c0556ff7b8e3 | 224 | printf("Calling IoTHubClient_SetOption logtrace with %d\r\n", traceOn); |
| markrad | 3:c0556ff7b8e3 | 225 | IoTHubClient_SetOption(iotHubClientHandle, "logtrace", &traceOn); |
| markrad | 3:c0556ff7b8e3 | 226 | |
| markrad | 3:c0556ff7b8e3 | 227 | // For mbed add the certificate information |
| markrad | 3:c0556ff7b8e3 | 228 | printf("Calling IoTHubClient_SetOption TrustedCerts\r\n"); |
| markrad | 3:c0556ff7b8e3 | 229 | |
| markrad | 3:c0556ff7b8e3 | 230 | if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) |
| markrad | 3:c0556ff7b8e3 | 231 | stall(pc, "ERROR: failure to set option \"TrustedCerts\"\n"); |
| markrad | 3:c0556ff7b8e3 | 232 | |
| markrad | 3:c0556ff7b8e3 | 233 | printf("Calling IoTHubClient_SetMessageCallback\r\n"); |
| markrad | 3:c0556ff7b8e3 | 234 | |
| markrad | 3:c0556ff7b8e3 | 235 | if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, receiveContext) != IOTHUB_CLIENT_OK) |
| markrad | 3:c0556ff7b8e3 | 236 | stall(pc, "ERROR: IoTHubClient_SetMessageCallback failed\r\n"); |
| markrad | 3:c0556ff7b8e3 | 237 | |
| markrad | 3:c0556ff7b8e3 | 238 | return iotHubClientHandle; |
| markrad | 3:c0556ff7b8e3 | 239 | } |
| markrad | 3:c0556ff7b8e3 | 240 | |
| markrad | 3:c0556ff7b8e3 | 241 | void terminateConnection(Serial &pc, IOTHUB_CLIENT_HANDLE iotHubClientHandle) |
| markrad | 3:c0556ff7b8e3 | 242 | { |
| markrad | 3:c0556ff7b8e3 | 243 | printf("Calling IoTHubClient_Destroy\r\n"); |
| markrad | 3:c0556ff7b8e3 | 244 | IoTHubClient_Destroy(iotHubClientHandle); |
| markrad | 3:c0556ff7b8e3 | 245 | printf("Calling platform_deinit\r\n"); |
| markrad | 3:c0556ff7b8e3 | 246 | platform_deinit(); |
| markrad | 3:c0556ff7b8e3 | 247 | printf("Connection terminated\r\n"); |
| markrad | 3:c0556ff7b8e3 | 248 | } |
| markrad | 3:c0556ff7b8e3 | 249 | |
| markrad | 5:3d814faa2b2e | 250 | void calibrate(double *pmean, double *pdeviation) |
| markrad | 5:3d814faa2b2e | 251 | { |
| markrad | 5:3d814faa2b2e | 252 | READING reading; |
| markrad | 5:3d814faa2b2e | 253 | const int calibrationPeriod = 10; // in seconds |
| markrad | 5:3d814faa2b2e | 254 | SingletonFXOS8700CQ &sfxos = SingletonFXOS8700CQ::getInstance(); |
| markrad | 5:3d814faa2b2e | 255 | |
| markrad | 5:3d814faa2b2e | 256 | int *data = new int[calibrationPeriod * 50]; |
| markrad | 5:3d814faa2b2e | 257 | int i; |
| markrad | 5:3d814faa2b2e | 258 | long sum = 0; |
| markrad | 5:3d814faa2b2e | 259 | double mean = 0; |
| markrad | 5:3d814faa2b2e | 260 | double temp; |
| markrad | 5:3d814faa2b2e | 261 | |
| markrad | 5:3d814faa2b2e | 262 | printf("Calibrating...\r\n"); |
| markrad | 5:3d814faa2b2e | 263 | |
| markrad | 5:3d814faa2b2e | 264 | for (i = 0; i < calibrationPeriod * 50; i++) |
| markrad | 5:3d814faa2b2e | 265 | { |
| markrad | 5:3d814faa2b2e | 266 | if (sfxos.getInt2Triggered()) |
| markrad | 5:3d814faa2b2e | 267 | { |
| markrad | 5:3d814faa2b2e | 268 | sfxos.setInt2Triggered(false); |
| markrad | 5:3d814faa2b2e | 269 | sfxos.getData(reading); |
| markrad | 5:3d814faa2b2e | 270 | data[i] = (reading.accelerometer.x * reading.accelerometer.x) + (reading.accelerometer.y * reading.accelerometer.y); |
| markrad | 5:3d814faa2b2e | 271 | printf("x=%d\t\ty=%d\r\n", reading.accelerometer.x, reading.accelerometer.y); |
| markrad | 5:3d814faa2b2e | 272 | sum += data[i]; |
| markrad | 5:3d814faa2b2e | 273 | wait_ms(20); |
| markrad | 5:3d814faa2b2e | 274 | } |
| markrad | 5:3d814faa2b2e | 275 | else |
| markrad | 5:3d814faa2b2e | 276 | { |
| markrad | 5:3d814faa2b2e | 277 | printf("no data\r\n"); |
| markrad | 5:3d814faa2b2e | 278 | i--; // Try that reading again |
| markrad | 5:3d814faa2b2e | 279 | } |
| markrad | 5:3d814faa2b2e | 280 | } |
| markrad | 5:3d814faa2b2e | 281 | |
| markrad | 5:3d814faa2b2e | 282 | mean = (double)sum / (double)(calibrationPeriod * 50); |
| markrad | 5:3d814faa2b2e | 283 | |
| markrad | 5:3d814faa2b2e | 284 | for (i = 0; i < calibrationPeriod * 50; i++) |
| markrad | 5:3d814faa2b2e | 285 | { |
| markrad | 5:3d814faa2b2e | 286 | temp += ((float)data[i] - mean) * ((float)data[i] - mean); |
| markrad | 5:3d814faa2b2e | 287 | } |
| markrad | 5:3d814faa2b2e | 288 | |
| markrad | 5:3d814faa2b2e | 289 | temp /= (double)(calibrationPeriod * 50); |
| markrad | 5:3d814faa2b2e | 290 | |
| markrad | 5:3d814faa2b2e | 291 | delete [] data; |
| markrad | 5:3d814faa2b2e | 292 | |
| markrad | 5:3d814faa2b2e | 293 | *pmean = mean; |
| markrad | 5:3d814faa2b2e | 294 | *pdeviation = sqrt(temp); |
| markrad | 5:3d814faa2b2e | 295 | |
| markrad | 5:3d814faa2b2e | 296 | printf("Calibration complete - mean=%f; devation=%f\r\n", *pmean, *pdeviation); |
| markrad | 5:3d814faa2b2e | 297 | } |
| markrad | 3:c0556ff7b8e3 | 298 | |
| markrad | 0:6c46c366f500 | 299 | int main() |
| markrad | 0:6c46c366f500 | 300 | { |
| markrad | 4:9b3da9969b1b | 301 | const char *connectionString = "HostName=MarkRadHub1.azure-devices.net;DeviceId=mrcc3200;SharedAccessKey=8pGKChTBsz0VGw234iLX7XDDKwcyWRC7hsrVZEHfZHs="; |
| markrad | 0:6c46c366f500 | 302 | |
| markrad | 0:6c46c366f500 | 303 | READING reading; |
| markrad | 0:6c46c366f500 | 304 | Serial pc(USBTX, USBRX); // Primary output to demonstrate library |
| markrad | 0:6c46c366f500 | 305 | SingletonFXOS8700CQ &sfxos = SingletonFXOS8700CQ::getInstance(); |
| markrad | 0:6c46c366f500 | 306 | IOTHUB_CLIENT_HANDLE iotHubClientHandle; |
| markrad | 0:6c46c366f500 | 307 | int receiveContext = 0; |
| markrad | 0:6c46c366f500 | 308 | int transmitCounter = 0; |
| markrad | 5:3d814faa2b2e | 309 | double mean; |
| markrad | 5:3d814faa2b2e | 310 | double deviation; |
| markrad | 2:2b9acda15ef0 | 311 | |
| markrad | 0:6c46c366f500 | 312 | pc.baud(115200); // Print quickly! 200Hz x line of output data! |
| markrad | 0:6c46c366f500 | 313 | |
| markrad | 5:3d814faa2b2e | 314 | printf("\n\nFXOS8700CQ identity = %X\r\n", sfxos.getWhoAmI()); |
| markrad | 0:6c46c366f500 | 315 | |
| markrad | 2:2b9acda15ef0 | 316 | msgLock = Lock_Init(); // TODO: Check error code |
| markrad | 2:2b9acda15ef0 | 317 | |
| markrad | 0:6c46c366f500 | 318 | sfxos.enable(); |
| markrad | 0:6c46c366f500 | 319 | sfxos.getData(reading); |
| markrad | 0:6c46c366f500 | 320 | |
| markrad | 0:6c46c366f500 | 321 | int rc; |
| markrad | 0:6c46c366f500 | 322 | |
| markrad | 1:0366fad6e60c | 323 | int LOOPCOUNT = -1; // Set to -1 to run forever |
| markrad | 0:6c46c366f500 | 324 | |
| markrad | 2:2b9acda15ef0 | 325 | int localMsgCount; |
| markrad | 2:2b9acda15ef0 | 326 | int *userContext; |
| markrad | 2:2b9acda15ef0 | 327 | IOTHUB_MESSAGE_HANDLE msgHandle; |
| markrad | 3:c0556ff7b8e3 | 328 | int elapsedTime = 0; |
| markrad | 2:2b9acda15ef0 | 329 | |
| markrad | 0:6c46c366f500 | 330 | char buffer[200]; |
| markrad | 0:6c46c366f500 | 331 | |
| markrad | 3:c0556ff7b8e3 | 332 | iotHubClientHandle = setupConnection(pc, connectionString, MQTT_Protocol, &receiveContext); |
| markrad | 3:c0556ff7b8e3 | 333 | |
| markrad | 5:3d814faa2b2e | 334 | calibrate(&mean, &deviation); |
| markrad | 5:3d814faa2b2e | 335 | |
| markrad | 4:9b3da9969b1b | 336 | int readCount = 0; |
| markrad | 4:9b3da9969b1b | 337 | int32_t maxVal = LONG_MIN; |
| markrad | 4:9b3da9969b1b | 338 | int32_t curVal; |
| markrad | 5:3d814faa2b2e | 339 | int32_t oneHour = 60 * 60 * 50; |
| markrad | 4:9b3da9969b1b | 340 | |
| markrad | 0:6c46c366f500 | 341 | while (LOOPCOUNT) |
| markrad | 0:6c46c366f500 | 342 | { |
| markrad | 0:6c46c366f500 | 343 | if (sfxos.getInt2Triggered()) |
| markrad | 0:6c46c366f500 | 344 | { |
| markrad | 0:6c46c366f500 | 345 | sfxos.setInt2Triggered(false); |
| markrad | 0:6c46c366f500 | 346 | sfxos.getData(reading); |
| markrad | 4:9b3da9969b1b | 347 | curVal = (reading.accelerometer.x * reading.accelerometer.x) + (reading.accelerometer.y * reading.accelerometer.y); |
| markrad | 4:9b3da9969b1b | 348 | |
| markrad | 4:9b3da9969b1b | 349 | if (curVal > maxVal) |
| markrad | 5:3d814faa2b2e | 350 | { |
| markrad | 5:3d814faa2b2e | 351 | printf("new maxVal=%d\r\n", maxVal); |
| markrad | 4:9b3da9969b1b | 352 | maxVal = curVal; |
| markrad | 5:3d814faa2b2e | 353 | } |
| markrad | 4:9b3da9969b1b | 354 | |
| markrad | 4:9b3da9969b1b | 355 | //rc = readingToJSON(buffer, sizeof(buffer), reading); |
| markrad | 4:9b3da9969b1b | 356 | |
| markrad | 4:9b3da9969b1b | 357 | //if (rc > sizeof(buffer)) |
| markrad | 4:9b3da9969b1b | 358 | //printf("ERROR: JSON buffer too small - require %d characters\n", rc); |
| markrad | 0:6c46c366f500 | 359 | |
| markrad | 4:9b3da9969b1b | 360 | if (++readCount >= 50) |
| markrad | 0:6c46c366f500 | 361 | { |
| markrad | 4:9b3da9969b1b | 362 | Lock(msgLock); |
| markrad | 4:9b3da9969b1b | 363 | localMsgCount = msgCount; |
| markrad | 4:9b3da9969b1b | 364 | Unlock(msgLock); |
| markrad | 4:9b3da9969b1b | 365 | |
| markrad | 4:9b3da9969b1b | 366 | if (localMsgCount < 2) |
| markrad | 0:6c46c366f500 | 367 | { |
| markrad | 4:9b3da9969b1b | 368 | rc = JSONifyData(buffer, sizeof(buffer), maxVal); |
| markrad | 4:9b3da9969b1b | 369 | printf("DATA >>>%s<<<\r\n", buffer); |
| markrad | 2:2b9acda15ef0 | 370 | |
| markrad | 4:9b3da9969b1b | 371 | if ((msgHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)buffer, rc)) == NULL) |
| markrad | 4:9b3da9969b1b | 372 | { |
| markrad | 4:9b3da9969b1b | 373 | (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n"); |
| markrad | 4:9b3da9969b1b | 374 | } |
| markrad | 4:9b3da9969b1b | 375 | else |
| markrad | 0:6c46c366f500 | 376 | { |
| markrad | 4:9b3da9969b1b | 377 | userContext = (int *) malloc(sizeof(userContext)); |
| markrad | 4:9b3da9969b1b | 378 | |
| markrad | 4:9b3da9969b1b | 379 | if (userContext != NULL) |
| markrad | 2:2b9acda15ef0 | 380 | { |
| markrad | 4:9b3da9969b1b | 381 | *userContext = transmitCounter; |
| markrad | 4:9b3da9969b1b | 382 | |
| markrad | 4:9b3da9969b1b | 383 | if (IoTHubClient_SendEventAsync(iotHubClientHandle, msgHandle, SendConfirmationCallback, userContext) != IOTHUB_CLIENT_OK) |
| markrad | 4:9b3da9969b1b | 384 | { |
| markrad | 4:9b3da9969b1b | 385 | (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n"); |
| markrad | 4:9b3da9969b1b | 386 | } |
| markrad | 4:9b3da9969b1b | 387 | else |
| markrad | 4:9b3da9969b1b | 388 | { |
| markrad | 4:9b3da9969b1b | 389 | (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)transmitCounter); |
| markrad | 4:9b3da9969b1b | 390 | } |
| markrad | 4:9b3da9969b1b | 391 | |
| markrad | 4:9b3da9969b1b | 392 | IoTHubMessage_Destroy(msgHandle); |
| markrad | 4:9b3da9969b1b | 393 | Lock(msgLock); |
| markrad | 4:9b3da9969b1b | 394 | msgCount++; |
| markrad | 4:9b3da9969b1b | 395 | t.start(); |
| markrad | 4:9b3da9969b1b | 396 | Unlock(msgLock); |
| markrad | 4:9b3da9969b1b | 397 | |
| markrad | 4:9b3da9969b1b | 398 | transmitCounter++; |
| markrad | 2:2b9acda15ef0 | 399 | } |
| markrad | 2:2b9acda15ef0 | 400 | else |
| markrad | 2:2b9acda15ef0 | 401 | { |
| markrad | 4:9b3da9969b1b | 402 | (void)printf("ERROR: malloc - unable to allocate user context\r\n"); |
| markrad | 2:2b9acda15ef0 | 403 | } |
| markrad | 0:6c46c366f500 | 404 | } |
| markrad | 0:6c46c366f500 | 405 | } |
| markrad | 4:9b3da9969b1b | 406 | else |
| markrad | 4:9b3da9969b1b | 407 | { |
| markrad | 4:9b3da9969b1b | 408 | (void)printf("Message dropped queue length %d\r\n", localMsgCount); |
| markrad | 4:9b3da9969b1b | 409 | } |
| markrad | 4:9b3da9969b1b | 410 | |
| markrad | 4:9b3da9969b1b | 411 | Lock(msgLock); |
| markrad | 4:9b3da9969b1b | 412 | elapsedTime = t.read_ms(); |
| markrad | 4:9b3da9969b1b | 413 | Unlock(msgLock); |
| markrad | 4:9b3da9969b1b | 414 | |
| markrad | 4:9b3da9969b1b | 415 | if (elapsedTime > CONNECTIONTIMEOUT) |
| markrad | 4:9b3da9969b1b | 416 | { |
| markrad | 4:9b3da9969b1b | 417 | printf("No response for %d milliseconds - attempt reconnection\r\n", elapsedTime); |
| markrad | 4:9b3da9969b1b | 418 | NVIC_SystemReset(); // Just blow it all away |
| markrad | 4:9b3da9969b1b | 419 | terminateConnection(pc, iotHubClientHandle); |
| markrad | 4:9b3da9969b1b | 420 | iotHubClientHandle = setupConnection(pc, connectionString, MQTT_Protocol, &receiveContext); |
| markrad | 4:9b3da9969b1b | 421 | printf("Reconnection complete\r\n"); |
| markrad | 4:9b3da9969b1b | 422 | } |
| markrad | 4:9b3da9969b1b | 423 | |
| markrad | 4:9b3da9969b1b | 424 | if (LOOPCOUNT > 0) |
| markrad | 4:9b3da9969b1b | 425 | LOOPCOUNT--; |
| markrad | 4:9b3da9969b1b | 426 | readCount = 0; |
| markrad | 4:9b3da9969b1b | 427 | maxVal = LONG_MIN; |
| markrad | 2:2b9acda15ef0 | 428 | } |
| markrad | 4:9b3da9969b1b | 429 | } |
| markrad | 4:9b3da9969b1b | 430 | else |
| markrad | 4:9b3da9969b1b | 431 | { |
| markrad | 4:9b3da9969b1b | 432 | printf("*** WARNING*** Sensor was not ready in time\r\n"); |
| markrad | 0:6c46c366f500 | 433 | } |
| markrad | 0:6c46c366f500 | 434 | |
| markrad | 4:9b3da9969b1b | 435 | // Read at 50 hz |
| markrad | 5:3d814faa2b2e | 436 | |
| markrad | 5:3d814faa2b2e | 437 | if (--oneHour == 0) |
| markrad | 5:3d814faa2b2e | 438 | { |
| markrad | 5:3d814faa2b2e | 439 | printf("INFO: Updating RTC from NTP server\r\n"); |
| markrad | 5:3d814faa2b2e | 440 | |
| markrad | 5:3d814faa2b2e | 441 | oneHour = 60 * 60 * 50; |
| markrad | 5:3d814faa2b2e | 442 | |
| markrad | 5:3d814faa2b2e | 443 | if (ntp.setTime("0.pool.ntp.org") != 0) |
| markrad | 5:3d814faa2b2e | 444 | { |
| markrad | 5:3d814faa2b2e | 445 | printf("ERROR: Failed to set current time from NTP server\r\n"); |
| markrad | 5:3d814faa2b2e | 446 | } |
| markrad | 5:3d814faa2b2e | 447 | } |
| markrad | 5:3d814faa2b2e | 448 | |
| markrad | 4:9b3da9969b1b | 449 | wait_ms(20); |
| markrad | 0:6c46c366f500 | 450 | } |
| markrad | 0:6c46c366f500 | 451 | |
| markrad | 0:6c46c366f500 | 452 | printf("Loop complete - clean up\n"); |
| markrad | 0:6c46c366f500 | 453 | |
| markrad | 3:c0556ff7b8e3 | 454 | terminateConnection(pc, iotHubClientHandle); |
| markrad | 2:2b9acda15ef0 | 455 | Lock_Deinit(msgLock); |
| markrad | 2:2b9acda15ef0 | 456 | |
| markrad | 0:6c46c366f500 | 457 | printf("Test complete\n"); |
| markrad | 0:6c46c366f500 | 458 | |
| markrad | 0:6c46c366f500 | 459 | |
| markrad | 4:9b3da9969b1b | 460 | while(true) |
| markrad | 4:9b3da9969b1b | 461 | { |
| markrad | 0:6c46c366f500 | 462 | pc.putc('.'); // idle dots |
| markrad | 0:6c46c366f500 | 463 | wait(1.0); |
| markrad | 0:6c46c366f500 | 464 | } |
| markrad | 0:6c46c366f500 | 465 | } |