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

User avatar
By termipl
#22818 Hi
J have problem with this script. J have 12x DS18B20 connected to GPIO 0 and scripts not working for me can everyone help me j just wont display temperature from all ds18b20 on the webpage please help me with that.
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)

J search the internet and found this script but this one print temp only on Esplorer
Code: Select all-- Read temperature from all DS18B20 sensors on the OneWireBus
-- Using NodeMCU 0.9.5 build 20150106

function prinTemp ()

 local pin = 3   -- use GPIO-0
 local tempL, tempH, temp = 0

 local power, nopower = 1, 0

 ow.setup(pin)

 if ow.reset(pin) == 1 then

  counter = counter + 1
  print("Start conversion: " .. counter)

 -- Select all devices on the owbus and start conversion on all devices
 -- power the bus during conversion
  ow.skip(pin)
  ow.write(pin, 0x44, power)

  tmr.wdclr() -- keep the watchdog happy
  tmr.delay(750000) -- wait max 750 msec for conversion to complete

-- start searching DS18B20 (0x28) devices only
  ow.reset_search(pin)
  ow.target_search(pin,0x28)

-- search the first device
  local addr = ow.search(pin)

-- and loop through all devices
  while addr do

   tmr.wdclr()

  -- print(addr:byte(1,8))

   ow.reset(pin)
   ow.select(pin, addr)   -- select the found sensor
   ow.write(pin, 0xBE, nopower) -- READ_SCRATCHPAD

-- read temperature LSB + MSB bytes from sensor
   temp = ( ow.read(pin) + ow.read(pin) * 256 ) * 625

-- stop reading rest of scratchpad ram after the first 2 bytes
   ow.reset(pin)

-- floating point temperature with 1 decimal
   tempH = temp / 10000
   tempL = ( temp % 10000 ) / 1000

-- round up 1st decimal using second decimal
   round = ( temp % 1000 ) / 100
   if round >= 5 then
    tempL = tempL + 1
   end

--   print("T=" .. tempH .. "." .. tempL .. " C - " .. temp)
   print("T=" .. tempH .. "." .. tempL)

-- search next device
   addr = ow.search(pin)

  end

 else

  print("No device present on the OneWireBus.")

 end
end

-- Main progrom starts here

 counter = 0

-- set timer to print temperature every 10 seconds
    tmr.alarm(1, 10 * 1000, 1, prinTemp )


Please Help me :( :( :(