esp32 arduino wifi example

Esp32 Arduino WiFi Tutorial: Connect & Control with Examples

By Rachel King
AD

Are you looking to bring Wi-Fi capabilities to your Arduino projects? The ESP32, a low-cost, low-power system on a chip, is an excellent choice. It contains dual-mode Wi-Fi and Bluetooth, making it perfect for IoT applications. Today, we'll guide you through an ESP32 Arduino WiFi example, helping you establish a Wi-Fi connection and send data. Let's dive in!

First, ensure you have the ESP32 board and the Arduino IDE set up on your computer. You'll also need the ESP32 board package in the Arduino IDE. If you haven't installed it yet, go to `File > Preferences > Additional Boards Manager URLs` and add the following URL: `http://arduino.esp8266.com/stable/package_esp8266com_index.json`. Then, install the ESP32 board package.

ESP32 Board Initialization

Before we start, let's set up your ESP32 board in the Arduino IDE. Select your board in the `Tools > Board` menu. For this example, we'll use the "ESP32 Dev Module".

AD
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
ESP32/ESP8266 Thermostat Web Server - Control Output Based on Temperature | Random Nerd Tutorials
ESP32/ESP8266 Thermostat Web Server - Control Output Based on Temperature | Random Nerd Tutorials

Next, choose the right port by going to `Tools > Port`. Your ESP32 board should appear as a serial port. If it doesn't, reinstall the ESP32 board package and double-check your board and connection.

Wi-Fi Connection Setup

Now, let's connect to a Wi-Fi network. Create a new sketch and include the necessary libraries:

How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
```cpp #include ```

Replace these placeholders with your network credentials:

```cpp const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; ```

In the `setup()` function, initialize the Serial communication and connect to the Wi-Fi network:

```cpp void setup() { Serial.begin(115200); WiFi.begin(ssid, password); } ```

Add this within your `loop()` function to monitor the connection status:

```cpp void loop() { if (WiFi.status() != WL_CONNECTED) { delay(5000); Serial.println("Connecting to WiFi..."); WiFi.reconnect(); } else { Serial.println("Connected to WiFi"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } } ```

Upload the sketch to your ESP32 board. After uploading, open the Serial Monitor at 115200 baud rate. After a successful connection, you'll see the message "Connected to WiFi" and your board's IP address.

Sending Data via Wi-Fi

Now that your ESP32 is connected to Wi-Fi, let's send data. You can use the below example to transmit data over Wi-Fi:

Include the required libraries and define the server address and port:

```cpp #include #include const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* server = "192.168.0.100!"; int port = 8080; ```

Add this code to your `setup()` function to connect to the server:

```cpp WiFiClient client; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); } ```

Use the `loop()` function to send data. Modify the `data` string to your needs:

```cpp void loop() { if (WiFi.status() == WL_CONNECTED) { if (!client.connect(server, port)) { Serial.println("Connection to server failed!"); delay(5000); return; } String data = "Hello from ESP32! Data: "; data += String(micros()); client.println(data); Serial.println("Sent: " + data); delay(1000); } } ```

After uploading the sketch, open the Serial Monitor and look for your board's IP address and the message "Sent: Hello from ESP32! Data: ". This shows that your ESP32 is successfully sending data via Wi-Fi.

Conclusion and Next Steps

Congratulations! You've successfully connected your ESP32 to a Wi-Fi network and sent data. The ESP32 Arduino WiFi integration opens doors to numerous IoT project possibilities. Now that you have a solid foundation, consider exploring advanced topics like reception of data, HTTP requests, or using popular IoT protocols like MQTT. To start, why not try connecting two ESP32 boards and exchange data between them? Happy tinkering!

Anatomy esp32
Anatomy esp32
how to send sms from esp32 on the breadboard with an esp23
how to send sms from esp32 on the breadboard with an esp23
💡 ESP32 Web Server – Control an LED from Your Browser!
💡 ESP32 Web Server – Control an LED from Your Browser!
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
ESP-MESH with ESP32 and ESP8266: Getting Started | Random Nerd Tutorials
ESP-MESH with ESP32 and ESP8266: Getting Started | Random Nerd Tutorials
Custom Arduino Library for ESP32 📦⚙️
Custom Arduino Library for ESP32 📦⚙️
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32 Tutorials for Beginners
ESP32 Tutorials for Beginners
Esp32 Connect To Wifi
Esp32 Connect To Wifi
ESP32: BME680 Environmental Sensor using Arduino IDE | Random Nerd Tutorials
ESP32: BME680 Environmental Sensor using Arduino IDE | Random Nerd Tutorials
⚡ ESP32 OTA Update: Upload Firmware Over Wi-Fi!
⚡ ESP32 OTA Update: Upload Firmware Over Wi-Fi!
🌐 ESP32 + IFTTT = Smart Home Magic! ✨
🌐 ESP32 + IFTTT = Smart Home Magic! ✨
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
Introduction to the ESP32 - From Prototype to Production
Introduction to the ESP32 - From Prototype to Production
How to Use ESP32 to Control LED With Blynk Via WiFi
How to Use ESP32 to Control LED With Blynk Via WiFi
AD