ESP32连接校园网/企业WIFI WPA2-Enterprise方法

Introduction

本示例为 使用ESP32连接校园网-WPA2-Enterprise的代码。
由于学校主要是使用企业WIFI,需要SSID、ID、PW,常规ESP32连接网络的方法不可用,因此参考资料后整理,以便在学校或企业wifi下的ESP32使用,为各位在校大学生开发适合于学校场景的物联网项目提供小小帮助。

适用范围

在这里插入图片描述
在这里插入图片描述

Code

/*|----------------------------------------------------------|*/
/*|WORKING EXAMPLE FOR ESP32 WIFI ENTERPRISE CONNECTION      |*/
/*|TESTED BOARDS: ESP32                                      |*/
/*|CORE: Jan. 2024                                           |*/
/*|----------------------------------------------------------|*/
#include <WiFi.h>
#include <HTTPClient.h>
#if __has_include("esp_eap_client.h")
#include "esp_eap_client.h"
#else
#include "esp_wpa2.h"
#endif
#include <Wire.h>
#define EAP_IDENTITY "203980342"   //请输入学号 或类似的网络账号
#define EAP_PASSWORD "Wmimamima"  //请输入密码
const char *ssid = "PKU-Scholar"; // 网络SSID号,比如CMCC-PKU
int counter = 0;

void setup() {
  Serial.begin(115200);  
  delay(10);    
  Serial.println();
  Serial.print("Connecting to network: ");    
  Serial.println(ssid);  
  WiFi.disconnect(true);  //disconnect form wifi to set new wifi connection
  WiFi.mode(WIFI_STA); //init wifi mode
#if __has_include ("esp_eap_client.h")
  esp_eap_client_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
  esp_eap_client_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username
  esp_eap_client_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
  esp_wifi_sta_enterprise_enable();
#else
  esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
  esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same
  esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
  esp_wifi_sta_wpa2_ent_enable();
#endif
  WiFi.begin(ssid); //connect to wifi
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    counter++;
    if (counter >= 60) {  //after 30 seconds timeout - reset board
      ESP.restart();
    }
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address set: ");
  Serial.println(WiFi.localIP());  //print LAN IP
}
void loop() {
  if (WiFi.status() == WL_CONNECTED) {  //if we are connected to Eduroam network
    counter = 0;                        //reset counter
    Serial.println("Wifi is still connected with IP: ");
    Serial.println(WiFi.localIP());            //inform user about his IP address
  } else if (WiFi.status() != WL_CONNECTED) {  //if we lost connection, retry
    WiFi.begin(ssid);
  }
  while (WiFi.status() != WL_CONNECTED) {  //during lost connection, print dots
    delay(500);
    Serial.print(".");
    counter++;
    if (counter >= 60) {  //30 seconds timeout - reset board
      ESP.restart();
    }
  }
  Serial.print("Connecting to website: ");
  HTTPClient http;
  http.begin("http://ip-api.com/json/");  // 准备启用连接
  int httpCode = http.GET();              // 发起GET请求
  if (httpCode > 0)                       // 如果状态码大于0说明请求过程无异常
  {
    if (httpCode == HTTP_CODE_OK)  // 请求被服务器正常响应,等同于httpCode == 200
    {
      String payload = http.getString();  // 读取服务器返回的响应正文数据
                                          // 如果正文数据很多该方法会占用很大的内存
      Serial.println(payload);
    }
  } else {
    Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  }
  http.end();  // 结束当前连接
  delay(10000);
   
}

请注意,上方的代码只需要更改下面三行即可:

#define EAP_IDENTITY "203980342"   //请输入学号 或类似的网络账号
#define EAP_PASSWORD "Wmimamima"  //请输入密码
const char *ssid = "PKU-Scholar"; // 网络SSID号,比如CMCC-PKU

Reference

  1. https://github.com/espressif/arduino-esp32/blob/master/libraries/HTTPClient/examples/HTTPClientEnterprise/HTTPClientEnterprise.ino
  2. https://blog.csdn.net/lxw1844912514/article/details/131016363
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
ESP32C3, esp-idf 4.3.5,编译报错。FAILED: wifi_softAP.elf cmd.exe /C "cd . && D:\Espressif\tools\riscv32-esp-elf\esp-2021r2-patch3-8.4.0\riscv32-esp-elf\bin\riscv32-esp-elf-g++.exe -march=rv32imc -nostartfiles -march=rv32imc --specs=nosys.specs @CMakeFiles\wifi_softAP.elf.rsp -o wifi_softAP.elf && cd ." d:/espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/../lib/gcc/riscv32-esp-elf/8.4.0/../../../../riscv32-esp-elf/bin/ld.exe: esp-idf/main/libmain.a(softap_example_main.c.obj): in function `ch623_task': d:\espressif\frameworks\esp-idf-v4.3.5\examples\wifi\esp32_config_wifi\build/../main/softap_example_main.c:164: undefined reference to `CH623_Init' d:/espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/../lib/gcc/riscv32-esp-elf/8.4.0/../../../../riscv32-esp-elf/bin/ld.exe: d:\espressif\frameworks\esp-idf-v4.3.5\examples\wifi\esp32_config_wifi\build/../main/softap_example_main.c:166: undefined reference to `pcd_lpcd_start' d:/espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/../lib/gcc/riscv32-esp-elf/8.4.0/../../../../riscv32-esp-elf/bin/ld.exe: d:\espressif\frameworks\esp-idf-v4.3.5\examples\wifi\esp32_config_wifi\build/../main/softap_example_main.c:168: undefined reference to `pcd_lpcd_application' d:/espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/../lib/gcc/riscv32-esp-elf/8.4.0/../../../../riscv32-esp-elf/bin/ld.exe: d:\espressif\frameworks\esp-idf-v4.3.5\examples\wifi\esp32_config_wifi\build/../main/softap_example_main.c:171: undefined reference to `pcd_lpcd_start' collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed. ninja failed with exit code 1
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值