1-Wire – Apparaat ID’s uitlezen (Arduino)
Er is op het Arduino forum een script te vinden voor het uitlezen van 1-Wire apparaten met behulp van de OneWire bibliotheek:
Wat heb je nodig?
Het script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// This sketch looks for 1-wire devices and // prints their addresses (serial number) to // the UART, in a format that is useful in Arduino sketches // Tutorial: // http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html #include <OneWire.h> OneWire ds(3); // Connect your 1-wire device to pin 3 void setup(void) { Serial.begin(9600); discoverOneWireDevices(); } void discoverOneWireDevices(void) { { byte i; byte present = 0; byte done = 0; byte data[12]; byte addr[8]; while ( !done ) { if ( ds.search(addr) != 1) { Serial.print("No more addresses.\n"); ds.reset_search(); done = 1; return; } else { Serial.print("Unique ID="); for( i = 1; i < 7; i++) { Serial.print(addr[i], HEX); Serial.print(":"); } Serial.println(); } delay(100); } } return; } void loop(void) { } |
Met mijn iButton DS1982 geeft dit als output:
Een “verbeterde” versie welke de ID’s in HEX codes weergeeft is hier te vinden, dit is de code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// This sketch looks for 1-wire devices and // prints their addresses (serial number) to // the UART, in a format that is useful in Arduino sketches // Tutorial: // http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html #include <OneWire.h> OneWire ds(3); // Connect your 1-wire device to pin 3 void setup(void) { Serial.begin(9600); discoverOneWireDevices(); } void discoverOneWireDevices(void) { byte i; byte present = 0; byte addr[8]; Serial.print("Looking for 1-Wire devices...\n\r"); while(ds.search(addr)) { Serial.print("\n\rFound \'1-Wire\' device with address:\n\r"); for( i = 0; i < 8; i++) { Serial.print("0x"); if (addr[i] < 16) { Serial.print('0'); } Serial.print(addr[i], HEX); if (i < 7) { Serial.print(", "); } } if ( OneWire::crc8( addr, 7) != addr[7]) { Serial.print("CRC is not valid!\n"); return; } } Serial.print("\n\r\n\rThat's it.\r\n"); ds.reset_search(); return; } void loop(void) { // nothing to see here } |
Met mijn iButton DS1982 geeft dit als output: