Initial pico w port

This commit is contained in:
bissen21 committed 2026-11-01 07:52:19 -06:00
1 parent 50e59ce2a2
commit ead5d48ce0
17 files changed
+184 -7818

No files matched your search

+5 -3
View File
@@ -1,9 +1,11 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"Jason2866.esp-decoder",
"pioarduino.pioarduino-ide"
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
"ms-vscode.cpptools-extension-pack",
"pioarduino.pioarduino-ide"
]
}
-3
View File
@@ -1,3 +0,0 @@
cmake_minimum_required(VERSION 3.16.0)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(eInk_F1)
+16
View File
@@ -0,0 +1,16 @@
# E-Ink F1 Display
## Raspberry Pi Pico W (Arduino)
This project targets the Raspberry Pi Pico W using the Arduino-Pico core
through PlatformIO and the Waveshare 4.2-inch V2 display. The display GPIO
assignments are in `include/config.h`.
1. Copy `src/secrets.h.example` to `src/secrets.h` and set your Wi-Fi SSID and
password. The actual `src/secrets.h` is ignored by Git.
2. Build with `pio run -e pico_w`.
3. Upload with `pio run -e pico_w -t upload`.
4. Open the serial monitor with `pio device monitor -e pico_w`.
The display uses GPIO 8 (DC), GPIO 9 (CS), GPIO 10 (SCK), GPIO 11 (MOSI),
GPIO 12 (RST), and GPIO 13 (BUSY).
+8 -8
View File
@@ -1,12 +1,12 @@
//Pin Definitions
#define EPD_RST_PIN 4
#define EPD_BUSY_PIN 5
#define EPD_DC_PIN 2
#define EPD_CS_PIN 15
#define SPI_MOSI 13
#define SPI_CLK 14
// Pico W GPIO assignments for the Waveshare 4.2-inch V2 display.
#define EPD_RST_GPIO 12
#define EPD_BUSY_GPIO 13
#define EPD_DC_GPIO 8
#define EPD_CS_GPIO 9
#define EPD_MOSI_GPIO 11
#define EPD_CLK_GPIO 10
#define ESP_HOSTNAME "F1_Dashboard"
#define DEVICE_HOSTNAME "F1_Dashboard"
#define TIMEZONE "CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00"
// Display resolution
-32
View File
@@ -1,32 +0,0 @@
#ifndef wifi_h
#define wifi_h
/* WiFi station Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#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.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data);
void wifi_init_sta(void);
#endif
+47 -92
View File
@@ -29,37 +29,30 @@
******************************************************************************/
#include "config.h"
#include "DEV_Config.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <string.h>
#define SPI_PORT SPI2_HOST
#define SPI_PORT spi1
// /**
// * GPIO
// **/
// int EPD_RST_PIN;
// int EPD_DC_PIN;
// int EPD_CS_PIN;
// int EPD_BUSY_PIN;
// int EPD_CLK_PIN;
// int EPD_MOSI_PIN;
spi_device_handle_t spi_device;
/**
* GPIO
**/
int EPD_RST_PIN;
int EPD_DC_PIN;
int EPD_CS_PIN;
int EPD_BUSY_PIN;
int EPD_CLK_PIN;
int EPD_MOSI_PIN;
/**
* GPIO read and write
**/
void DEV_Digital_Write(UWORD Pin, UBYTE Value)
{
gpio_set_level(Pin, Value);
gpio_put(Pin, Value);
}
UBYTE DEV_Digital_Read(UWORD Pin)
{
return gpio_get_level(Pin);
return gpio_get(Pin);
}
/**
@@ -67,23 +60,12 @@ UBYTE DEV_Digital_Read(UWORD Pin)
**/
void DEV_SPI_WriteByte(uint8_t Value)
{
spi_transaction_t trans;
memset(&trans, 0, sizeof(spi_transaction_t));
trans.flags = SPI_TRANS_USE_TXDATA;
trans.length = 8;
trans.tx_data[0] = Value;
spi_device_transmit(spi_device, &trans);
spi_write_blocking(SPI_PORT, &Value, 1);
}
void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
{
if (Len == 0) return;
spi_transaction_t trans;
memset(&trans, 0, sizeof(spi_transaction_t));
trans.length = 8*Len;
trans.tx_buffer = pData;
spi_device_transmit(spi_device, &trans);
spi_write_blocking(SPI_PORT, pData, Len);
}
/**
@@ -91,20 +73,12 @@ void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
**/
void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
{
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << Pin),
.intr_type = GPIO_INTR_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = GPIO_PULLUP_DISABLE
};
if(Mode == 0 || Mode == GPIO_MODE_INPUT) {
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_init(Pin);
if(Mode == 0 || Mode == GPIO_IN) {
gpio_set_dir(Pin, GPIO_IN);
} else {
io_conf.mode = GPIO_MODE_OUTPUT;
gpio_set_dir(Pin, GPIO_OUT);
}
gpio_config(&io_conf);
}
/**
@@ -112,19 +86,17 @@ void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
**/
void DEV_Delay_ms(UDOUBLE xms)
{
vTaskDelay(pdMS_TO_TICKS(xms));
sleep_ms(xms);
}
void DEV_GPIO_Init(void)
{
// EPD_RST_PIN = 12;
// EPD_DC_PIN = 8;
// EPD_BUSY_PIN = 13;
// EPD_CS_PIN = 9;
// EPD_CLK_PIN = 10;
// EPD_MOSI_PIN = 11;
EPD_RST_PIN = EPD_RST_GPIO;
EPD_DC_PIN = EPD_DC_GPIO;
EPD_BUSY_PIN = EPD_BUSY_GPIO;
EPD_CS_PIN = EPD_CS_GPIO;
EPD_CLK_PIN = EPD_CLK_GPIO;
EPD_MOSI_PIN = EPD_MOSI_GPIO;
DEV_GPIO_Mode(EPD_RST_PIN, 1);
DEV_GPIO_Mode(EPD_DC_PIN, 1);
@@ -143,26 +115,9 @@ UBYTE DEV_Module_Init(void)
// GPIO Config
DEV_GPIO_Init();
spi_bus_config_t buscfg = {
.mosi_io_num = SPI_MOSI,
.miso_io_num = -1,
.sclk_io_num = SPI_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4096
};
esp_err_t ret = spi_bus_initialize(SPI_PORT, &buscfg, 1);
ESP_ERROR_CHECK(ret);
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 4000 * 1000,
.mode = 0,
.queue_size = 7,
.spics_io_num = -1
};
// Attach the device configuration to the initialized bus
ret = spi_bus_add_device(SPI_PORT, &devcfg, &spi_device);
ESP_ERROR_CHECK(ret);
spi_init(SPI_PORT, 4000 * 1000);
gpio_set_function(EPD_CLK_PIN, GPIO_FUNC_SPI);
gpio_set_function(EPD_MOSI_PIN, GPIO_FUNC_SPI);
printf("DEV_Module_Init OK \r\n");
return 0;
@@ -170,55 +125,55 @@ UBYTE DEV_Module_Init(void)
void DEV_GPIO_Init_1(void)
{
//spi_deinit(SPI_PORT);
//gpio_set_function(EPD_CLK_PIN, GPIO_FUNC_SPI);
//gpio_set_function(EPD_MOSI_PIN, GPIO_FUNC_SPI);
spi_deinit(SPI_PORT);
gpio_set_function(EPD_CLK_PIN, GPIO_FUNC_SPI);
gpio_set_function(EPD_MOSI_PIN, GPIO_FUNC_SPI);
}
void DEV_SPI_Init(void)
{
//spi_init(SPI_PORT, 4000 * 1000);
//gpio_set_function(EPD_CLK_PIN, GPIO_FUNC_SPI);
//gpio_set_function(EPD_MOSI_PIN, GPIO_FUNC_SPI);
spi_init(SPI_PORT, 4000 * 1000);
gpio_set_function(EPD_CLK_PIN, GPIO_FUNC_SPI);
gpio_set_function(EPD_MOSI_PIN, GPIO_FUNC_SPI);
}
void DEV_SPI_SendData(UBYTE Reg)
{
UBYTE i,j=Reg;
DEV_GPIO_Mode(SPI_MOSI, 1);
DEV_GPIO_Mode(SPI_CLK, 1);
DEV_GPIO_Mode(EPD_MOSI_PIN, 1);
DEV_GPIO_Mode(EPD_CLK_PIN, 1);
DEV_Digital_Write(EPD_CS_PIN, 0);
for(i = 0; i<8; i++)
{
DEV_Digital_Write(SPI_CLK, 0);
DEV_Digital_Write(EPD_CLK_PIN, 0);
if (j & 0x80)
{
DEV_Digital_Write(SPI_MOSI, 1);
DEV_Digital_Write(EPD_MOSI_PIN, 1);
}
else
{
DEV_Digital_Write(SPI_MOSI, 0);
DEV_Digital_Write(EPD_MOSI_PIN, 0);
}
DEV_Digital_Write(SPI_CLK, 1);
DEV_Digital_Write(EPD_CLK_PIN, 1);
j = j << 1;
}
DEV_Digital_Write(SPI_CLK, 0);
DEV_Digital_Write(EPD_CLK_PIN, 0);
DEV_Digital_Write(EPD_CS_PIN, 1);
}
UBYTE DEV_SPI_ReadData(void)
{
UBYTE i,j=0xff;
DEV_GPIO_Mode(SPI_MOSI, 0);
DEV_GPIO_Mode(SPI_CLK, 1);
DEV_GPIO_Mode(EPD_MOSI_PIN, 0);
DEV_GPIO_Mode(EPD_CLK_PIN, 1);
DEV_Digital_Write(EPD_CS_PIN, 0);
for(i = 0; i<8; i++)
{
DEV_Digital_Write(SPI_CLK, 0);
DEV_Digital_Write(EPD_CLK_PIN, 0);
j = j << 1;
if (DEV_Digital_Read(SPI_MOSI))
if (DEV_Digital_Read(EPD_MOSI_PIN))
{
j = j | 0x01;
}
@@ -226,9 +181,9 @@ UBYTE DEV_SPI_ReadData(void)
{
j= j & 0xfe;
}
DEV_Digital_Write(SPI_CLK, 1);
DEV_Digital_Write(EPD_CLK_PIN, 1);
}
DEV_Digital_Write(SPI_CLK, 0);
DEV_Digital_Write(EPD_CLK_PIN, 0);
DEV_Digital_Write(EPD_CS_PIN, 1);
return j;
}
+8 -8
View File
@@ -48,8 +48,8 @@
#ifndef _DEV_CONFIG_H_
#define _DEV_CONFIG_H_
//#include "pico/stdlib.h"
//#include "hardware/spi.h"
#include "pico/stdlib.h"
#include "hardware/spi.h"
#include "stdio.h"
/**
@@ -62,12 +62,12 @@
/**
* GPIOI config
**/
// extern int EPD_RST_PIN;
// extern int EPD_DC_PIN;
// extern int EPD_CS_PIN;
// extern int EPD_BUSY_PIN;
// extern int EPD_CLK_PIN;
// extern int EPD_MOSI_PIN;
extern int EPD_RST_PIN;
extern int EPD_DC_PIN;
extern int EPD_CS_PIN;
extern int EPD_BUSY_PIN;
extern int EPD_CLK_PIN;
extern int EPD_MOSI_PIN;
/*------------------------------------------------------------------------------------------------------*/
void DEV_Digital_Write(UWORD Pin, UBYTE Value);
+20 -22
View File
@@ -29,10 +29,7 @@
#
******************************************************************************/
#include "EPD_4in2_V2.h"
#include "DEV_Config.h"
#include "Debug.h"
#include "config.h"
#include <string.h>
const unsigned char LUT_ALL[233]={
0x01, 0x0A, 0x1B, 0x0F, 0x03, 0x01, 0x01,
@@ -111,26 +108,17 @@ static void EPD_4IN2_V2_SendData(UBYTE Data)
DEV_Digital_Write(EPD_CS_PIN, 1);
}
static void EPD_4IN2_V2_SendDataBuffer(UBYTE *Data, UWORD Length)
{
DEV_Digital_Write(EPD_DC_PIN, 1);
DEV_Digital_Write(EPD_CS_PIN, 0);
DEV_SPI_Write_nByte(Data, Length);
DEV_Digital_Write(EPD_CS_PIN, 1);
}
/******************************************************************************
function : Wait until the busy_pin goes LOW
parameter:
******************************************************************************/
void EPD_4IN2_V2_ReadBusy(void)
{
printf("Waiting for e-Paper BUSY (GPIO%d=%d)...\r\n",
EPD_BUSY_PIN, DEV_Digital_Read(EPD_BUSY_PIN));
Debug("e-Paper busy\r\n");
while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
DEV_Delay_ms(10);
}
printf("e-Paper BUSY released.\r\n");
Debug("e-Paper busy release\r\n");
}
/******************************************************************************
@@ -342,19 +330,21 @@ parameter:
void EPD_4IN2_V2_Clear(void)
{
UWORD Width, Height;
UBYTE white_row[EPD_4IN2_V2_WIDTH / 8];
Width = (EPD_4IN2_V2_WIDTH % 8 == 0)? (EPD_4IN2_V2_WIDTH / 8 ): (EPD_4IN2_V2_WIDTH / 8 + 1);
Height = EPD_4IN2_V2_HEIGHT;
memset(white_row, 0xFF, sizeof(white_row));
EPD_4IN2_V2_SendCommand(0x24);
for (UWORD j = 0; j < Height; j++) {
EPD_4IN2_V2_SendDataBuffer(white_row, Width);
for (UWORD i = 0; i < Width; i++) {
EPD_4IN2_V2_SendData(0xFF);
}
}
EPD_4IN2_V2_SendCommand(0x26);
for (UWORD j = 0; j < Height; j++) {
EPD_4IN2_V2_SendDataBuffer(white_row, Width);
for (UWORD i = 0; i < Width; i++) {
EPD_4IN2_V2_SendData(0xFF);
}
}
EPD_4IN2_V2_TurnOnDisplay();
}
@@ -371,12 +361,16 @@ void EPD_4IN2_V2_Display(UBYTE *Image)
EPD_4IN2_V2_SendCommand(0x24);
for (UWORD j = 0; j < Height; j++) {
EPD_4IN2_V2_SendDataBuffer(&Image[j * Width], Width);
for (UWORD i = 0; i < Width; i++) {
EPD_4IN2_V2_SendData(Image[i + j * Width]);
}
}
EPD_4IN2_V2_SendCommand(0x26);
for (UWORD j = 0; j < Height; j++) {
EPD_4IN2_V2_SendDataBuffer(&Image[j * Width], Width);
for (UWORD i = 0; i < Width; i++) {
EPD_4IN2_V2_SendData(Image[i + j * Width]);
}
}
EPD_4IN2_V2_TurnOnDisplay();
}
@@ -393,12 +387,16 @@ void EPD_4IN2_V2_Display_Fast(UBYTE *Image)
EPD_4IN2_V2_SendCommand(0x24);
for (UWORD j = 0; j < Height; j++) {
EPD_4IN2_V2_SendDataBuffer(&Image[j * Width], Width);
for (UWORD i = 0; i < Width; i++) {
EPD_4IN2_V2_SendData(Image[i + j * Width]);
}
}
EPD_4IN2_V2_SendCommand(0x26);
for (UWORD j = 0; j < Height; j++) {
EPD_4IN2_V2_SendDataBuffer(&Image[j * Width], Width);
for (UWORD i = 0; i < Width; i++) {
EPD_4IN2_V2_SendData(Image[i + j * Width]);
}
}
EPD_4IN2_V2_TurnOnDisplay_Fast();
}
+1 -5
View File
@@ -31,11 +31,7 @@
#ifndef _EPD_4IN2_V2_H_
#define _EPD_4IN2_V2_H_
#include <stdint.h>
#define UBYTE uint8_t
#define UWORD uint16_t
#define UDOUBLE uint32_t
#include "DEV_Config.h"
// Display resolution
#define EPD_4IN2_V2_WIDTH 400
+5 -4
View File
@@ -8,9 +8,10 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
[env:pico_w]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = rpipicow
framework = arduino
monitor_speed = 115200
build_flags = -I include
build_src_filter = +<main.c> +<pico_arduino.cpp>
-3734
View File
File diff suppressed because it is too large. Load diff
-3720
View File
File diff suppressed because it is too large. Load diff
-6
View File
@@ -1,6 +0,0 @@
# This file was automatically generated for projects
# without default 'CMakeLists.txt' file.
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
idf_component_register(SRCS ${app_sources})
+5 -62
View File
@@ -1,52 +1,12 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "wifi.h"
#include "esp_sntp.h"
#include "esp_netif_sntp.h"
#include "driver/gpio.h"
#include "config.h"
#include <stdlib.h>
#include "DEV_Config.h"
#include "EPD_4in2_V2.h"
#include "GUI_Paint.h"
#include "ImageData.h"
#undef LOG_LOCAL_LEVEL
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include "esp_log.h"
static const char* TAG = "Main";
void print_task(void *pvParameters) {
while (1) {
ESP_LOGI(TAG, "here");
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
void sync_time() {
setenv("TZ", TIMEZONE, 1);
tzset();
ESP_LOGI(TAG, "Syncing time with NTP server...");
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
esp_netif_sntp_init(&config);
if (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(10000)) != ESP_OK) {
ESP_LOGE(TAG, "Failed to update system time within 10s timeout");
}
struct tm timeinfo;
time_t now;
time(&now);
if (localtime_r(&now, &timeinfo) != NULL) {
ESP_LOGI(TAG, "Time synchronized: %s", asctime(&timeinfo));
} else {
ESP_LOGE(TAG, "Failed to obtain time");
}
}
void wifi_connect(void);
void sync_time(void);
int EPD_4in2_V2_test(void)
{
@@ -279,25 +239,8 @@ int EPD_4in2_V2_test(void)
return 0;
}
void app_main(void) {
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
gpio_set_direction(EPD_RST_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(EPD_BUSY_PIN, GPIO_MODE_INPUT);
xTaskCreate(print_task, "print_task", 4096, NULL, 1, NULL);
wifi_init_sta();
vTaskDelay(pdMS_TO_TICKS(1000)); // Wait for WiFi to connect and get IP
void app_setup(void) {
wifi_connect();
sync_time();
EPD_4in2_V2_test();
vTaskDelete(NULL);
}
+66
View File
@@ -0,0 +1,66 @@
#include <Arduino.h>
#include <WiFi.h>
#include <time.h>
#include "config.h"
#include "secrets.h"
extern "C" void app_setup(void);
extern "C" void wifi_connect(void) {
WiFi.setHostname(DEVICE_HOSTNAME);
Serial.println("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
for (int attempt = 0; attempt < WIFI_MAXIMUM_RETRY; ++attempt) {
const unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 10000) {
delay(250);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.print("Connected. IP address: ");
Serial.println(WiFi.localIP());
return;
}
if (attempt + 1 < WIFI_MAXIMUM_RETRY) {
Serial.println("WiFi connection failed; retrying...");
WiFi.disconnect();
delay(500);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
}
Serial.println("Failed to connect to WiFi.");
}
extern "C" void sync_time(void) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Skipping NTP synchronization because WiFi is disconnected.");
return;
}
NTP.begin("pool.ntp.org", "time.nist.gov");
const bool synchronized = NTP.waitSet(10000);
setenv("TZ", TIMEZONE, 1);
tzset();
struct tm timeinfo;
const time_t now = time(nullptr);
if (synchronized && localtime_r(&now, &timeinfo) != nullptr) {
Serial.print("Time synchronized: ");
Serial.println(asctime(&timeinfo));
} else {
Serial.println("Failed to synchronize time within 10 seconds.");
}
}
void setup() {
Serial.begin(115200);
app_setup();
}
void loop() {
delay(1000);
}
+3
View File
@@ -0,0 +1,3 @@
#define WIFI_SSID "your-wifi-ssid"
#define WIFI_PASSWORD "your-wifi-password"
#define WIFI_MAXIMUM_RETRY 3
-119
View File
@@ -1,119 +0,0 @@
#include "wifi.h"
#include "secrets.h"
#include "config.h"
/* WiFi station Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static const char *TAG = "wifi station";
static int s_retry_num = 0;
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void wifi_init_sta(void) {
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
#ifndef ESP_HOSTNAME
#error "ESP_HOSTNAME is not defined in config.h"
#endif
esp_err_t err = esp_netif_set_hostname(sta_netif, ESP_HOSTNAME);
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = ESP_WIFI_SSID,
.password = ESP_WIFI_PASS,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
},
};
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() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s.",
ESP_WIFI_SSID);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s.",
ESP_WIFI_SSID);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
/* The event will not be processed after unregister */
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupDelete(s_wifi_event_group);
}