делает некоторые правки
This commit is contained in:
parent
dae8020ee5
commit
5ac3add676
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,4 +4,4 @@
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
|
||||
include/config.h
|
||||
include/config.hpp
|
25
.vscode/settings.json
vendored
25
.vscode/settings.json
vendored
@ -1,13 +1,14 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"deque": "cpp",
|
||||
"list": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"string_view": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"ranges": "cpp"
|
||||
}
|
||||
}
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"deque": "cpp",
|
||||
"list": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"string_view": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"ranges": "cpp"
|
||||
},
|
||||
"clang-tidy.checks": ["readability-*", "modernize-*", "bugprone-*"]
|
||||
}
|
||||
|
@ -1,3 +1,7 @@
|
||||
/*
|
||||
* Copyright 2023 Maxim Slipenko
|
||||
*/
|
||||
#pragma once
|
||||
// Настройки WiFi
|
||||
#define WIFI_SSID "SSID_HERE"
|
||||
#define WIFI_PASS "PASS_HERE"
|
@ -16,9 +16,9 @@ private:
|
||||
HALightBrightnessController *light;
|
||||
|
||||
protected:
|
||||
JSON_DOCUMENT_TYPE createConfigJSON();
|
||||
JSON_DOCUMENT_TYPE createStateJSON();
|
||||
void innerHanlder(JSON_DOCUMENT_TYPE &doc);
|
||||
JSON_DOCUMENT_TYPE createConfigJSON() override;
|
||||
JSON_DOCUMENT_TYPE createStateJSON() override;
|
||||
void innerHanlder(JSON_DOCUMENT_TYPE &doc) override;
|
||||
public:
|
||||
HALightBrightness(PubSubClient &client, const char *name, const char *unique_id,
|
||||
HALightBrightnessController &baseLight);
|
||||
|
@ -19,4 +19,8 @@ lib_deps =
|
||||
bblanchon/ArduinoJson@^6.19.4
|
||||
crankyoldgit/IRremoteESP8266@^2.8.2
|
||||
monitor_speed = 115200
|
||||
monitor_filters = default, esp8266_exception_decoder
|
||||
monitor_filters = default, esp8266_exception_decoder
|
||||
check_tool = cppcheck, clangtidy
|
||||
check_flags =
|
||||
cppcheck: -v --suppress=*:*/.pio/*
|
||||
clangtidy: --checks=-*,clang-diagnostic-*,-clang-diagnostic-unused-value,clang-analyzer-*,-*,bugprone-*,performance-*,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-readability-inconsistent-declaration-parameter-name,-readability-named-parameter --fix
|
66
src/main.cpp
66
src/main.cpp
@ -1,3 +1,6 @@
|
||||
/*
|
||||
* Copyright 2023 Maxim Slipenko
|
||||
*/
|
||||
#include <DHT.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <IRLight.h>
|
||||
@ -8,8 +11,8 @@
|
||||
#include <HomeAssistantDevices.hpp>
|
||||
#include <IRLightController.hpp>
|
||||
|
||||
#include <config.h>
|
||||
#include <utils.h>
|
||||
#include <config.hpp>
|
||||
#include <utils.hpp>
|
||||
|
||||
static const char ISR_Root_x1[] PROGMEM = R"EOF(
|
||||
-----BEGIN CERTIFICATE-----
|
||||
@ -68,7 +71,8 @@ float temp;
|
||||
float hum;
|
||||
long lastMsg = 0;
|
||||
|
||||
void setup() {
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
@ -84,8 +88,10 @@ void setup() {
|
||||
mqtt_publish_state();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
void loop()
|
||||
{
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
@ -104,7 +110,8 @@ void loop() {
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void setup_wifi() {
|
||||
void setup_wifi()
|
||||
{
|
||||
delay(10);
|
||||
Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
@ -112,7 +119,8 @@ void setup_wifi() {
|
||||
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
@ -126,12 +134,14 @@ void setup_wifi() {
|
||||
WiFi.persistent(true);
|
||||
}
|
||||
|
||||
void setup_time() {
|
||||
void setup_time()
|
||||
{
|
||||
configTime(0, 0, "pool.ntp.org");
|
||||
|
||||
Serial.print(F("Waiting for NTP time sync: "));
|
||||
time_t nowSecs = time(nullptr);
|
||||
while (nowSecs < 8 * 3600 * 2) {
|
||||
while (nowSecs < 8 * 3600 * 2)
|
||||
{
|
||||
delay(500);
|
||||
Serial.print(F("."));
|
||||
yield();
|
||||
@ -145,13 +155,15 @@ void setup_time() {
|
||||
Serial.print(asctime(&timeinfo));
|
||||
}
|
||||
|
||||
void setup_mqtt() {
|
||||
void setup_mqtt()
|
||||
{
|
||||
espClient.setTrustAnchors(&certISRG);
|
||||
|
||||
client.setServer(MQTT_SERVER, MQTT_PORT);
|
||||
client.setCallback(mqtt_callback);
|
||||
|
||||
if (!client.connected()) {
|
||||
if (!client.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
|
||||
@ -159,13 +171,18 @@ void setup_mqtt() {
|
||||
halight.subscribeToCommandTopic();
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
void reconnect()
|
||||
{
|
||||
// Loop until we're reconnected
|
||||
while (!client.connected()) {
|
||||
while (!client.connected())
|
||||
{
|
||||
Serial.print("Attempting MQTT connection...");
|
||||
if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS)) {
|
||||
if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS))
|
||||
{
|
||||
Serial.println("connected");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
@ -175,27 +192,34 @@ void reconnect() {
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_callback(char *topic, byte *payload, unsigned int length) {
|
||||
const int unused = 0;
|
||||
|
||||
void mqtt_callback(char *topic, byte *payload, unsigned int length)
|
||||
{
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
Serial.println();
|
||||
|
||||
if (strcmp(topic, "homeassistant/status") == 0) {
|
||||
if (strcmp(topic, "homeassistant/status") == 0)
|
||||
{
|
||||
payload[length] = '\0';
|
||||
if (strcmp((char *)payload, "online") == 0) {
|
||||
if (strcmp(reinterpret_cast<char *>(payload), "online") == 0)
|
||||
{
|
||||
Serial.println("Home Assistant is online");
|
||||
mqtt_publish_config();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
halight.handle(topic, payload, length);
|
||||
}
|
||||
|
||||
void mqtt_publish_config() {
|
||||
void mqtt_publish_config()
|
||||
{
|
||||
halight.sendConfig();
|
||||
}
|
||||
|
||||
void mqtt_publish_state() {
|
||||
void mqtt_publish_state()
|
||||
{
|
||||
halight.sendState();
|
||||
}
|
@ -1 +1 @@
|
||||
double round2(double value) { return (int)(value * 100 + 0.5) / 100.0; }
|
||||
// double round2(double value) { return (int)(value * 100 + 0.5) / 100.0; }
|
Loading…
Reference in New Issue
Block a user