ESP8266,NodeMCU,软AP-类似于UDP服务器的软AP,独立的接入点 [英] ESP8266, NodeMCU, soft AP - UDP server-like soft AP, independent access point

查看:403
本文介绍了ESP8266,NodeMCU,软AP-类似于UDP服务器的软AP,独立的接入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有已升级固件的NodeMCU(带ESP8266-E).所有基本命令都能正常运行,但是存在一个问题.

我想创建一个独立的访问点,该访问点的行为可能类似于UDP服务器.这意味着无需直接连接到任何其他接入点.像软AP这样的简单UDP服务器.

我遵循了以下步骤:

  1. 我已将新固件上传到NodeMCU.
  2. 我下载了ESPlorer,以便更好地与NodeMCU一起使用.
  3. 我已经在下面上传了源代码.
  4. 我已连接到台式机上的NodeMCU接入点.
  5. 我已使用Java UDP客户端程序将一些字符串发送到NodeMCU.
  6. 我已经查看了ESPlorer上的消息.
  7. NodeMCU尚未收到任何此类字符串.

-

print("ESP8266 Server")
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test",pwd="12345678"});
print("Server IP Address:",wifi.ap.getip())

-- 30s timeout for an inactive client
srv = net.createServer(net.UDP, 30)
-- server listens on 5000, if data received, print data to console
srv:listen(5000, function(sk)
  sk:on("receive", function(sck, data) 
    print("received: " .. data)
  end)
  sk:on("connection", function(s)
    print("connection established")
  end)
end)

当我尝试使用Java应用程序发送消息时,ESPlorer没有任何变化.甚至当我尝试使用Hercules程序(用于TCP,UDP通信的绝佳程序)发送消息时也没有.

我想也许这将是错误的IP地址.我使用的是AP的IP地址,而不是站点的IP地址.

换句话说,我正在使用以下地址:wifi.ap.getip(),而不是该地址wifi.sta.getip()用于连接UDP服务器.但是sta.getip()返回一个nil对象.真的不知道.

我将为您提供任何建议.

非常感谢您.

解决方案

好吧,因为您更新了问题,所以让我们重新开始.在给您第一个提示之前,我应该先动脑子.对此感到抱歉.

UDP是无连接的,因此,当然没有s:on("connection").因此,您不能在套接字上而是在服务器本身上注册回调.它在文档中,但是很容易遗漏. /p>

这应该可以帮助您:

wifi.setmode(wifi.STATIONAP)
wifi.ap.config({ ssid = "test", pwd = "12345678" })
print("Server IP Address:", wifi.ap.getip())

srv = net.createServer(net.UDP)
srv:listen(5000)
srv:on("receive", function(s, data)
    print("received: " .. data)
    s:send("echo: " .. data)
end)

我针对dev分支上的固件运行了此程序,并像这样从命令行进行了测试

$ echo "foo" | nc -w1 -u 192.168.4.1 5000
echo: foo

然后,ESPlorer也正确打印了"received:foo".

I am using NodeMCU (with ESP8266-E) with an upgraded firmware. All basic commands work perfectly but there is one problem.

I wanted to create an independent access point, which could have a behaviour like a UDP server. That means without direct connection to any other access points. A simple UDP server like soft AP.

I followed these steps:

  1. I have uploaded a new firmware to NodeMCU.
  2. I have downloaded ESPlorer for better work with NodeMCU.
  3. I have uploaded the source code below.
  4. I have connected to the NodeMCU access point on my desktop.
  5. I have sent some strings to the NodeMCU using a Java UDP client program.
  6. I have looked at the messages on ESPlorer.
  7. NodeMCU has not received any such strings.

--

print("ESP8266 Server")
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test",pwd="12345678"});
print("Server IP Address:",wifi.ap.getip())

-- 30s timeout for an inactive client
srv = net.createServer(net.UDP, 30)
-- server listens on 5000, if data received, print data to console
srv:listen(5000, function(sk)
  sk:on("receive", function(sck, data) 
    print("received: " .. data)
  end)
  sk:on("connection", function(s)
    print("connection established")
  end)
end)

When I tried to send a message using a Java application, there was no change in ESPlorer. Not even when I tried to send a message using the Hercules program (great program for TCP, UDP communication).

I guess that maybe it will be the wrong IP address. I am using the IP address of the AP and not the IP address of the station.

In other words I am using this address: wifi.ap.getip() and not this address wifi.sta.getip() for connections to the UDP server. But sta.getip() returns a nil object. Really I don't know.

I will be glad for any advice.

Thank you very much.

解决方案

Ok, let's restart this since you updated the question. I should have switched on my brain before I gave you the first hints, sorry about this.

UDP is connectionless and, therefore, there's of course no s:on("connection"). As a consequence you can't register your callbacks on a socket but on the server itself. It is in the documentation but it's easy to miss.

This should get you going:

wifi.setmode(wifi.STATIONAP)
wifi.ap.config({ ssid = "test", pwd = "12345678" })
print("Server IP Address:", wifi.ap.getip())

srv = net.createServer(net.UDP)
srv:listen(5000)
srv:on("receive", function(s, data)
    print("received: " .. data)
    s:send("echo: " .. data)
end)

I ran this against a firmware from the dev branch and tested from the command line like so

$ echo "foo" | nc -w1 -u 192.168.4.1 5000
echo: foo

ESPlorer then also correctly printed "received: foo".

这篇关于ESP8266,NodeMCU,软AP-类似于UDP服务器的软AP,独立的接入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