TransWikia.com

ESP-01 + UNO won't detect gateway

Arduino Asked by MrCabana on September 25, 2021

Apparently, this combination of hardware won’t accept anything other than WiFiEsp.h together with SoftwareSerial.h (if anyone knows something better/newer than these two, please let me know).
Thing is: I can connect to wifi, send and receive data (to an apache server inside my home network) just fine. After my project was almost done, I decided to connect it to a real server on the internet only to discover that ESP-01 couldn’t detect my gateway.

#include "WiFiEsp.h"
#include "SoftwareSerial.h"

int pinWifiRX = 6;
int pinWifiTX = 7;

char ssid[64]    = "*****";
char pass[32]    = "************";

SoftwareSerial Serial1(pinWifiRX, pinWifiTX);

int wifi_status;

void setup() {

  Serial.begin(9600);

  Serial1.begin(9600);
  while(!Serial1) (true);

  WiFi.init(&Serial1);

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("No shield"));
    while (true) {}
  }

  IPAddress localIp(192, 168, 1, 60);
  IPAddress subnetMask(255, 255, 255, 0);
  IPAddress gateway(192, 168, 1, 36);

  WiFi.config(localIp);  // <--- this line is accepted and works, but the other 3 gives compilation errors
//  WiFi.config(localIp, gateway);
//  WiFi.config(localIp, gateway, subnetMask);
//  WiFi.config(localIp, gateway, subnetMask, gateway);

  wifi_status = WiFi.begin(ssid, pass);

  if (wifi_status == WL_CONNECTED) {

    IPAddress ip;

    ip = WiFi.localIP();
    Serial.print(F("[WiFiEsp] IP Address: "));
    Serial.println(ip);

    ip = WiFi.subnetMask();
    Serial.print(F("[WiFiEsp] Subnet Mask: "));
    Serial.println(ip);

    ip = WiFi.gatewayIP();
    Serial.print(F("[WiFiEsp] Gateway: "));
    Serial.println(ip);

//    ip = WiFi.getDNS();  // <--- function doesn't exist
  }

}

void loop() {}

Results in:

[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.2.1
[WiFiEsp] IP address set 192.168.1.60
[WiFiEsp] Connected to *****
[WiFiEsp] IP Address: 192.168.1.60
[WiFiEsp] Subnet Mask: 255.255.255.0
[WiFiEsp] Gateway: 192.168.1.0

As one can see, Subnet Mask is corret, but it “detects” Geteway 192.168.1.0 when it should be 192.168.1.36. This way, I can connect to any device in LAN, but cannot access any address on the internet.

I even tried to send some AT commands directly to SoftwareSerial.h, but I don’t get any response.

Will I have to start the whole project over from scratch (using another hardware combination), or is there a way to change gateway by hand? Better yet, is there a way to make it detect the gateway’s address correctly?

EDIT: could it be related to the firmware I installed to the ESP-01? I followed this tutorial and got it from this link.

Thanks in advance.

EDIT: in this example, i used the WiFi.config(localIp) function to set a fix IP. I don’t really need that, it was just an attempt to make it get the correct Gateway address. Even if I comment that line, the Gateway is still wrong.

2 Answers

I've had some progress in my researches towards this problem.

In fact, Arduino gets the correct gateway address from my DHCP server, but displays the wrong one. There must be a bug somewhere in WiFiEsp library somewhere.

To debug this, I made a few changes to the following files:

  • WiFiEsp-Master/src/WiFiEsp.h
void config(IPAddress local_ip, IPAddress gateway);
void config(IPAddress local_ip, IPAddress gateway, IPAddress subnetMask);
  • WiFiEsp-Master/src/WiFiEsp.c
void WiFiEspClass::config(IPAddress ip, IPAddress gateway)
{
  EspDrv::config(ip, gateway);
}
void WiFiEspClass::config(IPAddress ip, IPAddress gateway, IPAddress subnetMask)
{
  EspDrv::config(ip, gateway, subnetMask);
}
  • WiFiEsp-Master/src/utility/EspDrv.h
static void config(IPAddress local_ip, IPAddress gateway);
static void config(IPAddress local_ip, IPAddress gateway, IPAddress subnetMask);
  • WiFiEsp-Master/src/utility/EspDrv.cpp
void EspDrv::config(IPAddress ip, IPAddress gateway) {
  LOGDEBUG(F("> config"));

  // disable station DHCP
  sendCmd(F("AT+CWDHCP_CUR=1,0"));

  // it seems we need to wait here...
  delay(500);

  char buf_ip[16];
  sprintf_P(buf_ip, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);

  char buf_gateway[16];
  sprintf_P(buf_gateway, PSTR("%d.%d.%d.%d"), gateway[0], gateway[1], gateway[2], gateway[3]);

  int ret = sendCmd(F("AT+CIPSTA_CUR="%s","%s""), 2000, buf_ip, buf_gateway);
  delay(500);

  if (ret==TAG_OK)
  {
    LOGINFO1(F("IP and gateway addresses set"), buf_ip);
  }
}

void EspDrv::config(IPAddress ip, IPAddress gateway, IPAddress subnet)
{

  LOGDEBUG(F("> config"));

  // disable station DHCP
  sendCmd(F("AT+CWDHCP_CUR=1,0"));

  // it seems we need to wait here...
  delay(500);

  char buf_ip[16];
  sprintf_P(buf_ip, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);

  char buf_gateway[16];
  sprintf_P(buf_gateway, PSTR("%d.%d.%d.%d"), gateway[0], gateway[1], gateway[2], gateway[3]);

  char buf_subnet[16];
  sprintf_P(buf_subnet, PSTR("%d.%d.%d.%d"), subnet[0], subnet[1], subnet[2], subnet[3]);

  int ret = sendCmd(F("AT+CIPSTA_CUR="%s","%s","%s""), 4000, buf_ip, buf_gateway, buf_subnet);
  delay(500);

  if (ret==TAG_OK)
  {
    LOGINFO1(F("IP, gateway and subnet mask addresses set"), buf_ip);
  }
}

These few changes should be enough to implement fix IP address and specify manual address for gateway and subnet mask (please feel free to correct me if I'm wrong).

But after doing this, I realized the gateway continued to be "192.168.1.0". Then I added this piece o code to the function EspDrv::readUntil, inside EspDrv.cpp:"

        ringBuf.push(c); // this line was already there
        Serial.print(c);

This way, I could see exactly all responses for the AT commands sent by WiFiEsp library. When asked, the results were:

+CIPSTA:ip:"192.168.1.60"
+CIPSTA:gateway:"192.168.1.36"
+CIPSTA:netmask:"255.255.255.0"

Then I realized the problem wasn't the gateway, but the DNS. If the library sends command "AT+CIPSTART=3,"TCP","www.google.com",80", the result was:

Not connected

busy p...

And, if increased the timeout value to 15000 instead of 5000, the result was:

DNS ERROR

Now my problem is partially solved. I can connect to an external server if I use an IP address, but I can't set a domain name because it won't be resolved by the DNS.

Apparently the firmware version of my ESP-01 won't accept "AT+CDNSCFG=?" required to set/retrive DNS address. I tried to set 8.8.8.8 as the DNS, but the result was "NOT OK".

I will stick to this "solution" for now, but would be glad to hear any comment about what I described here and a more final solution.

Thanks.

Answered by MrCabana on September 25, 2021

Using a static IP address with a gateway is not implemented (as you can see from the source code).

To get out to the internet with that library you can only use DHCP (by omitting the WiFi.config(localIp) call).

Answered by Majenko on September 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP