Streamlining configuration. Added static code analysis.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -6,6 +6,7 @@ steps:
|
|||||||
image: python:3
|
image: python:3
|
||||||
commands:
|
commands:
|
||||||
- pip install platformio
|
- pip install platformio
|
||||||
|
- pio check
|
||||||
- pio run
|
- pio run
|
||||||
- name: upload
|
- name: upload
|
||||||
image: python:3
|
image: python:3
|
||||||
|
BIN
.vscode/.BROWSE.C_CPP.DB
vendored
BIN
.vscode/.BROWSE.C_CPP.DB
vendored
Binary file not shown.
BIN
.vscode/.BROWSE.C_CPP.DB-shm
vendored
Normal file
BIN
.vscode/.BROWSE.C_CPP.DB-shm
vendored
Normal file
Binary file not shown.
BIN
.vscode/.BROWSE.C_CPP.DB-wal
vendored
Normal file
BIN
.vscode/.BROWSE.C_CPP.DB-wal
vendored
Normal file
Binary file not shown.
11
include/config.h
Normal file
11
include/config.h
Normal file
@ -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
|
50
src/main.cpp
50
src/main.cpp
@ -1,27 +1,30 @@
|
|||||||
#include <Homie.h>
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <Homie.h>
|
||||||
#include "DHTesp.h"
|
#include "DHTesp.h"
|
||||||
DHTesp dht;
|
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 timer = 0;
|
||||||
unsigned int next_timer_update = 0;
|
unsigned int next_timer_update = 0;
|
||||||
HomieNode powerNode("power", "Power", "switch");
|
HomieNode powerNode("power", "Power", "switch");
|
||||||
HomieNode temperatureNode("temperature", "Temperature", "temperature");
|
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;
|
if (value != "on" && value != "off") return false;
|
||||||
powerNode.setProperty("state").send(value);
|
powerNode.setProperty("state").send(value);
|
||||||
powerNode.setProperty("timer").send("0");
|
powerNode.setProperty("timer").send("0");
|
||||||
timer = 0;
|
timer = 0;
|
||||||
if(value == "on") {
|
if(value == "on")
|
||||||
|
{
|
||||||
OUTPUT_SET(true);
|
OUTPUT_SET(true);
|
||||||
Homie.getLogger() << "Power is on" << endl;
|
Homie.getLogger() << "Power is on" << endl;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
OUTPUT_SET(false);
|
OUTPUT_SET(false);
|
||||||
Homie.getLogger() << "Power is off" << endl;
|
Homie.getLogger() << "Power is off" << endl;
|
||||||
}
|
}
|
||||||
@ -29,7 +32,8 @@ bool powerStateHandler(const HomieRange& range, const String& value) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool powerTimerHandler(const HomieRange& range, const String& value) {
|
bool powerTimerHandler(const HomieRange& range, const String& value)
|
||||||
|
{
|
||||||
int settimer = value.toInt();
|
int settimer = value.toInt();
|
||||||
if(settimer == 0 || settimer > 120)
|
if(settimer == 0 || settimer > 120)
|
||||||
return false;
|
return false;
|
||||||
@ -44,17 +48,18 @@ bool powerTimerHandler(const HomieRange& range, const String& value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void setupHandler() {
|
void setupHandler()
|
||||||
pinMode(PIN_RELAY, OUTPUT);
|
{
|
||||||
|
pinMode(CONFIG_IO_RELAY, OUTPUT);
|
||||||
OUTPUT_SET(false);
|
OUTPUT_SET(false);
|
||||||
powerNode.setProperty("state").send("off");
|
powerNode.setProperty("state").send("off");
|
||||||
powerNode.setProperty("timer").send("0");
|
powerNode.setProperty("timer").send("0");
|
||||||
|
|
||||||
|
dht.setup(CONFIG_IO_DHT11, DHTesp::DHT11); // Connect DHT sensor to GPIO 17
|
||||||
dht.setup(PIN_DHT11, DHTesp::DHT11); // Connect DHT sensor to GPIO 17
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loopHandler() {
|
void loopHandler()
|
||||||
|
{
|
||||||
if(timer)
|
if(timer)
|
||||||
{
|
{
|
||||||
if(timer < millis())
|
if(timer < millis())
|
||||||
@ -65,16 +70,17 @@ void loopHandler() {
|
|||||||
powerNode.setProperty("state").send("off");
|
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;
|
next_timer_update = millis() + 60*1000;
|
||||||
unsigned int time_remaining = timer - millis();
|
unsigned int time_remaining = timer - millis();
|
||||||
unsigned int min_left = time_remaining / (60*1000);
|
unsigned int min_left = time_remaining / (60*1000);
|
||||||
powerNode.setProperty("timer").send(String(min_left));
|
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 humidity = dht.getHumidity();
|
||||||
float temperature = dht.getTemperature();
|
float temperature = dht.getTemperature();
|
||||||
@ -86,8 +92,9 @@ void loopHandler() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup()
|
||||||
Serial.begin(115200);
|
{
|
||||||
|
Serial.begin(CONFIG_SERIAL_BAUDRATE);
|
||||||
|
|
||||||
Homie_setFirmware("deskcontrol", "0.4.2");
|
Homie_setFirmware("deskcontrol", "0.4.2");
|
||||||
|
|
||||||
@ -104,6 +111,7 @@ void setup() {
|
|||||||
Homie.setup();
|
Homie.setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop()
|
||||||
|
{
|
||||||
Homie.loop();
|
Homie.loop();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user