Skip to content

File core.h

Location: include/core.h

Included by

graph RL
1["include/core.h"]
click 1 "core_8h.md#core_8h"
2 --> 1
3 --> 1

2["src/core.cpp"]
click 2 "core_8cpp.md#core_8cpp"

3["src/main.cpp"]
click 3 "main_8cpp.md#main_8cpp"

Functions

Function CoreSetup

void CoreSetup()

Initializes all core system components and peripherals.

This function performs comprehensive system initialization including:

Network Setup:
* Establishes WiFi connection with timeout handling

  • Configures MQTT client with unique sensor-based ID

  • Attempts initial MQTT broker connection

Hardware Initialization:
* Initializes DS3231 real-time clock module

  • Adjusts RTC time if power was lost (uses compilation timestamp)

  • Sets up SD card with SPI communication

  • Initializes ADT7410 temperature sensor

Data Recovery:
* Registers FAT file system timestamp callback

!> Warning \
This function will halt program execution (infinite loop) if any critical component fails to initialize (RTC, SD card, or temperature sensor)

?> The function uses compile-time constants for timeouts and configuration

See also: WIFI_CONNECT_TIMEOUT_MS, CLIENT_ID_BUFFER_SIZE, SD_SCK_FREQUENCY_MHZ

Return type: void

Function CoreLoop

void CoreLoop()

Main operational loop for continuous sensor monitoring, MQTT transmission, and robust data recovery.

This function implements the primary logic for the temperature monitoring system, including:
* Real-time sensor measurement and transmission via MQTT with QoS 1

  • Intelligent WiFi and MQTT connection management with automatic reconnection

  • Fallback to CSV batch storage during connectivity outages

  • Recovery and transmission of offline data after successful reconnection

  • Comprehensive error handling and status reporting

Operational Flow: 1. Time Management: Reads current time from RTC, tracks minute changes to avoid duplicate measurements. 2. WiFi Connection: Monitors status, attempts reconnection, falls back to CSV logging if offline. 3. MQTT Connection: Verifies broker connectivity, reconnects as needed, falls back to CSV logging if offline. 4. Data Recovery: Sends pending CSV data after reconnection, ensures recovery only once per cycle. 5. Normal Operation: Measures temperature, transmits via MQTT, polls for incoming messages.

Error Handling:
* Network or broker failures trigger CSV fallback storage for all measurements.

  • Connection attempts are rate-limited to prevent resource exhaustion.

  • All measurement data is preserved and recovered after connectivity is restored.

?> Maintains a fixed loop delay for consistent timing and system stability.

See also: RECONNECT_INTERVAL_MS, LOOP_DELAY_MS for timing configuration, saveToCsvBatch() for offline data storage, sendPendingData() for data recovery and MQTT retransmission

Return type: void

Function IsWifiConnected

bool IsWifiConnected()

Return type: bool

Function IsMqttConnected

bool IsMqttConnected()

Return type: bool

Function FatDateTime

void FatDateTime(uint16_t *date, uint16_t *time)

Callback function for FAT file system timestamp generation.

This function is used by the SdFat library to obtain current date and time for file system operations. It retrieves the time from the RTC and converts it to the FAT file system format using the appropriate macros.

Parameters:

  • date: Pointer to store the encoded FAT date (year, month, day)
  • time: Pointer to store the encoded FAT time (hour, minute, second)

?> This function is registered as a callback with SdFile::dateTimeCallback()

See also: FAT_DATE, FAT_TIME macros for encoding format details

Parameters:

  • uint16_t * date
  • uint16_t * time

Return type: void

Source

#pragma once

void CoreSetup();
void CoreLoop();
bool IsWifiConnected();
bool IsMqttConnected();
void FatDateTime(uint16_t* date, uint16_t* time);