As the title says... Chat on...

User avatar
By pete_l
#18187 I have an 8266 that successfully connects to a webserver. The LUA code is this:
Code: Select allfunction sendData()
print ("ts="..tstamp() .. "&id=".. node.chipid() .. "&ap=" .. this_ssid .. "&rssi=" .. best_rssi)

conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)

conn:connect(80,"192.168.1.40") -- local server: corv   
conn:send("GET /8266-boot.php?ts="..tstamp() .. "&id=".. node.chipid() .. "&ap=" .. this_ssid .. "&rssi=" .. best_rssi .. " HTTP/1.1\r\n")
conn:send("Host: corv\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn) conn:close() end)
conn:on("disconnection", function(conn) end)
end


which connects to a file called 8266-boot.php on the server that contains this:
Code: Select all<http><head>
<title>Form reply</title>
</head>
<body>
<?php
$TS=$_GET["ts"];
$K=$_GET["id"];
$AP=$_GET["ap"];
$RSSI=$_GET["rssi"];
$F=fopen("/tmp/8266-started.txt","a");
fprintf($F, date("D d-F-Y H:i:s")." timestamp=$TS, device ID was $K AP=$AP, RSSI=$RSSI");
fprintf($F,"\n");
fclose($F);

echo "Thank You\n";
 ?>

</body>
</html>

This works great at sending data from the 8266 to the server and the data is saved to the local file just as you'd expect. But I can't get anything sent back to the 8266.
On the 8266, I have a line conn:on("receive", function(conn, payload) print(payload) end)
which is supposed to catch stuff sent by the server and print it on the 8266 serial port. It doesn't.

Can someone please suggest what I need to add to the PHP file to send data back to the 8266. I'm hoping the replies will be of the form:
"just add this line ..... to your PHP file".

With thanks
Pete
User avatar
By pete_l
#18194 Good guess Cal, thanks.

Changing the close call to
Code: Select allconn:on("sent", function(conn)
  print("Closing connection")
  tmr.alarm(6, 1000, 0, function() conn:close() end)
  end )

seems to have fixed it. This inserts a 1 second delay after the data is sent before the port is closed. (At least, that's what I think it does :( )
User avatar
By cal
#18939
pete_l wrote:Good guess Cal, thanks.

Changing the close call to
Code: Select allconn:on("sent", function(conn)
  print("Closing connection")
  tmr.alarm(6, 1000, 0, function() conn:close() end)
  end )

seems to have fixed it. This inserts a 1 second delay after the data is sent before the port is closed. (At least, that's what I think it does :( )


Yes it does!
Note that the close problem is an open bug of nodemcu IMHO.
It's reported.

Cal