add ds18b20 temperature sensors

This commit is contained in:
interfisch 2023-04-04 07:53:20 +02:00
parent 880c46bc78
commit ca7ccc023d
2 changed files with 204 additions and 20 deletions

View File

@ -14,3 +14,6 @@ board = esp32doit-devkit-v1
framework = arduino
monitor_speed = 115200
lib_deps =
https://github.com/milesburton/Arduino-Temperature-Control-Library/

View File

@ -1,5 +1,7 @@
#include <Arduino.h>
// ######## EC
#define EC_PIN_ADC 4
#define EC_PIN_FREQ 5
#define EC_PWM_CH 0
@ -9,14 +11,61 @@
#define EC_ARRAY_SIZE 1024
uint16_t ec_array[EC_ARRAY_SIZE];
uint16_t ec_array_pos=0;
unsigned long ec_last_read=0;
unsigned long last_read_ec=0;
#define EC_READ_INTERVAL 1
// ######## Temperature
#include <OneWire.h>
#include <DallasTemperature.h>
//first address: 28FF6C1C7216058B
//second address:
#define ONE_WIRE_BUS 18 //GPIO pin
#define TEMPERATURE_PRECISION 12 //max is 12
#define READINTERVAL_DS18B20 1000 //ms
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
#define TEMPMEAN_SIZE 16
uint16_t tempCmean_pos=0;
// arrays to hold device addresses
DeviceAddress thermometerReservoir={0x28,0xFF,0x30,0xBA,0x85,0x16,0x03,0xB5};
float tempC_reservoir;
float tempCmean_reservoir[TEMPMEAN_SIZE];
DeviceAddress thermometerAir={0x28,0xFF,0x6C,0x1C,0x72,0x16,0x05,0x8B};
float tempC_air;
float tempCmean_air[TEMPMEAN_SIZE];
unsigned long last_print=0;
float getMean(uint16_t* parray);
uint16_t getMin(uint16_t *parray);
uint16_t getMax(uint16_t *parray);
float getMean(uint16_t *parray,uint16_t psize);
float getMeanf(float *parray,uint16_t psize);
uint16_t getMin(uint16_t *parray, uint16_t psize);
uint16_t getMax(uint16_t *parray, uint16_t psize);
bool isTempArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck);
bool isTempArrayOKf(float *parray,uint16_t psize, float pcheck);
void printAddress(DeviceAddress deviceAddress);
void printTemperature(DeviceAddress deviceAddress);
void printResolution(DeviceAddress deviceAddress);
void printData(DeviceAddress deviceAddress);
void setup() {
Serial.begin(115200);
@ -25,6 +74,51 @@ void setup() {
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
ledcWrite(EC_PWM_CH, 127);
//initialize mean array
for (uint16_t i=0;i<TEMPMEAN_SIZE;i++) {
tempCmean_reservoir[i]=-127;
tempCmean_air[i]=-127;
}
sensors.begin();
delay(1000);
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
delay(1000);
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
delay(1000);
//Just search for devices. Only needed when connecting a new sensor to find the address
oneWire.reset_search();
for (uint8_t i=0;i<sensors.getDeviceCount();i++){
DeviceAddress _addr;
if (!oneWire.search(_addr)) {
Serial.print("Error: Device not found");
}else{
Serial.print("Found device. Address:");
printAddress(_addr);
}
Serial.println();
}
sensors.setResolution(thermometerReservoir, TEMPERATURE_PRECISION);
sensors.setResolution(thermometerAir, TEMPERATURE_PRECISION);
Serial.println("Setup finished");
delay(500);
}
void loop() {
@ -32,8 +126,8 @@ void loop() {
bool flag_print=false;
if (loopmillis>ec_last_read+EC_READ_INTERVAL) {
ec_last_read=loopmillis;
if (loopmillis>last_read_ec+EC_READ_INTERVAL) {
last_read_ec=loopmillis;
ec_array_pos++;
flag_print= ec_array_pos==EC_ARRAY_SIZE;
ec_array_pos%=EC_ARRAY_SIZE;
@ -42,30 +136,104 @@ void loop() {
//Serial.print(ec_array[ec_array_pos]); Serial.print(" ");
}
static unsigned long last_read_ds18b20;
static bool flag_requestTemperatures=false;
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20) {
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20*10) { //timeout
Serial.println("Warn: Request Temperatures Timeout!");
flag_requestTemperatures=false;
}
if (!flag_requestTemperatures) {
sensors.requestTemperatures();
flag_requestTemperatures=true;
}
if (sensors.isConversionComplete()) {
flag_requestTemperatures=false;
last_read_ds18b20=loopmillis;
//if (flag_print) {
if (loopmillis>last_print+250) {
tempC_reservoir = sensors.getTempC(thermometerReservoir);
if (tempC_reservoir == DEVICE_DISCONNECTED_C)
{
Serial.print(" Error reading: "); printAddress(thermometerReservoir);
}else{
tempCmean_reservoir[tempCmean_pos]=tempC_reservoir;
}
tempC_air = sensors.getTempC(thermometerAir);
if (tempC_air == DEVICE_DISCONNECTED_C)
{
Serial.print(" Error reading: "); printAddress(thermometerReservoir);
}else{
tempCmean_air[tempCmean_pos]=tempC_air;
}
tempCmean_pos++;
tempCmean_pos%=TEMPMEAN_SIZE;
}
}
if (loopmillis>last_print+500) {
last_print=loopmillis;
Serial.print(getMean(ec_array),3);
Serial.print("\t"); Serial.print(getMax(ec_array) - getMin(ec_array));
Serial.print("EC=");
Serial.print(getMean(ec_array,EC_ARRAY_SIZE),3);
Serial.print("\t spread="); Serial.print(getMax(ec_array,EC_ARRAY_SIZE) - getMin(ec_array,EC_ARRAY_SIZE));
if (isTempArrayOKf(tempCmean_reservoir,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)){
Serial.print("\t Treservoir="); Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print("\t Tair="); Serial.print(getMeanf(tempCmean_air,TEMPMEAN_SIZE));
}else{
Serial.print("\t waiting for temperature array");
}
Serial.println();
}
}
float getMean(uint16_t *parray) {
float getMean(uint16_t *parray,uint16_t psize) {
double mean=0;
for (uint16_t i=0;i<EC_ARRAY_SIZE;i++) {
for (uint16_t i=0;i<psize;i++) {
mean+=parray[i];
}
return mean/EC_ARRAY_SIZE;
return mean/psize;
}
float getMeanf(float *parray,uint16_t psize) {
double mean=0;
for (uint16_t i=0;i<psize;i++) {
mean+=parray[i];
}
return mean/psize;
}
bool isTempArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck) { //check if array has error values
for (uint16_t i=0;i<psize;i++) {
if (parray[i]==pcheck){
return false;
}
}
return true;
}
bool isTempArrayOKf(float *parray,uint16_t psize, float pcheck) { //check if array has error values
for (uint16_t i=0;i<psize;i++) {
if (parray[i]==pcheck){
return false;
}
}
return true;
}
uint16_t getMin(uint16_t *parray) {
uint16_t getMin(uint16_t *parray, uint16_t psize) {
uint16_t min=65535;
for (uint16_t i=0;i<EC_ARRAY_SIZE;i++) {
for (uint16_t i=0;i<psize;i++) {
if (parray[i]<min) {
min=parray[i];
}
@ -74,9 +242,9 @@ uint16_t getMin(uint16_t *parray) {
return min;
}
uint16_t getMax(uint16_t *parray) {
uint16_t getMax(uint16_t *parray,uint16_t psize) {
uint16_t max=0;
for (uint16_t i=0;i<EC_ARRAY_SIZE;i++) {
for (uint16_t i=0;i<psize;i++) {
if (parray[i]>max) {
max=parray[i];
}
@ -84,3 +252,16 @@ uint16_t getMax(uint16_t *parray) {
return max;
}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}