From a48db73abb12cc4241e2b68cf90cdb16fc4bfe6d Mon Sep 17 00:00:00 2001 From: andrea Date: Sun, 15 Mar 2026 22:35:59 +0100 Subject: [PATCH] experiment with webserver, the goal is to show the score on a web server --- .gitignore | 2 +- arduino_pong.ino | 5 ++ src/arduino_network.cpp | 141 ++++++++++++++++++++++++++++++++++++++++ src/arduino_network.h | 11 ++++ src/config.h | 8 +++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 src/arduino_network.cpp create mode 100644 src/arduino_network.h 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

"); + client.print("

Click here turn the LED off

"); + + // 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"); + } +} diff --git a/src/arduino_network.h b/src/arduino_network.h new file mode 100644 index 0000000..cbc31b6 --- /dev/null +++ b/src/arduino_network.h @@ -0,0 +1,11 @@ +#ifndef PONG_NETWORK_H +#define PONG_NETWORK_H +#include "Arduino.h" + +void setup_network(); +void printMacAddress(byte mac[]); +void printWifiData(); +void printCurrentNet(); +void web_server(); + +#endif diff --git a/src/config.h b/src/config.h index 05245b9..46a21b5 100644 --- a/src/config.h +++ b/src/config.h @@ -1,3 +1,11 @@ +#if __has_include("secrets.h") + #include "secrets.h" +#else + #define SECRET_SSID "Fallback_SSID" + #define SECRET_PASS "Fallback_PASS" + #warning "⚠️ secrets.h not found" +#endif + #define P1_BTN_UP 13 #define P1_BTN_BOTTOM 12 #define P2_BTN_UP 11