2 Commits

Author SHA1 Message Date
andrea
c1cbc07e9c fix conflicts 2026-03-15 22:48:25 +01:00
andrea
a48db73abb experiment with webserver, the goal is to show the score on a web server 2026-03-15 22:35:59 +01:00
5 changed files with 166 additions and 1 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
.secrets
.arduino_cache/ .arduino_cache/
bin/ bin/
build/ build/
src/secrets.h

View File

@@ -4,6 +4,7 @@
#include "src/pong_render.h" #include "src/pong_render.h"
#include "src/pong_player.h" #include "src/pong_player.h"
#include "src/pong_ball.h" #include "src/pong_ball.h"
#include "src/arduino_network.h"
// create LED matrix object // create LED matrix object
ArduinoLEDMatrix matrix; ArduinoLEDMatrix matrix;
@@ -45,6 +46,8 @@ void setup() {
pinMode(P2_BTN_BOTTOM, INPUT_PULLUP); pinMode(P2_BTN_BOTTOM, INPUT_PULLUP);
randomSeed(millis()); randomSeed(millis());
setup_network();
} }
void loop() { void loop() {
@@ -61,4 +64,6 @@ void loop() {
need_refresh= 0; need_refresh= 0;
} }
delay(50); delay(50);
web_server();
} }

141
src/arduino_network.cpp Normal file
View File

@@ -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("<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");
}
}

11
src/arduino_network.h Normal file
View File

@@ -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

View File

@@ -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 12 #define P1_BTN_UP 12
#define P1_BTN_BOTTOM 11 #define P1_BTN_BOTTOM 11
#define P2_BTN_UP 10 #define P2_BTN_UP 10