2
votes

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.

2

2 Answers

3
votes

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".

1
votes

This line is invalid Lua code. connected is in the wrong place here. you can't just put a single word after a function call.

print(wifi.ap.getip()) connected

I guess you intended to do something like print(wifi.ap.getip() .. " connected") Although I think you should add som error handling here in case wifi.ap.getip() does not return an IP.

Here you do not finish the function definition. Neither did you complete the srv:on call

srv:on("receive", function(srv, pl)
print("Strings received")
srv:listen(port)

I assume you just did not copy/paste the complete code.