Chat freely about anything...

User avatar
By termipl
#22821 Hi I have a problem with displaying the values of the sensors DS18B20 on the website you want to connect 6 sensors to one port DS18B20 gpio how I do device does not respond as connection of 2 sensors the device to pin gpio0 appear on the page two values and it works but, as already connection of more than two device stops responding below please find the code can someone help me in this topic please ...
Code: Select allpin = 3
ow.setup(pin)

counter=0
temps={}

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()
      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,8 do sensor = sensor .. string.format("%02x", addr:byte(j)) end
                ow.reset(pin)
                ow.select(pin, addr)
                ow.write(pin, 0x44, 1)
                tmr.delay(1000000)
                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
                   temps[sensor] = t
                   print(sensor .. ": " .. t)
                end                   
                tmr.wdclr()
          end
        end
      end
      addr = ow.search(pin)
      until(addr == nil)
end

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    getTemp()
    fff = wifi.sta.getmac() .. "\n"
    for sensorID, temperature in pairs(temps) do
        t1 = temperature / 10000
        t2 = (temperature >= 0 and temperature % 10000) or (10000 - temperature % 10000)
        headers = "HTTP/1.0 200 OK\nContent-Type: text/html\nConnection:close\n"
        result = t1 .. "." .. string.format("%04d", t2)
        headers = headers .. "Content-Length:".. string.len(result) .."\r\n\r\n"

        output = headers .. result
    end

    conn:send(output)
  conn:on("sent",function(conn) conn:close() end)
end)

Please Help.