Skip to content

Commit 27aa7d4

Browse files
committed
[SD] Multiple DS18b20 events, multiple ds18b20 instances
1 parent f0f574a commit 27aa7d4

File tree

5 files changed

+80
-60
lines changed

5 files changed

+80
-60
lines changed

src/Wippersnapper_demo_wokwi.ino.cpp

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/components/ds18x20/controller.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,14 @@ void DS18X20Controller::update() {
224224
_DS18X20_model->GetDS18x20EventMsg();
225225
pb_size_t event_count = event_msg->sensor_events_count;
226226

227-
// Print the message's content out for debugging
228-
WS_DEBUG_PRINT("DS18x20: OneWire pin: ");
229-
WS_DEBUG_PRINT(temp_dsx_driver.GetOneWirePin());
230-
WS_DEBUG_PRINT(" got value(s): ");
231-
for (int i = 0; i < event_count; i++) {
232-
WS_DEBUG_PRINT(event_msg->sensor_events[i].value.float_value);
233-
}
234-
227+
if (! WsV2._sdCardV2->mode_offline)
228+
{
235229
// Encode the Ds18x20Event message
236230
if (!_DS18X20_model->EncodeDs18x20Event()) {
237231
WS_DEBUG_PRINTLN(
238232
"ERROR | DS18x20: Failed to encode Ds18x20Event message");
239233
continue;
240234
}
241-
242235
// Publish the Ds18x20Event message to the broker
243236
WS_DEBUG_PRINT("DS18x20: Publishing event to broker...");
244237
if (!WsV2.PublishSignal(
@@ -249,6 +242,12 @@ void DS18X20Controller::update() {
249242
continue;
250243
}
251244
WS_DEBUG_PRINTLN("Published!");
245+
} else {
246+
if (!WsV2._sdCardV2->LogDS18xSensorEventToSD(_DS18X20_model->GetDS18x20EventMsg())) {
247+
WS_DEBUG_PRINTLN("ERROR | DS18x20: Failed to log DS18x20 event to SD card");
248+
continue;
249+
}
250+
}
252251

253252
#ifdef DEBUG_PROFILE
254253
unsigned long sensor_end_time = millis();

src/provisioning/sdcard/ws_sdcard.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,19 @@ bool ws_sdcard::waitForSerialConfig() {
458458
"\"sensorTypeCount\": 2,"
459459
"\"sensorType1\": \"ambient-temp-fahrenheit\","
460460
"\"sensorType2\": \"ambient-temp\","
461-
"\"pinName\": \"D6\","
461+
"\"pinName\": \"D12\","
462462
"\"sensorResolution\": 12,"
463-
"\"period\": 900"
463+
"\"period\": 5"
464+
"},"
465+
"{"
466+
"\"componentAPI\": \"ds18x20\","
467+
"\"name\": \"DS18B20: Temperature Sensor (°F)\","
468+
"\"sensorTypeCount\": 2,"
469+
"\"sensorType1\": \"ambient-temp-fahrenheit\","
470+
"\"sensorType2\": \"ambient-temp\","
471+
"\"pinName\": \"D25\","
472+
"\"sensorResolution\": 12,"
473+
"\"period\": 5"
464474
"}"
465475
"]"
466476
"}\\n\r\n";
@@ -626,7 +636,7 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
626636
doc["timestamp"] = timestamp;
627637
doc["pin"] = c_pin_name;
628638
doc["value"] = value;
629-
doc["sensor_type"] = SensorTypeToString(read_type);
639+
doc["si_unit"] = SensorTypeToString(read_type);
630640
serializeJson(doc, Serial);
631641
return true;
632642
}
@@ -665,8 +675,27 @@ bool ws_sdcard::LogGPIOSensorEventToSD(
665675
doc["timestamp"] = timestamp;
666676
doc["pin"] = c_pin_name;
667677
doc["value"] = value;
668-
doc["sensor_type"] = SensorTypeToString(read_type);
678+
doc["si_unit"] = SensorTypeToString(read_type);
669679
serializeJson(doc, Serial);
670680
Serial.println("");
671681
return true;
682+
}
683+
684+
bool ws_sdcard::LogDS18xSensorEventToSD(
685+
wippersnapper_ds18x20_Ds18x20Event *event_msg) {
686+
// Get the RTC's timestamp
687+
uint32_t timestamp = GetTimestamp();
688+
689+
// Create the JSON document
690+
JsonDocument doc;
691+
// Iterate over the event message's sensor events
692+
for (int i = 0; i < event_msg->sensor_events_count; i++) {
693+
doc["timestamp"] = timestamp;
694+
doc["pin"] = event_msg->onewire_pin;
695+
doc["value"] = event_msg->sensor_events[i].value.float_value;
696+
doc["si_unit"] = SensorTypeToString(event_msg->sensor_events[i].type);
697+
serializeJson(doc, Serial);
698+
Serial.println("");
699+
}
700+
return true;
672701
}

src/provisioning/sdcard/ws_sdcard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ws_sdcard {
4545
wippersnapper_sensor_SensorType read_type);
4646
bool LogGPIOSensorEventToSD(uint8_t pin, uint16_t value,
4747
wippersnapper_sensor_SensorType read_type);
48+
bool LogDS18xSensorEventToSD(wippersnapper_ds18x20_Ds18x20Event *event_msg);
4849
// RTC
4950
uint32_t GetTimestamp();
5051
// Logging

tests/diagram.json

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@
3737
"left": 256.4,
3838
"rotate": 270,
3939
"attrs": { "color": "green" }
40+
},
41+
{ "type": "board-ds18b20", "id": "temp1", "top": -145.13, "left": 311.28, "attrs": {} },
42+
{
43+
"type": "wokwi-resistor",
44+
"id": "r1",
45+
"top": -52.8,
46+
"left": 345.05,
47+
"rotate": 90,
48+
"attrs": { "value": "7400" }
49+
},
50+
{ "type": "board-ds18b20", "id": "temp2", "top": -145.13, "left": 167.28, "attrs": {} },
51+
{
52+
"type": "wokwi-resistor",
53+
"id": "r2",
54+
"top": -63.8,
55+
"left": 191.15,
56+
"rotate": 270,
57+
"attrs": { "value": "7400" }
4058
}
4159
],
4260
"connections": [
@@ -57,6 +75,17 @@
5775
[ "bb1:33b.h", "bb1:33b.g", "green", [ "v0" ] ],
5876
[ "rtc1:5V", "esp:VIN", "red", [ "v-19.2", "h-269.2" ] ],
5977
[ "pot1:SIG", "esp:D14", "blue", [ "v67.2", "h-365.2", "v-163.2" ] ],
78+
[ "pot2:GND", "bb1:bn.31", "black", [ "v0" ] ],
79+
[ "pot2:VCC", "bb1:bn.33", "red", [ "v0" ] ],
80+
[ "pot1:GND", "bb1:bn.39", "black", [ "v0" ] ],
81+
[ "pot1:VCC", "bb1:bp.41", "red", [ "v0" ] ],
82+
[ "btn1:1.l", "esp:D4", "violet", [ "v67.2", "h-451.1" ] ],
83+
[ "btn1:2.r", "bb1:bn.46", "black", [ "v-9.8", "h-38.2", "v126.1" ] ],
84+
[ "temp1:GND", "bb1:bn.48", "black", [ "v0" ] ],
85+
[ "temp1:DQ", "r1:1", "green", [ "v0" ] ],
86+
[ "temp1:VCC", "r1:2", "red", [ "v0" ] ],
87+
[ "r1:2", "bb1:bp.50", "red", [ "v56.4", "h-37.6" ] ],
88+
[ "r1:1", "esp:D12", "green", [ "v-76.8", "h-557.5" ] ],
6089
[ "esp:VIN", "bb1:4t.a", "", [ "$bb" ] ],
6190
[ "esp:GND.2", "bb1:5t.a", "", [ "$bb" ] ],
6291
[ "esp:D13", "bb1:6t.a", "", [ "$bb" ] ],
@@ -99,22 +128,22 @@
99128
[ "rtc1:SDA", "bb1:33t.a", "", [ "$bb" ] ],
100129
[ "rtc1:SCL", "bb1:34t.a", "", [ "$bb" ] ],
101130
[ "rtc1:SQW", "bb1:35t.a", "", [ "$bb" ] ],
102-
[ "pot2:GND", "bb1:bn.31", "black", [ "v0" ] ],
103-
[ "pot2:VCC", "bb1:bn.33", "red", [ "v0" ] ],
104-
[ "pot2:GND", "bb1:39b.j", "", [ "$bb" ] ],
105-
[ "pot2:SIG", "bb1:40b.j", "", [ "$bb" ] ],
106-
[ "pot2:VCC", "bb1:41b.j", "", [ "$bb" ] ],
107-
[ "pot1:GND", "bb1:bn.39", "black", [ "v0" ] ],
108131
[ "pot1:GND", "bb1:48b.j", "", [ "$bb" ] ],
109132
[ "pot1:SIG", "bb1:49b.j", "", [ "$bb" ] ],
110133
[ "pot1:VCC", "bb1:50b.j", "", [ "$bb" ] ],
111-
[ "pot1:VCC", "bb1:bp.41", "red", [ "v0" ] ],
134+
[ "pot2:GND", "bb1:39b.j", "", [ "$bb" ] ],
135+
[ "pot2:SIG", "bb1:40b.j", "", [ "$bb" ] ],
136+
[ "pot2:VCC", "bb1:41b.j", "", [ "$bb" ] ],
112137
[ "btn1:1.l", "bb1:55b.i", "", [ "$bb" ] ],
113138
[ "btn1:2.l", "bb1:57b.i", "", [ "$bb" ] ],
114139
[ "btn1:1.r", "bb1:55t.d", "", [ "$bb" ] ],
115140
[ "btn1:2.r", "bb1:57t.d", "", [ "$bb" ] ],
116-
[ "btn1:1.l", "esp:D4", "violet", [ "v67.2", "h-451.1" ] ],
117-
[ "btn1:2.r", "bb1:bn.46", "black", [ "v-9.8", "h-38.2", "v126.1" ] ]
141+
[ "temp2:GND", "bb1:bn.36", "black", [ "v0" ] ],
142+
[ "temp2:DQ", "r2:2", "violet", [ "v0" ] ],
143+
[ "r2:1", "bb1:49t.b", "", [ "$bb" ] ],
144+
[ "temp2:VCC", "r2:1", "red", [ "v0" ] ],
145+
[ "r2:1", "bb1:bp.36", "red", [ "v19.2", "h-38.4", "v106.5" ] ],
146+
[ "esp:D25", "r2:2", "violet", [ "v-124.8", "h364.8" ] ]
118147
],
119148
"serialMonitor": { "display": "terminal", "newline": "lf" },
120149
"dependencies": {}

0 commit comments

Comments
 (0)