I couldn't get either the DS2xxx module or the code examples in this thread to work for writing to a DS2431, so I ended up porting some Java code I found to JavaScript and writing worked
// Used with GeekHeart https://www.tindie.com/products/MakersBox/geekheart/
var ow = new OneWire(4) // D2 on NodeMCU
var code = ow.search()[0] // will be empty array if it failed to find your DS2431
// write a Uint8Array of arbitrary length 8 bytes at a time with a 100ms delay (add callback if you need it)
function write (data) {
var buf = new Uint8Array(13)
var row = 0
function next () {
var pos = row * 8;
for (var i = 0; i < 8; i++) {
buf[i] = data[pos + i]
}
write8(pos, buf);
if (pos + 8 >= data.length) return console.log('done');
row++;
if (row >= 16) return console.log('done'); // EEPROM has 16 rows of 8 bytes
setTimeout(next, 100);
}
next()
}
// write 8 bytes to addr offset
// example write8(0, new Uint8Array([1,1,1,1,1,1,1,1)
function write8 (addr, data) {
console.log(addr, data)
var i
ow.reset();
ow.select(code);
ow.write(0x0F, 1); // Write ScratchPad
ow.write(addr, 1);
ow.write(0x00, 1);
for ( i = 0; i < 8; i++)
ow.write(data[i], 1);
ow.reset();
ow.select(code);
ow.write(0xAA); // Read Scratchpad
for ( i = 0; i < 13; i++)
data[i] = ow.read();
ow.reset();
ow.select(code);
ow.write(0x55, 1); // Copy ScratchPad
ow.write(data[0], 1);
ow.write(data[1], 1); // Send TA1 TA2 and ES for copy authorization
ow.write(data[2], 1);
}
// read num bytes from addr offset
// example read(0, 8)
function read(addr, num) {
ow.reset();
ow.select(code);
ow.write(0xF0); // read
ow.write(addr&0xFF);
ow.write(addr>>16);
var buf = new Uint8Array(num)
for (var i = 0; i < num; i++) buf[i] = ow.read()
return buf
}
// same as read() but decodes into string
function readString (addr, num) {
var buf = read(addr, num)
var str = ''
for (var i = 0; i < buf.length; i++) {
str += String.fromCharCode(buf[i])
}
return str
}
// write a long string starting at addr 0
function writeString (str) {
var data = new Uint8Array(str.split('').map(function (c){ return c.charCodeAt(0) }))
write(data)
}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I couldn't get either the DS2xxx module or the code examples in this thread to work for writing to a DS2431, so I ended up porting some Java code I found to JavaScript and writing worked
https://gist.github.com/maxogden/c0b33da285e0fe809a8aa7009bc510f7