UserDataWriteBatch.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // FILE: UserDataWriteBatch.ino
  3. // AUTHOR: Rob Tillaart
  4. // VERSION: 0.1.0
  5. // PURPOSE: use of alarm field as user identification demo
  6. // DATE: 2019-12-23
  7. // URL:
  8. //
  9. // Released to the public domain
  10. //
  11. #include <OneWire.h>
  12. #include <DallasTemperature.h>
  13. #define ONE_WIRE_BUS 2
  14. OneWire oneWire(ONE_WIRE_BUS);
  15. DallasTemperature sensors(&oneWire);
  16. uint8_t deviceCount = 0;
  17. void printAddress(DeviceAddress deviceAddress)
  18. {
  19. for (uint8_t i = 0; i < 8; i++)
  20. {
  21. // zero pad the address if necessary
  22. if (deviceAddress[i] < 16) Serial.print("0");
  23. Serial.print(deviceAddress[i], HEX);
  24. }
  25. }
  26. void setup(void)
  27. {
  28. Serial.begin(115200);
  29. Serial.println(__FILE__);
  30. Serial.println("Write user ID to DS18B20\n");
  31. sensors.begin();
  32. // count devices
  33. deviceCount = sensors.getDeviceCount();
  34. Serial.print("#devices: ");
  35. Serial.println(deviceCount);
  36. Serial.println();
  37. Serial.println("current ID's");
  38. for (uint8_t index = 0; index < deviceCount; index++)
  39. {
  40. DeviceAddress t;
  41. sensors.getAddress(t, index);
  42. printAddress(t);
  43. Serial.print("\t\tID: ");
  44. int id = sensors.getUserData(t);
  45. Serial.println(id);
  46. }
  47. Serial.println();
  48. Serial.print("Enter ID for batch: ");
  49. int c = 0;
  50. int id = 0;
  51. while (c != '\n' && c != '\r')
  52. {
  53. c = Serial.read();
  54. switch(c)
  55. {
  56. case '0'...'9':
  57. id *= 10;
  58. id += (c - '0');
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. Serial.println();
  65. Serial.println(id);
  66. Serial.println();
  67. Serial.println("Start labeling ...");
  68. for (uint8_t index = 0; index < deviceCount; index++)
  69. {
  70. Serial.print(".");
  71. DeviceAddress t;
  72. sensors.getAddress(t, index);
  73. sensors.setUserData(t, id);
  74. }
  75. Serial.println();
  76. Serial.println();
  77. Serial.println("Show results ...");
  78. for (uint8_t index = 0; index < deviceCount; index++)
  79. {
  80. DeviceAddress t;
  81. sensors.getAddress(t, index);
  82. printAddress(t);
  83. Serial.print("\t\tID: ");
  84. int id = sensors.getUserData(t);
  85. Serial.println(id);
  86. }
  87. Serial.println("Done ...");
  88. }
  89. void loop(void) {}
  90. // END OF FILE