SSL error on subsequent connections

ProfMoriarty
Posts: 3
Joined: Tue Nov 10, 2020 7:34 pm

SSL error on subsequent connections

Postby ProfMoriarty » Tue Nov 10, 2020 8:00 pm

I have an Olimex ESP32-EVB board with several sensors talking to a webserver.

The first time through, the board sends the data fine:
connecting to 172.16.0.172 - connected to: https://172.16.0.172/api/device/update
{"device_uuid":"692cc64f-c824-4d11-9252-b824f2dd18c4","last_mac":"BC:DD:C2:F1:72:F8","last_ip":"172.16.0.181","inputs":{"1":{"input_uuid":"5253c650-8b99-48f2-8bea-e581aa40fdac","value":2355,"unit":"C","scalefactor":100},"2":{"input_uuid":"9b754ac7-c5b0-4d2c-977c-7635ae7fa978","value":1950,"unit":"%","scalefactor":100},"3":{"input_uuid":"c96efdc5-9aee-406b-b755-140a87611442","value":85227,"unit":"hPa","scalefactor":1},"4":{"input_uuid":"9feefc89-9800-4c88-9524-a533810e0485","value":0,"unit":"oz","scalefactor":1}}}
HTTP Response code: (200)
closing connection

However, with the second and subsequent connections, I see an SSL error pop up ... [E][ssl_client.cpp:33] _handle_error(): [data_to_read():270]: (-80) UNKNOWN ERROR CODE (0050)

connecting to 172.16.0.172 - connected to: https://172.16.0.172/api/device/update
[E][ssl_client.cpp:33] _handle_error(): [data_to_read():270]: (-80) UNKNOWN ERROR CODE (0050)
{"device_uuid":"692cc64f-c824-4d11-9252-b824f2dd18c4","last_mac":"BC:DD:C2:F1:72:F8","last_ip":"172.16.0.181","inputs":{"1":{"input_uuid":"5253c650-8b99-48f2-8bea-e581aa40fdac","value":2352,"unit":"C","scalefactor":100},"2":{"input_uuid":"9b754ac7-c5b0-4d2c-977c-7635ae7fa978","value":1942,"unit":"%","scalefactor":100},"3":{"input_uuid":"c96efdc5-9aee-406b-b755-140a87611442","value":85226,"unit":"hPa","scalefactor":1},"4":{"input_uuid":"9feefc89-9800-4c88-9524-a533810e0485","value":0,"unit":"oz","scalefactor":1}}}
HTTP Response code: (200)
closing connection
My guess is that I'm not closing a connection somewhere in my code:

Code: Select all

void updateServer()
{
	Serial.print("\nconnecting to ");
	Serial.print(SERVER_HOST);

	WiFiClient client;
	if (!client.connect(SERVER_HOST, SERVER_PORT)) {
		Serial.println(" - connection failed");
		return;
	}

	Serial.println(" - connected to: " + SERVER_URL);

	// set http timeout after connected - default is 5 seconds, increase to 30
	httpClient.setTimeout(30 * 1000);

	// Domain name with URL path or IP address with FULL path
	httpClient.begin(SERVER_URL);

	// Specify content-type header
	httpClient.addHeader("X-API-AUTH", String(api_token));

	// HTTP request with a content type: application/json
	httpClient.addHeader("Content-Type", "application/json");

	// build the JSON object - NOTE: make sure to delimit all internal double-quotes 
	String json = "{"
		"\"device_uuid\":\"" + String(device_uuid) + "\","
		"\"last_mac\":\"" + String(ethMacAddress) + "\","
		"\"last_ip\":\"" + String(ethIpAddress) + "\","
		"\"inputs\":{"
			"\"1\":{"
				"\"input_uuid\":\"" + String(input_1_uuid) + "\","
				"\"value\":" + input_1_data + ","
				"\"unit\":\"" + String(input_1_unit) + "\","
				"\"scalefactor\":" + input_1_sf +
			"},"
			"\"2\":{"
				"\"input_uuid\":\"" + String(input_2_uuid) + "\","
				"\"value\":" + input_2_data + ","
				"\"unit\":\"" + String(input_2_unit) + "\","
				"\"scalefactor\":" + input_2_sf +
			"},"
			"\"3\":{"
				"\"input_uuid\":\"" + String(input_3_uuid) + "\","
				"\"value\":" + input_3_data + ","
				"\"unit\":\"" + String(input_3_unit) + "\","
				"\"scalefactor\":" + input_3_sf +
			"},"
			"\"4\":{"
				"\"input_uuid\":\"" + String(input_4_uuid) + "\","
				"\"value\":" + input_4_data + ","
				"\"unit\":\"" + String(input_4_unit) + "\","
				"\"scalefactor\":" + input_4_sf +
			"}"
		"}"
	"}";

	Serial.println(json);

	int httpResponseCode = httpClient.POST(json);

	Serial.print("HTTP Response code: (");
	Serial.print(httpResponseCode);
	Serial.print(")  ");
	Serial.println(httpClient.errorToString(httpResponseCode));

	// Free resources
	httpClient.end();

	client.stop();
	Serial.println("closing connection\n");
}

Note that the subsequent connections do have the SSL error, but are successful with a 200 result code.

Beyond the httpClient.end() and client.stop(), what else should I use to fully close this connection and reset the SSL?
Any pointers would be appreciated. I don't want to ignore this if I can fix it.

ProfMoriarty
Posts: 3
Joined: Tue Nov 10, 2020 7:34 pm

Re: SSL error on subsequent connections

Postby ProfMoriarty » Wed Nov 11, 2020 4:54 pm

Looks like moving from WiFiClient to WiFiSecureClient, and adding in the Cert/Key seems to fix the error.

Found a demo here: https://github.com/espressif/arduino-es ... Secure.ino

ProfMoriarty
Posts: 3
Joined: Tue Nov 10, 2020 7:34 pm

Re: SSL error on subsequent connections

Postby ProfMoriarty » Wed Nov 11, 2020 5:21 pm

Just the change to WiFiSecureClient didn't work once some debugging values were set back to a real value.

However, I did figure out that I had the HTTPClient as a global, instead of a local variable. This appears to be the key, since following Basic HTTPS Client example pattern works fine in my code.

https://github.com/espressif/arduino-es ... Client.ino

Code: Select all

Pseudo-code:
{	
	Create Client
	Client.connect()
	Set Certificate/Key or CA
	{
		Create HTTPClient
		Change HTTPClient options
		... do the work ...
		HTTPClient.end()
	}
	Client.stop()
}

Who is online

Users browsing this forum: No registered users and 74 guests