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