Weird NodeMCU DNS behavior

A project log for ESP8266 and thing

Updates sensor data live to the cloud - with no external microcontroller.

liam-marshallLiam Marshall 05/01/2015 at 20:232 Comments

Adafruit has this code for making a HTTP request:

sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:send("GET /testwifi/index.html HTTP/1.1\r\nHost:\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

The bizarre thing is that this works fine entered line-by-line in the terminal, but if I toss it in a lua file and run it with dofile, it doesn't work. But if I change "" in sk:connect to the IP for, it works in the file. I wonder if there's a race with the DNS where it can't do the DNS lookup fast enough, and then sk:send tries to send before it's completely connected. I'm going to report it to the NodeMCU devs, but for now I'm going to be using the Arduino port to the ESP8266.


plucker wrote 05/09/2015 at 14:11 point

hi Liam,

You are right, if you use a domain name, the esp8266 will do a DNS request first.

In that case, you need to put your send request in the connect event callback:

sk:on("connect", function(sck, c) " do your send " end )

Please visit forum or the nodeMCU forum for more details.

I have made a prototype to post temperature and humidity, and others system data to thingspeak without any issue.

But, I have switched to native programing because of lack of memory when using lua...

Good luck,


  Are you sure? yes | no

Liam Marshall wrote 05/09/2015 at 15:06 point

Thanks for the info! Arduino for ESP8266 is working great, way better in my opinion than either raw native programming or Lua.

  Are you sure? yes | no