Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: interval function #16

Merged
merged 15 commits into from
Sep 1, 2024
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"list": "cpp",
"xhash": "cpp",
"xstring": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"*.tpp": "cpp"
}
}
21 changes: 21 additions & 0 deletions examples/deploii-interval/deploii-interval.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <deploii.h>

Deploii oi("Board ID", Medium::WiFi, Protocol::WebSockets);

int myData = 10;
int myDataArray[] = {1, 2, 3};

void setup() {
oi.connect("WiFi SSID", "WiFi Password");

oi.interval(3000, sendData);
}

void loop() {
oi.loop();
}

void sendData() {
oi.send("DataStream ID", myData);
oi.send("DataStream ID", myDataArray);
}
16 changes: 16 additions & 0 deletions examples/deploii-send/deploii-send.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <deploii.h>

Deploii oi("Board ID", Medium::WiFi, Protocol::WebSockets);

int myData = 10;
int myDataArray[] = {1, 2, 3};

void setup() {
oi.connect("WiFi SSID", "WiFi Password");
oi.send("DataStream ID", myData);
oi.send("DataStream ID", myDataArray);
}

void loop() {
oi.loop();
}
22 changes: 22 additions & 0 deletions src/deploii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ Deploii::Deploii(char* boardID, Medium medium, Protocol protocol, bool debug) :
_medium(medium),
_protocol(protocol),
_debug(debug),
_intervals((struct Interval*)malloc(0)),
_intervalCount(0),
_handler(selectHandler()) {
}

Deploii::~Deploii() {
delete _handler;
free(_intervals);
}

void Deploii::connect() {
Expand All @@ -26,10 +29,29 @@ void Deploii::connect(char* ssid,

void Deploii::loop() {
_handler->loop();
checkIntervals();
}

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

return new DeploiiHandler(_debug);
}

void Deploii::interval(int intervalLength, void (*cb)(void)) {
_intervals = (struct Interval*)realloc(_intervals, sizeof(struct Interval) * (_intervalCount + 1));
_intervals[_intervalCount].intervalLength = intervalLength;
_intervals[_intervalCount].cb = cb;
_intervals[_intervalCount].previousTime = millis();
_intervalCount++;
}

void Deploii::checkIntervals() {
unsigned long currentTime = millis();
AasmundN marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < _intervalCount; i++) {
if (currentTime >= _intervals[i].previousTime + _intervals[i].intervalLength) {
_intervals[i].cb();
_intervals[i].previousTime = currentTime;
}
}
}
15 changes: 13 additions & 2 deletions src/deploii.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#define Deploii_h

#include "Arduino.h"
#include "./handler/deploii_handler.h"

#include <MsgPack.h>
#include "./handler/deploii_handler.h"

enum class Medium {
None,
Expand All @@ -19,6 +18,12 @@ enum class Protocol {
MQTT
};

struct Interval {
int intervalLength;
int previousTime;
void (*cb)(void);
};

class Deploii {
public:
Deploii(char* boardID, Medium medium, Protocol protocol, bool debug = false);
Expand All @@ -39,13 +44,19 @@ class Deploii {
const char* url = Deploii_WS_URL,
bool ssl = true);

void interval(int intervalLength, void (*cb)(void));

private:
Medium _medium;
Protocol _protocol;
bool _debug;
char* _boardID;
DeploiiHandler* _handler;
DeploiiHandler* selectHandler();

void checkIntervals();
struct Interval* _intervals;
int _intervalCount;
};

#include "deploii.tpp"
Expand Down
3 changes: 3 additions & 0 deletions src/handler/deploii_handler.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef Deploii_handler_h
#define Deploii_handler_h

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

/*
Constants for connection
Expand Down
7 changes: 5 additions & 2 deletions src/handler/deploii_handler_WiFi_WS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Constants
*/
#define Deploii_WIFI_RECONNECT_TIME 1000
#define DEPLOII_WS_RECONNECT_TIME 2000

/*
Class definitions
Expand Down Expand Up @@ -80,6 +81,7 @@ void DeploiiHandlerWiFiWS::connect(
#if defined(ESP32)

void DeploiiHandlerWiFiWS::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) {
Expand All @@ -90,14 +92,15 @@ void DeploiiHandlerWiFiWS::connectWiFi(char* ssid, const char* pwd) {
}
if (_debug) {
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
if (_debug) Serial.println(WiFi.localIP());
}

void DeploiiHandlerWiFiWS::connectWS(char* boardID, const char* host, const int port, const char* url, bool ssl) {
char authHeader[40];
char authHeader[60];
sprintf(authHeader, "%s%s", "Authorization: ", boardID);
_ws.setExtraHeaders(authHeader);

if (ssl)
_ws.beginSSL(host, port, url);
else
Expand Down
Loading