Skip to content

Commit

Permalink
Asynchronous 1-wire reading
Browse files Browse the repository at this point in the history
Currently, 1-wire is read synchronously, which means the program is paused for up to 750ms. During this pause, a yield is called, ensuring that WiFi works correctly. However, other issues may arise, such as UART read buffer overflow.
I have modified the code so that everything operates asynchronously.
The previous solution to this problem contained a bug:
  if ((DALLASASYNC) && ((unsigned long)(millis() - dallasTimer) > ((1000 * dallasTimerWait) - 1000)) ) {
    DS18B20.requestTemperatures(); // get temperatures for next run 1 second before getting the temperatures (async)
  }
The author assumed the code would execute only once, while in reality, it was executed multiple times per second before the reading.
  • Loading branch information
krzbor authored Nov 30, 2024
1 parent 8332c5d commit dfee667
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions HeishaMon/dallas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define MAXTEMPDIFFPERSEC 0.5 // what is the allowed temp difference per second which is allowed (to filter bad values)

#define DALLASASYNC 0 //async dallas yes or no (default no, because async seems to break 1wire sometimes with current code)
#define DALLASASYNC 1 //async dallas yes or no (default no, because async seems to break 1wire sometimes with current code)

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
Expand All @@ -25,6 +25,7 @@ int dallasDevicecount = 0;
unsigned long lastalldatatime_dallas = 0;

unsigned long dallasTimer = 0;
unsigned long dallasTimer1 = 0;
unsigned int updateAllDallasTime = 30000; // will be set using heishmonSettings
unsigned int dallasTimerWait = 30000; // will be set using heishmonSettings
void loadDallasAlias();
Expand Down Expand Up @@ -115,14 +116,20 @@ void readNewDallasTemp(PubSubClient &mqtt_client, void (*log_message)(char*), ch
}

void dallasLoop(PubSubClient &mqtt_client, void (*log_message)(char*), char* mqtt_topic_base) {
if ((DALLASASYNC) && ((unsigned long)(millis() - dallasTimer) > ((1000 * dallasTimerWait) - 1000)) ) {
DS18B20.requestTemperatures(); // get temperatures for next run 1 second before getting the temperatures (async)
}
if ((unsigned long)(millis() - dallasTimer) > (1000 * dallasTimerWait)) {
log_message((char*)"Requesting new 1wire temperatures");
dallasTimer = millis();
readNewDallasTemp(mqtt_client, log_message, mqtt_topic_base);
if (DALLASASYNC){
DS18B20.requestTemperatures();
dallasTimer1=millis();
}else{
readNewDallasTemp(mqtt_client, log_message, mqtt_topic_base);
}
}
if ((dallasTimer1!=0) && ((millis() - dallasTimer1)>750)){
dallasTimer1=0;
readNewDallasTemp(mqtt_client, log_message, mqtt_topic_base);
}
}

void dallasJsonOutput(struct webserver_t *client) {
Expand Down

0 comments on commit dfee667

Please sign in to comment.