diff --git a/.drone.yml b/.drone.yml index 09f4866..63226e9 100644 --- a/.drone.yml +++ b/.drone.yml @@ -6,6 +6,7 @@ steps: image: python:3 commands: - pip install platformio + - pio check - pio run - name: upload image: python:3 diff --git a/.vscode/.BROWSE.C_CPP.DB b/.vscode/.BROWSE.C_CPP.DB index f057130..3420cd8 100644 Binary files a/.vscode/.BROWSE.C_CPP.DB and b/.vscode/.BROWSE.C_CPP.DB differ diff --git a/.vscode/.BROWSE.C_CPP.DB-shm b/.vscode/.BROWSE.C_CPP.DB-shm new file mode 100644 index 0000000..dee6580 Binary files /dev/null and b/.vscode/.BROWSE.C_CPP.DB-shm differ diff --git a/.vscode/.BROWSE.C_CPP.DB-wal b/.vscode/.BROWSE.C_CPP.DB-wal new file mode 100644 index 0000000..ab4c493 Binary files /dev/null and b/.vscode/.BROWSE.C_CPP.DB-wal differ diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..c21a748 --- /dev/null +++ b/include/config.h @@ -0,0 +1,11 @@ +#ifndef CONFIG_H + +#define CONFIG_H + +#define CONFIG_SERIAL_BAUDRATE 115200 + +#define CONFIG_IO_RELAY D1 +#define CONFIG_IO_DHT11 D2 + +#define CONFIG_TEMPERATURE_SEND_INTERVAL 60 +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b5d36f4..5363f7a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,27 +1,30 @@ -#include +#include "config.h" +#include #include "DHTesp.h" DHTesp dht; -const int TEMPERATURE_INTERVAL = 60; -unsigned long lastTemperatureSent = 0; -const int PIN_RELAY = D1; -const int PIN_DHT11 = D2; -#define OUTPUT_SET(x) digitalWrite(PIN_RELAY, x ? LOW : HIGH) + + +#define OUTPUT_SET(x) digitalWrite(CONFIG_IO_RELAY, x ? LOW : HIGH) unsigned int timer = 0; unsigned int next_timer_update = 0; HomieNode powerNode("power", "Power", "switch"); HomieNode temperatureNode("temperature", "Temperature", "temperature"); -bool powerStateHandler(const HomieRange& range, const String& value) { +bool powerStateHandler(const HomieRange& range, const String& value) +{ if (value != "on" && value != "off") return false; powerNode.setProperty("state").send(value); powerNode.setProperty("timer").send("0"); timer = 0; - if(value == "on") { + if(value == "on") + { OUTPUT_SET(true); Homie.getLogger() << "Power is on" << endl; - } else { + } + else + { OUTPUT_SET(false); Homie.getLogger() << "Power is off" << endl; } @@ -29,7 +32,8 @@ bool powerStateHandler(const HomieRange& range, const String& value) { return true; } -bool powerTimerHandler(const HomieRange& range, const String& value) { +bool powerTimerHandler(const HomieRange& range, const String& value) +{ int settimer = value.toInt(); if(settimer == 0 || settimer > 120) return false; @@ -44,17 +48,18 @@ bool powerTimerHandler(const HomieRange& range, const String& value) { } -void setupHandler() { - pinMode(PIN_RELAY, OUTPUT); +void setupHandler() +{ + pinMode(CONFIG_IO_RELAY, OUTPUT); OUTPUT_SET(false); powerNode.setProperty("state").send("off"); powerNode.setProperty("timer").send("0"); - - dht.setup(PIN_DHT11, DHTesp::DHT11); // Connect DHT sensor to GPIO 17 + dht.setup(CONFIG_IO_DHT11, DHTesp::DHT11); // Connect DHT sensor to GPIO 17 } -void loopHandler() { +void loopHandler() +{ if(timer) { if(timer < millis()) @@ -65,16 +70,17 @@ void loopHandler() { powerNode.setProperty("state").send("off"); } - if(next_timer_update && millis() > next_timer_update) { + if(next_timer_update && millis() > next_timer_update) + { next_timer_update = millis() + 60*1000; unsigned int time_remaining = timer - millis(); unsigned int min_left = time_remaining / (60*1000); powerNode.setProperty("timer").send(String(min_left)); - } } - if (millis() - lastTemperatureSent >= TEMPERATURE_INTERVAL * 1000UL || lastTemperatureSent == 0) + static unsigned long lastTemperatureSent = 0; + if (millis() - lastTemperatureSent >= CONFIG_TEMPERATURE_SEND_INTERVAL * 1000UL || lastTemperatureSent == 0) { float humidity = dht.getHumidity(); float temperature = dht.getTemperature(); @@ -86,8 +92,9 @@ void loopHandler() { } } -void setup() { - Serial.begin(115200); +void setup() +{ + Serial.begin(CONFIG_SERIAL_BAUDRATE); Homie_setFirmware("deskcontrol", "0.4.2"); @@ -104,6 +111,7 @@ void setup() { Homie.setup(); } -void loop() { +void loop() +{ Homie.loop(); }