'Module name = "esp_ds18b20"
'Libs: https://github.com/PaulStoffregen/OneWire; https://github.com/milesburton/Arduino-Temperature-Control-Library
'1wire port is defined below in Inline-C code
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Private pin1wire As Pin
Dim pinNumber As Int = 17
Public temperature_ds18b20 As Float 'centigrees
Private Timer1 As Timer
Dim Ready_flag As Boolean
End Sub
Sub Start_ds18b20
Log("Start reading ds18b20...")
pin1wire.Initialize(pinNumber, pin1wire.MODE_INPUT_PULLUP) 'it helps ! But how to pass "pinNumber" to Inline-C correctly ?
RunNative("setup_ds18b20", Null)
Timer1.Initialize("Timer1_Tick", 2000)
Timer1.Enabled = True
End Sub
Sub Stop
Timer1.Enabled = False
Log("ds18b20 stopped")
End Sub
Private Sub Timer1_Tick
RunNative("read_ds18b20",Null)
Log("ds18b20 temp=", others.Numb2(temperature_ds18b20))
Ready_flag = True
End Sub
#if C
// Include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 temperature sensor data wire is plugged into pin 17
#define ONE_WIRE_BUS 17 // SECOND DECLARATION :(((
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors_ds18b20(&oneWire);
void setup_ds18b20 (B4R::Object* unused) {
// Start up the library.
sensors_ds18b20.begin();
sensors_ds18b20.setResolution(12); //ADC resulution 9...12
}
void read_ds18b20 (B4R::Object* unused) {
// Request temperature readings from ALL devices on the bus
sensors_ds18b20.requestTemperatures();
delay(20);
// You can have more than one DS18B20 on the same bus. 0 refers to the first IC on the wire. sensors_ds18b20.getTempCByIndex(0) = °C, sensors_ds18b20.getTempFByIndex(0) = °F
b4r_esp_ds18b20::_temperature_ds18b20 = sensors_ds18b20.getTempCByIndex(0);
}
#End if