Read temperature with DS18B20
Posted: Fri Dec 06, 2024 9:37 pm
Dear Purebasic programmers,
I was frustrated to obtain a good temperature measuring system for my home-beer-brewing system.
During several months I tested a lot of listings, not only in Purebasic but also in several other basic-like compilersystems.
A lot of the listings did not work at all, or in best case they worked but after a time the execution they stopt execution because of errors.
Most errors where file errors (file not found, could not open file, EOF errors and so on...
This is now one thing we do not will obtain in our listings for/with a temperature measuring system.
So I decide to experiment my self.
The listing I deal with You, is tested with 8000 loops, each reading 3 DB18B20 sensors, during 5,5 hours (= 24.000 times reading a DB18B20 file).
And this without obtaining no one error.
Looking forwards for Your comments (and possible tips to make the listing better...)
Greatings,
Rudy M
I was frustrated to obtain a good temperature measuring system for my home-beer-brewing system.
During several months I tested a lot of listings, not only in Purebasic but also in several other basic-like compilersystems.
A lot of the listings did not work at all, or in best case they worked but after a time the execution they stopt execution because of errors.
Most errors where file errors (file not found, could not open file, EOF errors and so on...
This is now one thing we do not will obtain in our listings for/with a temperature measuring system.
So I decide to experiment my self.
The listing I deal with You, is tested with 8000 loops, each reading 3 DB18B20 sensors, during 5,5 hours (= 24.000 times reading a DB18B20 file).
And this without obtaining no one error.
Looking forwards for Your comments (and possible tips to make the listing better...)
Greatings,
Rudy M
Code: Select all
EnableExplicit
;Measuring T is important, so always use EnableExplicit!!!
; ===============================
;This listing Read one or more DS18B20 without using PIGPIO or other libs.
;=========================================================================
;I saw a lot of listings on the internet, but lot of them did not work good or gave errors.
;Those using libs were sometimes to complicated.
;So I was very frustrated to obtain no good working T-measurement.
;So I decide to make my own listing.
;Purebasic surprised my with an (till to day!!!) errorless working T-measuring.
;So I will deal with You my listing.
;If You can make something better or efficienter, please deal it on this forum.
;Tested on RasperryPI3 Model B+ met Linux en xfce desktop:
;Testtime: 3 ours, continue reading 3 DB18B20 sensors... Works without errors!
;I let run this listing during 5 1/2 hours with 8000 loops, each with 3 DB18B20 sensors.
;I'm very glad to announce I became:
;24000 times a reading of the DSB18B20 file without no one error!
;================================================================
;Do not forget there is always a sensor-toleration for temperature-result AND for responstime.
;So, if You use 1 DB18B20 (only 1 responstime!!!) You need to incorporate one or more delay()
;statements in Your listing to obtain no errors. In that case, You need to test it out Yourself.
;In case of program errors during runtime,
;then test again With Delay(xxx) milliseconds, see listing
;Tip: start With 25 milliseconds and increase of necessery.
;AND... test again during minimal several ours!!! (I take 3 ours as absolute minimum test time).
;Do such test again when You replace a sensor.
;=====================================Disclaimer =========================================
; Do all your test very carefully And punctual.
;=========================================================================================
; With a failing temperature-control, You easely obtain very dangerous situations!
;=========================================================================================
;For all Your home made constructions: Read and follow all rules and laws of Your country.
;=========================================================================================
;P.e.: All metal parts of my home made beerboiling installations have perfect earth connections.
Define.s PathName , FullPathName, Path_to_w1slave, sensormap
Define.s Temperature_string, content
Define.l File, start, sensornr, test, x
Define.l totalSeconds , hr, min, sec, MaxNumberSensors
Define.d Temperature
Define.q Tstart,Telapsed,Tmeetpunt
;Full pathname of my sensor3 is:
;Path_to_w1slave = "/sys/devices/w1_bus_master1/28-3c01d60706a2/w1_slave"
; xxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxx = the sensormap
; Replace white those of Your specifieke sensor(s) in the select case below.
;Tip: In Linux You can also search in terminal with "locate w1_slave" to find the path
MaxNumberSensors = 3
;For my beerbrewing I only use 3 sensors
;1 in my beervessel, 1 in my watercooker, 1 in my plate cooler (to not spoil water...).
;Line below for testing purposes
Tstart = ElapsedMilliseconds()
;For next loop for testpurpose.
For test = 1 To 50
For sensornr = 1 To MaxNumberSensors
Select sensornr
Case 1
sensormap = "28-0316a279f80d"
Case 2
sensormap = "28-0319a27920d7"
Case 3
sensormap = "28-3c01d60706a2"
Case 4
sensormap = "28-3c01d607bcca"
Case 5
sensormap = "28-3c01d607e340"
Default
sensormap = "28-0316a279f80d" ;I take sensor 1 as Default sensor (my beervessel...)
EndSelect
FullPathName = "/sys/devices/w1_bus_master1/" + sensormap +"/w1_slave"
File = ReadFile(#PB_Any, FullPathName)
;Delay (50)
If File
Repeat
Temperature = #False
content = ReadString(File)
;Delay (50)
;Debug content , for testpurpose
start = FindString(Content, "t=", 1)
If start > 0
Temperature_string = Mid(Content, start + 2, 7) ;+2 because we do'nt need t=
Temperature = ValF(Temperature_string) / 1000 ; Convert to centigrade Celsius
Temperature = Round (Temperature,#PB_Round_Nearest) ;1°C is OK for my beerbrewing installation
If Temperature <> #False
Debug Temperature
; First possibility to store here the temp-data:
; P.e.: Store temperatures in a global array to use elders in Your programma.
; See further: "Second possibility"
Else
Debug "Could not measure Temperature"
Break
EndIf
Break
EndIf
Until Eof(File)
CloseFile(File)
Else
Debug "Could not read sensor number :" +Str (sensornr)
Debug "Could not open sensorfile: "
Debug FullPathName
Break
; P.E.: For safety call here a procedure,
; play an alarmsound, swith off all heaterelements, close all taps etc,etc...
EndIf
Next sensornr
;Second Possibility to call from here procedures.
;P.E. Switch heaterelements on or off.
;The outer For next loop could be p.e. a Repeat-Forever or Repeat-while and other.
Next test ;For next loop for testpurpose, see other comments.
;Measuring time, for tespurpose:
;Telapsed = ElapsedMilliseconds() - Tstart
;totalSeconds=Telapsed/1000
;hr = totalSeconds/3600
;min = (totalSeconds - 3600*hr) / 60
;sec = totalSeconds - 3600*hr - 60*min
;Debug Str(hr) + " uur " + Str(min) + " min " + Str(sec) + " sec"
;Other possibility to obtain data on Your screen:
;P.E.: MessageRequester("Temperature", Temp, #PB_MessageRequester_Ok)