2

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.

dda
  • 6,030
  • 2
  • 25
  • 34
Luke
  • 1,163
  • 2
  • 11
  • 19

2 Answers2

3

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

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • I have repaired my question. Thanks – Luke Apr 21 '16 at 19:48
  • Many thanks. I found out that finally it works perfectly. The reason why it does not work it was my wrong understanding about UDP protocol. When I tried to send a string using by Hercules then it happend nothing. But after sending more strings/messages I found out that the UDP server received it all right. In other words the UDP protocol is an unreliable protocol :) – Luke Apr 24 '16 at 10:16
  • "UDP protocol is an unreliable protocol" - oh yes, [very much so](https://en.wikipedia.org/wiki/User_Datagram_Protocol#Reliability_and_congestion_control_solutions). – Marcel Stör Apr 24 '16 at 15:04
1

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.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • I have repaired my question. Thanks – Luke Apr 21 '16 at 19:48
  • Many thanks. I found out that finally it works perfectly. The reason why it does not work it was my wrong understanding about UDP protocol. When I tried to send a string using by Hercules then it happend nothing. But after sending more strings/messages I found out that the UDP server received it all right. In other words the UDP protocol is an unreliable protocol :) – Luke Apr 24 '16 at 10:15