Streamlining configuration. Added static code analysis.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
True 2019-11-28 09:50:54 +01:00
parent 8387a0610b
commit b8e68bed7c
6 changed files with 41 additions and 21 deletions

@ -6,6 +6,7 @@ steps:
image: python:3
commands:
- pip install platformio
- pio check
- pio run
- name: upload
image: python:3

Binary file not shown.

BIN
.vscode/.BROWSE.C_CPP.DB-shm vendored Normal file

Binary file not shown.

BIN
.vscode/.BROWSE.C_CPP.DB-wal vendored Normal file

Binary file not shown.

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

@ -1,27 +1,30 @@
#include <Homie.h>
#include "config.h"
#include <Homie.h>
#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();
}