Connect ESP32C3 to replit Python HTTP Server

Question:
I’m trying to connect my ESP32C3 XIAO to a python http server. On the microntroller side, I’m using the WiFi library from Arduino, and I’m just using the python http server template on replit. I keep getting a 308 permanent redirect response. (I’m expecting to get the content in templates/index.html).

HTTP/1.1 308 Permanent Redirect
Content-Type: text/html; charset=utf-8
Location: https://server.joshuabas.repl.co/
Replit-Cluster: global
Date: Sun, 26 Feb 2023 03:05:13 GMT
Content-Length: 69
Via: 1.1 google

<a href="https://server.joshuabas.repl.co/">Permanent Redirect</a>.

I then tried using the WIFiClientSecure library to use https, but then it just wouldn’t receive anything at all.

Is connecting a microcontroller to an http server possible on replit? If so, why exactly am I getting the redirect response?

Here’s the link to the python server replit:
Repl link: https://replit.com/@joshuabas/server#templates/index.html

Here’s my Arduino code that is running on the ESP3C23 XIAO (you’d need to follow the Arduino setup instruction from Getting Started with Seeed Studio XIAO ESP32C3 - Seeed Wiki if you want to run this):

#include <WiFi.h>

WiFiClient client;
const char* ssid     = "ssid";
const char* password = "pass";

const char* server = "server.joshuabas.repl.co";
const uint8_t port = 80;

void connectNetwork(const char* ssid, const char* password) {
    WiFi.begin(ssid, password);
 
    // wait until connected
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
    }
}

void connectServer(const char* server, uint8_t port) {

    while (!client.connect(server, port)) {
        delay(500);
    }
}

void setup() {
    Serial.begin(115200);
    delay(10);

    connectNetwork(ssid, password);
    connectServer(server, port);

    client.println("GET / HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: disconnect");
    client.println();
}

void loop() {
    while (client.available()) {
        char c = client.read();
        Serial.write(c);
    }
    
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
        Serial.println("Disconnecting from server.");
        client.stop();
        
        // do nothing forevermore:
        while (true);
    }
}

I appreciate any help!

You are using http and not https. All repls are forced to use https.
Instead of using server.joshuabas.repl.co in your Arduino code, replace it with https://server.joshuabas.repl.co/ to start a secure connection.

4 Likes

As @joecooldoo said, you need to use HTTPS, but adding “https” to the URL won’t work (I think)

Replace:

-const uint8_t port = 80;
+const uint8_t port = 443;

and

-    while (!client.connect(server, port)) {
+    while (!client.connectSSL(server, port)) {

Try doing this rather than using WiFiClientSecure. If it still doesn’t work, then your microcontroller may not support TLS.