|
| 1 | +/**************************************************************************************************************************** |
| 2 | + BasicAuthGet.ino - Simple Arduino web server sample for WiFi shield |
| 3 | +
|
| 4 | + For any WiFi shields, such as WiFiNINA W101, W102, W13x, or custom, such as ESP8266/ESP32-AT, Ethernet, etc |
| 5 | + |
| 6 | + WiFiWebServer is a library for the ESP32-based WiFi shields to run WebServer |
| 7 | + Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases |
| 8 | + Based on and modified from Arduino WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA |
| 9 | + Built by Khoi Hoang https://github.com/khoih-prog/WiFiWebServer |
| 10 | + Licensed under MIT license |
| 11 | +
|
| 12 | + GET client with HTTP basic authentication for ArduinoHttpClient library |
| 13 | + Connects to server once every five seconds, sends a GET request |
| 14 | +
|
| 15 | + created 14 Feb 2016 |
| 16 | + by Tom Igoe |
| 17 | + modified 3 Jan 2017 to add HTTP basic authentication |
| 18 | + by Sandeep Mistry |
| 19 | + modified 22 Jan 2019 |
| 20 | + by Tom Igoe |
| 21 | + *****************************************************************************************************************************/ |
| 22 | + |
| 23 | +#include "defines.h" |
| 24 | + |
| 25 | +char serverAddress[] = "192.168.2.112"; // server address |
| 26 | +int port = 8080; |
| 27 | + |
| 28 | +WiFiClient client; |
| 29 | +WiFiHttpClient httpClient(client, serverAddress); |
| 30 | + |
| 31 | +int status = WL_IDLE_STATUS; // the Wifi radio's status |
| 32 | + |
| 33 | +void printWifiStatus() |
| 34 | +{ |
| 35 | + // print the SSID of the network you're attached to: |
| 36 | + // you're connected now, so print out the data |
| 37 | + Serial.print(F("You're connected to the network, IP = ")); |
| 38 | + Serial.println(WiFi.localIP()); |
| 39 | + |
| 40 | + Serial.print(F("SSID: ")); |
| 41 | + Serial.print(WiFi.SSID()); |
| 42 | + |
| 43 | + // print the received signal strength: |
| 44 | + int32_t rssi = WiFi.RSSI(); |
| 45 | + Serial.print(F(", Signal strength (RSSI):")); |
| 46 | + Serial.print(rssi); |
| 47 | + Serial.println(F(" dBm")); |
| 48 | +} |
| 49 | + |
| 50 | +void setup() |
| 51 | +{ |
| 52 | + Serial.begin(115200); |
| 53 | + while (!Serial); |
| 54 | + |
| 55 | + Serial.print("\nStarting BasicAuthGet on " + String(BOARD_NAME)); |
| 56 | + Serial.println(" with " + String(SHIELD_TYPE)); |
| 57 | + |
| 58 | + // check for the presence of the shield |
| 59 | +#if USE_WIFI_NINA |
| 60 | + if (WiFi.status() == WL_NO_MODULE) |
| 61 | +#else |
| 62 | + if (WiFi.status() == WL_NO_SHIELD) |
| 63 | +#endif |
| 64 | + { |
| 65 | + Serial.println(F("WiFi shield not present")); |
| 66 | + // don't continue |
| 67 | + while (true); |
| 68 | + } |
| 69 | + |
| 70 | +#if USE_WIFI_NINA |
| 71 | + String fv = WiFi.firmwareVersion(); |
| 72 | + if (fv < WIFI_FIRMWARE_LATEST_VERSION) |
| 73 | + { |
| 74 | + Serial.println(F("Please upgrade the firmware")); |
| 75 | + } |
| 76 | +#endif |
| 77 | + |
| 78 | + // attempt to connect to WiFi network |
| 79 | + while ( status != WL_CONNECTED) |
| 80 | + { |
| 81 | + Serial.print(F("Connecting to SSID: ")); |
| 82 | + Serial.println(ssid); |
| 83 | + // Connect to WPA/WPA2 network |
| 84 | + status = WiFi.begin(ssid, pass); |
| 85 | + } |
| 86 | + |
| 87 | + // you're connected now, so print out the data |
| 88 | + printWifiStatus(); |
| 89 | +} |
| 90 | + |
| 91 | +void loop() |
| 92 | +{ |
| 93 | + Serial.println("making GET request with HTTP basic authentication"); |
| 94 | + httpClient.beginRequest(); |
| 95 | + httpClient.get("/secure"); |
| 96 | + httpClient.sendBasicAuth("username", "password"); // send the username and password for authentication |
| 97 | + httpClient.endRequest(); |
| 98 | + |
| 99 | + // read the status code and body of the response |
| 100 | + int statusCode = httpClient.responseStatusCode(); |
| 101 | + String response = httpClient.responseBody(); |
| 102 | + |
| 103 | + Serial.print("Status code: "); |
| 104 | + Serial.println(statusCode); |
| 105 | + Serial.print("Response: "); |
| 106 | + Serial.println(response); |
| 107 | + |
| 108 | + Serial.println("Wait five seconds"); |
| 109 | + delay(5000); |
| 110 | +} |
0 commit comments