Arduino wifi ip address

WiFi — WiFi.config()

WiFi.config() allows you to configure a static IP address as well as change the DNS, gateway, and subnet addresses on the WiFi shield.

Unlike WiFi.begin() which automatically configures the WiFi shield to use DHCP, WiFi.config() allows you to manually set the network address of the shield.

Calling WiFi.config() before WiFi.begin() forces begin() to configure the WiFi shield with the network addresses specified in config().

You can call WiFi.config() after WiFi.begin(), but the shield will initialize with begin() in the default DHCP mode. Once the config() method is called, it will change the network address as requested.

Syntax

WiFi.config(ip); WiFi.config(ip, dns); WiFi.config(ip, dns, gateway); WiFi.config(ip, dns, gateway, subnet); 

Parameters

ip: the IP address of the device (array of 4 bytes)

dns: the address for a DNS server.

gateway: the IP address of the network gateway (array of 4 bytes). optional: defaults to the device IP address with the last octet set to 1

subnet: the subnet mask of the network (array of 4 bytes). optional: defaults to 255.255.255.0

Returns

Example

This example shows how to set the static IP address, 192.168.0.177, of the LAN network to the WiFi shield: #include #include // the IP address for the shield: IPAddress ip(192, 168, 0, 177); char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; void setup() < // Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) < ; // wait for serial port to connect. Needed for Leonardo only >// check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) < Serial.println("WiFi shield not present"); while(true); // don't continue >WiFi.config(ip); // attempt to connect to Wifi network: while ( status != WL_CONNECTED) < Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); >// print your WiFi shield's IP address: Serial.print("IP Address: "); Serial.println(WiFi.localIP()); > void loop () <> 

Источник

WiFi — WiFi.config()

WiFi.config() allows you to configure a static IP address as well as change the DNS, gateway, and subnet addresses on the WiFi shield.

Unlike WiFi.begin() which automatically configures the WiFi shield to use DHCP, WiFi.config() allows you to manually set the network address of the shield.

Calling WiFi.config() before WiFi.begin() forces begin() to configure the WiFi shield with the network addresses specified in config().

You can call WiFi.config() after WiFi.begin(), but the shield will initialize with begin() in the default DHCP mode. Once the config() method is called, it will change the network address as requested.

Syntax

WiFi.config(ip); WiFi.config(ip, dns); WiFi.config(ip, dns, gateway); WiFi.config(ip, dns, gateway, subnet); 

Parameters

ip: the IP address of the device (array of 4 bytes)

dns: the address for a DNS server.

gateway: the IP address of the network gateway (array of 4 bytes). optional: defaults to the device IP address with the last octet set to 1

Читайте также:  Общая папка через wifi windows 10

subnet: the subnet mask of the network (array of 4 bytes). optional: defaults to 255.255.255.0

Returns

Example

This example shows how to set the static IP address, 192.168.0.177, of the LAN network to the WiFi shield: #include #include // the IP address for the shield: IPAddress ip(192, 168, 0, 177); char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; void setup() < // Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) < ; // wait for serial port to connect. Needed for Leonardo only >// check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) < Serial.println("WiFi shield not present"); while(true); // don't continue >WiFi.config(ip); // attempt to connect to Wifi network: while ( status != WL_CONNECTED) < Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); >// print your WiFi shield's IP address: Serial.print("IP Address: "); Serial.println(WiFi.localIP()); > void loop () <> 

Источник

ESP32 Static/Fixed IP Address

This tutorial shows how to set a static/fixed IP address for your ESP32 board. If you’re running a web server or Wi-Fi client with your ESP32 and every time you restart your board, it has a new IP address, you can follow this tutorial to assign a static/fixed IP address.

ESP32-static-IP-address

Static/Fixed IP Address Sketch

To show you how to fix your ESP32 IP address, we’ll use the ESP32 Web Sever code as an example. By the end of our explanation you should be able to fix your IP address regardless of the web server or Wi-Fi project you’re building.

Copy the code below to your Arduino IDE, but don’t upload it yet. You need to make some changes to make it work for you.

Note: if you upload the next sketch to your ESP32 board, it should automatically assign the fixed IP address 192.168.1.184.

