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