Using BLE to control WIFI configuration as SSID and PW.

Dependencies:   BLE_API WIFI_API_32kRAM mbed nRF51822

Fork of NNN40_WiFi by Delta

BLE_WIFIControl enables user to setup Wifi connection via BLE link. Here is iPhone app that teaches you how to use this BLE_WIFIControl example. /media/uploads/Marcomissyou/ios_app_for_wifi_configure.pdf

Files at this revision

API Documentation at this revision

Comitter:
Marcomissyou
Date:
Mon May 18 07:13:04 2015 +0000
Parent:
6:9aad563fec38
Child:
8:d39bc94f139b
Commit message:
First commit

Changed in this revision

BLEControlWIFIService.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BLEControlWIFIService.h	Mon May 18 07:13:04 2015 +0000
@@ -0,0 +1,98 @@
+#ifndef __BLE_HEART_RATE_SERVICE_H__
+#define __BLE_HEART_RATE_SERVICE_H__
+
+#include "BLEDevice.h"
+#include "WIFIDevice.h"
+
+static const uint8_t UUID_WIFI_CONFIGURATION_SERVICE[] = { 0x44, 0x38, 0x70, 0x22, 0x40, 0x02, 0x11, 0xe4, 0x85, 0x14, 0x16, 0x42, 0x30, 0xd1, 0xdf, 0x67 };
+static const uint8_t UUID_CONNECTION_STATUS_CHAR[] = { 0x44, 0x38, 0x73, 0x7e, 0x40, 0x02, 0x11, 0xe4, 0x85, 0x14, 0x16, 0x42, 0x30, 0xd1, 0xdf, 0x67 };
+static const uint8_t UUID_SSID_INFO_CHAR[] = { 0x44, 0x38, 0x75, 0xd6, 0x40, 0x02, 0x11, 0xe4, 0x85, 0x14, 0x16, 0x42, 0x30, 0xd1, 0xdf, 0x67 };
+static const uint8_t UUID_NETWORK_SECURITY_PASSWORD_CHAR[] = { 0x44, 0x38, 0x78, 0x1a, 0x40, 0x02, 0x11, 0xe4, 0x85, 0x14, 0x16, 0x42, 0x30, 0xd1, 0xdf, 0x67 };
+
+static const uint8_t MAX_PAYLOAD_SIZE = 20;
+
+class BLEControlWIFIService {
+public:
+    bool is_config;
+    char *ssid;
+    uint8_t uint_ssid[20];
+    char *pw;
+    uint8_t uint_pw[20];
+
+public:
+
+    BLEControlWIFIService(BLEDevice &_ble, WIFIDevice &_wifi) :
+        ble(_ble),
+        wifi(_wifi),
+        is_config(false),
+        statusCharacteristic(UUID_CONNECTION_STATUS_CHAR, statusPayload, 1, MAX_PAYLOAD_SIZE,
+                                      GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
+        ssidCharacteristic (UUID_SSID_INFO_CHAR, ssidPayload, 1, MAX_PAYLOAD_SIZE,
+                                      GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
+        pwCharacteristic (UUID_NETWORK_SECURITY_PASSWORD_CHAR, statusPayload, 1, MAX_PAYLOAD_SIZE,
+                                      GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)
+    {
+        setupService();
+    }
+
+
+    virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
+            uint8_t rssi, i;
+            uint16_t bytesRead = params->len;         
+            if (params->charHandle == statusCharacteristic.getValueAttribute().getHandle()) {
+                memcpy(statusPayload, params->data, bytesRead);
+        
+                ble.updateCharacteristicValue(statusCharacteristic.getValueAttribute().getHandle(), statusPayload, bytesRead);
+            } else if (params->charHandle == ssidCharacteristic.getValueAttribute().getHandle()) {
+                rssi = ssidPayload[0];
+                for(i=1; i<bytesRead; i++)
+                    uint_ssid[i-1] = params->data[i];
+                    uint_ssid[bytesRead-1] = '\0';   
+                    ssid = (char*) uint_ssid; 
+                    memcpy(ssidPayload, params->data, bytesRead);
+
+                    ssidPayload[0] = rssi;      // Added by Tsungta, restore RSSI value
+                    ble.updateCharacteristicValue(ssidCharacteristic.getValueAttribute().getHandle(), ssidPayload, bytesRead);
+            } else if (params->charHandle == pwCharacteristic.getValueAttribute().getHandle()) {
+                for(i=1; i<bytesRead; i++)
+                    uint_pw[i-1] = params->data[i];
+                    uint_pw[bytesRead-1] = '\0';
+                    pw = (char*) uint_pw;
+                    memcpy(pwPayload, params->data, bytesRead);
+                    ble.updateCharacteristicValue(pwCharacteristic.getValueAttribute().getHandle(), pwPayload, bytesRead);
+                    wifi.setNetwork(ssid, pw, 0);
+                    is_config = true;
+            }
+    }
+
+
+private:
+    void setupService(void) {
+        static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
+        if (serviceAdded) {
+            return;
+        }
+
+        GattCharacteristic *charTable[] = {&statusCharacteristic, &ssidCharacteristic, &pwCharacteristic};
+        GattService         BLEWIFIService(UUID_WIFI_CONFIGURATION_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+
+        ble.addService(BLEWIFIService);
+        serviceAdded = true;
+
+        ble.onDataWritten(this, &BLEControlWIFIService::onDataWritten);
+    }
+
+
+private:
+    BLEDevice               &ble;
+    WIFIDevice              &wifi;
+    uint8_t statusPayload[MAX_PAYLOAD_SIZE];
+    uint8_t ssidPayload[MAX_PAYLOAD_SIZE];
+    uint8_t pwPayload[MAX_PAYLOAD_SIZE];
+    
+    GattCharacteristic                      statusCharacteristic;
+    GattCharacteristic                      ssidCharacteristic;
+    GattCharacteristic                      pwCharacteristic;
+};
+
+#endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/
\ No newline at end of file
--- a/main.cpp	Tue Mar 31 08:20:24 2015 +0000
+++ b/main.cpp	Mon May 18 07:13:04 2015 +0000
@@ -1,32 +1,70 @@
 #include "mbed.h"
+#include "BLEDevice.h"
+#include "BLEControlWIFIService.h"
+#include "DeviceInformationService.h"
 #include "WIFIDevice.h"
 #include "EthernetInterface.h"
+ 
 
+#define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0
+ 
+BLEDevice  ble;
 WIFIDevice wifi;
+EthernetInterface eth;
+ 
+Ticker tickerSensorPolling; 
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut RFSWIO(p19);
+
+const static char     DEVICE_NAME[]        = "BLE WCS";
+static volatile bool  triggerSensorPolling = false;
+ 
+void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
+{
+    ble.startAdvertising(); // restart advertising
+}
 
 const char* PC_SERVER_ADDRESS = "192.168.1.234";
 uint16_t PC_PORT = 5222;
-DigitalOut LED01(LED1);
-DigitalOut LED02(LED2);
-
+ 
 int main(void)
-{   
-    EthernetInterface eth;
+{
     eth.init(); //Use DHCP
-    wait(1);
-    // set given SSID and PW as the highest priority  
-    wifi.setNetwork("HTCSV", "11111111", 0);
-    wait(1);
-    LED01 = 1;
-    eth.connect(40000);
-
-    LED02 = 1;
-    TCPSocketConnection socket;     
-    socket.connect(PC_SERVER_ADDRESS,PC_PORT); 
-    char msg[] = "Hello World";
-    socket.send(msg, sizeof(msg));
-
-    socket.close();
-    eth.disconnect();
-    wifi.sleep();
-}
+    RFSWIO = 0;
+    ble.init();
+    ble.onDisconnection(disconnectionCallback);
+ 
+    /* Setup primary service. */
+    BLEControlWIFIService BLEWIFIService(ble, wifi);
+ 
+    /* Setup auxiliary service. */
+    DeviceInformationService deviceInfo(ble, "ARM", "Cyntec", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+ 
+    /* Setup advertising. */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UUID_WIFI_CONFIGURATION_SERVICE, sizeof(UUID_WIFI_CONFIGURATION_SERVICE));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.setAdvertisingInterval(1000);
+    ble.startAdvertising();
+ 
+    // infinite loop
+    while (1) {
+        if (ble.getGapState().connected && BLEWIFIService.is_config) {
+            BLEWIFIService.is_config = false;
+            eth.connect(40000);
+            TCPSocketConnection sock;
+            sock.connect(PC_SERVER_ADDRESS,PC_PORT);
+            led1 = 1;
+                if(sock.is_connected()){
+                    led2 = 1;   
+                    char msg[] = "Hello World";
+                    sock.send(msg, sizeof(msg));
+                }
+            
+        } else {
+            ble.waitForEvent(); // low power wait for event
+        }
+    }
+}
\ No newline at end of file