Skip to content

Commit ce4ea6d

Browse files
committed
changed implementation to use the generic wifi libraries instead of esp8266 specific
1 parent c5f46b1 commit ce4ea6d

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

src/CloudStorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,4 @@ class BaseCloudStorage {
214214
}
215215
};
216216

217-
typedef BaseCloudStorage<http::Esp8266Request> CloudStorage;
217+
typedef BaseCloudStorage<http::GenericEspRequest> CloudStorage;

src/Http/EspClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

33
#include "Client.h"
4-
#include "Esp8266RequestImpl.h"
4+
#include "GenericEspRequestImpl.h"
55

66
namespace http {
7-
typedef Request<Esp8266RequestImpl> Esp8266Request;
7+
typedef Request<GenericEspRequestImpl> GenericEspRequest;
88
};

src/Http/GenericEspRequestImpl.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include "RequestInterface.h"
4+
#include <HTTPClient.h>
5+
6+
namespace http {
7+
class GenericEspRequestImpl : public RequestInterface {
8+
public:
9+
GenericEspRequestImpl() : RequestInterface() {}
10+
11+
void setUrl(String url) override {
12+
_http.begin(url);
13+
}
14+
15+
void setMethod(Method m) override {
16+
_method = m;
17+
}
18+
19+
void setBody(String body) override {
20+
this->_body = body;
21+
}
22+
23+
void addHeader(String key, String value) override {
24+
_http.addHeader(key, value);
25+
}
26+
27+
Response execute() override {
28+
int httpCode = -1;
29+
if(this->_method == Method::GET) httpCode = this->_http.sendRequest("GET", this->_body);
30+
else if(this->_method == Method::POST) httpCode = this->_http.sendRequest("POST", this->_body);
31+
32+
Response response;
33+
response.statusCode = httpCode;
34+
35+
if(httpCode == 200) response.body = this->_http.getString();
36+
else response.body = this->_http.errorToString(httpCode);
37+
38+
this->_http.end();
39+
40+
return response;
41+
}
42+
43+
virtual ~GenericEspRequestImpl() {}
44+
45+
private:
46+
Method _method;
47+
String _body, _url;
48+
HTTPClient _http;
49+
};
50+
};

src/WifiConnection.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#pragma once
22

3-
#include <ESP8266WiFi.h>
4-
#include <ESP8266WiFiMulti.h>
3+
#include <WiFi.h>
4+
#include <WiFiMulti.h>
55

66
namespace WifiConnection {
7-
ESP8266WiFiMulti WiFiMulti;
7+
WiFiMulti wifiMulti;
88

99
// Sets wifi mode to STA and tries to connect to a network
1010
void tryConnect(String ssid, String pass) {
1111
WiFi.mode(WIFI_STA);
12-
WiFiMulti.addAP(ssid.c_str(), pass.c_str());
12+
wifiMulti.addAP(ssid.c_str(), pass.c_str());
1313
}
1414

1515
// Method for checking if currently connected to wifi
1616
boolean isConnected() {
17-
return WiFiMulti.run() == WL_CONNECTED;
17+
return wifiMulti.run() == WL_CONNECTED;
1818
}
1919
};

0 commit comments

Comments
 (0)