Skip to content

Commit

Permalink
Merge pull request #10 from Company-of-Things/feat/wifi-connection
Browse files Browse the repository at this point in the history
feat: wifi connection
  • Loading branch information
Bissas authored Mar 10, 2024
2 parents 374cfb4 + 5258fe0 commit 1e6f535
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 6 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@ on:
- push
- pull_request

env:
GLOBAL_LIBS: |
- source-path: ./
- name: WiFi
- name: WebSockets
- name: Ethernet
jobs:
build-for-arduino:
runs-on: ubuntu-latest

strategy:
matrix:
fqbn:
- arduino:samd:mkrwifi1010
- arduino:renesas_uno:unor4wifi

steps:
- uses: actions/checkout@v3
- uses: arduino/compile-sketches@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
fqbn: ${{ matrix.fqbn }}
libraries: ${{env.GLOBAL_LIBS}}


build-for-esp32:
runs-on: ubuntu-latest

Expand All @@ -37,4 +47,5 @@ jobs:
- name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
cli-compile-flags: |
- --warnings="none"
- --warnings="none"
libraries: ${{env.GLOBAL_LIBS}}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.development
.development
ignore
21 changes: 21 additions & 0 deletions .vscode/c_cpp_properties.json
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
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-vscode.cpptools",
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 3, TabWidth: 3, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2, SortIncludes: false }",
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: google, UseTab: Never, IndentWidth: 3, TabWidth: 3, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2, SortIncludes: false }",
"files.associations": {
"map": "cpp",
"__bit_reference": "cpp",
Expand Down
11 changes: 11 additions & 0 deletions examples/deploii-connect-wifi/deploii-connect-wifi.ino
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();
}
38 changes: 37 additions & 1 deletion src/deploii.cpp
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();
}
42 changes: 41 additions & 1 deletion src/deploii.h
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
14 changes: 14 additions & 0 deletions src/handler/deploii_handler.cpp
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) {}
51 changes: 51 additions & 0 deletions src/handler/deploii_handler.h
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
37 changes: 37 additions & 0 deletions src/handler/deploii_handler_WiFi_WS.cpp
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);
}

0 comments on commit 1e6f535

Please sign in to comment.