Able to get 01OS server address and ping it

pull/58/head
Zohaib Rauf 11 months ago
parent 79e7159fc7
commit edee74b70b

@ -7,5 +7,6 @@ To set up audio recording + playback on the ESP32 (M5 Atom), do the following:
3. Go to Tools -> Manage Libraries, then install the following:
- M5Atom by M5Stack
- WebSockets by Markus Sattler
- HttpClient by Adrian McEwan
4. The board needs to connect to WiFi. Go to the playback.ino code, then add your IP to COMPUTER_IP (line 15) and add your WiFi details (line 180). To find IP on macOS run `ipconfig getifaddr en0` in a terminal window.
5. To flash the .ino to the board, connect the board to the USB port, select the port from the dropdown on the IDE, then select the M5Atom board (or M5Stack-ATOM if you have that). Click on upload to flash the board.

@ -5,6 +5,9 @@
#include <DNSServer.h>
#include <ESPAsyncWebServer.h> //https://github.com/me-no-dev/ESPAsyncWebServer using the latest dev version from @me-no-dev
#include <esp_wifi.h> //Used for mpdu_rx_disable android workaround
#include <HttpClient.h>
#include <WiFi.h>
#include <WiFiClient.h>
// Pre reading on the fundamentals of captive portals https://textslashplain.com/2022/06/24/captive-portals/
@ -40,7 +43,7 @@ const char index_html[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 WiFi Setup</title>
<title>WiFi Setup</title>
<style>
body {background-color:#06cc13;}
h1 {color: white;}
@ -61,6 +64,29 @@ const char index_html[] PROGMEM = R"=====(
</html>
)=====";
const char post_connected_html[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<title>01OS Setup</title>
<style>
body {background-color:#06cc13;}
h1 {color: white;}
h2 {color: white;}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>01OS Setup</h1>
<form action="/submit_01os" method="post">
<label for="server_address">01OS Server Address:</label><br>
<input type="text" id="server_address" name="server_address"><br>
<input type="submit" value="Connect">
</form>
</body>
</html>
)=====";
DNSServer dnsServer;
AsyncWebServer server(80);
@ -126,6 +152,88 @@ void connectToWifi(String ssid, String password)
}
}
// Number of milliseconds to wait without receiving any data before we give up
const int kNetworkTimeout = 30*1000;
// Number of milliseconds to wait if no data is available before trying again
const int kNetworkDelay = 1000;
void connectTo01OS(String server_address)
{
int err = 0;
WiFiClient c;
HttpClient http(c);
String domain = server_address.substring(0, server_address.indexOf(':'));
String portStr = server_address.substring(server_address.indexOf(':') + 1);
int port = portStr.toInt();
Serial.println("Connecting to 01OS at "+domain+":"+port+"/ping");
err = http.get(domain.c_str(), port, "/ping");
//err = http.get("arduino.cc", "/");
if (err == 0)
{
Serial.println("Started the ping request");
err = http.responseStatusCode();
if (err >= 0)
{
Serial.print("Got status code: ");
Serial.println(err);
err = http.skipResponseHeaders();
if (err >= 0)
{
int bodyLen = http.contentLength();
Serial.print("Content length is: ");
Serial.println(bodyLen);
Serial.println();
Serial.println("Body returned follows:");
// Now we've got to the body, so we can print it out
unsigned long timeoutStart = millis();
char c;
// Whilst we haven't timed out & haven't reached the end of the body
while ((http.connected() || http.available()) &&
((millis() - timeoutStart) < kNetworkTimeout))
{
if (http.available())
{
c = http.read();
// Print out this character
Serial.print(c);
bodyLen--;
// We read something, reset the timeout counter
timeoutStart = millis();
}
else
{
// We haven't got any data, so let's pause to allow some to
// arrive
delay(kNetworkDelay);
}
}
}
else
{
Serial.print("Failed to skip response headers: ");
Serial.println(err);
}
}
else
{
Serial.print("Getting response failed: ");
Serial.println(err);
}
}
else
{
Serial.print("Connect failed: ");
Serial.println(err);
}
}
void setUpWebserver(AsyncWebServer &server, const IPAddress &localIP)
{
//======================== Webserver ========================
@ -210,7 +318,31 @@ void setUpWebserver(AsyncWebServer &server, const IPAddress &localIP)
connectToWifi(ssid, password);
// Redirect user or send a response back
request->send(200, "text/plain", "Attempting to connect to " + ssid); });
if (WiFi.status() == WL_CONNECTED) {
String htmlContent = post_connected_html;
AsyncWebServerResponse *response = request->beginResponse(200, "text/html", htmlContent);
response->addHeader("Cache-Control", "public,max-age=31536000"); // save this file to cache for 1 year (unless you refresh)
request->send(response);
Serial.println("Served Post connection HTML Page");
} else {
request->send(200, "text/plain", "Failed to connect to " + ssid);
}
});
server.on("/submit_01os", HTTP_POST, [](AsyncWebServerRequest *request)
{
String server_address;
// Check if SSID parameter exists and assign it
if(request->hasParam("server_address", true)) {
server_address = request->getParam("server_address", true)->value();
}
// Attempt to connect to the Wi-Fi network with these credentials
connectTo01OS(server_address);
// Redirect user or send a response back
request->send(200, "text/plain", "Attempting to connect to 01OS " + server_address); });
}
void setup()
@ -250,11 +382,11 @@ void loop()
delay(DNS_INTERVAL); // seems to help with stability, if you are doing other things in the loop this may not be needed
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED)
{
//if (WiFi.status() == WL_CONNECTED)
//{
// If connected, you might want to do something, like printing the IP address
Serial.println("Connected to WiFi!");
Serial.println("IP Address: " + WiFi.localIP().toString());
Serial.println("SSID " + WiFi.SSID());
}
//Serial.println("Connected to WiFi!");
//Serial.println("IP Address: " + WiFi.localIP().toString());
//Serial.println("SSID " + WiFi.SSID());
//}
}
Loading…
Cancel
Save