Using BLE to control WIFI configuration as SSID and PW.

Dependencies:   BLE_API WIFI_API_32kRAM mbed nRF51822

Fork of NNN40_WiFi by Delta

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BLE.h"
00003 #include "BLEControlWIFIService.h"
00004 #include "DeviceInformationService.h"
00005 #include "WIFIDevice.h"
00006 #include "EthernetInterface.h"
00007  
00008 
00009 #define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0
00010  
00011 BLEDevice  ble;
00012 WIFIDevice wifi;
00013 EthernetInterface eth;
00014  
00015 Ticker tickerSensorPolling; 
00016 DigitalOut led1(LED1);
00017 DigitalOut led2(LED2);
00018 DigitalOut RFSWIO(p19);
00019 
00020 bool is_AP_connect = false;
00021 
00022 const static char     DEVICE_NAME[]        = "TSUNGTA_BLE WCS";
00023 static volatile bool  triggerSensorPolling = false;
00024  
00025 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00026 {
00027     ble.startAdvertising();
00028 }
00029 
00030 const char* PC_SERVER_ADDRESS = "172.20.10.2";
00031 uint16_t PC_PORT = 5222;
00032  
00033 int main(void)
00034 {
00035     TCPSocketConnection sock;
00036                 
00037     RFSWIO = 0;
00038     ble.init();
00039     ble.onDisconnection(disconnectionCallback);
00040  
00041     /* Setup primary service. */
00042     BLEControlWIFIService BLEWIFIService(ble, wifi);
00043  
00044     /* Setup auxiliary service. */
00045     DeviceInformationService deviceInfo(ble, "ARM", "Cyntec", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00046  
00047     /* Setup advertising. */
00048     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00049     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UUID_WIFI_CONFIGURATION_SERVICE, sizeof(UUID_WIFI_CONFIGURATION_SERVICE));
00050     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00051     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00052     ble.setAdvertisingInterval(1000);
00053     ble.startAdvertising();
00054  
00055     // infinite loop
00056     while (1) {
00057         if (ble.getGapState().connected && BLEWIFIService.is_config) {
00058             BLEWIFIService.is_config = false;
00059             //ble.shutdown();
00060             eth.init(); //Use DHCP
00061             RFSWIO = 1; //Switch RF to WiFi
00062             while (eth.connect()) {}
00063             is_AP_connect = true;
00064 
00065             
00066         } else {
00067             
00068           if (is_AP_connect) {
00069             if(!sock.is_connected()){ 
00070                 sock.connect(PC_SERVER_ADDRESS,PC_PORT);
00071                 led1 = 1;
00072             } else {
00073                 char msg[] = "Hello World";
00074                 sock.send(msg, sizeof(msg));
00075                 wait(1);
00076             }
00077           }
00078             ble.waitForEvent(); // low power wait for event
00079         }
00080     }
00081 }