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

User avatar
By ChrixXenon
#10446 I'm writing a system to read a line of DS18B20 temperature sensors periodically, and upload the readings to a web server, so there are three components:

1. The timer
2. The sensor reader
3. The data uploader

The uploader does an HTTPGet, passing the data as a query string in the URL. So the code does a get on, e.g., http://myserver.org/loggit.php?sensor1= ... nsor2=23.6.

Here is the timer:

Code: Select alltmr.alarm (1, 10 * 1000, 1,
           function ()
               data = getTemps(OWpin)
               loggit(data)
           end
          )


The getTemps() function works fine in the loop. The debug shows the data string containing the temperatures.

The loggit() function works fine only if I comment out getTemps() and upload a small test string literal ("test") but when I leave the code as shown above the upload of data doesn't happen. Debug inside loggit() shows the routine is being called and it's internals are being executed, but no GET happens - my server page doesn't run, and no page data is returned to LUA.

I can't see why.

Is this a timing issue whereby the timer function takes too long? I've tried sprinkling tmr.wdclr() around the place, but it doesn't fix anything.

Any ideas would be appreciated.

Thanks.
User avatar
By ChrixXenon
#10490 OK well here's the code:

Code: Select all
-- I/O Pin constants --
OWpin     = 3

-- Setup I/O pins --
gpio.mode(OWpin, gpio.OUTPUT)

ow.setup(OWpin)

tmr.alarm (1, 10 * 1000, 1,
           function ()
               data = getTemps(OWpin)
               tmr.wdclr()
               loggit(data)
               tmr.wdclr()     
           end
          )

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 loggit()
    print("loggit")
    conn=net.createConnection(net.TCP, false)
    conn:on("receive",
            function(conn, pl)
                print("Receiving page...")
                print(pl)
            end)
 
    conn:on("disconnection",
            function(conn, pl) conn:close() print("disconnected")
            end)

    print("connecting")
    conn:connect(80,"99.99.99.99")
    print("getting page")
    conn:send("GET /serverPage.php HTTP/1.1\r\nHost: myserver.org\r\n"
                 .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
end
 
function getTemps()
      tempsData = ""
      sensorCounter = 1
      addr = ow.reset_search(OWpin)
      addr = ow.search(OWpin)
      while (addr ~= mil) do
            tmr.wdclr()     
            crc = ow.crc8(string.sub(addr,1,7))
            if (crc ~= addr:byte(8)) then print("CRC failed") return nil end
            if ((addr:byte(1) ~= 0x10) and (addr:byte(1) ~= 0x28)) then print("wrong device family") return nil end
            sensor = ""
            for j = 1,8 do
                sensor = sensor .. string.format("%02x", addr:byte(j))
            end
            ow.reset(OWpin)
            ow.select(OWpin, addr)
            ow.write(OWpin, 0x44, 1)
            tmr.delay(1000000)
            present = ow.reset(OWpin)
            ow.select(OWpin, addr)
            ow.write(OWpin,0xBE, 1)
            data = nil
            data = string.char(ow.read(OWpin))
            for i = 1, 8 do
                data = data .. string.char(ow.read(OWpin))
            end
            crc = ow.crc8(string.sub(data,1,8))
            if (crc ~= data:byte(9)) then print("CRC read failed") return nil end
            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
            if  (tempsData ~= "") then tempsData = tempsData .. "&" end
            tempsData = tempsData .. "sensor" .. sensorCounter .. "=" .. t
            sensorCounter = sensorCounter + 1
            addr = ow.search(OWpin)
       end   -- while devices found
       print(tempsData)
       return tempsData
end
User avatar
By sevic71
#10491
ChrixXenon wrote:
Code: Select all

function loggit()
    print("loggit")
    conn=net.createConnection(net.TCP, false)
    conn:on("receive",
            function(conn, pl)
                print("Receiving page...")
                print(pl)
            end)
 
    conn:on("disconnection",
            function(conn, pl) conn:close() print("disconnected")
            end)

    print("connecting")
    conn:connect(80,"99.99.99.99")
    print("getting page")
    conn:send("GET /serverPage.php HTTP/1.1\r\nHost: myserver.org\r\n"
                 .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
end
 


Looks like code mentioned above doesn't send any data to a server. It just connect, send dummy get and hangs up. You should pass data to loggit function and include into GET line your data like below:
[code]
function loggit (data)
...
...
conn:send("GET /serverPage.php?data="..data.." HTTP/1.1\r\nHost: myserver.org\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
....
end
[/dode]
This will send line "GET data=<yourdata>" to /serverPage.php. If you are able to monitor HTTP-requests on server side ("99.99.99.99") this will help to tune-up your request format to match expected one