Other OneWire (DallasTemperature)

Erel

B4X founder
Staff member
Licensed User
Longtime User
Working library is available here: https://www.b4x.com/android/forum/threads/onewire-dallas-1-wire-protocol.66672/

rWireOne library wraps the following libraries:
https://github.com/milesburton/Arduino-Temperature-Control-Library
http://www.pjrc.com/teensy/td_libs_OneWire.html

It allows reading the temperature from one wire temperature sensors (see the first link for more details).

This is a beta version.

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private deviceAddress() As Byte = Array As Byte(0x28, 0xFF, 0x5E, 0x18, 0x04, 0x15, 0x03, 0x34)
   Private dt As DallasTemperature
   Private timer1 As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   dt.Initialize(3)
   dt.SetResolution(deviceAddress, 10)
   timer1.Initialize("timer1_Tick", 250)
   timer1.Enabled = True
End Sub

Sub Timer1_Tick
   dt.RequestTemperatures
   Dim tempC As Double = dt.GetTempC(deviceAddress)
   If tempC = -127 Then
     Log("Error getting temperature.")
     Log(dt.ToFahrenheit(tempC))
     Log(dt.ToCelsius(dt.ToFahrenheit(tempC)))
   Else
     Log("Temperature: C: ", tempC, ", F: ", dt.ToFahrenheit(tempC))
   End If
End Sub
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

tested the B4R code and receiving error:
B4X:
Error getting temperature.
Celsius: -127
Fahrenheit: -196.6000
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
The device address in your example is hardcoded. It would make the library more versatile if address search method is exposed. pardon me if it is already done. I just had a cursory glance at the example.
PS:Same result as reported by Rob, even after changing the device address and pin.
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I have one, i only need to find it, lol... I will also test it...

Any chance for a "one wire" dedicated lib, so we can control 1 wire devices like the adressable LEDs?
 
Upvote 0

raphaelcno

Active Member
Licensed User
Longtime User
B4X:
Sub Timer1_Tick
   dt.RequestTemperatures
   Dim tempC As Double = dt.GetTempC(deviceAddress)
   [...]
End Sub

I have not tested the code yet, but I just had a look at the code here and was thinking about a previous Arduino project where I was using several DS18B20 temperature sensors.
I see that you send "GetTempC" right after "RequestTemperatures".
Maybe there should be a delay (up to 750 ms when using the highest resolution) between these 2 commands.

I had an Arduino program with a sequence (finite-state machine) approximately like this:
- Send request ("Convert"/0x44) to all sensors.
- Wait 1 second (not blocking the rest of the code)
- Send "Read Scratchpad" (0xBE) to sensor #1, then extract the temperature data.
- Next loop: The same for sensor #2
- ...
- Next loop: The same for sensor #n
- Wait 4 seconds (not blocking)
- And back to start (Send request to all sensors)
That means the temperatures are measured every 5 seconds (+ a few milliseconds...).

I preferred using the OneWire library alone, without the Dallas library, because the Dallas library contained some hard coded delays blocking the rest of the code. I'm not completely updated, so maybe the most recent Dallas library has been changed.

2016-04-21 - B4R Forum - DS18B20 Resolution + Conversion time.png

Datasheet:
http://www.mouser.com/ds/2/256/DS18B20-PAR-315769.pdf
 
Upvote 0

raphaelcno

Active Member
Licensed User
Longtime User
I see that you send "GetTempC" right after "RequestTemperatures".
Maybe there should be a delay (up to 750 ms when using the highest resolution) between these 2 commands.

I see now in the file "DallasTemperature.cpp" that the delay is included in the command "RequestTemperatures":

// sends command for all devices on the bus to perform a temperature conversion
void DallasTemperature::requestTemperatures(){

_wire->reset();
_wire->skip();
_wire->write(STARTCONVO, parasite);

// ASYNC mode?
if (!waitForConversion) return;
blockTillConversionComplete(bitResolution, NULL);

}

// Continue to check if the IC has responded with a temperature
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution, const uint8_t* deviceAddress){

int delms = millisToWaitForConversion(bitResolution);
if (deviceAddress != NULL && checkForConversion && !parasite){
unsigned long now = millis();
while(!isConversionAvailable(deviceAddress) && (millis() - delms < now));
} else {
delay(delms);
}

}

// returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution){

switch (bitResolution){
case 9:
return 94;
case 10:
return 188;
case 11:
return 375;
default:
return 750;
}

}
 
Upvote 0

George Anifantakis

Member
Licensed User
Longtime User
Working library is available here: https://www.b4x.com/android/forum/threads/onewire-dallas-1-wire-protocol.66672/

rWireOne library wraps the following libraries:
https://github.com/milesburton/Arduino-Temperature-Control-Library
http://www.pjrc.com/teensy/td_libs_OneWire.html

It allows reading the temperature from one wire temperature sensors (see the first link for more details).

This is a beta version.

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private deviceAddress() As Byte = Array As Byte(0x28, 0xFF, 0x5E, 0x18, 0x04, 0x15, 0x03, 0x34)
   Private dt As DallasTemperature
   Private timer1 As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   dt.Initialize(3)
   dt.SetResolution(deviceAddress, 10)
   timer1.Initialize("timer1_Tick", 250)
   timer1.Enabled = True
End Sub

Sub Timer1_Tick
   dt.RequestTemperatures
   Dim tempC As Double = dt.GetTempC(deviceAddress)
   If tempC = -127 Then
     Log("Error getting temperature.")
     Log(dt.ToFahrenheit(tempC))
     Log(dt.ToCelsius(dt.ToFahrenheit(tempC)))
   Else
     Log("Temperature: C: ", tempC, ", F: ", dt.ToFahrenheit(tempC))
   End If
End Sub


Hi, why using this library i am getting an error at definition dim dt As DallasTemperature
Error description: Unknown type: dallastemperature
Are you missing a library reference?
Error occurred on line: 10 (Main)
Private dt As DallasTemperature

I am using library rOneWire
 
Upvote 0
Top