|
| 1 | +/**************************************************************************************************************************** |
| 2 | + WiFiWebServer.ino |
| 3 | +
|
| 4 | + A simple web server that shows the value of the analog input pins. |
| 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 | + Analog inputs attached to pins A0 through A5 (optional) |
| 11 | +
|
| 12 | + created 13 July 2010 |
| 13 | + by dlf (Metodo2 srl) |
| 14 | + modified 31 May 2012 |
| 15 | + by Tom Igoe |
| 16 | +
|
| 17 | + Based on and modified from WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA |
| 18 | + to support nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, Teensy, etc. boards besides Nano-33 IoT, MKRWIFI1010, MKRVIDOR400, etc. |
| 19 | +
|
| 20 | + Built by Khoi Hoang https://github.com/khoih-prog/WiFiNINA_Generic |
| 21 | + Licensed under MIT license |
| 22 | +
|
| 23 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 24 | + Copyright (c) 2011-2014 Arduino LLC. All right reserved. |
| 25 | +
|
| 26 | + This library is free software; you can redistribute it and/or |
| 27 | + modify it under the terms of the GNU Lesser General Public |
| 28 | + License as published by the Free Software Foundation; either |
| 29 | + version 2.1 of the License, or (at your option) any later version. |
| 30 | +
|
| 31 | + This library is distributed in the hope that it will be useful, |
| 32 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 33 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 34 | + Lesser General Public License for more details. |
| 35 | +
|
| 36 | + You should have received a copy of the GNU Lesser General Public |
| 37 | + License along with this library; if not, write to the Free Software |
| 38 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 39 | + *****************************************************************************************************************************/ |
| 40 | + |
| 41 | +#include "defines.h" |
| 42 | +#include "arduino_secrets.h" |
| 43 | + |
| 44 | +// To eliminate FW warning when using not latest nina-fw version |
| 45 | +// To use whenever WiFi101-FirmwareUpdater-Plugin is not sync'ed with nina-fw version |
| 46 | +#define WIFI_FIRMWARE_LATEST_VERSION "1.4.8" |
| 47 | + |
| 48 | +#include <SPI.h> |
| 49 | +#include <WiFiNINA_Generic.h> |
| 50 | + |
| 51 | +///////please enter your sensitive data in the Secret tab/arduino_secrets.h |
| 52 | +char ssid[] = SECRET_SSID; // your network SSID (name) |
| 53 | +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP), length must be 8+ |
| 54 | + |
| 55 | +int keyIndex = 0; // your network key Index number (needed only for WEP) |
| 56 | + |
| 57 | +int status = WL_IDLE_STATUS; |
| 58 | + |
| 59 | +WiFiServer server(80); |
| 60 | + |
| 61 | +void setup() |
| 62 | +{ |
| 63 | + //Initialize serial and wait for port to open: |
| 64 | + Serial.begin(115200); |
| 65 | + |
| 66 | + while (!Serial && millis() < 5000); |
| 67 | + |
| 68 | + Serial.print(F("\nStart WiFiWebServer on ")); |
| 69 | + Serial.println(BOARD_NAME); |
| 70 | + //Serial.println(WIFININA_GENERIC_VERSION); |
| 71 | + |
| 72 | + // check for the WiFi module: |
| 73 | + if (WiFi.status() == WL_NO_MODULE) |
| 74 | + { |
| 75 | + Serial.println(F("Communication with WiFi module failed!")); |
| 76 | + |
| 77 | + // don't continue |
| 78 | + while (true); |
| 79 | + } |
| 80 | + |
| 81 | + String fv = WiFi.firmwareVersion(); |
| 82 | + |
| 83 | + if (fv < WIFI_FIRMWARE_LATEST_VERSION) |
| 84 | + { |
| 85 | + Serial.print(F("Your current firmware NINA FW v")); |
| 86 | + Serial.println(fv); |
| 87 | + Serial.print(F("Please upgrade the firmware to NINA FW v")); |
| 88 | + Serial.println(WIFI_FIRMWARE_LATEST_VERSION); |
| 89 | + } |
| 90 | + |
| 91 | + // attempt to connect to Wifi network: |
| 92 | + while (status != WL_CONNECTED) |
| 93 | + { |
| 94 | + Serial.print(F("Attempting to connect to SSID: ")); |
| 95 | + Serial.println(ssid); |
| 96 | + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: |
| 97 | + status = WiFi.begin(ssid, pass); |
| 98 | + |
| 99 | + // wait 10 seconds for connection: |
| 100 | + //delay(10000); |
| 101 | + } |
| 102 | + |
| 103 | + // you're connected now, so print out the status: |
| 104 | + printWiFiStatus(); |
| 105 | + |
| 106 | + server.begin(); |
| 107 | +} |
| 108 | + |
| 109 | +void loop() |
| 110 | +{ |
| 111 | + // listen for incoming clients |
| 112 | + WiFiClient client = server.available(); |
| 113 | + |
| 114 | + if (client) |
| 115 | + { |
| 116 | + Serial.println(F("New client")); |
| 117 | + // an http request ends with a blank line |
| 118 | + boolean currentLineIsBlank = true; |
| 119 | + |
| 120 | + while (client.connected()) |
| 121 | + { |
| 122 | + if (client.available()) |
| 123 | + { |
| 124 | + char c = client.read(); |
| 125 | + Serial.write(c); |
| 126 | + |
| 127 | + // if you've gotten to the end of the line (received a newline |
| 128 | + // character) and the line is blank, the http request has ended, |
| 129 | + // so you can send a reply |
| 130 | + if (c == '\n' && currentLineIsBlank) |
| 131 | + { |
| 132 | + // send a standard http response header |
| 133 | + client.println("HTTP/1.1 200 OK"); |
| 134 | + client.println("Content-Type: text/html"); |
| 135 | + client.println("Connection: close"); // the connection will be closed after completion of the response |
| 136 | + client.println("Refresh: 5"); // refresh the page automatically every 5 sec |
| 137 | + client.println(); |
| 138 | + client.println("<!DOCTYPE HTML>"); |
| 139 | + client.println("<html>"); |
| 140 | + |
| 141 | + int sensorReading; |
| 142 | + |
| 143 | + // output the value of each analog input pin |
| 144 | + for (int analogChannel = 0; analogChannel < 6; analogChannel++) |
| 145 | + { |
| 146 | + // analogRead() will hang system in arduino-pico core |
| 147 | + sensorReading = millis() % 1024; //analogRead(analogChannel); |
| 148 | + client.print("analog input "); |
| 149 | + client.print(analogChannel); |
| 150 | + client.print(" is "); |
| 151 | + client.print(sensorReading); |
| 152 | + client.println("<br />"); |
| 153 | + } |
| 154 | + |
| 155 | + client.println("</html>"); |
| 156 | + break; |
| 157 | + } |
| 158 | + |
| 159 | + if (c == '\n') |
| 160 | + { |
| 161 | + // you're starting a new line |
| 162 | + currentLineIsBlank = true; |
| 163 | + } |
| 164 | + else if (c != '\r') |
| 165 | + { |
| 166 | + // you've gotten a character on the current line |
| 167 | + currentLineIsBlank = false; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // give the web browser time to receive the data |
| 173 | + delay(1); |
| 174 | + |
| 175 | + // close the connection: |
| 176 | + client.stop(); |
| 177 | + Serial.println(F("Client disconnected")); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +void printWiFiStatus() |
| 182 | +{ |
| 183 | + // print the SSID of the network you're attached to: |
| 184 | + Serial.print(F("SSID: ")); |
| 185 | + Serial.println(WiFi.SSID()); |
| 186 | + |
| 187 | + // print your board's IP address: |
| 188 | + IPAddress ip = WiFi.localIP(); |
| 189 | + Serial.print(F("IP Address: ")); |
| 190 | + Serial.println(ip); |
| 191 | + |
| 192 | + // print the received signal strength: |
| 193 | + long rssi = WiFi.RSSI(); |
| 194 | + Serial.print(F("Signal strength (RSSI):")); |
| 195 | + Serial.print(rssi); |
| 196 | + Serial.println(F(" dBm")); |
| 197 | +} |
0 commit comments