USB thermometer using DS18B20 as a temperature sensor.
The temperature can be displayed in Chrome, Edge and Opera browsers https://developer.chrome.com/en/articles/serial/#browser-support . Run the Arduino connected to PC, open file web_thermo.html in the browser and click Connect. For temperature below zero the display is in blue. The html includes javascript.
The temperature can be displayed in Chrome, Edge and Opera browsers https://developer.chrome.com/en/articles/serial/#browser-support . Run the Arduino connected to PC, open file web_thermo.html in the browser and click Connect. For temperature below zero the display is in blue. The html includes javascript.
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private AStream As AsyncStreams
Private onewire As OneWire
Private timer1 As Timer
Private address(8) As Byte
Private type_s As Boolean
End Sub
Private Sub AppStart
Serial1.Initialize(9600)
AStream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
onewire.Initialize(10)
timer1.Initialize("timer1_Tick", 2000)
timer1.Enabled = True
End Sub
Sub Timer1_Tick
If onewire.Search(address) = False Then
onewire.ResetSearch
Return
End If
If onewire.CRC8(address, 7) <> address(7) Then
Return
End If
Select address(0)
Case 0x10
type_s = True
Case 0x28
type_s = False
Case 0x22
type_s = False
Case Else
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)
Dim ps(8) As Byte
Dim temp As String
onewire.Reset
onewire.Select(address)
onewire.Write(0xBE, False)
Dim data(12) As Byte
onewire.ReadBytes(data, 9)
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
'raw=0xFF5E '-10.125
Dim celsius As Double = raw / 16
temp = celsius
ps = temp.GetBytes
AStream.Write(" ")
AStream.Write(ps)
If celsius < 0 Then AStream.Write("!")
AStream.Write("C")
End Sub