daniel23
Published © LGPL

ESP32 WiFi sketch for "Access Point" or "Station Mode"

This "setup" sketch allows quickly change to "Access Point" or "Station Mode" with fixed IP or DHCP by only modifying a configuration file.

IntermediateProtip1 hour365
ESP32 WiFi sketch for "Access Point" or "Station Mode"

Things used in this project

Hardware components

Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

ESP32_WiFi

C Header File
Copy the three files to a directory named "Example_WiFi", compile with the Arduino IDE and burn to the ESP32.
// **********************************************************************************
// *  ESP32 WiFi sketch for "Access Point" or "Station Mode" with fixed IP or DHCP  *
// *                                                                                *
// *   This file must be used in conjunction with the "wifi.cfg" file containing    *
// *   your network settings and credentials.                                       *
// *                                                Daniel Engel    21/10/2023      *
// **********************************************************************************

#include <WiFi.h>
#include "wifi_cfg.h"  // this is the file where you wrote the WiFi credentials

void WiFi_setup() {
  String ssid, password;

#if defined(AP)  // ESP32 will work as an access point
  ssid = SSID_AP;
  password = PWD_AP;
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);
  WiFi.softAPConfig(AP_IP, AP_GW, AP_SUB, AP_DHCP);
  Serial.print("[+] AP Created with SSID: ");
  Serial.print(ssid);
  Serial.print("   IP: ");
  Serial.println(WiFi.softAPIP());

#elif defined(STATIC_IP)  // ESP32 will work as an access point
  ssid = SSID_STA;
  password = PWD_STA;
  WiFi.mode(WIFI_STA);
  WiFi.setHostname(hostname.c_str());  //define hostname
  IPAddress staticIP(STA_IP);
  IPAddress gateway(STA_GW);
  IPAddress subnet(STA_SUB);
  IPAddress primaryDNS(STA_DNS1);
  IPAddress secondaryDNS(STA_DNS2);
  if (WiFi.config(staticIP, gateway, subnet, primaryDNS, secondaryDNS) == false) {
    Serial.println("Configuration failed.");
  }
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.print("Static IP mode --- ");
  Serial.print("Connected to network with IP address: ");
  Serial.println(WiFi.localIP());

#else  // ESP32 will connect to your router in Station mode using DHCP
  ssid = SSID_STA;
  password = PWD_STA;
  WiFi.mode(WIFI_STA);
  WiFi.setHostname(hostname.c_str());  //define hostname
  WiFi.begin(ssid, password);
  Serial.println("Establishing connection to WiFi with SSID: " + String(ssid));
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.print("DHCP mode --- ");
  Serial.print("Connected to network with IP address: ");
  Serial.println(WiFi.localIP());
#endif
}

wifi_cfg

C Header File
/* ******************************************************************************** 
   *  Complete this file with the settings you want and your network credentials  *
   *                                                                              *
   *  Comment/uncomment "#define AP" and "#define STATIC_IP" to match your usage  *
   ******************************************************************************** */

/* Choice your hostname */
String hostname = "ESP32_WiFi_CFG";

/*___________________________________________________________*/
/*  Comment/uncomment to choice WiFi mode and IP parameters  */
/**/
#define AP         // Comment for Station mode, Uncomment: Access Point mode
#define STATIC_IP  // Comment for using DHCP in Station mode

//    ssid and password for the Access Point
// -> DO NOT use the SSID/Password of your router !!!
#define SSID_AP "My_ESP32"
#define PWD_AP "12345678"  //  empty string for open or at least 8 characters
// if there are not enough characters it doesn't work but no compiler warning !!!

// IP parameters for Access Point  (modify to meet your needs)
IPAddress AP_IP = IPAddress(192, 168, 22, 22);
IPAddress AP_GW = IPAddress(192, 168, 22, 22);
IPAddress AP_SUB = IPAddress(255, 255, 255, 0);
IPAddress AP_DHCP = IPAddress(192, 168, 22, 30);

// SSID and password of your router for "Station mode" (used for dhcp and fixed IP)
#define SSID_STA "MY_SSID"
#define PWD_STA "MY_PASSWORD"

// IP parameters in case of "Station mode with fixed IP"
IPAddress STA_IP(192, 168, 0, 121);
IPAddress STA_GW(192, 168, 0, 1);
IPAddress STA_SUB(255, 255, 255, 0);
IPAddress STA_DNS1(192, 168, 0, 1);
IPAddress STA_DNS2(192, 168, 0, 1);

Example_WiFi

Arduino
#include "ESP32_WiFi.h"

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi_setup();
}

void loop() {
  // put your main code here, to run repeatedly:
}

Example_WiFi.zip

Markdown
For your convenience this .zip file contains the three previous files. You just need to download this last one.
No preview (download only).

Credits

daniel23
9 projects • 12 followers
Contact

Comments

Please log in or sign up to comment.