Skip to content

Commit 47ad6e8

Browse files
author
Diego Ernst
authored
add support for esp32 (#4)
1 parent 8b8c209 commit 47ad6e8

21 files changed

+432
-120
lines changed

examples/Simple/Simple.ino

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public:
88

99
class WifiConfig: public Configuration {
1010
public:
11-
persistentStringVar(ssid, "Antel");
12-
persistentStringVar(password, "4532xyzh");
11+
persistentStringVar(ssid, "ssid");
12+
persistentStringVar(password, "password");
1313
subconfig(Network, network);
1414
};
1515

@@ -37,10 +37,6 @@ public:
3737

3838
void setup() {
3939

40-
configureLed(2);
41-
42-
Serial.setDebugOutput(true);
43-
4440
Bleeper
4541
.verbose()
4642
.configuration
@@ -51,9 +47,9 @@ void setup() {
5147
.addDefaultWebServer()
5248
.done()
5349
.connection
54-
.setMultipleConnections({
55-
new AP(),
56-
new Wifi(&C.wifi.ssid, &C.wifi.password)
50+
.setSingleConnectionFromPriorityList({
51+
new Wifi(&C.wifi.ssid, &C.wifi.password),
52+
new AP()
5753
})
5854
.done()
5955
.storage

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"url": "https://github.com/neman-io/Bleeper.git"
99
},
1010
"frameworks": "arduino",
11-
"platforms": "espressif8266",
11+
"platforms": ["espressif8266", "espressif32"],
1212
"version": "1.0.1"
1313
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ sentence=A library to store generic configurations.
66
paragraph=Easily define your configuration hierarchy, the type of each property and weather or not it should be persisted.
77
category=Other
88
url=https://github.com/neman-io/Bleeper.git
9-
architectures=esp8266
9+
architectures=esp8266,esp32
1010
includes=Bleeper.h

platformio.ini

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
# Automatic targets - enable auto-uploading
1818
# targets = upload
1919

20-
[env:nodemcuv2]
20+
[env:esp8266dev]
2121
platform = espressif
2222
framework = arduino
2323
board = nodemcuv2
24+
25+
[env:esp32dev]
26+
platform = espressif32
27+
framework = arduino
28+
board = esp32doit-devkit-v1

src/Bleeper/Bleeper+ConfigurationInterface.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
#include "ConfigurationInterface/WebServer/DefaultWebServer.h"
1+
#ifdef ESP8266
2+
#include "ConfigurationInterface/WebServer/ESP8266/ESP8266DefaultWebServer.h"
3+
#elif ESP32
4+
#include "ConfigurationInterface/WebServer/ESP32/ESP32DefaultWebServer.h"
5+
#endif
6+
27
#include "Bleeper/BleeperClass.h"
38
#include "Helpers/macros.h"
49

@@ -10,6 +15,10 @@ FunctionsContainer& FunctionsContainer::add(ConfigurationInterface* i) {
1015
}
1116

1217
FunctionsContainer& FunctionsContainer::addDefaultWebServer() {
13-
Bleeper.userProperties.interfaces.push_back(new DefaultWebServer(80));
18+
#ifdef ESP8266
19+
Bleeper.userProperties.interfaces.push_back(new ESP8266DefaultWebServer(80));
20+
#elif ESP32
21+
Bleeper.userProperties.interfaces.push_back(new ESP32DefaultWebServer(80));
22+
#endif
1423
return *this;
1524
}

src/Bleeper/Bleeper+Connection.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ FunctionsContainer& FunctionsContainer::setSingleConnectionFromPriorityList(std:
1717
Bleeper.userProperties.connection = new OneOfMultipleConnection(connections);
1818
return *this;
1919
}
20+
21+
Connection* FunctionsContainer::get() {
22+
return Bleeper.userProperties.connection;
23+
}

src/Bleeper/BleeperClass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#include "Bleeper/BleeperClass.h"
22
#include "Helpers/Logger.h"
33
#include "Helpers/macros.h"
4+
5+
#ifdef ESP8266
46
#include <ESP8266WiFi.h>
7+
#elif ESP32
8+
#include <WiFi.h>
9+
#endif
510

611
BleeperClass* BleeperClass::sharedInstance = NULL;
712

src/Bleeper/BleeperClass.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class BleeperClass: public Initable {
4747
class ConnectionFunctions: public Chainable {
4848
public:
4949
ConnectionFunctions& set(Connection*);
50+
Connection* get();
5051
ConnectionFunctions& setMultipleConnections(std::vector<Connection*> connections);
5152
ConnectionFunctions& setSingleConnectionFromPriorityList(std::vector<Connection*> connections);
5253
} connection;

src/ConfigurationInterface/WebServer/DefaultWebServer.cpp

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#ifdef ESP32
2+
3+
#include "ConfigurationInterface/WebServer/ESP32/ESP32DefaultWebServer.h"
4+
#include "Bleeper/BleeperClass.h"
5+
#include "Helpers/Logger.h"
6+
#include <functional>
7+
8+
ESP32DefaultWebServer::ESP32DefaultWebServer(int port): WebServer(port) {
9+
server = NULL;
10+
}
11+
12+
void ESP32DefaultWebServer::init() {
13+
server = new WiFiServer(port);
14+
using std::placeholders::_1;
15+
using std::placeholders::_2;
16+
registerRoute(
17+
GET,
18+
"/",
19+
std::bind(&ESP32DefaultWebServer::handleRoot, this, _1, _2)
20+
);
21+
registerRoute(
22+
GET,
23+
"/script",
24+
std::bind(&ESP32DefaultWebServer::handleScript, this, _1, _2)
25+
);
26+
registerRoute(
27+
GET,
28+
"/style",
29+
std::bind(&ESP32DefaultWebServer::handleStyle, this, _1, _2)
30+
);
31+
registerRoute(
32+
POST,
33+
"/save",
34+
std::bind(&ESP32DefaultWebServer::handleSave, this, _1, _2)
35+
);
36+
}
37+
38+
void ESP32DefaultWebServer::handle(){
39+
// This server only starts working when
40+
// some connection is active.
41+
// It's safe to call it multiple times.
42+
server->begin();
43+
44+
WiFiClient client = server->available();
45+
if (!client) return;
46+
Log("New client");
47+
HTTPRequest request;
48+
while (client.connected()) {
49+
if (request.isFinished()) {
50+
handleRequest(request, client);
51+
break;
52+
}
53+
if (client.available()) {
54+
char c = client.read();
55+
request.add(c);
56+
}
57+
}
58+
client.stop();
59+
Log("Client disconnected");
60+
}
61+
62+
void ESP32DefaultWebServer::handleRequest(HTTPRequest & request, WiFiClient & client) {
63+
RouteHandler* handler = getRequestHandler(request);
64+
if (!handler)
65+
handleError(client);
66+
67+
(*handler)(request, client);
68+
}
69+
70+
void ESP32DefaultWebServer::handleRoot(HTTPRequest &, WiFiClient & client){
71+
send(client, true, CONTENT_TYPE_HTML, buildPage(Bleeper.configuration.getVariables()).c_str());
72+
}
73+
74+
void ESP32DefaultWebServer::handleScript(HTTPRequest &, WiFiClient & client){
75+
send(client, true, CONTENT_TYPE_JS, getJS());
76+
}
77+
78+
void ESP32DefaultWebServer::handleStyle(HTTPRequest &, WiFiClient & client){
79+
send(client, true, CONTENT_TYPE_CSS, getStyle());
80+
}
81+
82+
void ESP32DefaultWebServer::handleSave(HTTPRequest & request, WiFiClient & client){
83+
send(client, true, "", NULL);
84+
saveValues(request.getArgs());
85+
}
86+
87+
void ESP32DefaultWebServer::handleError(WiFiClient & client) {
88+
send(client, false, "", NULL);
89+
}
90+
91+
void ESP32DefaultWebServer::send(WiFiClient & client, bool ok, String contentType, const char* data) {
92+
client.println(ok ? "HTTP/1.1 200 OK" : "HTTP/1.1 500 Internal Server Error");
93+
if (contentType != "") client.println("Content-type:" + contentType);
94+
client.println();
95+
if (data != NULL) client.print(data);
96+
client.println();
97+
}
98+
99+
void ESP32DefaultWebServer::registerRoute(HTTPMethod method, String route, RouteHandler handler) {
100+
router[String(method) + route] = handler;
101+
}
102+
103+
RouteHandler* ESP32DefaultWebServer::getRequestHandler(HTTPRequest & request) {
104+
auto key = request.getMethod() + request.getRoute();
105+
if (router.find(key) == router.end())
106+
return NULL;
107+
return &router[key];
108+
}
109+
110+
#endif // ESP32

0 commit comments

Comments
 (0)