Left here for archival purposes.

User avatar
By sancho
#5913 Hello, all.
I have a strange problem.
This code suddenly stopped working since 0.9.4:
Code: Select allfunction sendData()
    sk=net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c) print(c) end )
    sk:connect(8888, "192.168.11.1")
    sk:send("asdf jklo asa ads das")
    sk:close()
end
sendData()

When I am writing each line one-by-one, after the sk:close() the packet is sent.
However, when I call the function and do it quickly, the packet is not sent.
I cannot even receive an event on("connection", ...).
The same code worked with 0.9.2, the data within sk:send were transmitted imediatelly and I could use on("sent", ...) statement to close the connection.
Can anyone advice what am I doing wrong?
Thanks.
User avatar
By zeroday
#5928
sancho wrote:Hello, all.
I have a strange problem.
This code suddenly stopped working since 0.9.4:
Code: Select allfunction sendData()
    sk=net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c) print(c) end )
    sk:connect(8888, "192.168.11.1")
    sk:send("asdf jklo asa ads das")
    sk:close()
end
sendData()

When I am writing each line one-by-one, after the sk:close() the packet is sent.
However, when I call the function and do it quickly, the packet is not sent.
I cannot even receive an event on("connection", ...).
The same code worked with 0.9.2, the data within sk:send were transmitted imediatelly and I could use on("sent", ...) statement to close the connection.
Can anyone advice what am I doing wrong?
Thanks.


connection close too fast?
maybe?
Code: Select allsk:on("sent",function(sck) sck:close() end)
sk:send("hello")
User avatar
By sancho
#5933
zeroday wrote:connection close too fast?
maybe?
Code: Select allsk:on("sent",function(sck) sck:close() end)
sk:send("hello")

That was my first guess, but that's not the case - this on(„sent„,...) was the first version - again, the close part never gets executed.
I put some print statement into thevsent part and, again, never executed :-(
I must have had missed something :-(
User avatar
By sancho
#5953 OK, to avoid confustion, this is the full script:
Code: Select allpin = 4
ow.setup(pin)

function bxor(a,b)
   local r = 0
   for i = 0, 31 do
      if ( a % 2 + b % 2 == 1 ) then r = r + 2^i end
      a = a / 2
      b = b / 2
   end
   return r
end

function getTemp()
      print("Measurement start")
      sensors={}
      temps={}
      counter=0
      addr = ow.reset_search(pin)
      repeat
      tmr.wdclr()
      if (addr ~= nil) then
        crc = ow.crc8(string.sub(addr,1,7))
        if (crc == addr:byte(8)) then
          if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
      sensor = ""
      for j = 1,7 do sensor = sensor .. string.format("%02x", addr:byte(j)) end
                print("Have sensor " .. sensor)
                sensors[counter] = sensor
                ow.reset(pin)
                ow.select(pin, addr)
                ow.write(pin, 0x44, 1)
                tmr.delay(1000 * 1000)
                present = ow.reset(pin)
                ow.select(pin, addr)
                ow.write(pin,0xBE, 1)
                data = nil
                data = string.char(ow.read(pin))
                for i = 1, 8 do data = data .. string.char(ow.read(pin)) end
                crc = ow.crc8(string.sub(data,1,8))
                if (crc == data:byte(9)) then
                   t = (data:byte(1) + data:byte(2) * 256)
         if (t > 32768) then
                    t = (bxor(t, 0xffff)) + 1
                    t = (-1) * t
                   end
         t = t * 625
                   if(addr:byte(1) == 0x10) then
                     -- we have DS18S20, the measurement must change
                     t = t * 8;  -- compensating for the 9-bit resolution only
                     t = t - 2500 + ((10000 * (data:byte(8) - data:byte(7))) / data:byte(8))
                   end
                   print("Got temp: " .. t)
                   temps[counter] = t
                end
          end
        end
      end
      addr = ow.search(pin)
      counter = counter + 1
      until(addr == nil)

    srv=net.createConnection(net.TCP, 0)
    srv:connect(8888, "192.168.11.1")
    srv:on("connection", function(conn)
                             print "We have connection"
                         end
                         )
    output = ""
    for i, sensorID in ipairs(sensors) do
        t1 = temps[i] / 10000
        t2 = (temps[i] >= 0 and temps[i] % 10000) or (10000 - temps[i] % 10000)
        output = output .. wifi.sta.getmac()
        output = output .."\t" .. sensorID .. "\t" .. t1 .. "." .. string.format("%04d", t2) .. "\n"
    end
    print("Sending: " .. output)
    srv:send(output)
    srv:on("sent",function(conn)
                      print("Data sent")
                      conn:close()
                  end)
end

function checkIP()
    if wifi.sta.getip() ~= nil then
        tmr.stop(0)
        tmr.wdclr()
        print("Got IP")
        print(wifi.sta.getip())
        print("Starting Temp measurement")
        getTemp()
        tmr.alarm(0,30*1000, 1, getTemp)
    end
end

tmr.alarm(0, 2 * 1000, 1, checkIP)


The code correctly prints the sensors and temperatures, the whole output is OK, however it is never sent.
And the on("sent", ...) part is also never triggered.
Any suggestions?