#include "config.h"
#include "sensor.h"
#include "wlan.h"
#include <Arduino.h>
#include <DHT.h>

DHT dht(D0, DHT22);

const char *config_file_path = "/config.json";
Config *config;

float data[2];
unsigned long previous_millis = 0;

void setup() {
    Serial.begin(9600);
    config = (Config *)malloc(sizeof(Config));

    if (!load_config_file(config_file_path, config))
        Serial.println("ERROR: The config file could not be loaded");

    initialize_wlan();
    initialize_mqtt();
    connect_wlan();
    initialize_sensor(dht);
}

void loop() {
    unsigned long current_millis = millis();

    if (current_millis - previous_millis >= config->sleep_time) {
        previous_millis = current_millis;
        read_values(dht, data);
        mqtt_transfer(data);
    }
}
