net.createConnection() crashes ESP8266
Posted: Mon May 11, 2015 3:59 am
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.
This is the result:
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 all
print("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 all
dofile("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