receive message via BLE and send data to treasure data via postback API using wifi

Dependencies:   BSP_B-L475E-IOT01

Committer:
JunkoNakajima
Date:
Tue Nov 27 06:35:39 2018 +0000
Revision:
39:8990b0ce7233
Parent:
27:ff1fb7b5f9ee
first version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 2:864ddfb70a9c 1 /* mbed Microcontroller Library
mbed_official 2:864ddfb70a9c 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 2:864ddfb70a9c 3 *
mbed_official 2:864ddfb70a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 2:864ddfb70a9c 5 * you may not use this file except in compliance with the License.
mbed_official 2:864ddfb70a9c 6 * You may obtain a copy of the License at
mbed_official 2:864ddfb70a9c 7 *
mbed_official 2:864ddfb70a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 2:864ddfb70a9c 9 *
mbed_official 2:864ddfb70a9c 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 2:864ddfb70a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 2:864ddfb70a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 2:864ddfb70a9c 13 * See the License for the specific language governing permissions and
mbed_official 2:864ddfb70a9c 14 * limitations under the License.
mbed_official 2:864ddfb70a9c 15 */
mbed_official 2:864ddfb70a9c 16
mbed_official 11:7404978b24e7 17 #include <events/mbed_events.h>
mbed_official 2:864ddfb70a9c 18 #include <mbed.h>
JunkoNakajima 39:8990b0ce7233 19 #include "mbed.h"
JunkoNakajima 39:8990b0ce7233 20 #include "TCPSocket.h"
JunkoNakajima 39:8990b0ce7233 21 #include "wifi-ism43362/ISM43362Interface.h"
JunkoNakajima 39:8990b0ce7233 22 #include "treasure-data-rest.h"
mbed_official 2:864ddfb70a9c 23 #include "ble/BLE.h"
mbed_official 2:864ddfb70a9c 24 #include "LEDService.h"
JunkoNakajima 39:8990b0ce7233 25 #define BUFF_SIZE 200
mbed_official 2:864ddfb70a9c 26
mbed_official 2:864ddfb70a9c 27 DigitalOut alivenessLED(LED1, 0);
mbed_official 2:864ddfb70a9c 28 DigitalOut actuatedLED(LED2, 0);
JunkoNakajima 39:8990b0ce7233 29 ISM43362Interface net;
JunkoNakajima 39:8990b0ce7233 30 LEDService *ledServicePtr;
mbed_official 2:864ddfb70a9c 31
mbed_official 2:864ddfb70a9c 32 const static char DEVICE_NAME[] = "LED";
mbed_official 2:864ddfb70a9c 33 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
JunkoNakajima 39:8990b0ce7233 34 static EventQueue eventQueue(/* event count */ 10 * EVENTS_EVENT_SIZE);
JunkoNakajima 39:8990b0ce7233 35 char payload_buff [BUFF_SIZE] = {0};
JunkoNakajima 39:8990b0ce7233 36 int x = 0;
JunkoNakajima 39:8990b0ce7233 37 TreasureData_RESTAPI* payload = new TreasureData_RESTAPI(&net,"iot_test","payload", MBED_CONF_APP_API_KEY);
mbed_official 2:864ddfb70a9c 38
JunkoNakajima 39:8990b0ce7233 39 void ConnectToWifi(){
JunkoNakajima 39:8990b0ce7233 40 // Connect to Wifi
JunkoNakajima 39:8990b0ce7233 41 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
JunkoNakajima 39:8990b0ce7233 42 int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
JunkoNakajima 39:8990b0ce7233 43 if (ret != 0) {
JunkoNakajima 39:8990b0ce7233 44 printf("\nConnection error: %d\n", ret);
JunkoNakajima 39:8990b0ce7233 45 }
mbed_official 2:864ddfb70a9c 46
JunkoNakajima 39:8990b0ce7233 47 printf("Success\n\n");
JunkoNakajima 39:8990b0ce7233 48 printf("MAC: %s\n", net.get_mac_address());
JunkoNakajima 39:8990b0ce7233 49 printf("IP: %s\n", net.get_ip_address());
JunkoNakajima 39:8990b0ce7233 50 printf("Netmask: %s\n", net.get_netmask());
JunkoNakajima 39:8990b0ce7233 51 printf("Gateway: %s\n", net.get_gateway());
JunkoNakajima 39:8990b0ce7233 52 printf("RSSI: %d\n\n", net.get_rssi());
JunkoNakajima 39:8990b0ce7233 53 }
mbed_official 2:864ddfb70a9c 54
mbed_official 2:864ddfb70a9c 55 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
mbed_official 2:864ddfb70a9c 56 {
mbed_official 2:864ddfb70a9c 57 (void) params;
mbed_official 2:864ddfb70a9c 58 BLE::Instance().gap().startAdvertising();
mbed_official 2:864ddfb70a9c 59 }
mbed_official 2:864ddfb70a9c 60
mbed_official 2:864ddfb70a9c 61 void blinkCallback(void)
mbed_official 2:864ddfb70a9c 62 {
mbed_official 2:864ddfb70a9c 63 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
mbed_official 2:864ddfb70a9c 64 }
mbed_official 2:864ddfb70a9c 65
mbed_official 2:864ddfb70a9c 66 /**
mbed_official 2:864ddfb70a9c 67 * This callback allows the LEDService to receive updates to the ledState Characteristic.
mbed_official 2:864ddfb70a9c 68 *
mbed_official 2:864ddfb70a9c 69 * @param[in] params
mbed_official 2:864ddfb70a9c 70 * Information about the characterisitc being updated.
mbed_official 2:864ddfb70a9c 71 */
mbed_official 2:864ddfb70a9c 72 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
mbed_official 2:864ddfb70a9c 73 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
mbed_official 2:864ddfb70a9c 74 actuatedLED = *(params->data);
JunkoNakajima 39:8990b0ce7233 75 // Construct strings to send
JunkoNakajima 39:8990b0ce7233 76 x = sprintf(payload_buff,"{\"payload\":\"%u\"}", *(params->data));
JunkoNakajima 39:8990b0ce7233 77 payload_buff[x]=0; // null terminate the string
JunkoNakajima 39:8990b0ce7233 78
JunkoNakajima 39:8990b0ce7233 79 // Send data to Treasure data
JunkoNakajima 39:8990b0ce7233 80 printf("\r\n Sending Payload: '%s'\r\n",payload_buff);
JunkoNakajima 39:8990b0ce7233 81 payload->sendData(payload_buff,strlen(payload_buff));
mbed_official 2:864ddfb70a9c 82 }
mbed_official 2:864ddfb70a9c 83 }
mbed_official 2:864ddfb70a9c 84
mbed_official 2:864ddfb70a9c 85 /**
mbed_official 2:864ddfb70a9c 86 * This function is called when the ble initialization process has failled
mbed_official 2:864ddfb70a9c 87 */
mbed_official 2:864ddfb70a9c 88 void onBleInitError(BLE &ble, ble_error_t error)
mbed_official 2:864ddfb70a9c 89 {
mbed_official 2:864ddfb70a9c 90 /* Initialization error handling should go here */
mbed_official 2:864ddfb70a9c 91 }
mbed_official 2:864ddfb70a9c 92
mbed_official 2:864ddfb70a9c 93 /**
mbed_official 2:864ddfb70a9c 94 * Callback triggered when the ble initialization process has finished
mbed_official 2:864ddfb70a9c 95 */
mbed_official 2:864ddfb70a9c 96 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
mbed_official 2:864ddfb70a9c 97 {
mbed_official 2:864ddfb70a9c 98 BLE& ble = params->ble;
mbed_official 2:864ddfb70a9c 99 ble_error_t error = params->error;
mbed_official 2:864ddfb70a9c 100
mbed_official 2:864ddfb70a9c 101 if (error != BLE_ERROR_NONE) {
mbed_official 2:864ddfb70a9c 102 /* In case of error, forward the error handling to onBleInitError */
mbed_official 2:864ddfb70a9c 103 onBleInitError(ble, error);
mbed_official 2:864ddfb70a9c 104 return;
mbed_official 2:864ddfb70a9c 105 }
mbed_official 2:864ddfb70a9c 106
mbed_official 2:864ddfb70a9c 107 /* Ensure that it is the default instance of BLE */
mbed_official 2:864ddfb70a9c 108 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
mbed_official 2:864ddfb70a9c 109 return;
mbed_official 2:864ddfb70a9c 110 }
mbed_official 2:864ddfb70a9c 111
mbed_official 2:864ddfb70a9c 112 ble.gap().onDisconnection(disconnectionCallback);
mbed_official 2:864ddfb70a9c 113 ble.gattServer().onDataWritten(onDataWrittenCallback);
mbed_official 2:864ddfb70a9c 114
mbed_official 2:864ddfb70a9c 115 bool initialValueForLEDCharacteristic = false;
mbed_official 2:864ddfb70a9c 116 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
mbed_official 2:864ddfb70a9c 117
mbed_official 2:864ddfb70a9c 118 /* setup advertising */
mbed_official 2:864ddfb70a9c 119 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
mbed_official 2:864ddfb70a9c 120 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
mbed_official 2:864ddfb70a9c 121 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
mbed_official 2:864ddfb70a9c 122 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
mbed_official 2:864ddfb70a9c 123 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
mbed_official 2:864ddfb70a9c 124 ble.gap().startAdvertising();
mbed_official 2:864ddfb70a9c 125 }
mbed_official 2:864ddfb70a9c 126
mbed_official 2:864ddfb70a9c 127 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
mbed_official 2:864ddfb70a9c 128 BLE &ble = BLE::Instance();
mbed_official 11:7404978b24e7 129 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
JunkoNakajima 39:8990b0ce7233 130
mbed_official 2:864ddfb70a9c 131 }
mbed_official 2:864ddfb70a9c 132
mbed_official 2:864ddfb70a9c 133 int main()
mbed_official 2:864ddfb70a9c 134 {
JunkoNakajima 39:8990b0ce7233 135 ConnectToWifi();
JunkoNakajima 39:8990b0ce7233 136
mbed_official 11:7404978b24e7 137 eventQueue.call_every(500, blinkCallback);
JunkoNakajima 39:8990b0ce7233 138
mbed_official 2:864ddfb70a9c 139 BLE &ble = BLE::Instance();
mbed_official 2:864ddfb70a9c 140 ble.onEventsToProcess(scheduleBleEventsProcessing);
mbed_official 2:864ddfb70a9c 141 ble.init(bleInitComplete);
mbed_official 2:864ddfb70a9c 142
mbed_official 11:7404978b24e7 143 eventQueue.dispatch_forever();
mbed_official 2:864ddfb70a9c 144
mbed_official 2:864ddfb70a9c 145 return 0;
mbed_official 2:864ddfb70a9c 146 }