diff --git a/.vscode/settings.json b/.vscode/settings.json index ae30acc..feebf3f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -84,6 +84,7 @@ "list": "cpp", "xhash": "cpp", "xstring": "cpp", - "xutility": "cpp" + "xutility": "cpp", + "*.tpp": "cpp" } } diff --git a/examples/deploii-interval/deploii-interval.ino b/examples/deploii-interval/deploii-interval.ino new file mode 100644 index 0000000..a5a36b6 --- /dev/null +++ b/examples/deploii-interval/deploii-interval.ino @@ -0,0 +1,21 @@ +#include + +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); +} \ No newline at end of file diff --git a/examples/deploii-send/deploii-send.ino b/examples/deploii-send/deploii-send.ino new file mode 100644 index 0000000..010d520 --- /dev/null +++ b/examples/deploii-send/deploii-send.ino @@ -0,0 +1,16 @@ +#include + +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(); +} diff --git a/src/deploii.cpp b/src/deploii.cpp index 4d50111..cca1cf5 100644 --- a/src/deploii.cpp +++ b/src/deploii.cpp @@ -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() { @@ -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() { + for (int i = 0; i < _intervalCount; i++) { + unsigned long currentTime = millis(); + if (currentTime >= _intervals[i].previousTime + _intervals[i].intervalLength) { + _intervals[i].cb(); + _intervals[i].previousTime = currentTime; + } + } } \ No newline at end of file diff --git a/src/deploii.h b/src/deploii.h index 0fb6e9b..64852cd 100644 --- a/src/deploii.h +++ b/src/deploii.h @@ -2,9 +2,8 @@ #define Deploii_h #include "Arduino.h" -#include "./handler/deploii_handler.h" - #include +#include "./handler/deploii_handler.h" enum class Medium { None, @@ -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); @@ -39,6 +44,8 @@ class Deploii { const char* url = Deploii_WS_URL, bool ssl = true); + void interval(int intervalLength, void (*cb)(void)); + private: Medium _medium; Protocol _protocol; @@ -46,6 +53,10 @@ class Deploii { char* _boardID; DeploiiHandler* _handler; DeploiiHandler* selectHandler(); + + void checkIntervals(); + struct Interval* _intervals; + int _intervalCount; }; #include "deploii.tpp" diff --git a/src/handler/deploii_handler.h b/src/handler/deploii_handler.h index 1ca7d59..e287315 100644 --- a/src/handler/deploii_handler.h +++ b/src/handler/deploii_handler.h @@ -1,8 +1,11 @@ #ifndef Deploii_handler_h #define Deploii_handler_h +#include "Arduino.h" #include +#if defined(ESP32) #include +#endif /* Constants for connection diff --git a/src/handler/deploii_handler_WiFi_WS.cpp b/src/handler/deploii_handler_WiFi_WS.cpp index 9d9570b..feae884 100644 --- a/src/handler/deploii_handler_WiFi_WS.cpp +++ b/src/handler/deploii_handler_WiFi_WS.cpp @@ -8,6 +8,7 @@ Constants */ #define Deploii_WIFI_RECONNECT_TIME 1000 +#define DEPLOII_WS_RECONNECT_TIME 2000 /* Class definitions @@ -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) { @@ -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