Skip to content

Issue with VPN connection on ESP32 using WireGuard library #36

@mauro-midolo

Description

@mauro-midolo

I am encountering difficulties in establishing a VPN connection using the WireGuard-ESP32 library on an ESP32 device. I have included my sample code below, but it seems not to be functioning as expected.

#include "WiFi.h"
#include <WireGuard-ESP32.h>
#include <ESPping.h>

static WireGuard wg;
IPAddress targetIP(10, 210, 144, 3);  // Destination IP address

char private_key[] = "<PRIVATE_KEY>";  // [Interface] PrivateKey
IPAddress local_ip(10, 210, 144, 9);            // [Interface] Address
char public_key[] = "<PUBLIC_KEY>";     // [Peer] PublicKey
char endpoint_address[] = "vpn.myhost.com";    // [Peer] Endpoint
int endpoint_port = 51820;              // [Peer] Endpoint

void setup() {
  // ... (WiFi initialization code)

  Serial.println("\nConnected to WiFi network");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Adjusting system time...");
  configTime(9 * 60 * 60, 0, "ntp.jst.mfeed.ad.jp", "ntp.nict.jp", "time.google.com");
  Serial.println("\Connecting to VPN network");

  // Initializing WireGuard connection
  wg.begin(
    local_ip,
    private_key,
    endpoint_address,
    public_key,
    endpoint_port);
  Serial.println("\nConnected to VPN network");
}

void loop() {
  // ... (Loop code)

  bool pingResult = Ping.ping(targetIP);

  if (pingResult) {
    Serial.print("Response from ");
    Serial.print(targetIP);
    Serial.print(" in ");
    Serial.print(Ping.averageTime());
    Serial.println(" ms");
  } else {
    Serial.println("Ping failed");
  }

  // ... (Other commands in the loop)
}

I have followed the WiFi connection process and WireGuard initialization as per the documentation, but it seems that the VPN connection fails to establish correctly. I have also included code for pinging a destination IP address.

Thank you for your assistance in resolving this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions