Simple sample that demonstrates reading the FXOS8700CQ accelerometer, convert the data to JSON and send to an Azure IoT Hub.
Dependencies: azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip
Diff: main.cpp
- Revision:
- 3:c0556ff7b8e3
- Parent:
- 2:2b9acda15ef0
- Child:
- 4:9b3da9969b1b
--- a/main.cpp Thu Oct 27 22:44:50 2016 +0000 +++ b/main.cpp Thu Dec 08 00:11:40 2016 +0000 @@ -106,6 +106,8 @@ static LOCK_HANDLE msgLock; static int msgCount = 0; +static Timer t; +static int CONNECTIONTIMEOUT = (20 * 1000); static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback) { @@ -138,6 +140,13 @@ free(userContextCallback); Lock(msgLock); msgCount--; + + if (result == IOTHUB_CLIENT_CONFIRMATION_OK) + { + t.stop(); + t.reset(); + } + Unlock(msgLock); } @@ -153,6 +162,56 @@ } } +IOTHUB_CLIENT_HANDLE setupConnection(Serial &pc, const char *connectionString, IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol, void *receiveContext) +{ + IOTHUB_CLIENT_HANDLE iotHubClientHandle = NULL; + + printf("Calling platform_init\r\n"); + + while (platform_init()) + { + pc.putc('P'); + wait(1.0); + platform_deinit(); + } + +// if (platform_init() != 0) +// stall(pc, "Failed to initialize platform\n"); + + printf("Calling IoTHubClient_CreateFromConnectionString\r\n"); + if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, protocol)) == NULL) + stall(pc, "ERROR: Could not create iotHubClientHandle\n"); + + bool traceOn = false; + //bool traceOn = true; + + printf("Calling IoTHubClient_SetOption logtrace with %d\r\n", traceOn); + IoTHubClient_SetOption(iotHubClientHandle, "logtrace", &traceOn); + + // For mbed add the certificate information + printf("Calling IoTHubClient_SetOption TrustedCerts\r\n"); + + if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) + stall(pc, "ERROR: failure to set option \"TrustedCerts\"\n"); + + printf("Calling IoTHubClient_SetMessageCallback\r\n"); + + if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, receiveContext) != IOTHUB_CLIENT_OK) + stall(pc, "ERROR: IoTHubClient_SetMessageCallback failed\r\n"); + + return iotHubClientHandle; +} + +void terminateConnection(Serial &pc, IOTHUB_CLIENT_HANDLE iotHubClientHandle) +{ + printf("Calling IoTHubClient_Destroy\r\n"); + IoTHubClient_Destroy(iotHubClientHandle); + printf("Calling platform_deinit\r\n"); + platform_deinit(); + printf("Connection terminated\r\n"); +} + + int main() { const char *connectionString = "HostName=MarkRadML.azure-devices.net;DeviceId=MBEDTest;SharedAccessKey=rgxlnR0rIBW4vnnnDkrbAv+mSOc/Mt60mg1CEjLx7pY="; @@ -174,33 +233,18 @@ sfxos.getData(reading); int rc; - - if (platform_init() != 0) - stall(pc, "Failed to initialize platform\n"); - - if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL) - stall(pc, "ERROR: Could not create iotHubClientHandle\n"); - - bool traceOn = false; - //bool traceOn = true; - - IoTHubClient_SetOption(iotHubClientHandle, "logtrace", &traceOn); - - // For mbed add the certificate information - if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) - stall(pc, "ERROR: failure to set option \"TrustedCerts\"\n"); - - if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK) - stall(pc, "ERROR: IoTHubClient_SetMessageCallback failed\r\n"); int LOOPCOUNT = -1; // Set to -1 to run forever int localMsgCount; int *userContext; IOTHUB_MESSAGE_HANDLE msgHandle; + int elapsedTime = 0; char buffer[200]; + iotHubClientHandle = setupConnection(pc, connectionString, MQTT_Protocol, &receiveContext); + while (LOOPCOUNT) { if (sfxos.getInt2Triggered()) @@ -242,6 +286,7 @@ IoTHubMessage_Destroy(msgHandle); Lock(msgLock); msgCount++; + t.start(); Unlock(msgLock); transmitCounter++; @@ -257,7 +302,21 @@ (void)printf("Message dropped queue length %d\r\n", localMsgCount); } - LOOPCOUNT--; + Lock(msgLock); + elapsedTime = t.read_ms(); + Unlock(msgLock); + + if (elapsedTime > CONNECTIONTIMEOUT) + { + printf("No response for %d milliseconds - attempt reconnection\r\n", elapsedTime); + NVIC_SystemReset(); // Just blow it all away + terminateConnection(pc, iotHubClientHandle); + iotHubClientHandle = setupConnection(pc, connectionString, MQTT_Protocol, &receiveContext); + printf("Reconnection complete\r\n"); + } + + if (LOOPCOUNT > 0) + LOOPCOUNT--; } wait_ms(500); @@ -265,12 +324,9 @@ printf("Loop complete - clean up\n"); - IoTHubClient_Destroy(iotHubClientHandle); - + terminateConnection(pc, iotHubClientHandle); Lock_Deinit(msgLock); - platform_deinit(); - printf("Test complete\n");