|
| 1 | +/* |
| 2 | + Example of connection using Static IP |
| 3 | + by Evandro Luis Copercini |
| 4 | + Public domain - 2017 |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <WiFi.h> |
| 8 | + |
| 9 | +const char* ssid = "your_network_name"; |
| 10 | +const char* password = "your_network_password"; |
| 11 | +const char* host = "example.com"; |
| 12 | +const char* url = "/index.html"; |
| 13 | + |
| 14 | +IPAddress local_IP(192, 168, 31, 115); |
| 15 | +IPAddress gateway(192, 168, 31, 1); |
| 16 | +IPAddress subnet(255, 255, 0, 0); |
| 17 | +IPAddress primaryDNS(8, 8, 8, 8); //optional |
| 18 | +IPAddress secondaryDNS(8, 8, 4, 4); //optional |
| 19 | + |
| 20 | +void setup() |
| 21 | +{ |
| 22 | + Serial.begin(115200); |
| 23 | + |
| 24 | + if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { |
| 25 | + Serial.println("STA Failed to configure"); |
| 26 | + } |
| 27 | + |
| 28 | + Serial.print("Connecting to "); |
| 29 | + Serial.println(ssid); |
| 30 | + |
| 31 | + WiFi.begin(ssid, password); |
| 32 | + |
| 33 | + while (WiFi.status() != WL_CONNECTED) { |
| 34 | + delay(500); |
| 35 | + Serial.print("."); |
| 36 | + } |
| 37 | + |
| 38 | + Serial.println(""); |
| 39 | + Serial.println("WiFi connected!"); |
| 40 | + Serial.print("IP address: "); |
| 41 | + Serial.println(WiFi.localIP()); |
| 42 | + Serial.print("ESP Mac Address: "); |
| 43 | + Serial.println(WiFi.macAddress()); |
| 44 | + Serial.print("Subnet Mask: "); |
| 45 | + Serial.println(WiFi.subnetMask()); |
| 46 | + Serial.print("Gateway IP: "); |
| 47 | + Serial.println(WiFi.gatewayIP()); |
| 48 | + Serial.print("DNS: "); |
| 49 | + Serial.println(WiFi.dnsIP()); |
| 50 | +} |
| 51 | + |
| 52 | +void loop() |
| 53 | +{ |
| 54 | + delay(5000); |
| 55 | + |
| 56 | + Serial.print("connecting to "); |
| 57 | + Serial.println(host); |
| 58 | + |
| 59 | + // Use WiFiClient class to create TCP connections |
| 60 | + WiFiClient client; |
| 61 | + const int httpPort = 80; |
| 62 | + if (!client.connect(host, httpPort)) { |
| 63 | + Serial.println("connection failed"); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + Serial.print("Requesting URL: "); |
| 68 | + Serial.println(url); |
| 69 | + |
| 70 | + // This will send the request to the server |
| 71 | + client.print(String("GET ") + url + " HTTP/1.1\r\n" + |
| 72 | + "Host: " + host + "\r\n" + |
| 73 | + "Connection: close\r\n\r\n"); |
| 74 | + unsigned long timeout = millis(); |
| 75 | + while (client.available() == 0) { |
| 76 | + if (millis() - timeout > 5000) { |
| 77 | + Serial.println(">>> Client Timeout !"); |
| 78 | + client.stop(); |
| 79 | + return; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Read all the lines of the reply from server and print them to Serial |
| 84 | + while (client.available()) { |
| 85 | + String line = client.readStringUntil('\r'); |
| 86 | + Serial.print(line); |
| 87 | + } |
| 88 | + |
| 89 | + Serial.println(); |
| 90 | + Serial.println("closing connection"); |
| 91 | +} |
| 92 | + |
0 commit comments