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: BSP_B-L475E-IOT01
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include <events/mbed_events.h> 00018 #include <mbed.h> 00019 #include "mbed.h" 00020 #include "TCPSocket.h" 00021 #include "wifi-ism43362/ISM43362Interface.h" 00022 #include "treasure-data-rest.h" 00023 #include "ble/BLE.h" 00024 #include "LEDService.h" 00025 #define BUFF_SIZE 200 00026 00027 DigitalOut alivenessLED(LED1, 0); 00028 DigitalOut actuatedLED(LED2, 0); 00029 ISM43362Interface net; 00030 LEDService *ledServicePtr; 00031 00032 const static char DEVICE_NAME[] = "LED"; 00033 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; 00034 static EventQueue eventQueue(/* event count */ 10 * EVENTS_EVENT_SIZE); 00035 char payload_buff [BUFF_SIZE] = {0}; 00036 int x = 0; 00037 TreasureData_RESTAPI* payload = new TreasureData_RESTAPI(&net,"iot_test","payload", MBED_CONF_APP_API_KEY); 00038 00039 void ConnectToWifi(){ 00040 // Connect to Wifi 00041 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); 00042 int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); 00043 if (ret != 0) { 00044 printf("\nConnection error: %d\n", ret); 00045 } 00046 00047 printf("Success\n\n"); 00048 printf("MAC: %s\n", net.get_mac_address()); 00049 printf("IP: %s\n", net.get_ip_address()); 00050 printf("Netmask: %s\n", net.get_netmask()); 00051 printf("Gateway: %s\n", net.get_gateway()); 00052 printf("RSSI: %d\n\n", net.get_rssi()); 00053 } 00054 00055 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00056 { 00057 (void) params; 00058 BLE::Instance().gap().startAdvertising(); 00059 } 00060 00061 void blinkCallback(void) 00062 { 00063 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ 00064 } 00065 00066 /** 00067 * This callback allows the LEDService to receive updates to the ledState Characteristic. 00068 * 00069 * @param[in] params 00070 * Information about the characterisitc being updated. 00071 */ 00072 void onDataWrittenCallback(const GattWriteCallbackParams *params) { 00073 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { 00074 actuatedLED = *(params->data); 00075 // Construct strings to send 00076 x = sprintf(payload_buff,"{\"payload\":\"%u\"}", *(params->data)); 00077 payload_buff[x]=0; // null terminate the string 00078 00079 // Send data to Treasure data 00080 printf("\r\n Sending Payload: '%s'\r\n",payload_buff); 00081 payload->sendData(payload_buff,strlen(payload_buff)); 00082 } 00083 } 00084 00085 /** 00086 * This function is called when the ble initialization process has failled 00087 */ 00088 void onBleInitError(BLE &ble, ble_error_t error) 00089 { 00090 /* Initialization error handling should go here */ 00091 } 00092 00093 /** 00094 * Callback triggered when the ble initialization process has finished 00095 */ 00096 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00097 { 00098 BLE& ble = params->ble; 00099 ble_error_t error = params->error; 00100 00101 if (error != BLE_ERROR_NONE) { 00102 /* In case of error, forward the error handling to onBleInitError */ 00103 onBleInitError(ble, error); 00104 return; 00105 } 00106 00107 /* Ensure that it is the default instance of BLE */ 00108 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00109 return; 00110 } 00111 00112 ble.gap().onDisconnection(disconnectionCallback); 00113 ble.gattServer().onDataWritten(onDataWrittenCallback); 00114 00115 bool initialValueForLEDCharacteristic = false; 00116 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); 00117 00118 /* setup advertising */ 00119 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00120 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00121 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00122 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00123 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ 00124 ble.gap().startAdvertising(); 00125 } 00126 00127 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { 00128 BLE &ble = BLE::Instance(); 00129 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); 00130 00131 } 00132 00133 int main() 00134 { 00135 ConnectToWifi(); 00136 00137 eventQueue.call_every(500, blinkCallback); 00138 00139 BLE &ble = BLE::Instance(); 00140 ble.onEventsToProcess(scheduleBleEventsProcessing); 00141 ble.init(bleInitComplete); 00142 00143 eventQueue.dispatch_forever(); 00144 00145 return 0; 00146 }
Generated on Fri Jul 22 2022 08:37:01 by
1.7.2