首页 > 解决方案 > 用 C++ 在 ESP32 上连接 wifi

问题描述

我一直在尝试使用 C++ 将我的 ESP32 连接到 Wifi。我有一个小问题,我完全不明白。并最终将其连接到 AWS IOT。我可以毫无问题地使用 Arduino 本地语言完美运行它,但是当尝试使用 C++ 运行它时,我什至无法连接 wifi。

当我在运行 connectWifi() 函数(arduino 代码)时在循环中读回 localIP 时,它很容易读回 IP,并且我已经将它与其他代码完美地与 AWS IOT 一起使用。尝试运行 initialiseWifi() 函数 (C++) 时,它似乎不起作用,它返回的 IP 为 0.0.0.0。

我使用 Visual Studio PlatformIO 上的 C++ 从 ESP32 AWS IOT 教程中获得了这段代码,我也无法让教程正常工作。

我觉得这是一个简单的错误,我是 C++ 新手,所以希望有人能指出它,以便我可以修复它!

#include "secrets.h"
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <ArduinoJson.h>
#include "WiFi.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"
#include "lwip/inet.h"
#include "lwip/dns.h"

const int CONNECTED_BIT = BIT0;

WiFiClientSecure net = WiFiClientSecure();
MQTTClient client = MQTTClient(256);

void messageHandler(String &topic, String &payload) {
  Serial.println("incoming: " + topic + " - " + payload);
}

static esp_err_t event_handler(void *ctx, system_event_t *event)
    {
        switch(event->event_id) {
        case SYSTEM_EVENT_STA_START:
            esp_wifi_connect();
            break;
        case SYSTEM_EVENT_STA_GOT_IP:
           
            break;
        case SYSTEM_EVENT_STA_DISCONNECTED:
            /* This is a workaround as ESP32 WiFi libs don't currently
            auto-reassociate. */
            esp_wifi_connect();
           
            break;
        default:
            break;
        }
        return ESP_OK;
    }

void initialise_wifi(void)
    {
        tcpip_adapter_init();
        wifi_event_group = xEventGroupCreate();
        ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
        wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
        ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
        ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
        //Allocate storage for the struct
        wifi_config_t wifi_config = {};

        //Assign ssid & password strings
        strcpy((char*)wifi_config.sta.ssid, "XXXXXXXXXX");
        strcpy((char*)wifi_config.sta.password, "XXXXXXXXXXX");
        wifi_config.sta.bssid_set = false;
       // ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
        ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
        ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
        ESP_ERROR_CHECK( esp_wifi_start() );
    }

void connectWifi() {
        WiFi.mode(WIFI_STA);
        WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

        Serial.println("Connecting to Wi-Fi");

        while (WiFi.status() != WL_CONNECTED){
            delay(500);
            Serial.print(".");
        }

        printf("\n");
        printf("Hello world!\n");
        printf("Local IP: ");

        Serial.println(WiFi.localIP());
    }

void setup() {
  Serial.begin(9600);
  //connectWifi();
  initialise_wifi();
}

void loop() {
  Serial.println(WiFi.localIP());
  delay(1000);
}

标签: c++esp32

解决方案


推荐阅读