Arduino ESP32 WiFi连接

基于Arduino IDE环境下的ESP WiFi连接的代码网上无一例外几乎都是使用WiFi.h实现,用这个实现的好像不能得到连接,断开这些事件,需要做一个等待来返回连接,好像是同步连接的,自己也尝试使用标准的ESP-IDF中的esp_wifi库写法,折腾了很久,不报错,一直不触发事件,今天终于搞出来了,仔细看,貌似和标准IDF下调用时一样的,前面我也这样写了,就是不行,现贴出来,做一个记录,便于下次使用能够找到,同时也为后面的ESP32下WiFi Mesh打一下基础,已验证连接,IP地址都正常通过事件获取。

#include <Arduino.h>
#include <string.h>
#include <stdlib.h>
#include <esp_wifi.h>
#include <esp_event.h>
#include <nvs_flash.h>

#ifndef MAC2STR
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
#endif

#define STATION_SSID "WiFi name"    //你的WiFi名称(这两个不能搞错)
#define STATION_PASSWORD "password" //你的WiFi密码(这两个不能搞错)

static uint8_t s_retry_num = 0;  //WiFi重连计数器

void mac2str(uint8_t *mac, char *macStr) {
  // 将 MAC 地址转换为字符串
  sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

/* IP 和 WiFi事件回调 */
void ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
  if (event_base == WIFI_EVENT) {
    switch (event_id) {
      case WIFI_EVENT_STA_START:
        {  //WIFI开始运行
          if (esp_wifi_connect() == ESP_OK) {
            Serial.println("Success start connecting to WiFi\n");
          } else {
            Serial.println("Failed to start connecting to WiFi\n");
          }
        }
        break;
      case WIFI_EVENT_STA_CONNECTED:
        {  //已连接到路由器
          wifi_event_sta_connected_t *connect_event = (wifi_event_sta_connected_t *)event_data;
          Serial.printf("Connected to WiFi. SSID: %s, Channel: %d, BSSID: " MACSTR "\n", 
          connect_event->ssid, connect_event->channel, MAC2STR(connect_event->bssid));
        }
        break;
      case WIFI_EVENT_STA_DISCONNECTED:
        {  //断开路由器连接
          if (s_retry_num < 10) {
            esp_wifi_connect();
            s_retry_num++;
            Serial.printf("Retrying connection to WiFi %d times", s_retry_num);
          } else {
            Serial.printf("Connection to WiFi failed");
            s_retry_num = 0;
          }
        }
        break;
      default:
        {
          Serial.printf("Other WiFi events: %s, %d\n", event_base, event_id);
        }
    }
  } else if (event_base == IP_EVENT) {
    ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
    Serial.printf("<IP_EVENT_STA_GOT_IP>IP:" IPSTR, IP2STR(&event->ip_info.ip));
  }
}
void setup() {
  //设置打印串口波特率
  Serial.begin(115200);
  // 初始化非易失存储
  ESP_ERROR_CHECK(nvs_flash_init());
  /* TCP/IP初始化 */
  ESP_ERROR_CHECK(esp_netif_init());
  /*  事件初始化 */
  ESP_ERROR_CHECK(esp_event_loop_create_default());
  // 创建了一个默认的WiFi站网络接口(这是动态DHCP的关键)
  esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
  assert(sta_netif);
  // 初始化TCP/IP堆栈
  // tcpip_adapter_init();   //该函数在新版中已经被启用,通常使用上面的函数来代替
  /* wifi初始化 */
  wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
  ESP_ERROR_CHECK(esp_wifi_init(&config));
  //注册IP和WiFi事件
  ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL));
  ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL));
  //设置WiFi模式
  esp_wifi_set_mode(WIFI_MODE_STA);
  // 设置WiFi配置
  wifi_config_t wifi_config = {};
  wifi_config.sta.bssid_set = 0;
  wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
  strncpy((char *)wifi_config.sta.ssid, STATION_SSID, sizeof(wifi_config.sta.ssid) - 1);
  strncpy((char *)wifi_config.sta.password, STATION_PASSWORD, sizeof(wifi_config.sta.password) - 1);
  esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
  //WiFi开始
  ESP_ERROR_CHECK(esp_wifi_start());
}

void loop() {
  // put your main code here, to run repeatedly:
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值