Skip to content

Commit

Permalink
Merge pull request #23 from Company-of-Things/21-esp32-http-support
Browse files Browse the repository at this point in the history
21 esp32 http support
  • Loading branch information
Bissas authored Nov 24, 2024
2 parents 999d010 + f39572e commit ca6949d
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v4
- uses: arduino/arduino-lint-action@v1
with:
library-manager: submit
library-manager: update
compliance: strict

build-for-arduino:
Expand Down Expand Up @@ -58,4 +58,4 @@ jobs:
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
cli-compile-flags: |
- --warnings="none"
libraries: ${{env.GLOBAL_LIBS}}
libraries: ${{env.GLOBAL_LIBS}}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ Currently supports:

| | WiFi/WebSockets | WiFi/HTTP |
|--------------|------------------|-----------|
| ESP32 || |
| Arduino WiFi |||
| ESP32 || |
| Arduino WiFi |||
1 change: 1 addition & 0 deletions src/deploii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void Deploii::loop() {

DeploiiHandler* Deploii::selectHandler() {
if (_medium == Medium::WiFi && _protocol == Protocol::WebSockets) return new DeploiiHandlerWiFiWS(_debug);
if (_medium == Medium::WiFi && _protocol == Protocol::HTTP) return new DeploiiHandlerWiFiHTTP(_debug);

return new DeploiiHandler(_debug);
}
Expand Down
12 changes: 6 additions & 6 deletions src/deploii.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef Deploii_h
#define Deploii_h
#ifndef DEPLOII_h
#define DEPLOII_h

#include "Arduino.h"
#include <MsgPack.h>
Expand Down Expand Up @@ -39,9 +39,9 @@ class Deploii {
void connect();
void connect(char* ssid,
const char* pwd,
const char* host = Deploii_HOST,
const int port = Deploii_PORT,
const char* url = Deploii_WS_URL,
const char* host = DEPLOII_HOST,
const int port = DEPLOII_PORT,
const char* url = DEPLOII_WS_URL,
bool ssl = true);

void interval(int intervalLength, void (*cb)(void));
Expand All @@ -61,4 +61,4 @@ class Deploii {

#include "deploii.tpp"

#endif
#endif
32 changes: 32 additions & 0 deletions src/deploii_certs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
static const char* buypass_cert =
"-----BEGIN CERTIFICATE-----\n"
"MIIFWTCCA0GgAwIBAgIBAjANBgkqhkiG9w0BAQsFADBOMQswCQYDVQQGEwJOTzEd\n"
"MBsGA1UECgwUQnV5cGFzcyBBUy05ODMxNjMzMjcxIDAeBgNVBAMMF0J1eXBhc3Mg\n"
"Q2xhc3MgMiBSb290IENBMB4XDTEwMTAyNjA4MzgwM1oXDTQwMTAyNjA4MzgwM1ow\n"
"TjELMAkGA1UEBhMCTk8xHTAbBgNVBAoMFEJ1eXBhc3MgQVMtOTgzMTYzMzI3MSAw\n"
"HgYDVQQDDBdCdXlwYXNzIENsYXNzIDIgUm9vdCBDQTCCAiIwDQYJKoZIhvcNAQEB\n"
"BQADggIPADCCAgoCggIBANfHXvfBB9R3+0Mh9PT1aeTuMgHbo4Yf5FkNuud1g1Lr\n"
"6hxhFUi7HQfKjK6w3Jad6sNgkoaCKHOcVgb/S2TwDCo3SbXlzwx87vFKu3MwZfPV\n"
"L4O2fuPn9Z6rYPnT8Z2SdIrkHJasW4DptfQxh6NR/Md+oW+OU3fUl8FVM5I+GC91\n"
"1K2GScuVr1QGbNgGE41b/+EmGVnAJLqBcXmQRFBoJJRfuLMR8SlBYaNByyM21cHx\n"
"MlAQTn/0hpPshNOOvEu/XAFOBz3cFIqUCqTqc/sLUegTBxj6DvEr0VQVfTzh97QZ\n"
"QmdiXnfgolXsttlpF9U6r0TtSsWe5HonfOV116rLJeffawrbD02TTqigzXsu8lkB\n"
"arcNuAeBfos4GzjmCleZPe4h6KP1DBbdi+w0jpwqHAAVF41og9JwnxgIzR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K+efrCh2gdC/1cacwG0Jp9VJkqyTkaGa9LKkPz\n"
"Y11aWOIv4x3kqdbQCtCev9eBCfHJxyYNrJgWVqA=\n"
"-----END CERTIFICATE-----\n";
52 changes: 43 additions & 9 deletions src/handler/deploii_handler.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#ifndef Deploii_handler_h
#define Deploii_handler_h
#ifndef DEPLOII_handler_h
#define DEPLOII_handler_h

#include "deploii_certs.h"

#include "Arduino.h"
#include <WiFi.h>
#if defined(ESP32)
#include <WebSocketsClient.h>
#include <HTTPClient.h>
#endif

/*
Constants for connection
*/

#define Deploii_HOST "deploii.no"
#define Deploii_PORT 443
#define Deploii_WS_URL "/ws"
#define DEPLOII_HOST "deploii.no"
#define DEPLOII_PORT 443
#define DEPLOII_WS_URL "/mcu/ws"
#define DEPLOII_HTTP_URL "/mcu/data"

#define DEPLOII_WIFI_RECONNECT_TIME 1000
#define DEPLOII_WS_RECONNECT_TIME 2000

class DeploiiHandler {
public:
Expand All @@ -26,9 +33,9 @@ class DeploiiHandler {
virtual void connect(char* boardID,
char* ssid,
const char* pwd,
const char* host = Deploii_HOST,
const int port = Deploii_PORT,
const char* url = Deploii_WS_URL,
const char* host = DEPLOII_HOST,
const int port = DEPLOII_PORT,
const char* url = DEPLOII_WS_URL,
bool ssl = true);

private:
Expand Down Expand Up @@ -68,4 +75,31 @@ class DeploiiHandlerWiFiWS : public DeploiiHandler {
#endif
};

#endif
class DeploiiHandlerWiFiHTTP : public DeploiiHandler {
public:
DeploiiHandlerWiFiHTTP(bool debug = false);
~DeploiiHandlerWiFiHTTP();

virtual void send(const uint8_t* data, size_t size);
virtual void loop();
virtual void connect(char* boardID,
char* ssid,
const char* pwd,
const char* host,
const int port,
const char* url,
bool ssl);

private:
bool _debug;
void connectWiFi(char* ssid, const char* pwd);

#if defined(ESP32)
HTTPClient _http;
#elif defined(ARDUINO)

#else

#endif
};
#endif
78 changes: 78 additions & 0 deletions src/handler/deploii_handler_WiFi_HTTP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "deploii_handler.h"

DeploiiHandlerWiFiHTTP::DeploiiHandlerWiFiHTTP(bool debug)
#if defined(ESP32)
: _http(), _debug(debug)
#elif defined(ARDUINO)
: _debug(debug)
#else
: _debug(debug)
#endif
{
}

DeploiiHandlerWiFiHTTP::~DeploiiHandlerWiFiHTTP() {
#if defined(ESP32)
#elif defined(ARDUINO)
#else
#endif
}

void DeploiiHandlerWiFiHTTP::connect(
char* boardID,
char* ssid,
const char* pwd,
const char* host,
const int port,
const char* url,
bool ssl) {
connectWiFi(ssid, pwd);
#if defined(ESP32)
if (ssl)
_http.begin(host, port, url, buypass_cert);
else
_http.begin(host, port, url);

_http.addHeader("Authorization", boardID, false, false);
#elif defined(ARDUINO)
#else
#endif
}

void DeploiiHandlerWiFiHTTP::loop() {
}

void DeploiiHandlerWiFiHTTP::send(const uint8_t* data, size_t size) {
#if defined(ESP32)
_http.POST((uint8_t*)data, size);
#elif defined(ARDUINO)
#else
#endif
}

#if defined(ESP32)

void DeploiiHandlerWiFiHTTP::connectWiFi(char* ssid, const char* pwd) {
if (_debug) Serial.println("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pwd);
while (WiFi.status() != WL_CONNECTED) {
delay(DEPLOII_WIFI_RECONNECT_TIME);
if (_debug) {
Serial.println("Connecting to WiFi");
}
}
if (_debug) {
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
if (_debug) Serial.println(WiFi.localIP());
}
#elif defined(ARDUINO)

void DeploiiHandlerWiFiHTTP::connectWiFi(char* ssid, const char* pwd) {
while (WiFi.begin(ssid, pwd) != WL_CONNECTED) delay(DEPLOII_WIFI_RECONNECT_TIME);
}
#else
void DeploiiHandlerWiFiHTTP::connectWiFi(char* ssid, const char* pwd) {}
#endif
12 changes: 3 additions & 9 deletions src/handler/deploii_handler_WiFi_WS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
Deploii handler for communication using WiFI and WebSockets
*/

/*
Constants
*/
#define Deploii_WIFI_RECONNECT_TIME 1000
#define DEPLOII_WS_RECONNECT_TIME 2000

/*
Class definitions
*/
Expand Down Expand Up @@ -85,7 +79,7 @@ void DeploiiHandlerWiFiWS::connectWiFi(char* ssid, const char* pwd) {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pwd);
while (WiFi.status() != WL_CONNECTED) {
delay(Deploii_WIFI_RECONNECT_TIME);
delay(DEPLOII_WIFI_RECONNECT_TIME);
if (_debug) {
Serial.println("Connecting to WiFi");
}
Expand Down Expand Up @@ -113,7 +107,7 @@ void DeploiiHandlerWiFiWS::connectWS(char* boardID, const char* host, const int
#elif defined(ARDUINO)

void DeploiiHandlerWiFiWS::connectWiFi(char* ssid, const char* pwd) {
while (WiFi.begin(ssid, pwd) != WL_CONNECTED) delay(Deploii_WIFI_RECONNECT_TIME);
while (WiFi.begin(ssid, pwd) != WL_CONNECTED) delay(DEPLOII_WIFI_RECONNECT_TIME);
}

void DeploiiHandlerWiFiWS::connectWS(char* boardID, const char* host, const int port, const char* url, bool ssl) {
Expand All @@ -125,4 +119,4 @@ void DeploiiHandlerWiFiWS::connectWiFi(char* ssid, const char* pwd) {}
void DeploiiHandlerWiFiWS::connectWS(char* boardID, const char* host, const int port, const char* url, bool ssl) {
}

#endif
#endif

0 comments on commit ca6949d

Please sign in to comment.