diff --git a/drivers/COMPONENT_wifi_ism43362.lib b/drivers/COMPONENT_wifi_ism43362.lib new file mode 100644 index 0000000..1abfaca --- /dev/null +++ b/drivers/COMPONENT_wifi_ism43362.lib @@ -0,0 +1 @@ +https://github.com/ARMmbed/wifi-ism43362/#9e1851a7fe5e2ffb07533aacc7114df2e73f7784 diff --git a/iothub_ll_telemetry_sample.cpp b/iothub_ll_telemetry_sample.cpp new file mode 100644 index 0000000..a237ada --- /dev/null +++ b/iothub_ll_telemetry_sample.cpp @@ -0,0 +1,256 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +// CAVEAT: This sample is to demonstrate azure IoT client concepts only and is not a guide design principles or style +// Checking of return codes and error values shall be omitted for brevity. Please practice sound engineering practices +// when writing production code. + +#include "mbed.h" +#include "rtc_api.h" +#include "NTPClient.h" + +#include +#include + +#include "iothub.h" +#include "iothub_device_client_ll.h" +#include "iothub_client_options.h" +#include "iothub_message.h" +#include "azure_c_shared_utility/threadapi.h" +#include "azure_c_shared_utility/crt_abstractions.h" +#include "azure_c_shared_utility/shared_util_options.h" + +#define SET_TRUSTED_CERT_IN_SAMPLES + +#ifdef SET_TRUSTED_CERT_IN_SAMPLES +#include "certs.h" +#endif // SET_TRUSTED_CERT_IN_SAMPLES + +/* This sample uses the _LL APIs of iothub_client for example purposes. +Simply changing the using the convenience layer (functions not having _LL) +and removing calls to _DoWork will yield the same results. */ + +// The protocol you wish to use should be uncommented +// +#define SAMPLE_MQTT +//#define SAMPLE_MQTT_OVER_WEBSOCKETS +//#define SAMPLE_AMQP +//#define SAMPLE_AMQP_OVER_WEBSOCKETS +//#define SAMPLE_HTTP + +#ifdef SAMPLE_MQTT + #include "iothubtransportmqtt.h" +#endif // SAMPLE_MQTT +#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS + #include "iothubtransportmqtt_websockets.h" +#endif // SAMPLE_MQTT_OVER_WEBSOCKETS +#ifdef SAMPLE_AMQP + #include "iothubtransportamqp.h" +#endif // SAMPLE_AMQP +#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS + #include "iothubtransportamqp_websockets.h" +#endif // SAMPLE_AMQP_OVER_WEBSOCKETS +#ifdef SAMPLE_HTTP + #include "iothubtransporthttp.h" +#endif // SAMPLE_HTTP + + +/* Your iothub connection string */ +static const char* connectionString = MBED_CONF_APP_IOTHUB_CONNECTION_STRING; +#define MESSAGE_COUNT 5 +static size_t g_message_count_send_confirmations = 0; + +static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback) +{ + (void)userContextCallback; + // When a message is sent this callback will get envoked + g_message_count_send_confirmations++; + (void)printf("Confirmation callback received for message %lu with result %s\r\n", (unsigned long)g_message_count_send_confirmations, MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result)); +} + +static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context) +{ + (void)reason; + (void)user_context; + // This sample DOES NOT take into consideration network outages. + if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED) + { + (void)printf("The device client is connected to iothub\r\n"); + } + else + { + (void)printf("The device client has been disconnected\r\n"); + } +} + +static IOTHUBMESSAGE_DISPOSITION_RESULT receive_msg_callback(IOTHUB_MESSAGE_HANDLE message, void* user_context) +{ + const unsigned char* msg; + size_t len; + + if (IoTHubMessage_GetByteArray(message, &msg, &len) != IOTHUB_MESSAGE_OK) + { + printf("Error getting message\n"); + } + else + { + printf("Received from cloud: %.*s\n", (int)len, msg); + } + + return IOTHUBMESSAGE_ACCEPTED; +} + +int run(void) +{ + IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol; + IOTHUB_MESSAGE_HANDLE message_handle; + size_t messages_sent = 0; + const char* telemetry_msg = "Hello from the device"; + + // Select the Protocol to use with the connection +#ifdef SAMPLE_MQTT + protocol = MQTT_Protocol; +#endif // SAMPLE_MQTT +#ifdef SAMPLE_MQTT_OVER_WEBSOCKETS + protocol = MQTT_WebSocket_Protocol; +#endif // SAMPLE_MQTT_OVER_WEBSOCKETS +#ifdef SAMPLE_AMQP + protocol = AMQP_Protocol; +#endif // SAMPLE_AMQP +#ifdef SAMPLE_AMQP_OVER_WEBSOCKETS + protocol = AMQP_Protocol_over_WebSocketsTls; +#endif // SAMPLE_AMQP_OVER_WEBSOCKETS +#ifdef SAMPLE_HTTP + protocol = HTTP_Protocol; +#endif // SAMPLE_HTTP + + // Used to initialize IoTHub SDK subsystem + (void)IoTHub_Init(); + + IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle; + + (void)printf("Creating IoTHub Device handle\r\n"); + // Create the iothub handle here + device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol); + if (device_ll_handle == NULL) + { + (void)printf("Failure createing Iothub device. Hint: Check you connection string.\r\n"); + } + else + { + // Set any option that are neccessary. + // For available options please see the iothub_sdk_options.md documentation + +#ifndef SAMPLE_HTTP + // Can not set this options in HTTP + bool traceOn = true; + IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_LOG_TRACE, &traceOn); +#endif + +#ifdef SET_TRUSTED_CERT_IN_SAMPLES + // Setting the Trusted Certificate. This is only necessary on system with without + // built in certificate stores. + IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_TRUSTED_CERT, certificates); +#endif // SET_TRUSTED_CERT_IN_SAMPLES + +#if defined SAMPLE_MQTT || defined SAMPLE_MQTT_WS + //Setting the auto URL Encoder (recommended for MQTT). Please use this option unless + //you are URL Encoding inputs yourself. + //ONLY valid for use with MQTT + //bool urlEncodeOn = true; + //IoTHubDeviceClient_LL_SetOption(iothub_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn); +#endif + + // Setting connection status callback to get indication of connection to iothub + (void)IoTHubDeviceClient_LL_SetConnectionStatusCallback(device_ll_handle, connection_status_callback, NULL); + + if (IOTHUB_CLIENT_OK != IoTHubDeviceClient_LL_SetMessageCallback(device_ll_handle, receive_msg_callback, NULL)) { + printf("error IoTHubDeviceClient_LL_SetMessageCallback\n"); + } + + while (true) + { + if (messages_sent < MESSAGE_COUNT) + { + // Construct the iothub message from a string or a byte array + message_handle = IoTHubMessage_CreateFromString(telemetry_msg); + //message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))); + + // Set Message property + /*(void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID"); + (void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID"); + (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson"); + (void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");*/ + + // Add custom properties to message + (void)IoTHubMessage_SetProperty(message_handle, "property_key", "property_value"); + + (void)printf("Sending message %d to IoTHub\r\n", (int)(messages_sent + 1)); + IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL); + + // The message is copied to the sdk so the we can destroy it + IoTHubMessage_Destroy(message_handle); + + messages_sent++; + } + + IoTHubDeviceClient_LL_DoWork(device_ll_handle); + ThreadAPI_Sleep(1); + } + + // Clean up the iothub sdk handle + IoTHubDeviceClient_LL_Destroy(device_ll_handle); + } + // Free all the sdk subsystem + IoTHub_Deinit(); + + printf("Press any key to continue"); + (void)getchar(); + + return 0; +} + +// Global symbol used by mbed-azure-client/deps/c-utility/adapters/tcpsocketconnection_mbed_os5.cpp +NetworkInterface *_defaultSystemNetwork; + +int main() +{ + printf("Getting network interface\n"); + + _defaultSystemNetwork = NetworkInterface::get_default_instance(); + if (_defaultSystemNetwork == NULL) { + printf("ERROR: No WiFiInterface found.\n"); + return -1; + } + + printf("\nConnecting...\n"); + int ret = _defaultSystemNetwork->connect(); + if (ret != 0) { + printf("\nConnection error: %d\n", ret); + return -1; + } + + printf("MAC: %s\n", _defaultSystemNetwork->get_mac_address()); + printf("Connection Success\n"); + + NTPClient ntp(_defaultSystemNetwork); + rtc_init(); + + time_t timestamp = ntp.get_timestamp(); + if (timestamp < 0) { + printf("An error occurred when getting the time. Code: %ld\r\n", timestamp); + return 1; + } + + rtc_write(timestamp); + printf("Current time %s written to RTC\r\n", ctime(×tamp)); + + time_t rtc_timestamp = rtc_read(); + printf("RTC reports %s\n", ctime(&rtc_timestamp)); + + run(); + + while(true) { + sleep(); + } +} diff --git a/mbed-azure-client.lib b/mbed-azure-client.lib new file mode 100644 index 0000000..beaac0e --- /dev/null +++ b/mbed-azure-client.lib @@ -0,0 +1 @@ +https://github.com/LDong-Arm/mbed-azure-client#fa7b265ed992b50043b2cfce66e8c21dda28ad36 diff --git a/mbed-os.lib b/mbed-os.lib new file mode 100644 index 0000000..25bf85c --- /dev/null +++ b/mbed-os.lib @@ -0,0 +1 @@ +https://github.com/ARMmbed/mbed-os#a2ada74770f043aff3e61e29d164a8e78274fcd4 diff --git a/mbed_app.json b/mbed_app.json new file mode 100644 index 0000000..db37add --- /dev/null +++ b/mbed_app.json @@ -0,0 +1,21 @@ +{ + "config": { + "iothub_connection_string": { + "help": "Azure IoT Hub connection string", + "value": "\"HostName=...;DeviceId=...;SharedAccessKey=...\"" + } + }, + "target_overrides": { + "*": { + "platform.stdio-convert-newlines": true, + "platform.stdio-baud-rate": 115200 + }, + "DISCO_L475VG_IOT01A": { + "target.components_add": ["wifi_ism43362"], + "target.network-default-interface-type": "WIFI", + "nsapi.default-wifi-security": "WPA_WPA2", + "nsapi.default-wifi-ssid": "\"SSID\"", + "nsapi.default-wifi-password": "\"PASSWORD\"" + } + } +} diff --git a/ntp-client.lib b/ntp-client.lib new file mode 100644 index 0000000..914a266 --- /dev/null +++ b/ntp-client.lib @@ -0,0 +1 @@ +https://github.com/ARMmbed/ntp-client.git#a4ccf62992b9adc5086a9afea08fa4deb2e2fbd7