Kleines Update:
Ich habe mich nochmal mit der Firmware und der Verkabelung beschäftigt.
Neue Verkabelung:
NC (grau) => D29 (output)
NO (braun) => D2 (output)
COM (weiß) => D1 (input)
Um dauerhaften Stromfluss zu verhindern, liegt Spannung jetzt entweder an NC (D29) oder NO (D2) an. Der Pin wird so ausgewählt, dass kein Strom fließt.
Wenn sich der Zustand des Magnetkontakts ändert, fließt kurzzeitig Strom. Der Puck reagiert das und vertauscht die Polung.
Neue Firmware für den Puck:
! ````
NRF.setScanResponse([
//0x02,
//0x01,0x05, // flags
5, // length (including type byte)
0x09, // name
'T', 'u', 'e', 'r'
]);
! const advertisingData = {
0xEFA1: [0], // magnet sensor
0x180F: [Puck.getBatteryPercentage()], // battery
};
! function send(value) {
advertisingData[0xEFA1] = [value];
NRF.setAdvertising(advertisingData, {showName: false});
}
! // ^^^ BLUETOOTH ^^^
// =================================
// vvv SENSOR CODE vvv
! const pinNC = D29;
const pinNO = D2;
const pinCOM = D1;
! const HIGH = 1;
const LOW = 0;
! let circuit = "NO";
let rawState = null;
let bluetoothState = null; // the state reported over bluetooth
! function writeNO() {
digitalWrite(pinNO, HIGH);
digitalWrite(pinNC, LOW);
circuit = "NO";
console.log("using NO circuit");
}
function writeNC() {
digitalWrite(pinNC, HIGH);
digitalWrite(pinNO, LOW);
circuit = "NC";
console.log("using NC circuit");
}
! function parseState(valCOM) {
valCOM = valCOM || digitalRead(pinCOM) === 1;
if (circuit === "NO") {
// when the NO circuit is used, valCOM is true when the door is open
rawState = valCOM ? "open" : "closed";
} else {
// when the NC circuit is used, valCOM is true when the door is closed
rawState = valCOM ? "closed" : "open";
}
! return rawState;
}
! function isConducting() {
console.log(checking for conductance: circuit=${circuit}, rawState=${rawState});
if (circuit === "NO" /* NO / && rawState === "open" / open /) return true;
if (circuit === "NC" / NC / && rawState === "closed" / closed /) return true;
return false;
}
! function switchCircuit() {
console.log("switching circuit");
if (circuit === "NO") { // NO
writeNC();
} else { // NC
writeNO();
}
}
! // set NC and NO pin to output, COM pin to input with pulldown
pinMode(pinNC, "output");
pinMode(pinNO, "output");
pinMode(pinCOM, "input_pulldown");
! function onChange(e) {
// parse the state depending on the current circuit;
parseState(e.state);
console.log("pin state changed: " + rawState);
// switch circuit if neccessary
if (isConducting()) switchCircuit();
! // do the switch stuff
if (rawState == "open" && bluetoothState != "open") {
// magnet open
bluetoothState = "open";
console.log(bluetoothState);
digitalPulse(LED2, 1, 500);
send(1);
} else if (rawState == "closed" && bluetoothState != "closed") {
// magnet closed
bluetoothState = "closed";
console.log(bluetoothState);
digitalPulse(LED1, 1, 500);
send(0);
}
}
! // watch the input for changes
setWatch(onChange, pinCOM, {
repeat: true,
debounce: 150
});
// also update the battery level every hour
setInterval(() => {
advertisingData[0x180F] = [Puck.getBatteryPercentage()];
send();
}, 36001000);
! // initially try to close the NO circuit
writeNO();
onChange({
state: digitalRead(pinCOM) === 1
});