Skip to content

Instantly share code, notes, and snippets.

@lonelybinary
Last active June 17, 2023 22:40
Show Gist options
  • Save lonelybinary/fe0cc653927cbb270198e0e8a43ff905 to your computer and use it in GitHub Desktop.
Save lonelybinary/fe0cc653927cbb270198e0e8a43ff905 to your computer and use it in GitHub Desktop.
Arduino ESP32 WIFI - STA
#include <WiFi.h>
//To switch to DHCP instead, simply modify to #define STATIC_IP 1
#define STATIC_IP 0
const char *ssid = "YOUR_WIFI_SSID";
const char *password = "YOUR_WIFI_PASSWORD";
#if defined(STATIC_IP) && STATIC_IP == 1
IPAddress local_IP(192, 168, 31, 115);
IPAddress gateway(192, 168, 31, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);
#endif
void setup()
{
Serial.begin(115200);
#if defined(STATIC_IP) && STATIC_IP == 1
WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS))
#endif
WiFi.begin(ssid, password);
// try to connect to WiFi network every 0.5 second until connected
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("ESP32's IP Address : ");
Serial.println(WiFi.localIP());
Serial.print("ESP32's Mac Adress : ");
Serial.println(WiFi.macAddress());
Serial.print("ESP32's Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("ESP32's Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("ESP32's DNS: ");
Serial.println(WiFi.dnsIP());
Serial.print("Wireless SSID : ");
Serial.println(WiFi.SSID());
Serial.print("Wireless AP's Mac Address : ");
Serial.println(WiFi.BSSIDstr());
Serial.print("Wireless Channel : ");
Serial.println(WiFi.channel());
Serial.print("Wireless Signal Strength : ");
Serial.println(WiFi.RSSI());
}
void loop()
{
/*
if the wifi is disconnected, try connect again
*/
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment