Example Code for Arduino-WiFi

Last revision 2026/02/05

This article offers sample Arduino code for setting up an ESP32 as a WiFi server to control LEDs remotely and synchronize time via network time server, including hardware requirements and step-by-step instructions.

1. WiFi Basic Tutorial

The ESP32 has WiFi capability. The following example uses ESP32 to create a WiFi server. The client can connect to the server and remotely control the on/off state of the LED.

Hardware Preparation

Sample Code

Program Function: Connect to the WiFi server created by FireBeetle 2 ESP32-E using a mobile phone, access 192.168.4.1, and remotely control the on/off state of the onboard LED.

/*WiFiAccessPoint.ino creates a wifi hotspot and provides a web service
  Steps:
  1. Connect to the wifi "yourAp"
  2. Access https://192.168.4.1/H to turn on the LED, or access https://192.168.4.1/L to turn off the LED*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

// Set your wifi name and password
const char *ssid = "esp32";
const char *password = "123456789";

WiFiServer server(80);

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); //Set the LED pin as output
  Serial.begin(115200);
  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  // Configure wifi and get IP address
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();
  Serial.println("Server started");
}

void loop() {
  WiFiClient client = server.available();   // Listen to the server

  if (client) {                             // If there is message from the server 
    Serial.println("New Client.");           // Print the message on the serial port
    String currentLine = "";                // Create a String to save the incoming data from the client
    while (client.connected()) {           
        char c = client.read();          
        Serial.write(c);                    
        if (c == '\n') {                  
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
            client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
            client.println();
            break;
          } else {   
            currentLine = "";
          }
        } else if (c != '\r') {  
          currentLine += c;     
        }
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(LED_BUILTIN, HIGH);               // GET /H turns on the LED
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(LED_BUILTIN, LOW);                // GET /L turns off the LED
      }
    }
    client.stop();     
    Serial.println("Client Disconnected.");
  }
}

Result

After connecting to the ESP32, accessing the given URL, and clicking turn on the light, the onboard LED light will turn on; clicking turn off the light will turn off the onboard LED light.

  1. Connect to ESP32
    DFR0654-WiFi Result 1

  2. 192.168.4.1 Website interface
    DFR0654-WiFi Result 2

2. Getting Network Time via WiFi

The ESP32 supports both STA and AP mode for WiFi connection.

  • STA mode: The ESP32 module connects to the Internet through a router, and remote control of the device is achieved through the Internet via a mobile phone or computer.
  • AP mode: The ESP32 module serves as a hotspot, allowing direct communication between a mobile phone or computer and the module, enabling wireless control within a local area network.
  • STA+AP mode: The coexistence mode of the two modes, which can realize seamless switching between Internet control and local area network control.

The following example code defaults to STA mode.

Hardware Preparation

Sample Code

Get and set time from Network Time Server.

#include <WiFi.h>

const char* ssid="WIFI_SSID";  //Fill in the WIFI name
const char* password="WIFI_PASSWORD";   //Fill in the WIFI password
const char* ntpServer = "pool.ntp.org";  //Get the time from the network time server
const long gmtOffset_sec = 28800;      //UTC time is used here, China is in the UTC+8 time zone, which is 8*60*60
const int daylightOffset_sec = 0;      //Use daylight saving time daylightOffset_sec = 3600, otherwise it is equal to 0

void printLocalTime(){    
 struct tm timeinfo;
 if(!getLocalTime(&timeinfo)){    //If the local time is obtained, put it into the timeinfo structure
   Serial.println("Failed to obtian time");
   return ;
 }
 Serial.println(&timeinfo,"%A, %B %d %Y %H:%M:%S");   //Output the obtained time in this format
}

void setup() {
Serial.begin(115200);
  Serial.printf("Connecting to %s",ssid);
  WiFi.begin(ssid,password);     //Connect to WIFI
  while(WiFi.status()!=WL_CONNECTED){      //Wait for the connection to be successful
    delay(500);
    Serial.print(".");
  }
  Serial.println(" CONNECTED");
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); /Configure network time as the system time of the ESP32-E mainboard
}

void loop() 
{
    printLocalTime();  //Print local time
    delay(1000);
}

Result

When the program is uploaded, you can see the obtained time as shown in the figure below.

DFR0654-WiFi Result 3

Was this article helpful?

TOP