Page 1 of 1

Unable to Connect to HTTP Server

Posted: Mon Mar 28, 2022 3:57 pm
by pscott
I am trying to modify the ESP32_Async_Demo example from https://github.com/ayushsharma82/AsyncE ... Async_Demo to work over ethernet instead of WiFi.

I am able to compile and upload the program and ping the IP address, but when I try to access the web page, I get the following message:
This site can’t be reached
192.168.167.101 refused to connect.
Try:
  • Checking the connection
  • Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

Hardware I am using:
WeMos D1 Mini32
W5500 Ethernet Module

Wired connections from ESP32 to W5500:
GPIO5 -> CS
GPIO18 -> SCK
GPIO23 -> MO
GPIO19 -> MI
GPIO26 -> RST
GPIO22 -> INT
3V3 -> 3V3
GND -> GND

Here is my code:

Code: Select all

#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>

#include "lwip/dns.h"
#include "lwip/tcpip.h"  

byte mac[] = { 0x02, 0xF0, 0x0D, 0xBE, 0xEF, 0x01 };
IPAddress ip = { 192, 168, 167, 101 };
IPAddress sn = { 255, 255, 255, 0 };
IPAddress gw = { 192, 168, 167, 1 };
IPAddress dns = { 8, 8, 8, 8 };
EthernetUDP Udp;
AsyncWebServer server(80);

bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1)
{
    esp_err_t err = ESP_OK;
    tcpip_adapter_ip_info_t info;

    tcpip_adapter_init();

    if(local_ip != (uint32_t)0x00000000 && local_ip != INADDR_NONE){
        info.ip.addr = static_cast<uint32_t>(local_ip);
        info.gw.addr = static_cast<uint32_t>(gateway);
        info.netmask.addr = static_cast<uint32_t>(subnet);
    } else {
        info.ip.addr = 0;
        info.gw.addr = 0;
        info.netmask.addr = 0;
	}

    err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
    if(err != ESP_OK && err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED){
        Serial.print("DHCP could not be stopped! Error: ");
        Serial.println(err);
        return false;
    }

    err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
    if(err != ERR_OK){
        Serial.print("STA IP could not be configured! Error: ");
        Serial.println(err);
        return false;
    }

    ip_addr_t d;
    d.type = IPADDR_TYPE_V4;

    if(dns1 != (uint32_t)0x00000000 && dns1 != INADDR_NONE) {
        // Set DNS1-Server
        d.u_addr.ip4.addr = static_cast<uint32_t>(dns1);
        dns_setserver(0, &d);
    }

    return true;
}

void setup(void) {
  Serial.begin(115200);
  
  SPI.begin();
  Ethernet.init(5);
  // start the Ethernet connection:
  Serial.println("Initializing Ethernet...");
  Ethernet.begin(mac, ip);
  config(ip, gw, sn, gw);
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("DNS: ");
  Serial.println(Ethernet.dnsServerIP());
  Serial.print("Gateway: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("Subnet Mask: ");
  Serial.println(Ethernet.subnetMask());

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(200, "text/plain", "Hi! I am ESP32.");
  });

  Udp.begin(4004);
  AsyncElegantOTA.begin(&server);    // Start ElegantOTA
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
}
Here is the Serial Monitor output:

Code: Select all

Initializing Ethernet...
Local IP: 192.168.167.101
DNS: 192.168.167.1
Gateway: 192.168.167.1
Subnet Mask: 255.255.255.0
HTTP server started
I think the problem lies in the initialization of the TCP server:

Code: Select all

void AsyncServer::begin(){
    if(_pcb) {
        return;
    }
    if(!_start_async_task()){
        Serial.println("failed to start task");
        return;
    }
    int8_t err;
    _pcb = tcp_new_ip_type(IPADDR_TYPE_V4);
    if (!_pcb){
        Serial.println("_pcb == NULL");
        return;
    }
    ip_addr_t local_addr;
    local_addr.type = IPADDR_TYPE_V4;
    local_addr.u_addr.ip4.addr = (uint32_t) _addr;
    err = _tcp_bind(_pcb, &local_addr, _port);
    if (err != ERR_OK) {
        _tcp_close(_pcb, -1);
        Serial.print("bind error: ");
        Serial.println(err);
        return;
    }
    static uint8_t backlog = 5;
    _pcb = _tcp_listen_with_backlog(_pcb, backlog);
    if (!_pcb) {
        Serial.println("listen_pcb == NULL");
        return;
    }
    tcp_arg(_pcb, (void*) this);
    tcp_accept(_pcb, &_s_accept);
}