Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions WST-FC/include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ enum DroneStatus
BROKEN
};

#define USE_WIREGUARD 1

#if USE_WIREGUARD
#define WG_LOCAL_IP "10.0.0.2"
#define WG_PRIVATE_KEY "KLUCZ_PRYWATNY_ESP32="
#define WG_PEER_ADDRESS "198.51.100.1"
#define WG_PEER_PORT 51820
#define WG_PEER_PUBLIC_KEY "KLUCZ_PUBLICZNY_SERWERA="
#endif

#endif
6 changes: 5 additions & 1 deletion WST-FC/include/DCMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ class DCMotor : public IActuator
private:
int _pinIN1, _pinIN2, _pinEN;
int _pwmChannel;
bool _revDir;

public:
DCMotor(int pinIN1, int pinIN2, int pinEN, int pwmChannel)
DCMotor(int pinIN1, int pinIN2, int pinEN, int pwmChannel, bool revDir = false)
{
_pinIN1 = pinIN1;
_pinIN2 = pinIN2;
_pinEN = pinEN;
_pwmChannel = pwmChannel;
_revDir = revDir;
}

void Init() override
Expand All @@ -32,6 +34,8 @@ class DCMotor : public IActuator
int pwmValue = map(abs(speed), 0, 1000, 0, 255);
if (pwmValue > 255)
pwmValue = 255;

if(_revDir == true) speed = -speed;

if (speed > 0)
{
Expand Down
10 changes: 10 additions & 0 deletions WST-FC/include/communicationModules/CommunicationWiFiUDPModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "DroneData.h"
#include "ICommunicationInterface.h"
#include "Configuration.h"
#ifdef USE_WIREGUARD
#include <WireGuard-ESP32.h>
#endif
class CommunicationWiFiUDPModule : public ICommunicationInterface{
private:
DroneControlData *sharedData;
Expand All @@ -21,6 +24,13 @@ class CommunicationWiFiUDPModule : public ICommunicationInterface{
IPAddress remoteIP;
unsigned int remotePort {0};

#ifdef USE_WIREGUARD
WireGuard wg;
unsigned long lastKeepaliveTime = 0;
const unsigned long KEEPALIVE_INTERVAL = 20000;
const uint8_t KEEPALIVE_BYTE = 0xFF;
#endif

public:
CommunicationWiFiUDPModule(DroneControlData *dataPtr, unsigned int port, DroneStatus *status);

Expand Down
2 changes: 2 additions & 0 deletions WST-FC/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ lib_deps =
br3ttb/PID@^1.2.1
adafruit/Adafruit ADXL345@^1.3.4
adafruit/Adafruit Unified Sensor@^1.1.14
ciniml/WireGuard-ESP32 @ ^0.1.5

[env:esp32-c3-super-mini]
platform = espressif32
Expand All @@ -38,3 +39,4 @@ lib_deps =
br3ttb/PID@^1.2.1
adafruit/Adafruit ADXL345@^1.3.4
adafruit/Adafruit Unified Sensor@^1.1.14
ciniml/WireGuard-ESP32 @ ^0.1.5
40 changes: 40 additions & 0 deletions WST-FC/src/communicationModules/CommunicationWiFiUDPModule.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "communicationModules\CommunicationWiFiUDPModule.h"
#include "Configuration.h"


CommunicationWiFiUDPModule::CommunicationWiFiUDPModule(DroneControlData *dataPtr, unsigned int port, DroneStatus *status)
{
sharedData = dataPtr;
Expand All @@ -19,6 +20,31 @@ void CommunicationWiFiUDPModule::Init()
delay(500);
}
Serial.print("Connected!");

#if USE_WIREGUARD
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2)
{
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("\nTime synchronized!");

IPAddress localIP;
localIP.fromString(WG_LOCAL_IP);

wg.begin(
localIP,
WG_PRIVATE_KEY,
WG_PEER_ADDRESS,
WG_PEER_PUBLIC_KEY,
WG_PEER_PORT
);
Serial.println("WireGuard initialized!");
#endif

udp.begin(localPort);
}
void CommunicationWiFiUDPModule::Loop()
Expand Down Expand Up @@ -50,6 +76,17 @@ void CommunicationWiFiUDPModule::Loop()
{
*droneStatus = WORKS;
}

#if USE_WIREGUARD
if (remotePort != 0 && (millis() - lastKeepaliveTime > KEEPALIVE_INTERVAL))
{
udp.beginPacket(remoteIP, remotePort);
udp.write(&KEEPALIVE_BYTE, 1);
udp.endPacket();

lastKeepaliveTime = millis();
}
#endif
}

void CommunicationWiFiUDPModule::SendData(SensorsData* data)
Expand All @@ -58,4 +95,7 @@ void CommunicationWiFiUDPModule::SendData(SensorsData* data)
udp.beginPacket(remoteIP, remotePort);
udp.write((const uint8_t*)data, sizeof(SensorsData));
udp.endPacket();
#if USE_WIREGUARD
lastKeepaliveTime = millis();
#endif
}