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

User avatar
By Pigs Fly
#5822 How best to handle this? My code needs to send a series of GETs to a server, but I'm not getting how to wait for the first connection to close before moving on to the next one. So far it only works when doing one GET. If I add any more in sequence the module restarts.

By adding
Code: Select allconn:close() conn=nil
to the receive function it speeds up the process considerably, but it's not enough. It needs some kind of wait loop to prevent a new connection from being created until the last one closes, and none of the usual while loops make any difference. The module just crashes and restarts. I also tried Connection: close. That crashed also.

Code: Select allconn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) conn:close() conn=nil end)
conn:on("disconnection", function() print("disconnected") end)
conn:connect(80,'<IP>')
conn:send("GET /insert.php?mode=insert&sensor="..sid1.."&value="..val1.." HTTP/1.1\r\n")
conn:send("Host: <hostname>\r\n")
conn:send("Accept: */*\r\n")
--conn:send("Connection: close")
conn:send("\r\n")
User avatar
By Pigs Fly
#5846 This works great, except the DS18B20's all report 85C the first time the code is run after a chip reboot. Once that has happened the sensors read correctly. Why would they do that?

Code: Select allnode_id = node.chipid()
-- read temperature with DS18B20
t=require("ds18b20")
t.setup(4)
addrs=t.addrs()
-- debug: print out values
sensor_count=table.getn(addrs)
if (sensor_count>0) then
     for i=1,sensor_count do
          sid=string.format("%02X%02X%02X%02X%02X%02X%02X%02X",string.byte(addrs[i],1,9))
          sval=t.read(addrs[i],t.C)
          print("SensorID: "..sid..", Value: "..sval)
     end
end

--configure remote server
server_ip="<server IP>"
server_port=<port>
server_host="<hostname>"

-- if connected to AP, send data
if (wifi.sta.getip()) then
     conn=net.createConnection(net.TCP, 0)
     -- debug: report server response
     conn:on("receive", function(conn, payload) print(payload) end)
     conn:connect(server_port,server_ip)
     -- begin sending sensor data. Use keep-alive to send multiple GET requests,
     -- then kill connection when finished.
     if (sensor_count>0) then
          for i=1,sensor_count do
               sid=string.format("%02X%02X%02X%02X%02X%02X%02X%02X",string.byte(addrs[i],1,9))
               sval=t.read(addrs[i],t.C)
               if i==sensor_count then
                    http_conn_type="close"
               else
                    http_conn_type="keep-alive"
               end
               conn:send("GET /telemetry/telemetry.php?mode=insert&sensor="..node_id.."."..sid.."&value="..sval
               .." HTTP/1.1\r\nHost: "..server_host.."\r\nAccept: */*\r\nConnection: "..http_conn_type.."\r\n\r\n")
          end
     end
end

--clean-up, probably not required.
node_id=nil
server_ip=nil
server_host=nil
server_port=nil
sensor_count=nil
addrs=nil
t = nil
ds18b20 = nil
package.loaded["ds18b20"]=nil


Output from the debug output at the top where the sensor values are printed:

1st run after reboot:
SensorID: 28FF30372B04006C, Value: 85.0
SensorID: 28FF71362B0400C7, Value: 85.0
SensorID: 28FF6F092E0400FE, Value: 85.0



All subsequent runs show accurate temperature:

SensorID: 28FF30372B04006C, Value: 18.8125
SensorID: 28FF71362B0400C7, Value: 18.6875
SensorID: 28FF6F092E0400FE, Value: 18.8125