157 lines
3.4 KiB
Arduino
157 lines
3.4 KiB
Arduino
#include <ESP8266WiFi.h>
|
|
#include <IRremote.hpp>
|
|
|
|
#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);
|
|
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(SSID);
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(SSID, PASSWORD);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
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.accept();
|
|
|
|
if (client) {
|
|
Serial.println("\n[Client connected]");
|
|
while (client.connected()) {
|
|
byte data[3];
|
|
if (client.available()) {
|
|
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();
|
|
}
|
|
}
|