// NodeMCU v3 pin map. Zero external wiring needed for the PoC: // relay 1 -> D1 (GPIO5) + mirrored on the on-board LED (GPIO2, active LOW) // relay 2 -> D2 (GPIO4) (the watchdog's "PC power" relay) // relay 3 -> D5 (GPIO14) // relay 4 -> D6 (GPIO12) // input 1 -> the on-board FLASH button (GPIO0, pulled up, LOW when pressed) // input 2 -> D7 (GPIO13), INPUT_PULLUP: short to GND to "press" // inputs 3-4 are virtual and idle HIGH. #include "io.h" static const int8_t RELAY_PIN[CHANNELS] = {5, 4, 14, 12}; static const int8_t INPUT_PIN[CHANNELS] = {0, 13, -1, -1}; static const uint8_t LED_PIN = 2; void Io::begin() { for (int i = 0; i < CHANNELS; i++) { pinMode(RELAY_PIN[i], OUTPUT); digitalWrite(RELAY_PIN[i], LOW); if (INPUT_PIN[i] >= 0) pinMode(INPUT_PIN[i], INPUT_PULLUP); } pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, HIGH); // LED off } void Io::setRelay(int ch, bool on) { if (ch < 1 || ch > CHANNELS) return; relays_[ch - 1] = on; digitalWrite(RELAY_PIN[ch - 1], on ? HIGH : LOW); if (ch == 1) digitalWrite(LED_PIN, on ? LOW : HIGH); } bool Io::relay(int ch) const { return ch >= 1 && ch <= CHANNELS && relays_[ch - 1]; } bool Io::inputHigh(int ch) { if (ch < 1 || ch > CHANNELS) return true; int8_t pin = INPUT_PIN[ch - 1]; return pin < 0 ? true : digitalRead(pin) == HIGH; } uint8_t Io::relayMask() const { uint8_t m = 0; for (int i = 0; i < CHANNELS; i++) if (relays_[i]) m |= 1 << i; return m; } uint8_t Io::inputMask() { uint8_t m = 0; for (int i = 0; i < CHANNELS; i++) if (inputHigh(i + 1)) m |= 1 << i; return m; }