B4R Library OneWire - Dallas 1-wire protocol

This library is based on the following open source project: http://www.pjrc.com/teensy/td_libs_OneWire.html

License: https://github.com/PaulStoffregen/OneWire/blob/master/OneWire.cpp

It implements Dallas 1-wire protocol.

Example of reading the temperature:
Depends on rOneWire and rRandomAccessFile libraries.
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private onewire As OneWire
   Private timer1 As Timer
   Private bc As ByteConverter
   Private address(8) As Byte
   Private type_s As Boolean
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   onewire.Initialize(10)
   timer1.Initialize("timer1_Tick", 2000)
   timer1.Enabled = True
End Sub

Sub Timer1_Tick
   If onewire.Search(address) = False Then
     onewire.ResetSearch
     Log("No more addresses.")
     Return
   End If
   Log("ROM = ", bc.HexFromBytes(address))
   If onewire.CRC8(address, 7) <> address(7) Then
     Log("CRC is not valid!")
     Return
   End If
   Select address(0)
     Case 0x10
       Log("Chip = DS18S20")
       type_s = True
     Case 0x28
       Log("Chip = DS18B20")
       type_s = False
     Case 0x22
       Log("Chip = DS1822")
       type_s = False
     Case Else
       Log("Device is not a DS18x20 family device.")
       Return
   End Select
   onewire.Reset
   onewire.Select(address)
   onewire.Write(0x44, True)
   'give it 1 second to read the temperature
   CallSubPlus("ReadTemparature", 1000, 0)
End Sub

Private Sub ReadTemparature (u As Byte)
   onewire.Reset
   onewire.Select(address)
   onewire.Write(0xBE, False)
   Dim data(12) As Byte
   onewire.ReadBytes(data, 9)
   Log("Data = ", bc.HexFromBytes(data))
   Log("CRC = ", bc.HexFromBytes(Array As Byte(onewire.CRC8(data, 8))))
   Dim raw As Int = Bit.Or(Bit.ShiftLeft(data(1), 8), data(0))
   If type_s Then
     raw = Bit.ShiftLeft(raw, 3)
     If data(7) = 0x10 Then
       raw = Bit.And(raw, 0xFFF0) + 12 - data(6)
     End If
   Else
     Dim cfg As Byte = Bit.And(data(4), 0x60)
     If cfg = 0 Then
       raw = Bit.And(raw, Bit.Not(7))
     Else if cfg = 0x20 Then
       raw = Bit.And(raw, Bit.Not(3))
     Else if cfg = 0x40 Then
       Bit.And(raw, Bit.Not(1))
     End If
   End If
   Dim celsius As Double = raw / 16
   Dim fahrenheit As Double = celsius * 1.8 + 32
   Log("Temperature = ", NumberFormat(celsius, 0, 2), " Celsius, " _
     , NumberFormat(fahrenheit, 0, 2), " Fahrenheit")
End Sub

Note that unlike the original C example, the main thread is not blocked here while waiting for the data to be ready.
 

Attachments

  • rOneWire.zip
    13 KB · Views: 1,353

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

just to confirm the above code successfully tested. Log:
B4X:
AppStart
ROM = 28FF5E1804150334
Chip = DS18B20
Data = 780155003FFF3F10CF051000
CRC = CF
Temperature = 23.50 Celsius, 74.30 Fahrenheit
No more addresses.
...
 

rbghongade

Active Member
Licensed User
Longtime User
Confirmed! Works great . Tested in simulation environment though!
 

Attachments

  • onewire_example.png
    onewire_example.png
    68.7 KB · Views: 1,748

cliv

Member
Licensed User
Longtime User
Example of reading the temperature
work well with arduino...but not work with NodeMCU - ESP8266

With 3 sensor connected on pin D4 (with other pins not work) with 4.7kΩ resistor and voltage 5V directly from NodeMCU(i use nodeMcu Base) i try this ... :
https://www.b4x.com/android/forum/t...18b20-via-inline-c-c-again.76331/#post-486017
... but Temperature is not read correctly:

B4X:
ROM = 28FFD97B4A0400B9
Chip = DS18B20
Data = 500555007FFF0C1021000000
CRC = 21
Temperature = 85.00 Celsius, 185.00 Fahrenheit
----------------------------------------------
ROM = 28FF0DFF4E0400B1
Chip = DS18B20
Data = 500555007FFF0C1021000000
CRC = 21
Temperature = 85.00 Celsius, 185.00 Fahrenheit
----------------------------------------------
ROM = 28FFE7FE4C040016
Chip = DS18B20
Data = 500555007FFF0C1021000000
CRC = 21
Temperature = 85.00 Celsius, 185.00 Fahrenheit
----------------------------------------------
 
Last edited:

cliv

Member
Licensed User
Longtime User
Can you show a schematic of the connections? Data pin of 1-wire changes quickly from Output to Input and back to output
Thanks for the reply. I realized my mistake. I use a NodeMcu BASE like :

a7082e87-4965-49c2-aa16-8e7217cc4a2b (1).jpg


...and i take voltage 5V directly from NodeMCU Base ... but this is a mistake.
Now i use 3.3V directly from NodeMCu and everything is OK.

Schematic is this:

mySmatHome.jpg
 

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
I am facing a weird issue. I am using the exact same code given by Erel in first post, using the same circuit as in my post above (#3) , same simulator (proteus) ,but have recompiled the code with B4R 2.51 and not getting to read the sensor! Arduino code in IDE example works OK, meaning the circuit models are fine. Not able to figure out what has changed!


Issue with DS18B20.png
 

rbghongade

Active Member
Licensed User
Longtime User
Many apologies, it must be a model error somehow as the code works perfectly with real hardware, tested with UNO and Wemos D1 R2 both. Nothing to do with B4R release.
 

Laurent95

Active Member
Licensed User
Longtime User
Hello,

I know it's an old thread, but maybe it's better to ask here than create a new thread ?

I just need to know if someone have a link or an idea where i could find a table who give me indications when a device don't work properly on the onewire's bus.
I've several ds18b20, original Dallas and clones, but i can't know if i've one who is not properly wired or a wire miss, in case that one is broken.
Can i get information reading the ROM, data or crc ?
By example if i disconnect the VCC the device continue to work, due to parasite mode i think, but the temperature is really weird ->example, 44°C for 20.5°C/21°C on others :eek:
If i disconnect another wire, didn't remember which one, temperature stay at -0.06°C ???
But i saw that sometime some device on the bus need more time to return valid data, any idea ?

Thanks in advance for any tips, if ever any indications like that exist

Laurent
 
Last edited:

phil31

Member
dear users,

sorry, i'm newbie with BAR !
i try to use this library. i install it and she's right identified in the right window.
but i got an error when i try to declare this object : "unknow type, are you missing a library reference" ?
(line 19 ) ! ??
what is miss please ?

1600121739179.png


thanks, regards
Phil
 
Top