Skip to content

Commit 5a2a4d3

Browse files
committed
fixed response issues
1 parent 22b2142 commit 5a2a4d3

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

src/esp8266/ESP8266AWSImplementations.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/* HttpClient implementation to be used on the Esp8266 Core device. */
88
class Esp8266HttpClient: public IHttpClient {
9-
WiFiClientSecure client;
9+
WiFiClientSecure sclient;
1010
//TCPClient client;
1111
public:
1212
Esp8266HttpClient();
@@ -19,7 +19,7 @@ class Esp8266HttpClient: public IHttpClient {
1919
class Esp8266DateTimeProvider: public IDateTimeProvider {
2020
/* The time as a cstring in yyyyMMddHHmmss format. Is written to within and
2121
* returned by getDateTime(). */
22-
WiFiClient client2;
22+
WiFiClient client;
2323
//char dateTime[15];
2424
public:
2525
char dateTime[15];

src/esp8266/ESP8266AWSImplentations.cpp

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,43 @@ Esp8266HttpClient::Esp8266HttpClient() {
1414

1515
char* Esp8266HttpClient::send(const char* request, const char* serverUrl, int port) {
1616

17-
// WiFiClientSecure client;
17+
WiFiClientSecure sclient;
1818
Serial.println(serverUrl);
1919
Serial.println(port);
2020
Serial.println(request);
2121
Serial.println("");
2222
Serial.println("");
2323

24-
/* Arduino String to build the response with. */
25-
String responseBuilder = "Response: ";
26-
if (client.connect(serverUrl, port)) {
27-
/* Send the requests */
28-
client.print(request);
29-
client.print("\n");
30-
/* Read the request into responseBuilder. */
31-
delay(delayTime);
32-
Serial.println("<");
33-
while (client.available()) {
34-
char c = client.read();
35-
responseBuilder.concat(c);
36-
Serial.print(".");
24+
//
25+
String response = "";
26+
if (sclient.connect(serverUrl, port)) {
27+
28+
// Send the request
29+
sclient.print(request);
30+
31+
// keep reading the response until it's finished
32+
while(sclient.connected()) {
33+
while(sclient.available()){
34+
char c = sclient.read();
35+
response.concat(c);
36+
Serial.print('.');
37+
}
3738
}
38-
Serial.println(">");
39-
client.stop();
39+
40+
// disconnect any open connections
41+
sclient.stop();
42+
4043
} else {
41-
client.stop();
42-
/* Error connecting. */
44+
// connection was unsuccessful
45+
sclient.stop();
4346
return "can't setup SSL connection";
4447
}
45-
/* Copy responseBuilder into char* */
46-
int len = responseBuilder.length();
47-
char* response = new char[len + 1]();
48-
responseBuilder.toCharArray(response, len + 1);
49-
return response;
48+
49+
// convert the string into a char and return
50+
int len = response.length();
51+
char* response_char = new char[len + 1]();
52+
response.toCharArray(response_char, len + 1);
53+
return response_char;
5054
}
5155

5256
bool Esp8266HttpClient::usesCurl() {
@@ -109,26 +113,26 @@ char* updateCurTime(void) {
109113
char utctimeraw[80];
110114
char* dpos;
111115

112-
WiFiClient client2;
113-
if (client2.connect(timeServer, 80)) {
116+
WiFiClient client;
117+
if (client.connect(timeServer, 80)) {
114118
//Send Request
115-
client2.println(timeServerGet);
116-
client2.println();
117-
while((!client2.available())&&(timeout_busy++<5000)){
119+
client.println(timeServerGet);
120+
client.println();
121+
while((!client.available())&&(timeout_busy++<5000)){
118122
// Wait until the client sends some data
119123
delay(1);
120124
}
121125

122126
// kill client if timeout
123127
if(timeout_busy>=5000) {
124-
client2.flush();
125-
client2.stop();
128+
client.flush();
129+
client.stop();
126130
Serial.println("timeout receiving timeserver data\n");
127131
return dateStamp;
128132
}
129133

130134
// read the http GET Response
131-
String req2 = client2.readString();
135+
String req2 = client.readString();
132136
// Serial.println("");
133137
// Serial.println("");
134138
// Serial.print(req2);
@@ -137,8 +141,8 @@ char* updateCurTime(void) {
137141

138142
// close connection
139143
delay(1);
140-
client2.flush();
141-
client2.stop();
144+
client.flush();
145+
client.stop();
142146

143147
ipos = req2.indexOf("Date:");
144148
if(ipos>0) {

0 commit comments

Comments
 (0)