nodemcu / nodemcu-firmware

Lua based interactive firmware for ESP8266, ESP8285 and ESP32
https://nodemcu.readthedocs.io
MIT License
7.64k stars 3.12k forks source link

Cannot access internet through my ESP8266 #3424

Closed goshante closed 3 years ago

goshante commented 3 years ago

Using latest build of nodemcu, tried a lot of variations, I cannot access internet on my wifi client devices. I can connect to my home router and access internet from ESP, i can host AP and connect to this hotspot. And yes I can do both at the same time. But no matter what, I cannot access internet for (from example) my phone when it connected to ESP's hotspot. Why?

My init.lua

dofile("credentials.lua")

function startup()
    if file.open("init.lua") == nil then
        print("init.lua deleted or renamed")
    else
        print("Running")
        file.close("init.lua")
        -- the actual application is stored in 'application.lua'
        -- dofile("application.lua")
    end
end

-- Define WiFi station event callbacks
wifi_connect_event = function(T)
  print("Connection to AP("..T.SSID..") established!")
  print("Waiting for IP address...")
  if disconnect_ct ~= nil then disconnect_ct = nil end
end

wifi_got_ip_event = function(T)
  -- Note: Having an IP address does not mean there is internet access!
  -- Internet connectivity can be determined with net.dns.resolve().
  print("Wifi connection is ready! IP address is: "..T.IP)
  print("Startup will resume momentarily, you have 3 seconds to abort.")
  print("Waiting...")
  tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
  net.dns.setdnsserver("193.151.252.17", 0)
  net.dns.setdnsserver("8.8.8.8", 1)
  net.dns.resolve("google.com", function(sk, ip)
    if (ip == nil) then print("DNS fail!") else print(ip) end
  end)
end

wifi_disconnect_event = function(T)
  if T.reason == wifi.eventmon.reason.ASSOC_LEAVE then
    --the station has disassociated from a previously connected AP
    return
  end
  -- total_tries: how many times the station will attempt to connect to the AP. Should consider AP reboot duration.
  local total_tries = 75
  print("\nWiFi connection to AP("..T.SSID..") has failed!")

  --There are many possible disconnect reasons, the following iterates through
  --the list and returns the string corresponding to the disconnect reason.
  for key,val in pairs(wifi.eventmon.reason) do
    if val == T.reason then
      print("Disconnect reason: "..val.."("..key..")")
      break
    end
  end

  if disconnect_ct == nil then
    disconnect_ct = 1
  else
    disconnect_ct = disconnect_ct + 1
  end
  if disconnect_ct < total_tries then
    print("Retrying connection...(attempt "..(disconnect_ct+1).." of "..total_tries..")")
  else
    wifi.sta.disconnect()
    print("Aborting connection to AP!")
    disconnect_ct = nil
  end
end

-- Register WiFi Station event callbacks
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, wifi_connect_event)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, wifi_disconnect_event)

wifi.setmode(wifi.STATIONAP)
wifi.setphymode(wifi.PHYMODE_G)

print("Starting AP...")

APConfig = {}
APConfig.ssid = "ESP8266_Testing"
APConfig.pwd = "123QweQwe"
APConfig.auth = AUTH_WPA_WPA2_PSK
APConfig.channel = 6
APConfig.hidden = 0
APConfig.max = 4
APConfig.beacon = 5000

IPConfig = {}
IPConfig.ip = "10.0.0.1"
IPConfig.netmask = "255.255.255.0"
IPConfig.gateway = "10.0.0.1"

DHCPConfig = {}
DHCPConfig.start = "10.0.0.2"

IPClientConfig = {}
IPClientConfig.ip = "192.168.1.10"
IPClientConfig.netmask = "255.255.255.0"
IPClientConfig.gateway = "192.168.1.1"
1073741824
1073741824

wifi.ap.config(APConfig)
wifi.ap.setip(IPConfig)
wifi.ap.dhcp.config(DHCPConfig)
wifi.ap.dhcp.start()

print("Connecting to WiFi access point...")
wifi.sta.setip(IPClientConfig)
wifi.sta.config({ssid=SSID, pwd=PASSWORD})
wifi.sta.connect()
jmd13391 commented 3 years ago

The GitHub nodemcu-firmware issues list is not the right place to ask for help. It is intended to report bugs and to place feature requests. Questions like "how do I ..." or "I can't get this to work ..." should be directed to StackOverflow or esp8266.com.

BTW/FYI:

The wifi.STATIONAP mode is working as designed. In this mode, the AP and STA are isolated. For example: if your phone is connected to the AP, and the STA is connected to your home Internet WiFi router, then only the ESP8266 can access the Internet from the router, your phone cannot. Your phone can communicate with the ESP8266 and the ESP8266 can communicate with the Internet.

Also, the ESP8266 has only one radio transceiver. In the wifi.STATIONAP mode it will use this single radio for both the Internet AP and STA connection. Therefore, all devices involved (phone, ESP8266, home Internet AP, etc.) must be configured to all use the same WiFi channel.