|
| 1 | +// https://github.com/junkfix/esp32-ds18b20 |
| 2 | + |
| 3 | +#include "OneWireESP32.h" |
| 4 | +const uint8_t MaxDevs = 2; |
| 5 | +uint8_t tempErr[MaxDevs]; |
| 6 | +uint8_t tempReady[MaxDevs]; |
| 7 | +float currTemp[MaxDevs]; |
| 8 | +uint64_t addr[MaxDevs]; |
| 9 | + |
| 10 | +//uint64_t addr[] = { |
| 11 | +// 0x183c01f09506f428, |
| 12 | +// 0xf33c01e07683de28, |
| 13 | +//}; |
| 14 | + |
| 15 | +void tempTask(void *pvParameters){ |
| 16 | + OneWire32 ds(13); //gpio pin |
| 17 | + |
| 18 | + //to find addresses |
| 19 | + uint8_t devices = ds.search(addr, MaxDevs); |
| 20 | + for (uint8_t i = 0; i < devices; i += 1) { |
| 21 | + Serial.printf("%d: 0x%llx,\n", i, addr[i]); |
| 22 | + //char buf[20]; snprintf( buf, 20, "0x%llx,", addr[i] ); Serial.println(buf); |
| 23 | + } |
| 24 | + //end |
| 25 | + |
| 26 | + for(;;){ |
| 27 | + ds.request(); |
| 28 | + vTaskDelay(750 / portTICK_PERIOD_MS); |
| 29 | + for(byte i = 0; i < MaxDevs; i++){ |
| 30 | + tempErr[i] = ds.getTemp(addr[i], currTemp[i]); |
| 31 | + tempReady[i] = 1; |
| 32 | + } |
| 33 | + vTaskDelay(3000 / portTICK_PERIOD_MS); //sleep for 3 sec |
| 34 | + } |
| 35 | +} // tempTask |
| 36 | + |
| 37 | +void setup() { |
| 38 | + delay(1000); |
| 39 | + Serial.begin(115200); |
| 40 | + xTaskCreatePinnedToCore(tempTask, "tempTask", 2048, NULL, 1, NULL, 0); |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +void loop() { |
| 45 | + |
| 46 | + for(byte i = 0; i < MaxDevs; i++){ |
| 47 | + if(tempReady[i]){ |
| 48 | + tempReady[i] = 0; |
| 49 | + if(tempErr[i]){ |
| 50 | + const char *errt[] = {"", "CRC", "BAD","DC","DRV"}; |
| 51 | + Serial.print(i); Serial.print(": "); Serial.println(errt[tempErr[i]]); |
| 52 | + }else{ |
| 53 | + Serial.print(i); Serial.print(": "); Serial.println(currTemp[i]); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +} |
0 commit comments