|
| 1 | +/* |
| 2 | + Web client |
| 3 | +
|
| 4 | + This sketch connects to a website (http://example.com) using the WiFi module. |
| 5 | +
|
| 6 | + This example is written for a network using WPA encryption. For |
| 7 | + WEP or WPA, change the Wifi.begin() call accordingly. |
| 8 | +
|
| 9 | + Circuit: |
| 10 | + * Arduino Portenta H7 |
| 11 | +
|
| 12 | + created 13 July 2010 |
| 13 | + by dlf (Metodo2 srl) |
| 14 | + modified 31 May 2012 |
| 15 | + by Tom Igoe |
| 16 | + */ |
| 17 | + |
| 18 | +#include <WiFi.h> |
| 19 | + |
| 20 | +#include "arduino_secrets.h" |
| 21 | +///////please enter your sensitive data in the Secret tab/arduino_secrets.h |
| 22 | +char ssid[] = SECRET_SSID; // your network SSID (name) |
| 23 | +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) |
| 24 | +int keyIndex = 0; // your network key Index number (needed only for WEP) |
| 25 | + |
| 26 | +int status = WL_IDLE_STATUS; |
| 27 | +// if you don't want to use DNS (and reduce your sketch size) |
| 28 | +// use the numeric IP instead of the name for the server: |
| 29 | +// IPAddress server(93,184,216,34); // IP address for example.com (no DNS) |
| 30 | +char server[] = "example.com"; // host name for example.com (using DNS) |
| 31 | + |
| 32 | +WiFiClient client; |
| 33 | + |
| 34 | +void setup() { |
| 35 | + //Initialize serial and wait for port to open: |
| 36 | + Serial.begin(9600); |
| 37 | + while (!Serial) { |
| 38 | + ; // wait for serial port to connect. Needed for native USB port only |
| 39 | + } |
| 40 | + |
| 41 | + // check for the WiFi module: |
| 42 | + if (WiFi.status() == WL_NO_SHIELD) { |
| 43 | + Serial.println("Communication with WiFi module failed!"); |
| 44 | + // don't continue |
| 45 | + while (true); |
| 46 | + } |
| 47 | + |
| 48 | + // attempt to connect to Wifi network: |
| 49 | + while (status != WL_CONNECTED) { |
| 50 | + Serial.print("Attempting to connect to SSID: "); |
| 51 | + Serial.println(ssid); |
| 52 | + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: |
| 53 | + status = WiFi.begin(ssid, pass); |
| 54 | + |
| 55 | + // wait 3 seconds for connection: |
| 56 | + delay(3000); |
| 57 | + } |
| 58 | + Serial.println("Connected to wifi"); |
| 59 | + printWifiStatus(); |
| 60 | + |
| 61 | + Serial.println("\nStarting connection to server..."); |
| 62 | + // if you get a connection, report back via serial: |
| 63 | + if (client.connect(server, 80)) { |
| 64 | + Serial.println("connected to server"); |
| 65 | + // Make a HTTP request: |
| 66 | + client.println("GET /index.html HTTP/1.1"); |
| 67 | + client.print("Host: "); |
| 68 | + client.println(server); |
| 69 | + client.println("Connection: close"); |
| 70 | + client.println(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void loop() { |
| 75 | + // if there are incoming bytes available |
| 76 | + // from the server, read them and print them: |
| 77 | + while (client.available()) { |
| 78 | + char c = client.read(); |
| 79 | + Serial.write(c); |
| 80 | + } |
| 81 | + |
| 82 | + // if the server's disconnected, stop the client: |
| 83 | + if (!client.connected()) { |
| 84 | + Serial.println(); |
| 85 | + Serial.println("disconnecting from server."); |
| 86 | + client.stop(); |
| 87 | + |
| 88 | + // do nothing forevermore: |
| 89 | + while (true); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +void printWifiStatus() { |
| 94 | + // print the SSID of the network you're attached to: |
| 95 | + Serial.print("SSID: "); |
| 96 | + Serial.println(WiFi.SSID()); |
| 97 | + |
| 98 | + // print your board's IP address: |
| 99 | + IPAddress ip = WiFi.localIP(); |
| 100 | + Serial.print("IP Address: "); |
| 101 | + Serial.println(ip); |
| 102 | + |
| 103 | + // print the received signal strength: |
| 104 | + long rssi = WiFi.RSSI(); |
| 105 | + Serial.print("signal strength (RSSI):"); |
| 106 | + Serial.print(rssi); |
| 107 | + Serial.println(" dBm"); |
| 108 | +} |
0 commit comments