一 . ESP32-S简介
二 . 示例程序
#include <BluetoothSerial.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
static String data_app;
BluetoothSerial SerialBT;
bool init_wifi(){
int count = 0;WiFi.begin();while(WiFi.status()!=WL_CONNECTED){
Serial.print(".");delay(500);count++;if (count==20){
return false;}}return true;
}
void setup() {
Serial.begin(115200);WiFi.hostname("myESP32");SerialBT.begin("myESP32");Serial.println("现在可进行蓝牙配对!");if(init_wifi()){
Serial.println();Serial.println("wifi连接成功");}else{
Serial.println();Serial.println("wifi连接失败请使用蓝牙配网");}
}
void loop() {
bluetooth();
}
void bluetooth() {
if (Serial.available()) {
SerialBT.write(Serial.read());}if (SerialBT.available()){
data_app = "";data_app = SerialBT.readString();Serial.print("接收数据为:"); Serial.println(data_app); if (data_app=="ip_mac"){
String ip_mac = ipmac();SerialBT.println(ip_mac);Serial.println(ip_mac);}else if(data_app.indexOf("ssid")!=-1){
StaticJsonDocument<200> doc;DeserializationError error = deserializeJson(doc, data_app);if (error) {
Serial.println("数据格式错误"); SerialBT.print("Incorrect format of incoming data");}else {
String sid = doc["ssid"];String pwd = doc["password"];if (sid!="null"){
Serial.printf("SSID:%s\r\n",sid); if (pwd=="null"){
pwd = "";}Serial.printf("PAWD:%s\r\n",pwd); if(wifiPW(sid,pwd)){
SerialBT.println("True");}else{
SerialBT.println("False");}}else {
Serial.println("未识别到wifi数据"); SerialBT.print("Incorrect format of incoming data");}}}else{
Serial.println("未接收到指令"); SerialBT.print("Incorrect format of incoming data");}}
}
bool wifiPW(String sid,String pwd){
WiFi.begin(sid.c_str(), pwd.c_str());int count = 0;delay(1000);while (WiFi.status()!=WL_CONNECTED){
Serial.print(".");delay(500);count++;if (count>20){
Serial.println();Serial.println("配网失败");return false;}}Serial.println();Serial.println("配网成功");return true;
}
String ipmac(){
if(WiFi.isConnected()){
String ip = WiFi.localIP().toString();String mac = WiFi.macAddress();return "{'ip':'"+ip+"','mac':'"+mac+"'}";}else{
return "Not connected to wifi";}
}
三 . 示例程序说明
四 . 注意事项
- 需要用到的库:
#include <BluetoothSerial.h>
#include <ArduinoJson.h>
#include <WiFi.h>
- 此函数可以连接到上次连接成功的wifi
WiFi.begin();
- 以下函数分别设置设备名以及设备蓝牙名称
WiFi.hostname("myESP32");
SerialBT.begin("myESP32");