【问题标题】:Only one tcp server allowed ESP8266 with Lua只有一个 tcp 服务器允许使用 Lua 的 ESP8266
【发布时间】:2016-09-30 16:12:31
【问题描述】:

我正在研究 ESP8266,我正在尝试使用 Lua 进行编程。我试图创建 TCP 服务器,但是当我在下面编写示例代码时,我得到了一个错误消息名称“只允许一个 tcp 服务器”。我创建了一个服务器,但我无法关闭。

我该如何解决?

print("ESP8266 mode is: " .. wifi.getmode());

cfg = {};
-- Set the SSID of the module in AP mode and access password
cfg.ssid = "SSID";
cfg.pwd = "password";
if ssid and password then
    print("ESP8266 SSID is: " .. cfg.ssid .. " and PASSWORD is: " ..
            cfg.password)
end;
-- Now you should see an SSID wireless router named ESP_STATION when you scan for available WIFI networks
-- Lets connect to the module from a computer of mobile device. So, find the SSID and connect using the password selected
wifi.ap.config(cfg);
ap_mac = wifi.ap.getmac();
-- create a server on port 80 and wait for a connection, when a connection is coming in function c will be executed
sv = net.createServer(net.TCP, 30);
sv:listen(80, function(c)
    c:on("receive", function(c, pl)
        -- print the payload pl received from the connection
        print(pl);
        print(string.len(pl));
        -- wait until SSID comes back and parse the SSID and the password
        print(string.match(pl, "GET"));
        ssid_start, ssid_end = string.find(pl, "SSID=");
        if ssid_start and ssid_end then
            amper1_start, amper1_end = string.find(pl, "&", ssid_end + 1);
            if amper1_start and amper1_end then
                http_start, http_end = string.find(pl, "HTTP/1.1", ssid_end + 1);
                if http_start and http_end then
                    ssid = string.sub(pl, ssid_end + 1, amper1_start - 1);
                    password = string.sub(pl, amper1_end + 10, http_start - 2);
                    print("ESP8266 connecting to SSID: " .. ssid .. " with PASSWORD: " .. password);
                    if ssid and password then
                        sv:close();

                        -- close the server and set the module to STATION mode
                        wifi.setmode(wifi.STATION);
                        tmr.stop(2)



                        print("ESP8266 mode now is: " .. wifi.getmode());
                        -- configure the module wso it can connect to the network using the  received SSID and password
                        wifi.sta.config(ssid, password);
                        print("Setting up ESP8266 for station mode…");
                        print("Please restart your device");
                        tmr.delay(10000000);
                        print("Mode is " .. wifi.getmode());
                        print("Heap:" .. node.heap())
                        print("");
                    end;
                end;
            end;
        end;
        -- this is the web page that requests the SSID and password from the user
        c:send("<!DOCTYPE html> ")
        c:send("<html> ")
        c:send("<body> ")
        c:send("<h1>ESP8266 Wireless control setup</h1>")
        mac_mess1 = "The module MAC address is: " .. ap_mac
        mac_mess2 = "You will need this MAC address to find the IP address of  the module, please take note of it."
        c:send("<h2>" .. mac_mess1 .. "</h2>")
        c:send("<h2>" .. mac_mess2 .. "</h2>")
        c:send("<h2>Enter SSID and Password for your WIFI router</h2>")
        c:send("</form> </html>")
        c:send("<form action='' method='get'>")
        c:send("SSID:")
        c:send("<input type='text' name='SSID' value='' maxlength='100'/>")
        c:send("<br/>")
        c:send("Password:")
        c:send("<input type='text' name='Password' value='' maxlength='100'/>")
        c:send("<input type='submit' value='Submit' />")
    end);
end);

【问题讨论】:

  • 在 SO 上针对这两个问题打开两个问题。
  • 你的意思是两个不同的新主题下的两个问题吧?
  • 是的。由于 SO 是一个问答网站,因此问题是一个“主题”(可能是 BBS 类比)。
  • 最后说明:多个连续的socket:send() 调用只能在非常旧的 NodeMCU 固件中正常工作,有一个现成的 NodeMCU 模块用于您正在尝试构建的称为 enduser_setup

标签: lua tcpserver esp8266


【解决方案1】:

您需要关闭已经在 ESP8266 上运行的 TCP 服务器。 在创建服务器之前检查服务器是否已经启动,如果是则关闭它并创建一个新的..

if srv~=nil then
  srv:close()
end

srv=net.createServer(net.TCP) 
srv:listen(80,function(conn) 
    conn:on("receive",function(conn,payload) 
    print(payload) 
    conn:send("<h1> Hello, World.</h1>")
    end) 
end)

【讨论】:

  • 我同意这个答案。
【解决方案2】:

Nodemcu 只允许一个 tcp 服务器。 当您尝试保存 init.lua 文件并使用 esplorer ide 时,会发生这种情况。

  1. nodemcu 上已经存在的初始化文件被删除。
  2. 新的 init.lua 文件已写入。
  3. dofile("init.lua") 在执行 init.lua 文件的 nodemcu 上运行。

当第三步运行时,已经有一个 tcp 服务器在 nodemcu 上运行,不允许再创建一个。

分辨率:- 只需重置 nodemcu。最新的 init.lua 将被执行,并且您的 tcp 服务器应该已启动。

【讨论】:

  • 这可能是“纯粹”的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
相关资源
最近更新 更多