CLI example for NNN50
Dependencies: NNN50_WIFI_API
Fork of NNN50_WiFi_HelloWorld by
Revision 5:ffe1a4c4881e, committed 2017-05-02
- Comitter:
- sog_yang
- Date:
- Tue May 02 08:23:15 2017 +0000
- Parent:
- 4:11b81280c65b
- Child:
- 6:1dac7bcca23d
- Commit message:
- AS7000 driver update
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AS7000.lib Tue May 02 08:23:15 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/sog_yang/code/BSH_AS7000/#0b966dfb3767
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BNO055.lib Tue May 02 08:23:15 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/sog_yang/code/BNO055/#dee5f9d9f36c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DOORService.h Tue May 02 08:23:15 2017 +0000
@@ -0,0 +1,42 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __BLE_DOOR_SERVICE_H__
+#define __BLE_DOOR_SERVICE_H__
+
+class DOORService {
+public:
+ const static uint16_t DOOR_SERVICE_UUID = 0xA000;
+ const static uint16_t DOOR_STATE_CHARACTERISTIC_UUID = 0xA001 ;
+
+ DOORService(BLEDevice &_ble, uint8_t initialValueForDOORCharacteristic):
+ ble(_ble), doorState(DOOR_STATE_CHARACTERISTIC_UUID, &initialValueForDOORCharacteristic)
+ {
+ GattCharacteristic *charTable[] = {&doorState};
+ GattService doorCTL_Service(DOOR_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+ ble.addService(doorCTL_Service);
+ }
+
+ GattAttribute::Handle_t getValueHandle() const {
+ return doorState.getValueHandle();
+ }
+
+private:
+ BLEDevice &ble;
+ ReadWriteGattCharacteristic<uint8_t> doorState;
+};
+
+#endif /* #ifndef __BLE_LED_SERVICE_H__ */
--- a/main.cpp Mon Apr 17 14:19:36 2017 +0000
+++ b/main.cpp Tue May 02 08:23:15 2017 +0000
@@ -1,79 +1,251 @@
-/******************** (C) COPYRIGHT 2016 Delta Electronics, Inc. ***************
-*
-* File Name : main.cpp
-* Authors : Tsungta Wu - CPBG (tsungta.wu@deltaww.com)
-* Version : V.1.0.0
-* Date : 2016/Nov/24
-*
-* This example only show the most basic WiFi operation include AP scan and connect
-* The usage of TCP/UDP socket please refer to the mbed Handbook from the link below
-* https://developer.mbed.org/handbook/Socket
-*
-*******************************************************************************/
-
-#include "mbed.h"
-#include "EthernetInterface.h"
-#include "WIFIDevice.h"
-
-const char* ECHO_SERVER_ADDRESS = "192.168.2.13";
-const int ECHO_SERVER_PORT = 1030;
-
-void scanCallback(tstrM2mWifiscanResult result)
-{
- printf("SSID: %s \n", result.au8SSID);
- printf("RSSI: %i \n", result.s8rssi);
-}
-
-int main() {
-
- EthernetInterface eth;
- WIFIDevice wifi;
-
- eth.init();
-
- wifi.apScan(scanCallback);
-
- wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "TP-LINK_2.4G_TTWU", "0972753720");
-
- eth.connect();
-
- if(wifi.is_AP_connected())
- printf("Connect Success! \n");
- else
- printf("Connect Fail! \n");
-
- printf("MAC: %s\n", eth.getMACAddress());
- printf("IP: %s\n", eth.getIPAddress());
- printf("Gateway: %s\n", eth.getGateway());
- printf("NetworkMask: %s\n", eth.getNetworkMask());
-
- UDPSocket sock;
- sock.init();
-
- Endpoint echo_server;
- echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
-
- char out_buffer[] = "Hello World";
- printf("Sending message '%s' to server (%s)\n",out_buffer,ECHO_SERVER_ADDRESS);
- sock.sendTo(echo_server, out_buffer, sizeof(out_buffer));
-
- char in_buffer[256]; //IMPORTANT, array size MUST >= the actual received data size or set to maximum of 1400 if there is uncertainty
- int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
-
- if(n <0)
- in_buffer[0] = '\0';//IMPORTANT, in case n = -1 when set_bloacking is timeout, prevent the illegal array in_buffer[-1] access
- else
- in_buffer[n] = '\0';
-
- printf("Received message from server: '%s'\n", in_buffer);
-
- sock.close();
-
- eth.disconnect();
-
- wifi.sleep();
-
- while(1) {
- }
-}
-
+#include "mbed.h"
+#include "rtos.h"
+#include <events/mbed_events.h>
+
+#include "ble/BLE.h"
+#include "ble/Gap.h"
+#include "ble/services/BatteryService.h"
+#include "DOORService.h"
+#include "BNO055.h"
+#include "AS7000.h"
+#include "EthernetInterface.h"
+#include "WIFIDevice.h"
+
+BNO055 imu(p0,p30);
+AS7000 hrm(p0,p30);
+Serial pc(USBTX, USBRX);
+
+Semaphore two_slots(2);
+//static Thread t1;
+static Thread t2;
+static Ticker ticker;
+
+const static char DEVICE_NAME[] = "DOORCTL";
+static uint8_t ADV_manuf[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+static char out_buffer_tt[] = "hrm:120 yaw:250.2 pitch:359.1 roll:180.2\n";
+static uint8_t BLE_RX_CMD = 0xFF;
+static uint8_t doorStatusPayload[2] = {0xFF,};
+union IP {
+ unsigned int ip;
+ struct {
+ unsigned char d;
+ unsigned char c;
+ unsigned char b;
+ unsigned char a;
+ } ip2;
+};
+
+char ips[20];
+IP ip;
+unsigned short a, b, c, d;
+BLEDevice ble;
+EthernetInterface eth;
+WIFIDevice wifi;
+TCPSocketConnection sock_tcp;
+//UDPSocket sock_udp;
+//Endpoint echo_server;
+
+static DOORService *doorServicePtr;
+static EventQueue eventQueue(
+ /* event count */ 16 * /* event size */ 32
+);
+static char* ECHO_SERVER_ADDRESS = "192.168.2.110";
+static int ECHO_SERVER_PORT = 1030;
+static bool triggerTempValueUpdate = false;
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+ BLE::Instance().gap().startAdvertising();
+}
+
+void onDataWrittenCallback(const GattWriteCallbackParams *params) {
+
+
+ if ((params->handle == doorServicePtr->getValueHandle()) && (params->len == 1)) {
+ {
+ BLE_RX_CMD = *(params->data);
+ }
+ }
+}
+
+void ble_thread() {
+ while (true) {
+ switch (BLE_RX_CMD) {
+ case 0x00:
+ if ( ADV_manuf[5]== 0xA1) {
+ eth.disconnect();
+ sock_tcp.close();
+ wifi.sleep();
+ }
+
+ BLE_RX_CMD = 0xFF;
+ doorStatusPayload[0]=0xF0;
+ ADV_manuf[5] = 0xF0;
+
+ eth.init();
+ wifi.setNetwork(M2M_WIFI_SEC_WPA_PSK, "20-4F-5_Guest", "11223344");
+ eth.connect();
+ /*
+ if(wifi.is_AP_connected())
+ printf("Connect Success! \n");
+ else
+ printf("Connect Fail! \n");
+
+ printf("MAC: %s\n", eth.getMACAddress());
+ printf("IP: %s\n", eth.getIPAddress());
+ printf("Gateway: %s\n", eth.getGateway());
+ printf("NetworkMask: %s\n", eth.getNetworkMask());
+ */
+ while (sock_tcp.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
+ printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
+ wait(1);
+ }
+ //printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS);
+
+ snprintf(ips, sizeof(ips), "%s",eth.getIPAddress());
+ unsigned short a, b, c, d;
+ sscanf(ips, "%hu.%hu.%hu.%hu", &a, &b, &c, &d);
+ sprintf(ips, "%x.%x.%x.%x", a, b, c, d);
+ ADV_manuf[0] = a;
+ ADV_manuf[1] = b;
+ ADV_manuf[2] = c;
+ ADV_manuf[3] = d;
+ break;
+
+ case 0x02:
+ if ( ADV_manuf[5]== 0xA1) {
+ eth.disconnect();
+ sock_tcp.close();
+ wifi.sleep();
+ }
+ BLE_RX_CMD = 0xFF;
+
+ ADV_manuf[0] = 0x00;
+ ADV_manuf[1] = 0x00;
+ ADV_manuf[2] = 0x00;
+ ADV_manuf[3] = 0x00;
+
+ ADV_manuf[5] = 0xF2;
+ doorStatusPayload[0]=0xF2;
+
+ break;
+
+ case 0xA1:
+ //BLE_RX_CMD = 0xFF;
+ sprintf(out_buffer_tt,"hrm:%03d yaw:%6.2f pitch:%6.2f roll:%6.2f\n", hrm.hrm.hreat_rate, imu.euler.yaw, imu.euler.pitch, imu.euler.roll);
+ sock_tcp.send_all(out_buffer_tt, sizeof(out_buffer_tt) - 1);
+ ADV_manuf[5] = 0xA1;
+ doorStatusPayload[0]=0xA1;
+ break;
+ }
+
+ if (ble.getGapState().connected) {
+ ble.updateCharacteristicValue((doorServicePtr->getValueHandle()),doorStatusPayload, 1);
+ }
+ }
+}
+
+
+
+/**
+ * This function is called when the ble initialization process has failled
+ */
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+ /* Initialization error handling should go here */
+}
+
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+ BLE& ble = params->ble;
+ ble_error_t error = params->error;
+
+ if (error != BLE_ERROR_NONE) {
+ /* In case of error, forward the error handling to onBleInitError */
+ onBleInitError(ble, error);
+ return;
+ }
+
+ /* Ensure that it is the default instance of BLE */
+ if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+ return;
+ }
+
+ ble.gap().onDisconnection(disconnectionCallback);
+ ble.gattServer().onDataWritten(onDataWrittenCallback);
+
+ /* Setup primary services */
+ uint8_t initialValueForDOORCharacteristic = 0xFF;
+ doorServicePtr = new DOORService(ble, initialValueForDOORCharacteristic);
+
+ /* Setup advertising */
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf));
+ ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+ ble.gap().setAdvertisingInterval(200); /* 1000ms */
+ ble.gap().startAdvertising();
+}
+
+void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
+ BLE &ble = BLE::Instance();
+ eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
+}
+
+void network_thread(void const *name) {
+ while (true) {
+ two_slots.wait();
+ //printf("%s\n\r", (const char*)name);
+ ble_thread();
+ /*
+ wait_ms(5);
+ imu.get_angles(); //query the i2c device
+ //printf("get angles\n");
+ wait_ms(5);
+ hrm.hr_only();
+ //printf("get hrm\n");
+*/
+ wait_ms(5);
+ two_slots.release();
+ }
+}
+
+void periodicCallback(void){
+ triggerTempValueUpdate = true;
+}
+
+int main (void) {
+ pc.baud(38400);
+ hrm.enable();
+ imu.reset();
+ imu.setmode(OPERATION_MODE_NDOF);
+
+
+ BLE &ble = BLE::Instance();
+ ble.onEventsToProcess(scheduleBleEventsProcessing);
+ ble.init(bleInitComplete);
+
+ // t1.start(callback(sensor_thread, (void *)"Sensor Thread"));
+ t2.start(callback(network_thread, (void *)"BLE-WIFI CTL Thread"));
+
+
+ ticker.attach(periodicCallback, 1); // blink LED every 1 second
+
+
+ while (true) {
+ if (triggerTempValueUpdate) {
+ /* Do blocking calls or whatever hardware-specific action is
+ * necessary to poll the sensor. */
+ BLE::Instance(BLE::DEFAULT_INSTANCE).gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, ADV_manuf, sizeof(ADV_manuf));
+ printf("*HR=%03d#", hrm.hrm.hreat_rate);
+ triggerTempValueUpdate = false;
+ }
+ hrm.hr_only();
+ wait_ms(15);
+ imu.get_angles(); //query the i2c device
+ wait_ms(15);
+ ble.waitForEvent();
+ }
+}
