142 lines
4.3 KiB
C++
142 lines
4.3 KiB
C++
|
|
#include "config.h"
|
||
|
|
#include "Arduino.h"
|
||
|
|
#include "WiFiS3.h"
|
||
|
|
|
||
|
|
char ssid[]= SECRET_SSID;
|
||
|
|
char pass[]= SECRET_PASS;
|
||
|
|
int status= WL_IDLE_STATUS;
|
||
|
|
WiFiServer server(80);
|
||
|
|
|
||
|
|
|
||
|
|
void printMacAddress(byte mac[]) {
|
||
|
|
for (int i = 0; i < 6; i++) {
|
||
|
|
if (i > 0) {
|
||
|
|
Serial.print(":");
|
||
|
|
}
|
||
|
|
if (mac[i] < 16) {
|
||
|
|
Serial.print("0");
|
||
|
|
}
|
||
|
|
Serial.print(mac[i], HEX);
|
||
|
|
}
|
||
|
|
Serial.println();
|
||
|
|
}
|
||
|
|
|
||
|
|
void printWifiData() {
|
||
|
|
// print your board's IP address:
|
||
|
|
IPAddress ip = WiFi.localIP();
|
||
|
|
Serial.print("IP Address: ");
|
||
|
|
|
||
|
|
Serial.println(ip);
|
||
|
|
|
||
|
|
// print your MAC address:
|
||
|
|
byte mac[6];
|
||
|
|
WiFi.macAddress(mac);
|
||
|
|
Serial.print("MAC address: ");
|
||
|
|
printMacAddress(mac);
|
||
|
|
}
|
||
|
|
|
||
|
|
void printCurrentNet() {
|
||
|
|
// print the SSID of the network you're attached to:
|
||
|
|
Serial.print("SSID: ");
|
||
|
|
Serial.println(WiFi.SSID());
|
||
|
|
|
||
|
|
// print the MAC address of the router you're attached to:
|
||
|
|
byte bssid[6];
|
||
|
|
WiFi.BSSID(bssid);
|
||
|
|
Serial.print("BSSID: ");
|
||
|
|
printMacAddress(bssid);
|
||
|
|
|
||
|
|
// print the received signal strength:
|
||
|
|
long rssi = WiFi.RSSI();
|
||
|
|
Serial.print("signal strength (RSSI):");
|
||
|
|
Serial.println(rssi);
|
||
|
|
|
||
|
|
// print the encryption type:
|
||
|
|
byte encryption = WiFi.encryptionType();
|
||
|
|
Serial.print("Encryption Type:");
|
||
|
|
Serial.println(encryption, HEX);
|
||
|
|
Serial.println();
|
||
|
|
}
|
||
|
|
|
||
|
|
void setup_network() {
|
||
|
|
// check for the WiFi module:
|
||
|
|
if (WiFi.status() == WL_NO_MODULE) {
|
||
|
|
Serial.println("Communication with WiFi module failed!");
|
||
|
|
// don't continue
|
||
|
|
while (true);
|
||
|
|
}
|
||
|
|
String fv = WiFi.firmwareVersion();
|
||
|
|
Serial.print("Firmware version: ");
|
||
|
|
Serial.println(fv);
|
||
|
|
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
|
||
|
|
Serial.println("Please upgrade the firmware");
|
||
|
|
}
|
||
|
|
|
||
|
|
// attempt to connect to WiFi network:
|
||
|
|
// Definisci i parametri di rete prima del loop di connessione
|
||
|
|
IPAddress local_IP(192, 168, 1, 200);
|
||
|
|
IPAddress gateway(192, 168, 1, 1);
|
||
|
|
IPAddress subnet(255, 255, 255, 0);
|
||
|
|
IPAddress dns(9,9,9,9);
|
||
|
|
WiFi.config(local_IP, gateway, subnet, dns);
|
||
|
|
while (status != WL_CONNECTED) {
|
||
|
|
Serial.print("Attempting to connect to WPA SSID: ");
|
||
|
|
Serial.println(ssid);
|
||
|
|
// Connect to WPA/WPA2 network:
|
||
|
|
status = WiFi.begin(ssid, pass);
|
||
|
|
|
||
|
|
// wait 10 seconds for connection:
|
||
|
|
delay(10000);
|
||
|
|
}
|
||
|
|
|
||
|
|
// you're connected now, so print out the data:
|
||
|
|
Serial.println("You're connected to the network");
|
||
|
|
printCurrentNet();
|
||
|
|
printWifiData();
|
||
|
|
server.begin();
|
||
|
|
}
|
||
|
|
|
||
|
|
void web_server() {
|
||
|
|
WiFiClient client = server.available();
|
||
|
|
|
||
|
|
if (client) { // if you get a client,
|
||
|
|
Serial.println("new client"); // print a message out the serial port
|
||
|
|
String currentLine = ""; // make a String to hold incoming data from the client
|
||
|
|
while (client.connected()) { // loop while the client's connected
|
||
|
|
if (client.available()) { // if there's bytes to read from the client,
|
||
|
|
char c = client.read(); // read a byte, then
|
||
|
|
Serial.write(c); // print it out to the serial monitor
|
||
|
|
if (c == '\n') { // if the byte is a newline character
|
||
|
|
|
||
|
|
// if the current line is blank, you got two newline characters in a row.
|
||
|
|
// that's the end of the client HTTP request, so send a response:
|
||
|
|
if (currentLine.length() == 0) {
|
||
|
|
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
|
||
|
|
// and a content-type so the client knows what's coming, then a blank line:
|
||
|
|
client.println("HTTP/1.1 200 OK");
|
||
|
|
client.println("Content-type:text/html");
|
||
|
|
client.println();
|
||
|
|
|
||
|
|
// the content of the HTTP response follows the header:
|
||
|
|
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/H\">here</a> turn the LED on<br></p>");
|
||
|
|
client.print("<p style=\"font-size:7vw;\">Click <a href=\"/L\">here</a> turn the LED off<br></p>");
|
||
|
|
|
||
|
|
// The HTTP response ends with another blank line:
|
||
|
|
client.println();
|
||
|
|
// break out of the while loop:
|
||
|
|
break;
|
||
|
|
} else { // if you got a newline, then clear currentLine:
|
||
|
|
currentLine = "";
|
||
|
|
}
|
||
|
|
} else if (c != '\r') { // if you got anything else but a carriage return character,
|
||
|
|
currentLine += c; // add it to the end of the currentLine
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
// close the connection:
|
||
|
|
client.stop();
|
||
|
|
Serial.println("client disconnected");
|
||
|
|
}
|
||
|
|
}
|