Trouble setting up Argon web server

I’ve set up a simple web server on an Argon that will send the value of a variable as a JSON string back to the client. I can access this through cURL, but any other means (such as through a browser or python requests/pycurl libs) return an error saying the connection was closed by the peer.

Here is my Particle code:

WiFiSignal sig;
int strength;

TCPClient webClient;
TCPServer webServer = TCPServer(80);
char myIpAddress[24];

void setup() {
    //IPAddress myIp = Network.localIP();
    webServer.begin();
    Serial.begin();
    pinMode(D2, INPUT);
    pinMode(D3, OUTPUT);
    digitalWrite(D3, LOW);
    Particle.variable("measure", &strength, INT);
}

void loop() {
    strength = measureStrength();
    
    if (webClient.connected() && webClient.available()) {
        digitalWrite(D3, HIGH);
        webClient.print("{\"result\": ");
        webClient.print(strength);
        webClient.println("}");
        webClient.flush();
        delay(1000);
        webClient.stop();
        delay(100);
    }
    else {
        webClient = webServer.available();
    }
}

int measureStrength(){
    //digitalWrite(D3, HIGH);
    delay(200);
    sig = WiFi.RSSI();
    strength = sig.getQualityValue();
    delay(200);
    //digitalWrite(D3, LOW);
    return strength;
}

When I run curl 192.168.1.18, i get the response {“result”: n} as expected.

When I run the equivalent request in python i get the following error:
pycurl.error: (56, ‘Recv failure: Connection reset by peer’)

Any help would be much appreciated.

Thanks in advance.

This approach has been deprecated and should now be written like this:
Particle.variable("measure", strength);

Since strength is declared as a global int there is no need for measureStrength() to return an int.

The connection loop should look something like this instead:

TcpClient client = webClient.available();
if (client.connected()) {
        digitalWrite(D3, HIGH);
        cient.print("{\"result\": ");
        client.print(strength);
        client.println("}");
        client.flush();
        delay(1000);
        client.stop();
        delay(100);
    }

Basically, available() on the TCPServer object returns a TCPClient for a specific connection, allowing multiple (4) client connections. You do all of the communication with that client using the returned object instead of the TCPServer object.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.