Last active
March 9, 2022 18:03
-
-
Save luigibrancati/32141575fc1340e10b871a98206ebdef to your computer and use it in GitHub Desktop.
Code to run wifi and server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <WiFi.h> | |
| #include <WebServer.h> | |
| const char* SSID = "YOUR_SSID"; | |
| const char* PWD = "YOUR_PASSWORD"; | |
| const String serverHostname = "esp32"; | |
| const int serverPort = 80; | |
| // Set your Static IP address | |
| const IPAddress serverIP(192, 168, 1, 184); | |
| // Set your Gateway IP address | |
| const IPAddress serverGateway(192, 168, 1, 1); | |
| const IPAddress serverSubnet(255, 255, 0, 0); | |
| const IPAddress serverPrimaryDNS(8, 8, 8, 8); // optional | |
| const IPAddress serverSecondaryDNS(8, 8, 4, 4); // optional | |
| WebServer server(serverPort); | |
| void connectToWiFi() { | |
| preferences.begin(credentialsNamespace); | |
| Serial.print("Connecting to "); | |
| Serial.println(preferences.getString("ssid")); | |
| WiFi.begin(preferences.getString("ssid").c_str(), preferences.getString("pwd").c_str()); | |
| preferences.end(); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| Serial.print("."); | |
| delay(500); | |
| // we can even make the ESP32 to sleep | |
| } | |
| Serial.print("Connected. IP: "); | |
| Serial.println(WiFi.localIP()); | |
| } | |
| void endWifi(){ | |
| WiFi.disconnect(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment