-
Notifications
You must be signed in to change notification settings - Fork 94
Closed
Description
Dear @ciniml ,
In my project, I am trying connect ESP32 to HA with wireguard.
VPN tunnel working fine. In WG addon, I see handshake, I can connect via WG with MQTT explorer.
In ESP32 I use example of pubsub client and I get return code from pubsubclient -2 ( the network connection failed )
#include <WiFi.h>
#include "PubSubClient.h"
#include <WireGuard-ESP32.h>
#define ELMAX_DNS "haiot.xxxxxxxx.sk" // secured
// WireGuard configuration --- UPDATE this configuration from JSON
char private_key[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // [Interface] PrivateKey
IPAddress local_ip( 10 , 10 , 10 , 10 ); // [Interface] Address
char public_key[] = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"; // [Peer] PublicKey
char endpoint_address[] = ELMAX_DNS; // [Peer] Endpoint
int endpoint_port = 51820; // [Peer] Endpoint
static WireGuard wg;
const char* ssid = "proasnet";
const char* password = "zzzzzzzz";
int port = 1883;
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
WiFiClient espClient;
PubSubClient client( ELMAX_DNS , 1883 , callback , espClient );
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
wifiConnect();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.macAddress());
Serial.println("Adjusting system time...");
configTime(9 * 60 * 60, 0, "216.239.35.8");
wg.begin( local_ip , private_key , endpoint_address , public_key , endpoint_port );
Serial.println( "setup done..." );
}
void wifiConnect() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if ( client.connect("arduinoClient", "espcom", "EsPcom32*85" )) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
The log from WG in HA
peer: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
endpoint: XXX.XXX.XXX.XXX:51820
allowed ips: 10.10.10.10/32
latest handshake: 16 seconds ago
transfer: 601.39 KiB received, 21.87 MiB sent
persistent keepalive: every 25 seconds
Metadata
Metadata
Assignees
Labels
No labels