Skip to content

Commit 0ed6299

Browse files
Add a ds18b20 sensor task and libraries (#1)
* Added ds18b20 sensor task and libraries * Fixed SS pin issue. See espressif/arduino-esp32#10610
1 parent da3f4a6 commit 0ed6299

10 files changed

+488
-12
lines changed

Diff for: platformio.ini

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ build_flags =
2828
lib_deps =
2929
celliesprojects/MoonPhase@^1.0.3
3030
https://github.com/lovyan03/LovyanGFX#develop
31+
https://github.com/PaulStoffregen/OneWire#dae0fca
32+
https://github.com/milesburton/Arduino-Temperature-Control-Library/archive/refs/tags/3.9.1.zip
3133

3234
upload_speed = 921600
3335
monitor_speed = 115200
@@ -46,7 +48,7 @@ build_flags =
4648
-D LEDPIN_2=40
4749
-D LEDPIN_3=41
4850
-D LEDPIN_4=42
49-
-D SDCARD_SS=10
51+
-D ONE_WIRE_PIN=21
5052
${user.build_flags}
5153
${env.build_flags}
5254

@@ -63,7 +65,7 @@ build_flags =
6365
-D LEDPIN_2=17
6466
-D LEDPIN_3=2
6567
-D LEDPIN_4=5
66-
-D SDCARD_SS=4
68+
-D ONE_WIRE_PIN=26
6769
${user.build_flags}
6870
${env.build_flags}
6971
-D BOARD_HAS_PSRAM

Diff for: src/dimmerTask.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <mutex>
99
#include <moonPhase.h>
1010

11-
#include "lightTime_t.h"
12-
#include "lcdMessage_t.h"
11+
#include "lightTimer.h"
12+
#include "lcdMessage.h"
1313

1414
extern float mapf(const float x, const float in_min, const float in_max, const float out_min, const float out_max);
1515

Diff for: src/fonts/DejaVu24-modded.h

+354
Large diffs are not rendered by default.

Diff for: src/lcdMessage_t.h renamed to src/lcdMessage.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ enum lcdMessageType
66
SET_BRIGHTNESS,
77
LCD_SYSTEM_MESSAGE,
88
UPDATE_LIGHTS,
9-
MOON_PHASE
9+
MOON_PHASE,
10+
TEMPERATURE
1011
};
1112

1213
struct lcdMessage_t

Diff for: src/lcdTask.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "lcdTask.hpp"
2-
#include "lcdMessage_t.h"
32

43
float mapf(const float x, const float in_min, const float in_max, const float out_min, const float out_max)
54
{
@@ -113,6 +112,26 @@ static void updateClock(const struct tm &timeinfo)
113112
clock.pushSprite(0, lcd.height() - font.yAdvance);
114113
}
115114

115+
static void showTemp(const float temperature)
116+
{
117+
const GFXfont &font = DejaVu24Modded;
118+
static LGFX_Sprite temp(&lcd);
119+
if (temp.width() == 0 || temp.height() == 0)
120+
{
121+
temp.setColorDepth(lgfx::palette_2bit);
122+
if (!temp.createSprite(lcd.width(), font.yAdvance))
123+
{
124+
log_e("could not create sprite");
125+
return;
126+
}
127+
}
128+
char buffer[10];
129+
snprintf(buffer, sizeof(buffer), "%.2f°C", temperature);
130+
temp.drawCenterString(buffer, temp.width() >> 1, 6, &font);
131+
temp.pushSprite(0, 15);
132+
133+
}
134+
116135
void lcdTask(void *parameter)
117136
{
118137
lcd.init();
@@ -140,6 +159,10 @@ void lcdTask(void *parameter)
140159
showMoon(msg.float1, msg.int1);
141160
break;
142161

162+
case lcdMessageType::TEMPERATURE:
163+
showTemp(msg.float1);
164+
break;
165+
143166
default:
144167
break;
145168
}

Diff for: src/lcdTask.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#define LGFX_AUTODETECT
55
#include <LGFX_AUTODETECT.hpp>
66

7-
#include "lcdMessage_t.h"
7+
#include "lcdMessage.h"
8+
#include "fonts/DejaVu24-modded.h" /* contains percent sign and a modified superscript 2 - to subscript*/
9+
/* modded with https://tchapi.github.io/Adafruit-GFX-Font-Customiser/ */
810

911
extern float currentPercentage[NUMBER_OF_CHANNELS];
1012

Diff for: src/lightTime_t.h renamed to src/lightTimer.h

File renamed without changes.

Diff for: src/main.cpp

+21-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
#include <mutex>
88

99
#include "secrets.h"
10-
#include "lcdMessage_t.h"
11-
#include "lightTime_t.h"
10+
#include "lcdMessage.h"
11+
#include "lightTimer.h"
1212

1313
extern QueueHandle_t lcdQueue;
1414
extern void lcdTask(void *parameter);
1515
extern void messageOnLcd(const char *str);
1616

