ESP8266-AP + STA模式无法正常工作

问题描述 投票:1回答:1

我试图同时将ESP8266 ESP-12用作接入点和Station,在两种情况下都提供相同的网页。这就是我所拥有的:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include <DNSServer.h>
#include <ESP.h>

const byte DNS_PORT = 53;
DNSServer dnsServer;

MDNSResponder mdns;
ESP8266WebServer server(80);

String ssid = "";
String password = "";
String webPage = "";


void setup(void){

  IPAddress Ip(192, 168, 1, 1);
  IPAddress NMask(255, 255, 255, 0);
  WiFi.softAPConfig(Ip, Ip, NMask);
  WiFi.softAP(conf_ssid.c_str());
  WiFi.mode(WIFI_AP_STA);
  Serial.begin(115200);
  delay(1000);
  Serial.println("");
  Serial.println("Soft AP Started");
  Serial.println("");

  /* Setup the DNS server redirecting all the domains to the Ip */  
  dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  dnsServer.start(DNS_PORT, "www.example.com", Ip);

  //connectToWifi();
  getCredentials(); //Reads Home WiFi credentials from the EEPROM
  if (ssid != "" and password != ""){
    WiFi.begin(ssid.c_str(), password.c_str());
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    WiFi.mode(WIFI_AP_STA);
  }
  server.begin();

  // When no password is present
  if ((ssid == "" and password == "") or (ssid == "Error")){
    // ****** MAIN PAGE ******
    server.on("/", [](){ /* HANDLES CREDENTIAL INPUT */   });
    server.begin();
  }
  else // If we have ssid and password -- check if we have connection
  {
    webPage += "<h1>Main Webpage</h1>";
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    server.begin();
    //if (mdns.begin("esp8266", WiFi.localIP())) {
     // Serial.println("MDNS responder started");
    //}

    server.on("/", [](){
      server.send(200, "text/html", webPage);
    });
    Serial.println("HTTP server started");
  }
}

void loop(void){
  dnsServer.processNextRequest();
  server.handleClient();
} 

[基本上,它从EEPROM读取凭据,如果没有凭据,则显示WiFi配置页,否则它也连接到为AP和Station为主页面提供服务的WiFi。

问题是,即使我可以看到AP和从PC到WLAN Station Ip的ping操作,但只有通过AP(而不是通过Station)连接时,我才能访问网页。有什么想法吗?

非常感谢。

arduino wifi esp8266
1个回答
0
投票

我已经找到了解决该问题的方法,这对我来说有点安静。解决方案是创建2台服务器,其中一台具有站IP,另一台具有AP IP。此处出现的问题是,随着板上电源的打开,正在立即创建AP,但是连接到站点需要时间。因此,服务器不知道在发出请求时必须同时服务两个IP。

我已经准备好LUA代码:

wifi.setmode(wifi.STATIONAP)

ap = {}
sta = {}
ap_ip = {}
conn_table = {}

ap.ssid = "Node-Mcu1"
ap.pwd = "123456789"

sta.ssid = "Home WIFI"
sta.pwd = "iforgotmypassword"

ap_ip.ip = "192.168.1.1"
ap_ip.netmask = "255.255.255.0"
ap_ip.gateway = "192.168.0.2"

wifi.sta.sethostname("ESP8266")
wifi.ap.setip(ap_ip)
wifi.ap.config(ap)
wifi.sta.config(sta)
wifi.sta.connect()


timer1 = tmr.create()
timer1:alarm(1000,tmr.ALARM_AUTO ,function()
    conn_table = wifi.ap.getclient()
    --[[ GET THE IF ADDRESSES OF ALL CONNECCTED MODULES
    for mac,ip in pairs(conn_table) do
        print(mac,ip)
    end --]]
end
)

server1 = net.createServer(net.TCP,120)
server2 = net.createServer(net.TCP,120)
LEDpin = 2
gpio.mode(LEDpin, gpio.OUTPUT)



function SendHTML(sck, led) -- Send LED on/off HTML page
   htmlstring = "<!DOCTYPE html>\r\n"
   htmlstring = htmlstring.."<html>\r\n"
   htmlstring = htmlstring.."<head>\r\n"
   htmlstring = htmlstring.."<title>LED Control</title>\r\n"
   htmlstring = htmlstring.."</head>\r\n"
   htmlstring = htmlstring.."<body>\r\n"
   htmlstring = htmlstring.."<h1>LED</h1>\r\n"
   htmlstring = htmlstring.."<p>Click to switch LED on and off.</p>\r\n"
   htmlstring = htmlstring.."<form method=\"get\">\r\n"
  if (led)  then
   htmlstring = htmlstring.."<input type=\"button\" value=\"LED OFF\" onclick=\"window.location.href='/ledoff'\">\r\n"
  else
   htmlstring = htmlstring.."<input type=\"button\" value=\"LED ON\" onclick=\"window.location.href='/ledon'\">\r\n"
  end
   htmlstring = htmlstring.."</form>\r\n"
   htmlstring = htmlstring.."</body>\r\n"
   htmlstring = htmlstring.."</html>\r\n"
   sck:send(htmlstring)
end

function receiver(sck, data)-- process callback on recive data from client
print(data)
  if string.find(data, "GET /ledon")  then
   SendHTML(sck, true)
   gpio.write(LEDpin, gpio.HIGH)
  elseif string.find(data, "GET / ") or string.find(data, "GET /ledoff") then
   SendHTML(sck, false)
   gpio.write(LEDpin, gpio.LOW)
  else
   sck:send("<h2>Not found...!!</h2>")
   sck:on("sent", function(conn) conn:close() end)
  end
end

if server1 then
    print(wifi.ap.getip())
  server1:listen(80,wifi.ap.getip(), function(conn)-- listen to the port 80
  conn:on("receive", receiver) 

  end)
end

timer = tmr.create()
timer:alarm(1000,tmr.ALARM_AUTO ,function()
    if wifi.sta.getip() == nil then
        print("Obtaining IP...")
    else
        timer:stop()
        timer:unregister()
        print("Got IP. "..wifi.sta.getip())
        if server2 then
          print("server2")
          server2:listen(80,wifi.sta.getip(), function(conn)-- listen to the port 80
          conn:on("receive", receiver) 

          end)
        end

    end        
end
)
© www.soinside.com 2019 - 2024. All rights reserved.