делает некоторые правки
This commit is contained in:
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; }
|
Reference in New Issue
Block a user