-->
Page 1 of 1

net.createConnection() crashes ESP8266

PostPosted: Mon May 11, 2015 3:59 am
by pholcroft
Hi,

I am new to using these boards this weekend. I coded up the below which works when in small separate parts, but fails when I put it together causing the board to hard reset. Can anyone see what I am doing wrong, or is it a bug?

I tried updating to 0.9.6 from 0.9.5, but it didn't help. I also tried rearranging the code as much I could, but it still seems to crash. Is it because I am creating a connection from within another connection? I'm not sure how I can get round that as I can't make the 2nd connection until the first has been done.

Code: Select allprint("Get London Time...")
conn=net.createConnection(net.TCP, 0)

conn:on("connection", function(conn)
  conn:send("GET /LondonTime.php HTTP/1.1\r\n")
  conn:send("Host: www.holcroft.net\r\n")
  conn:send("Accept: */*\r\n")
  conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)\r\n")
  conn:send("\r\n")
end)

conn:on("receive", function(conn, payload)
  londonTime = string.sub(payload,string.find(payload,"<Time>")+6,string.find(payload,"</Time>")-1)
  sendTweet(londonTime)
  conn:close()
end)

function urlencode(str)
   if (str) then
      str = string.gsub (str, "\n", "\r\n")
      str = string.gsub (str, "([^%w ])",
         function (c) return string.format ("%%%02X", string.byte(c)) end)
      str = string.gsub (str, " ", "+")
   end
   return str   
end

function sendTweet (time)
  print("Sending tweet...")  thingtweetAPIKey = "XXXXXXXXXXXXXXXX"
  status = "I'm finished drying! (" .. londonTime .. ")"
  getRequest = "GET /apps/thingtweet/1/statuses/update?key=" .. thingtweetAPIKey
    .. "&status=" .. urlencode(status) .. " HTTP/1.1\r\n"

  tsConn=net.createConnection(net.TCP, 0)
  print("Connection Created")

  tsConn:on("receive", function(tsConn, payload) print("Received:\r\n"..payload) end)

  tsConn:on("connection", function(tsConn)
    print("Send Get Request...")
    tsConn:send(getRequest)
    tsConn:send("Host: api.thingspeak.com\r\n")
    tsConn:send("Accept: */*\r\n")
    tsConn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
    tsConn:send("\r\n")
  end)

  tsConn:on("disconnection", function(tsConn)
       print("Disconn:ected from api.thingspeak.com...")
       end)

  tsConn:connect(80,'api.thingspeak.com')
end

conn:connect(80,"www.holcroft.net")


This is the result:
Code: Select alldofile("tweet.lua")
Get London Time...
> Sending tweet...

ʯr¾Rhÿ‰RCú©R}±à»þá

NodeMCU 0.9.6 build 20150216  powered by Lua 5.1.4
lua: cannot open init.lua
>
Hard Restart 11 May 2015  09:49:26

Re: net.createConnection() crashes ESP8266

PostPosted: Tue May 12, 2015 3:39 pm
by cal
Moin,

that fits a bug I filed on github. There is a bug in the net module that the connection data structure
is sometimes being used after close.

I currently can't look into it more deeply.

Sorry,
Cal

Re: net.createConnection() crashes ESP8266

PostPosted: Fri May 15, 2015 10:52 am
by DavidMPerlman
Cal,
Could you point me to the issue number for that bug? I am struggling with this too and might be able to contribute useful information, but I couldn't find the specific bug you are referring to.

Thanks!

Re: net.createConnection() crashes ESP8266

PostPosted: Sat May 16, 2015 3:23 am
by cal
Oh, I see I didn't filed it but commented.

https://github.com/nodemcu/nodemcu-firmware/issues/340

The problem is obvious IMHO and needs some thought about integration between sdk / lua callback
integration.

Cal