onewire

Top  Previous  Next

The onewire table supplies support for the Maxim Integrated line of 1-Wire hardware. The support here is built with the 1-Wire public domain kit as such you can find example on how to use these function by following the examples in the kit. This is most definitely an advanced topic.

 

Examples

Opening a 1-Wire connection

The first examples shows how to open a 1-Wire connection using a DS2490 ( USB connected 1-Wire hardware )

 

if portnum then

    print("Already opened")

    return

end

 

local devices = onewire.DS2490.names()

 

if #devices == 0 then

    print("No 1-Wire devices.")

    return

end

 

table.print(devices)

 

portnum = onewire.DS2490.acquire( devices[1] )

print(portnum)

if portnum <= 0 then

    portnum = nil

    print("Could not open.")

end

 

Once this script runs a global variable called portnum will be set. This is your key to using the rest of the onewire functions.

 

Listing 1-Wire devices

This code prints a list of serial numbers of connected 1-Wire devices.

if not portnum then

    print("Port not opened.")

    return

end

 

 

local result = onewire.owFirst(portnum, true, false)

print("reuslt", result)

while ( result ) do

    print( onewire.owSerialNum( portnum ) )

    result = onewire.owNext(portnum, true, false)

end

Read temperature

The code below reads the temperature of the 1-Wire device with serial number DA000802A0AB5410.

print(onewire.DS1920.getTemperature(portnum, "DA000802A0AB5410", 1000))

Read humidity

While 1-Wire doesn't directly support humidity measurements there is hardware out there that uses a DS2438 together with a humidity sensor hooked to it's A2D port. (For example the HT3-R1-A from Hobby Boards does this ).The formula below will provide you with humidity.

 

local snum = "BD00000121AF4826"

local vdd = onewire.DS2438.readAtoD(portnum, true, snum)

local add = onewire.DS2438.readAtoD(portnum, false, snum)

 

print(vdd, add)

temp = onewire.DS2438.getTemperature(portnum, snum)

 

humid = (((add/vdd) - 0.16)/0.0062)/(1.0546 - 0.00216 * temp);

if(humid > 100)then

  humid = 100;

elseif(humid < 0) then

  humid = 0;

end