/********* Rui Santos Complete project details at https://randomnerdtutorials.com *********/ // Load Wi-Fi library #include // Replace with your network credentials const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Set web server port number to 80 WiFiServer server(80); // Variable to store the HTTP request String header; // Auxiliar variables to store the current output state String output26State = "off"; String output27State = "off"; // Assign output variables to GPIO pins const int output26 = 26; const int output27 = 27; // Set your Static IP address IPAddress local_IP(192, 168, 1, 184); // Set your Gateway IP address IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); IPAddress primaryDNS(8, 8, 8, 8); //optional IPAddress secondaryDNS(8, 8, 4, 4); //optional void setup() < Serial.begin(115200); // Initialize the output variables as outputs pinMode(output26, OUTPUT); pinMode(output27, OUTPUT); // Set outputs to LOW digitalWrite(output26, LOW); digitalWrite(output27, LOW); // Configures static IP address if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) < Serial.println("STA Failed to configure"); >// Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) < delay(500); Serial.print("."); >// Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); > void loop()< WiFiClient client = server.available(); // Listen for incoming clients if (client) < // If a new client connects, Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) < // loop while the client's connected if (client.available()) < // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') < // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) < // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // turns the GPIOs on and off if (header.indexOf("GET /26/on") >= 0) < Serial.println("GPIO 26 on"); output26State = "on"; digitalWrite(output26, HIGH); >else if (header.indexOf("GET /26/off") >= 0) < Serial.println("GPIO 26 off"); output26State = "off"; digitalWrite(output26, LOW); >else if (header.indexOf("GET /27/on") >= 0) < Serial.println("GPIO 27 on"); output27State = "on"; digitalWrite(output27, HIGH); >else if (header.indexOf("GET /27/off") >= 0) < Serial.println("GPIO 27 off"); output27State = "off"; digitalWrite(output27, LOW); >// Display the HTML web page client.println(""); client.println(""); client.println(""); // CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferences client.println("html < font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;>"); client.println(".button < background-color: #4CAF50; border: none; color: white; padding: 16px 40px;"); client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;>"); client.println(".button2 "); // Web Page Heading client.println("

ESP32 Web Server

"); // Display current state, and ON/OFF buttons for GPIO 26 client.println("

GPIO 26 - State " + output26State + "

"); // If the output26State is off, it displays the ON button if (output26State=="off") < client.println("

"); > // Display current state, and ON/OFF buttons for GPIO 27 client.println("

GPIO 27 - State " + output27State + "

"); // If the output27State is off, it displays the ON button if (output27State=="off") < client.println("

"); > client.println(""); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; > else < // if you got a newline, then clear currentLine currentLine = ""; >> else if (c != '\r') < // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine >> > // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); > >

Setting Your Network Credentials

You need to modify the following lines with your network credentials: SSID and password.

// Replace with your network credentials const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Setting your Static IP Address

Then, outside the setup() and loop() functions, you define the following variables with your own static IP address and corresponding gateway IP address.

By default, the next code assigns the IP address 192.168.1.184 that works in the gateway 192.168.1.1.

// Set your Static IP address IPAddress local_IP(192, 168, 1, 184); // Set your Gateway IP address IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); IPAddress primaryDNS(8, 8, 8, 8); // optional IPAddress secondaryDNS(8, 8, 4, 4); // optional

Important: you need to use an available IP address in your local network and the corresponding gateway.

setup()

In the setup() you need to call the WiFi.config() method to assign the configurations to your ESP32.

// Configures static IP address if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS))

Note: the primaryDNS and secondaryDNS parameters are optional and you can remove them.

Testing

After uploading the code to your board, open the Arduino IDE Serial Monitor at the baud rate 115200, restart your ESP32 board and the IP address defined earlier should be assigned to your board.

As you can see, it prints the IP address 192.168.1.184.

You can take this example and add it to all your Wi-Fi sketches to assign a fixed IP address to your ESP32.

Assigning IP Address with MAC Address

If you’ve tried to assign a fixed IP address to the ESP32 using the previous example and it doesn’t work, we recommend assigning an IP address directly in your router settings through the ESP32 MAC Address.

Add your network credentials (SSID and password). Then, upload the next code to your ESP32:

/********* Rui Santos Complete project details at https://randomnerdtutorials.com *********/ // Load Wi-Fi library #include // Replace with your network credentials const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Set web server port number to 80 WiFiServer server(80); void setup() < Serial.begin(115200); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) < delay(500); Serial.print("."); >// Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); // Print ESP MAC Address Serial.println("MAC address: "); Serial.println(WiFi.macAddress()); > void loop() < // put your main code here, to run repeatedly: >

In the setup() , after connecting to your network, it prints the ESP32 MAC Address in the Serial Monitor:

// Print ESP MAC Address Serial.println("MAC address: "); Serial.println(WiFi.macAddress());

In our case, the ESP32 MAC Address is B4:E6:2D:97:EE:F1. Copy the MAC Address, because you’ll need it in just a moment.

Router Settings

If you login into your router admin page, there should be a page/menu where you can assign an IP address to a network device. Each router has different menus and configurations. So, we can’t provide instructions on how do to it for all the routers available.

We recommend Googling “assign IP address to MAC address” followed by your router name. You should find some instructions that show how to assign the IP to a MAC address for your specific router.

In summary, if you go to your router configurations menu, you should be able to assign your desired IP address to your ESP32 MAC address (for example B4:E6:2D:97:EE:F1).

Wrapping Up

After following this tutorial you should be able to assign a fixed/static IP address to your ESP32.

We hope you’ve found this tutorial useful. If you like ESP32, you may also like:

Источник

Оцените статью
Adblock
detector