Last active
March 20, 2019 02:09
-
-
Save x43x61x69/93cd26d2d70191f144b06726990d62cc to your computer and use it in GitHub Desktop.
Dynamic Wi-Fi Connection via BLE for LinkIt 7697
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Dynamic Wi-Fi Connection via BLE. | |
Copyright (C) 2017 Zhi-Wei Cai. | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <LWiFi.h> | |
#include <LBLE.h> | |
#include <LBLEPeriphral.h> | |
bool _wasConnected; | |
String _ssidString; | |
String _passString; | |
LBLEService _periphralService("D709A00C-DA1A-4726-A33D-CF62B8F4C3D6"); | |
/* | |
* SSID & password must be sent as UTF-8 String and length | |
* must < 20 bytes due to BLE MTU limitation. | |
* | |
* If password length equals to 0, will connect to SSID as open. | |
*/ | |
LBLECharacteristicString _ssidRead("61DE21BC-6E02-4631-A0A7-1B6C7AF0DAEE", LBLE_WRITE); | |
LBLECharacteristicString _passRead("B882467F-77BC-4697-9A4A-4F3366BC6C35", LBLE_WRITE); | |
void connectWiFi(const String ssidString, const String passString) | |
{ | |
if (ssidString.length()) { | |
const int ssidLen = ssidString.length() + 1; | |
const int passLen = passString.length() + 1; | |
char ssidCString[ssidLen]; | |
char passCString[passLen]; | |
ssidString.toCharArray(ssidCString, ssidLen); | |
passString.toCharArray(passCString, passLen); | |
Serial.print("Connecting to: ["); | |
Serial.print(String(ssidCString)); | |
if (passString.length()) { | |
Serial.print("] ["); | |
Serial.print(String(passCString)); | |
} | |
Serial.println("]"); | |
if (passString.length()) { | |
WiFi.begin(ssidCString, passCString); | |
} else { | |
WiFi.begin(ssidCString); | |
} | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
LBLE.begin(); | |
while (!LBLE.ready()) { | |
delay(100); | |
} | |
_periphralService.addAttribute(_ssidRead); | |
_periphralService.addAttribute(_passRead); | |
LBLEPeripheral.addService(_periphralService); | |
LBLEPeripheral.begin(); | |
LBLEAdvertisementData _advertisement; | |
_advertisement.configAsConnectableDevice("LinkIt 7697"); | |
LBLEPeripheral.advertise(_advertisement); | |
Serial.println("BLE Ready!"); | |
} | |
void loop() | |
{ | |
delay(1000); | |
if (_ssidRead.isWritten()) { | |
WiFi.disconnect(); | |
_wasConnected = false; | |
_ssidString = _ssidRead.getValue(); | |
_passString = ""; | |
Serial.print("New SSID: "); | |
Serial.println(_ssidString); | |
} | |
if (_passRead.isWritten()) { | |
WiFi.disconnect(); | |
_wasConnected = false; | |
_passString = _passRead.getValue(); | |
Serial.print("New Password: "); | |
Serial.println(_passString); | |
connectWiFi(_ssidString, _passString); | |
} | |
if (_ssidString.length() && | |
WiFi.status() != WL_CONNECTED) { | |
Serial.print("Connecting Status: "); | |
Serial.println(WiFi.status()); | |
connectWiFi(_ssidString, _passString); | |
} else if (WiFi.status() == WL_CONNECTED && | |
!_wasConnected) { | |
_wasConnected = true; | |
Serial.print("Connected to: "); | |
Serial.println(_ssidString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment