-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Company-of-Things/feat/wifi-connection
feat: wifi connection
- Loading branch information
Showing
10 changed files
with
228 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.development | ||
.development | ||
ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "Win32", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"defines": [ | ||
"_DEBUG", | ||
"UNICODE", | ||
"_UNICODE" | ||
], | ||
"windowsSdkVersion": "10.0.22000.0", | ||
"compilerPath": "cl.exe", | ||
"cStandard": "c17", | ||
"cppStandard": "c++17", | ||
"intelliSenseMode": "windows-msvc-x64" | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <deploii.h> | ||
|
||
Deploii oi("BOARD-ID", Medium::WiFi, Protocol::WebSockets); | ||
|
||
void setup() { | ||
oi.connect("WiFi-SSID", "WiFi-PASSWORD"); | ||
} | ||
|
||
void loop() { | ||
oi.loop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
#include "deploii.h" | ||
#include "deploii.h" | ||
|
||
Deploii::Deploii(char* boardID, Medium medium, Protocol protocol) : _boardID(boardID), | ||
_medium(medium), | ||
_protocol(protocol), | ||
_handler(selectHandler()) { | ||
} | ||
|
||
Deploii::~Deploii() { | ||
delete _handler; | ||
} | ||
|
||
void Deploii::send() { | ||
} | ||
|
||
void Deploii::connect() { | ||
_handler->connect(); | ||
} | ||
|
||
void Deploii::connect(const char* ssid, | ||
const char* pwd, | ||
const char* host, | ||
const int port, | ||
const char* url, | ||
bool ssl) { | ||
_handler->connect(_boardID, ssid, pwd, host, port, url, ssl); | ||
} | ||
|
||
void Deploii::loop() { | ||
_handler->loop(); | ||
} | ||
|
||
DeploiiHandler* Deploii::selectHandler() { | ||
if (_medium == Medium::WiFi && _protocol == Protocol::WebSockets) return new DeploiiHandlerWiFiWS(); | ||
|
||
return new DeploiiHandler(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,43 @@ | ||
#pragma once | ||
#ifndef Deploii_h | ||
#define Deploii_h | ||
|
||
#include "Arduino.h" | ||
#include "./handler/deploii_handler.h" | ||
|
||
enum class Medium { | ||
None, | ||
WiFi, | ||
NarrowBand | ||
}; | ||
|
||
enum class Protocol { | ||
None, | ||
WebSockets, | ||
HTTP, | ||
MQTT | ||
}; | ||
|
||
class Deploii { | ||
public: | ||
Deploii(char* boardID, Medium medium, Protocol protocol); | ||
~Deploii(); | ||
|
||
void send(); | ||
void loop(); | ||
void connect(); | ||
void connect(const char* ssid, | ||
const char* pwd, | ||
const char* host = Deploii_HOST, | ||
const int port = Deploii_PORT, | ||
const char* url = Deploii_WS_URL, | ||
bool ssl = true); | ||
|
||
private: | ||
Medium _medium; | ||
Protocol _protocol; | ||
char* _boardID; | ||
DeploiiHandler* _handler; | ||
DeploiiHandler* selectHandler(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "deploii_handler.h" | ||
|
||
DeploiiHandler::DeploiiHandler() {} | ||
|
||
void DeploiiHandler::send() {} | ||
void DeploiiHandler::loop() {} | ||
void DeploiiHandler::connect() {} | ||
void DeploiiHandler::connect(char* boardID, | ||
const char* ssid, | ||
const char* pwd, | ||
const char* host, | ||
const int port, | ||
const char* url, | ||
bool ssl) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef Deploii_handler_h | ||
#define Deploii_handler_h | ||
|
||
/* | ||
Constants for connection | ||
*/ | ||
|
||
#define Deploii_HOST "deploii.no" | ||
#define Deploii_PORT 443 | ||
#define Deploii_WS_URL "/ws" | ||
|
||
#include <WiFi.h> | ||
#include <WebSocketsClient.h> | ||
|
||
class DeploiiHandler { | ||
public: | ||
DeploiiHandler(); | ||
|
||
virtual void send(); | ||
virtual void loop(); | ||
virtual void connect(); | ||
virtual void connect(char* boardID, | ||
const char* ssid, | ||
const char* pwd, | ||
const char* host = Deploii_HOST, | ||
const int port = Deploii_PORT, | ||
const char* url = Deploii_WS_URL, | ||
bool ssl = true); | ||
|
||
private: | ||
}; | ||
|
||
class DeploiiHandlerWiFiWS : public DeploiiHandler { | ||
public: | ||
DeploiiHandlerWiFiWS(); | ||
|
||
virtual void send(); | ||
virtual void loop(); | ||
virtual void connect(char* boardID, | ||
const char* ssid, | ||
const char* pwd, | ||
const char* host, | ||
const int port, | ||
const char* url, | ||
bool ssl); | ||
|
||
private: | ||
WebSocketsClient _ws; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "deploii_handler.h" | ||
|
||
/* | ||
Deploii handler for communication using WiFI and WebSockets | ||
*/ | ||
|
||
DeploiiHandlerWiFiWS::DeploiiHandlerWiFiWS() : _ws() { | ||
} | ||
|
||
void DeploiiHandlerWiFiWS::send() { | ||
} | ||
|
||
void DeploiiHandlerWiFiWS::loop() { | ||
_ws.loop(); | ||
} | ||
|
||
void DeploiiHandlerWiFiWS::connect( | ||
char* boardID, | ||
const char* ssid, | ||
const char* pwd, | ||
const char* host, | ||
const int port, | ||
const char* url, | ||
bool ssl) { | ||
WiFi.mode(WIFI_STA); | ||
WiFi.begin(ssid, pwd); | ||
while (WiFi.status() != WL_CONNECTED) delay(1000); | ||
|
||
char authHeader[40]; | ||
sprintf(authHeader, "%s%s", "Authorization: ", boardID); | ||
_ws.setExtraHeaders(authHeader); | ||
|
||
if (ssl) | ||
_ws.beginSSL(host, port, url); | ||
else | ||
_ws.begin(host, port, url); | ||
} |