diff --git a/fan-controller/fan-controller.ino b/fan-controller/fan-controller.ino index 524bfd9..c7c98d1 100644 --- a/fan-controller/fan-controller.ino +++ b/fan-controller/fan-controller.ino @@ -1,9 +1,33 @@ #include +#include #include "credentials.h" +#define APPLY_PERIOD 100 +#define COOLDOWN_MIN 29000 +#define COOLDOWN_MAX 30000 + WiFiServer server(1337); +enum IRCommands { + UP = 0xF20DF20DFE01, + DOWN = 0xEE11EE11FE01, + PWR = 0xFC03FC03FE01, +}; + +enum Commands { + SPEED = 0x01, + POWER = 0x02, +}; + +byte targetSpeed = 1; +byte currentSpeed = 1; +bool currentPowerStatus = true; +bool targetPowerStatus = true; + +long lastApplied = -APPLY_PERIOD; +long lastWakeUp = -COOLDOWN_MAX; + void setup() { Serial.begin(115200); @@ -27,22 +51,106 @@ void setup() { server.begin(); Serial.println("Server started"); + + IrSender.begin(12); +} + +void readControlData(byte* data) { + byte command = data[0]; + + switch (command) { + case Commands::SPEED: + targetSpeed = data[1]; + break; + case Commands::POWER: + if (data[1] == 0x01) { + targetPowerStatus = true; + } else { + targetPowerStatus = false; + } + break; + } +} + +void printStatus() { + Serial.print("targetSpeed: "); + Serial.println(targetSpeed, DEC); + Serial.print("targetPowerStatus: "); + Serial.println(targetPowerStatus, DEC); +} + +void sendIR(IRCommands command) { + Serial.print("Sending command: "); + Serial.println(command, HEX); + + long currentTime = millis(); + + Serial.print("currentTime: "); + Serial.println(currentTime); + Serial.print("lastWakeUp: "); + Serial.println(lastWakeUp); + Serial.print("COOLDOWN_MIN: "); + Serial.println(COOLDOWN_MIN); + if (currentTime > lastWakeUp + COOLDOWN_MIN && command != IRCommands::PWR) { + if (currentTime < lastWakeUp + COOLDOWN_MAX) { + Serial.print("Applying grace delay "); + Serial.println(currentTime - lastWakeUp - COOLDOWN_MIN); + delay(currentTime - lastWakeUp - COOLDOWN_MIN); + } + + // Wake up + Serial.print("Waking up after "); + Serial.println(currentTime - lastWakeUp); + IrSender.sendPulseDistanceWidth(38, 9050, 4450, 600, 1600, 600, 500, IRCommands::UP, 48, PROTOCOL_IS_LSB_FIRST, 0, NO_REPEATS); + delay(APPLY_PERIOD); + } + + IrSender.sendPulseDistanceWidth(38, 9050, 4450, 600, 1600, 600, 500, command, 48, PROTOCOL_IS_LSB_FIRST, 0, NO_REPEATS); + lastWakeUp = millis(); +} + +void applyCommands() { + if (targetPowerStatus != currentPowerStatus) { + sendIR(IRCommands::PWR); + currentPowerStatus = targetPowerStatus; + } + + if (targetSpeed > currentSpeed) { + sendIR(IRCommands::UP); + currentSpeed++; + } + + if (targetSpeed < currentSpeed) { + sendIR(IRCommands::DOWN); + currentSpeed--; + } } void loop() { - WiFiClient client = server.available(); + WiFiClient client = server.accept(); if (client) { Serial.println("\n[Client connected]"); while (client.connected()) { - // read line by line what the client (web browser) is requesting + byte data[3]; if (client.available()) { - char line = client.read(); - Serial.print(line); + client.readBytesUntil(0xFF, data, 3); + readControlData(data); + printStatus(); + } + + if (millis() > lastApplied + APPLY_PERIOD) { + applyCommands(); + lastApplied = millis(); } } client.stop(); Serial.println("[Client disconnected]"); } + + if (millis() > lastApplied + APPLY_PERIOD) { + applyCommands(); + lastApplied = millis(); + } } diff --git a/send-test.py b/send-test.py new file mode 100644 index 0000000..c359c46 --- /dev/null +++ b/send-test.py @@ -0,0 +1,19 @@ +import socket +import sys + +commands = { + "speed": 0x01, + "power": 0x02, +} + +client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +client.connect(("10.42.50.102", 1337)) + +command = sys.argv[1] +value = int(sys.argv[2]) + +print(hex(value)) + +_ = client.send(bytes([commands[command], value, 0xFF])) + +client.close() diff --git a/telemetry-listener.py b/telemetry-listener.py index 59a2cfe..7c3ca5c 100644 --- a/telemetry-listener.py +++ b/telemetry-listener.py @@ -1,17 +1,66 @@ import socket import struct -import math +import threading import time +import signal -sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) -sock.bind(("127.0.0.1", 1337)) +commands = { + "speed": 0x01, + "power": 0x02, +} -while True: - data, addr = sock.recvfrom(1024) - caca = struct.unpack('