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

Can't connect to server from ESP32 client #35

Open
dwaynez opened this issue Dec 8, 2023 · 0 comments
Open

Can't connect to server from ESP32 client #35

dwaynez opened this issue Dec 8, 2023 · 0 comments

Comments

@dwaynez
Copy link

dwaynez commented Dec 8, 2023

I am running Wireguard on a raspberry pi. I can connect with a number of devices (Windows, Android) and everything appears to be setup properly. I am trying to setup an ESP32 with a web server, but web server times out trying to load the web page. When I turn off wireguard I am able to access the webserver. Any advice?

#define DEVMODE 0 // - comment out for production mode (remove print statements)
#include <EEPROM.h>
#include <TimeLib.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebSerialLite.h>
#include <AsyncTCP.h>
#include <AsyncElegantOTA.h>
#include <ESPAsyncWebServer.h>
#include <esp_task_wdt.h>
#include <WireGuard-ESP32.h>
#include <ESPping.h>

#define PRINTLN(...) Serial.println(VA_ARGS); WebSerial.println(VA_ARGS); wifi_send(t_state,String(VA_ARGS),true)
#define PRINT(...) Serial.print(VA_ARGS); WebSerial.print(VA_ARGS); wifi_send(t_state,String(VA_ARGS))
#define PRINTLN2(...) Serial.println(VA_ARGS); WebSerial.println(VA_ARGS)
#define PRINT2(...) Serial.print(VA_ARGS); WebSerial.print(VA_ARGS)
// #define PRINTLN(...)
// #define PRINT(...)

// WIFI communication constants
#define wifi_retry_max 500
#define host "10.6.0.1"
#define ssid "xxxx"
#define password "yyyy"
#define private_key "zzz"
#define remote_address "xxxxx.duckdns.org"
#define public_key "aaaaa"
#define remote_port 51820

WiFiClient client;
static WireGuard wg;
IPAddress local_ip(10,6,0,6);
// Define OTA variables
AsyncWebServer server(80);
const char* serverIndex = "<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">\n<title>ZHM Garage</title>\n<a href=""/update"">Update firmware
<a href=""/webserial"">Web Serial
<a href=""/vars"">Variables";

void setup() {
Serial.begin(115200);
WiFi.onEvent(Wifi_connected,ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(Get_IPAddress, ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent(Wifi_disconnected, ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
WiFi.begin(ssid, password);
delay(5000);

// Create the OTA server
server.on("/",      HTTP_GET, [](AsyncWebServerRequest *request) {request->send(200, "text/html", serverIndex); });
server.on("/vars",  HTTP_GET, [](AsyncWebServerRequest *request) {request->send(200, "text/html", SendHTML()); });
server.on("/get",   HTTP_GET, [](AsyncWebServerRequest *request) {char inputMessage[5];

    if (request->hasParam("debug_sw")) {
        request->getParam("debug_sw")->value().toCharArray(inputMessage,5);
        debug_sw = atoi(inputMessage);
        request->send(200, "text/html", "Value set. Press go back" );
    } else if (request->hasParam("reboot")) {
        request->getParam("alarmoff")->value().toCharArray(inputMessage,5);
        if (strcmp(inputMessage, "111") == 0) {
            request->send(200, "text/html", "Rebooting Press Go back" );
            ESP.restart();
        } else {
            request->send(200, "text/html", "Bad reboot code" );
        }
    }

});

AsyncElegantOTA.begin(&server);
WebSerial.begin(&server);
server.begin();

}

// Main processing loop
void loop() {
if (!wg_connected) {
Serial.println("Attempting WG connection");
wg_connect();

}

//Format variable page
String SendHTML(){
String ptr = " \n";
ptr +="<meta name="viewport" content="no-cache" content="width=device-width, initial-scale=1.0, user-scalable=no">\n";
ptr +="<title>Boat Warehouse variables</title>\n";
ptr +="\n";
ptr +="

Boat Warehouse variables

\n";
ptr +="\n";

ptr +="<form action="/get">";
ptr +="Debug Switch: <input type="number" value="";
ptr +=(String)debug_sw;
ptr +="" name="debug_sw">";
ptr +="<input type="submit" value="Submit">";
ptr +="
";

ptr +="<form action="/get">";
ptr +="Reboot: <input type="number" value="" name="reboot">";
ptr +="<input type="submit" value="Submit">";
ptr +="
";

ptr +="<form action="/get">";
ptr +="Alarm set on/off: <input type="number" value="" name="alarmoff">";
ptr +="<input type="submit" value="Submit">";
ptr +="
";

ptr +="<div id="webpage">\n";
ptr +="

Horn Status: ";
ptr +=(String)state[s_horn_status];
ptr +="

";
ptr +="\n";
ptr +="\n";
ptr +="\n";
return ptr;
}

//WIFI Connected
void Wifi_connected(WiFiEvent_t event, WiFiEventInfo_t info){
// wifi_connected = true;
Serial.println("WIFI Connected");
}

// WIFI IP address assigned
void Get_IPAddress(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.print("WiFi connected with IP: ");
Serial.println(WiFi.localIP());
configTime(9 * 60 * 60, 0, "ntp.jst.mfeed.ad.jp", "ntp.nict.jp", "time.google.com");
wg_connect();
client_connect();
}

// WIF disconnected
void Wifi_disconnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Disconnected from WIFI access point");
// Serial.print("WiFi lost connection. Reason: ");
// Serial.println(info.disconnected.reason);
Serial.println("Reconnecting...");
WiFi.begin(ssid, password);
}

// Connect to wifi
void wg_connect() {
Serial.print("WireGuard IP1: ");
Serial.println(local_ip.toString());
if (wg.begin(
local_ip, // IP address of the local interface
private_key, // Private key of the local interface
remote_address, // Address of the endpoint peer.
public_key, // Public key of the endpoint peer.
remote_port)) { // Port pf the endpoint peer.
Serial.println("WG connected2");

      Serial.print("WireGuard IP: ");

Serial.println(local_ip.toString());
wg_connected = true;

    IPAddress ip (192,168,39,18);
    Serial.println("before ping 1");
    bool ret = Ping.ping(ip);
    Serial.println("after ping 1");
    if (ret)
        Serial.println("ping 1 ok");
    else
        Serial.println("ping 1 failed");
    IPAddress ip2 (10,6,0,1);
    if (Ping.ping(ip2))
        Serial.println("ping 2 ok");
    else
        Serial.println("ping 2 failed");
        
} else {
    Serial.println("WG connect failed");
    wg_connected = false;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant