receive message via BLE and send data to treasure data via postback API using wifi
Dependencies: BSP_B-L475E-IOT01
Diff: source/main.cpp
- Revision:
- 39:8990b0ce7233
- Parent:
- 27:ff1fb7b5f9ee
--- a/source/main.cpp Wed Jul 26 14:47:11 2017 +0200 +++ b/source/main.cpp Tue Nov 27 06:35:39 2018 +0000 @@ -16,18 +16,41 @@ #include <events/mbed_events.h> #include <mbed.h> +#include "mbed.h" +#include "TCPSocket.h" +#include "wifi-ism43362/ISM43362Interface.h" +#include "treasure-data-rest.h" #include "ble/BLE.h" #include "LEDService.h" +#define BUFF_SIZE 200 DigitalOut alivenessLED(LED1, 0); DigitalOut actuatedLED(LED2, 0); +ISM43362Interface net; +LEDService *ledServicePtr; const static char DEVICE_NAME[] = "LED"; static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; +static EventQueue eventQueue(/* event count */ 10 * EVENTS_EVENT_SIZE); +char payload_buff [BUFF_SIZE] = {0}; +int x = 0; +TreasureData_RESTAPI* payload = new TreasureData_RESTAPI(&net,"iot_test","payload", MBED_CONF_APP_API_KEY); -static EventQueue eventQueue(/* event count */ 10 * EVENTS_EVENT_SIZE); +void ConnectToWifi(){ + // Connect to Wifi + printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); + int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); + if (ret != 0) { + printf("\nConnection error: %d\n", ret); + } -LEDService *ledServicePtr; + printf("Success\n\n"); + printf("MAC: %s\n", net.get_mac_address()); + printf("IP: %s\n", net.get_ip_address()); + printf("Netmask: %s\n", net.get_netmask()); + printf("Gateway: %s\n", net.get_gateway()); + printf("RSSI: %d\n\n", net.get_rssi()); +} void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { @@ -49,6 +72,13 @@ void onDataWrittenCallback(const GattWriteCallbackParams *params) { if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) { actuatedLED = *(params->data); + // Construct strings to send + x = sprintf(payload_buff,"{\"payload\":\"%u\"}", *(params->data)); + payload_buff[x]=0; // null terminate the string + + // Send data to Treasure data + printf("\r\n Sending Payload: '%s'\r\n",payload_buff); + payload->sendData(payload_buff,strlen(payload_buff)); } } @@ -97,12 +127,15 @@ void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { BLE &ble = BLE::Instance(); eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); + } int main() { + ConnectToWifi(); + eventQueue.call_every(500, blinkCallback); - + BLE &ble = BLE::Instance(); ble.onEventsToProcess(scheduleBleEventsProcessing); ble.init(bleInitComplete);