diff --git a/src/main.cpp b/src/main.cpp index 1fa9c19..798b4c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,16 +11,55 @@ #include #include -WiFiClient espClient; +static const char ISR_Root_x1[] PROGMEM = R"EOF( +-----BEGIN CERTIFICATE----- +MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw +TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh +cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4 +WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu +ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY +MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc +h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+ +0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U +A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW +T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH +B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC +B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv +KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWn +OlFuhjuefXKnEgV4We0+UXgVCwOPjdAvBbI+e0ocS3MFEvzG6uBQE3xDk3SzynTn +jh8BCNAw1FtxNrQHusEwMFxIt4I7mKZ9YIqioymCzLq9gwQbooMDQaHWBfEbwrbw +qHyGO0aoSCqI3Haadr8faqU9GY/rOPNk3sgrDQoo//fb4hVC1CLQJ13hef4Y53CI +rU7m2Ys6xt0nUW7/vGT1M0NPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNV +HRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkq +hkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZL +ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ +3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK +NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5 +ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur +TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC +jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc +oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq +4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA +mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d +emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc= +-----END CERTIFICATE----- +)EOF"; + +BearSSL::X509List certISRG(ISR_Root_x1); +BearSSL::WiFiClientSecure espClient; + PubSubClient client(espClient); DHT dht(DHTPIN, DHTTYPE); IRLight irlight(4); - HALightBrightness halight(client, "light", "l-1", irlight); void setup_wifi(); +void setup_time(); +void setup_mqtt(); + void reconnect(); + void mqtt_callback(char *topic, byte *payload, unsigned int length); void mqtt_publish_config(); void mqtt_publish_state(); @@ -31,42 +70,18 @@ long lastMsg = 0; void setup() { Serial.begin(115200); - setup_wifi(); + delay(1000); - WiFi.setAutoReconnect(true); - WiFi.persistent(true); - - client.setServer(MQTT_SERVER, MQTT_PORT); - client.setCallback(mqtt_callback); - - if (!client.connected()) { - reconnect(); - } - - dht.begin(); irlight.begin(); + dht.begin(); - client.subscribe("homeassistant/status"); - halight.subscribeToCommandTopic(); + setup_wifi(); + setup_time(); + setup_mqtt(); - if (WiFi.status() == WL_CONNECTED) { - mqtt_publish_config(); - delay(100); - // mqtt_publish_state(); - halight.sendState(); - } - - /* mqtt_publish_config(); - - if (WiFi.status() == WL_CONNECTED) { - temp = dht.readTemperature(); - hum = dht.readHumidity(); - - mqtt_publish_state(); - halight.sendState(); - } - */ + delay(100); + mqtt_publish_state(); } void loop() { @@ -89,8 +104,78 @@ void loop() { delay(100); } -void mqtt_callback(char *topic, byte *payload, unsigned int length) { +void setup_wifi() { + delay(10); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(WIFI_SSID); + WiFi.begin(WIFI_SSID, WIFI_PASS); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + WiFi.setAutoReconnect(true); + WiFi.persistent(true); +} + +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) { + delay(500); + Serial.print(F(".")); + yield(); + nowSecs = time(nullptr); + } + + Serial.println(); + struct tm timeinfo; + gmtime_r(&nowSecs, &timeinfo); + Serial.print(F("Current time: ")); + Serial.print(asctime(&timeinfo)); +} + +void setup_mqtt() { + espClient.setTrustAnchors(&certISRG); + + client.setServer(MQTT_SERVER, MQTT_PORT); + client.setCallback(mqtt_callback); + + if (!client.connected()) { + reconnect(); + } + + client.subscribe("homeassistant/status"); + halight.subscribeToCommandTopic(); +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS)) { + Serial.println("connected"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + +void mqtt_callback(char *topic, byte *payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); @@ -107,64 +192,10 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length) { halight.handle(topic, payload, length); } -void setup_wifi() { - delay(10); - // We start by connecting to a WiFi network - Serial.println(); - Serial.print("Connecting to "); - Serial.println(WIFI_SSID); - - WiFi.begin(WIFI_SSID, WIFI_PASS); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); -} - -void reconnect() { - // Loop until we're reconnected - while (!client.connected()) { - Serial.print("Attempting MQTT connection..."); - // Attempt to connect - // If you do not want to use a username and password, change next line to - // if (client.connect("ESP8266Client")) { - if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS)) { - Serial.println("connected"); - } else { - Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); - // Wait 5 seconds before retrying - delay(5000); - } - } -} - void mqtt_publish_config() { halight.sendConfig(); } void mqtt_publish_state() { - // Loop until we're reconnected - while (!client.connected()) { - Serial.print("Attempting MQTT connection..."); - // Attempt to connect - // If you do not want to use a username and password, change next line to - // if (client.connect("ESP8266Client")) { - if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS)) { - Serial.println("connected"); - } else { - Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); - // Wait 5 seconds before retrying - delay(5000); - } - } + halight.sendState(); } \ No newline at end of file