/* ============================================
code is placed under the MIT license
Copyright (c) 2020 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
// I manually added in diagram.json the attribute "deviceID": "xxxxxxxxxxxx" (6 hex bytes)
// for each sensor. Tthat will form the base of the address (adding 0x10 in front and the crc8 at the end for 8 bytes)
#include <OneWire.h>
constexpr byte oneWirePin = 23;
OneWire onewire(oneWirePin);
void setup(void) {
byte addr[8];
byte ds18b20Count = 0;
Serial.begin(115200);
onewire.reset_search(); // start fresh
Serial.println(F("struct Ds18b20 {\n const char * name;\n const uint8_t address[8];\n};"));
Serial.println(F("constexpr Ds18b20 sensors[] = {"));
while (onewire.search(addr)) {
if (OneWire::crc8( addr, 7) != addr[7]) continue; // CRC is not valid
if (addr[0] != 0x10) continue; // Device is not a DS18S20 family device
// valid DS18S20, record the address
Serial.print(F(" {\"sensor")); Serial.print(ds18b20Count); Serial.print(F("\", {"));
for (byte i = 0; i < 8; i++) {
Serial.print(F("0x"));
if (addr[i] < 0x10) Serial.write('0');
Serial.print(addr[i], HEX);
if (i < 7) Serial.print(F(", ")); else Serial.println(F("}},"));
}
ds18b20Count++;
}
Serial.print(F("};\nconstexpr byte sensorCount = sizeof sensors / sizeof * sensors; // "));
Serial.println(ds18b20Count);
}
void loop(void) {}
s1
s2
s3
s4
s5