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: wifi connection #10

Merged
merged 12 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions examples/deploii-connect-wifi/deploii-connect-wifi.ino
Bissas marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <deploii.h>

Deploii oi("BOARD ID");

void setup() {
Serial.begin(9600);
oi.connect_wifi("SSID", "PASSWORD");
}

void loop() {
oi.loop();
}
39 changes: 38 additions & 1 deletion src/deploii.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
#include "deploii.h"
#include "deploii.h"

Deploii::Deploii(char* boardID, Medium medium, Protocol protocol) {
Bissas marked this conversation as resolved.
Show resolved Hide resolved
_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
39 changes: 39 additions & 0 deletions src/handler/deploii_handler_WiFi_WS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#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);

//_ws.setAuthorization(boardID);
Bissas marked this conversation as resolved.
Show resolved Hide resolved

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);
}
Loading