17+
extern void sensorTask(void *parameter);
18+
1719
extern void dimmerTask(void *parameter);
1820
extern std::vector<lightTimer_t> channel[NUMBER_OF_CHANNELS];
1921
extern std::mutex channelMutex;
@@ -30,7 +32,7 @@ static void startDimmerTask()
3032
NULL,
3133
4096 * 2,
3234
NULL,
33-
tskIDLE_PRIORITY + 2,
35+
tskIDLE_PRIORITY + 5,
3436
&dimmerTaskHandle);
3537
if (taskResult != pdPASS)
3638
{
@@ -46,6 +48,7 @@ static void ntpCb(void *cb_arg)
4648
sntp_set_time_sync_notification_cb(NULL);
4749
startDimmerTask();
4850
}
51+
4952
static void parseTimerFile(File &file)
5053
{
5154
log_i("parsing '%s'", file.path());
@@ -192,7 +195,7 @@ void setup(void)
192195
SPI.begin(SCK, MISO, MOSI);
193196
SPI.setHwCs(true);
194197

195-
if (!SD.begin(SDCARD_SS))
198+
if (!SD.begin(SS))
196199
log_w("could not mount SD");
197200
else
198201
{
@@ -230,7 +233,7 @@ void setup(void)
230233
NULL,
231234
4096,
232235
NULL,
233-
tskIDLE_PRIORITY,
236+
tskIDLE_PRIORITY + 1,
234237
NULL);
235238
if (result != pdPASS)
236239
{
@@ -239,6 +242,19 @@ void setup(void)
239242
delay(100);
240243
}
241244

245+
result = xTaskCreate(sensorTask,
246+
NULL,
247+
4096,
248+
NULL,
249+
tskIDLE_PRIORITY + 1,
250+
NULL);
251+
if (result != pdPASS)
252+
{
253+
log_e("could not start sensorTask. system halted!");
254+
while (1)
255+
delay(100);
256+
}
257+
242258
messageOnLcd("Wifi connecting...");
243259

244260
while (!WiFi.isConnected())

Diff for: src/sensorTask.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <sensorTask.hpp>
2+
3+
static bool updateTemperature(float temperatureC, float &lastTemperatureC)
4+
{
5+
lcdMessage_t msg;
6+
msg.type = TEMPERATURE;
7+
msg.float1 = temperatureC;
8+
9+
const BaseType_t result = xQueueSend(lcdQueue, &msg, 0);
10+
11+
lastTemperatureC = result ? temperatureC : lastTemperatureC;
12+
13+
log_d("%s lcd temperature %.2f°C", result ? "Updated" : "Could not update", temperatureC);
14+
return result == pdTRUE;
15+
}
16+
17+
void sensorTask(void *parameter)
18+
{
19+
pinMode(ONE_WIRE_PIN, INPUT_PULLUP);
20+
21+
OneWire oneWire(ONE_WIRE_PIN);
22+
DallasTemperature sensor(&oneWire);
23+
24+
sensor.begin();
25+
26+
DeviceAddress sensorAddress;
27+
if (!sensor.getAddress(sensorAddress, 0))
28+
{
29+
log_i("No DS18B20 sensor found. Deleting task.");
30+
vTaskDelete(NULL);
31+
}
32+
sensor.setResolution(sensorAddress, 12);
33+
34+
float lastTemperatureC = DEVICE_DISCONNECTED_C;
35+
int errorCount = 0;
36+
37+
while (1)
38+
{
39+
sensor.requestTemperatures();
40+
41+
vTaskDelay(pdMS_TO_TICKS(750));
42+
43+
const float temperatureC = sensor.getTempC(sensorAddress);
44+
45+
if (temperatureC == DEVICE_DISCONNECTED_C)
46+
{
47+
log_w("Sensor disconnected or error reading temperature.");
48+
49+
if (++errorCount >= MAX_ERROR_COUNT)
50+
{
51+
log_e("Persistent sensor error after %d retries. Deleting task.", MAX_ERROR_COUNT);
52+
vTaskDelete(NULL);
53+
}
54+
}
55+
else
56+
{
57+
errorCount = 0;
58+
59+
if (fabs(temperatureC - lastTemperatureC) > TEMPERATURE_THRESHOLD &&
60+
!updateTemperature(temperatureC, lastTemperatureC))
61+
log_w("Dropped temperature update");
62+
}
63+
}
64+
}

Diff for: src/sensorTask.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef _SENSOR_TASK_
2+
#define _SENSOR_TASK_
3+
4+
#include <OneWire.h>
5+
#include <DallasTemperature.h>
6+
7+
#include "lcdMessage.h"
8+
9+
static constexpr float TEMPERATURE_THRESHOLD = (0.05f);
10+
static constexpr int MAX_ERROR_COUNT = 10;
11+
12+
extern QueueHandle_t lcdQueue;
13+
14+
#endif

0 commit comments

Comments
 (0)