diff --git a/.gitignore b/.gitignore index da89dd2..a6ea1a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -.secrets .arduino_cache/ bin/ build/ +src/secrets.h diff --git a/arduino_pong.ino b/arduino_pong.ino index ebae28e..d83f84e 100644 --- a/arduino_pong.ino +++ b/arduino_pong.ino @@ -4,6 +4,7 @@ #include "src/pong_render.h" #include "src/pong_player.h" #include "src/pong_ball.h" +#include "src/arduino_network.h" // create LED matrix object ArduinoLEDMatrix matrix; @@ -45,6 +46,8 @@ void setup() { pinMode(P2_BTN_BOTTOM, INPUT_PULLUP); randomSeed(millis()); + + setup_network(); } void loop() { @@ -61,4 +64,6 @@ void loop() { need_refresh= 0; } delay(50); + + web_server(); } diff --git a/src/arduino_network.cpp b/src/arduino_network.cpp new file mode 100644 index 0000000..8820631 --- /dev/null +++ b/src/arduino_network.cpp @@ -0,0 +1,141 @@ +#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("
Click here turn the LED on
Click here turn the LED off