### BL0942 Sensor Library Full Example Source: https://github.com/santerilindfors/bl0942/blob/main/README.md A complete example demonstrating how to initialize the BL0942 sensor, set up a data received callback, and continuously update and process sensor data within the main loop. Ensure correct UART pins and baud rate are configured. ```cpp #include #define BL0942_RX 19 #define BL0942_TX 18 bl0942::BL0942 blSensor(Serial1); void dataReceivedCallback(bl0942::SensorData &data) { Serial.print("Voltage: "); Serial.println(data.voltage); Serial.print("Current: "); Serial.println(data.current); Serial.print("Power: "); Serial.println(data.watt); Serial.print("Energy: "); Serial.println(data.energy); Serial.print("Frequency: "); Serial.println(data.frequency); } void setup() { Serial.begin(115200); Serial1.begin(4800, SERIAL_8N1, BL0942_RX, BL0942_TX); // Must be called by user blSensor.setup(); // Use default ModeConfig blSensor.onDataReceived(dataReceivedCallback); } void loop() { blSensor.update(); blSensor.loop(); delay(3000); } ``` -------------------------------- ### setup() Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Configures the BL0942 sensor by writing a configuration packet to its internal registers. This setup includes frequency, waveform selection, UART speed, and data clearing behavior. ```APIDOC ## void setup(const ModeConfig &config = ModeConfig{}) ### Description Configures the BL0942 sensor by writing a configuration packet to its internal registers. This setup includes frequency, waveform selection, UART speed, and data clearing behavior. ### Parameters #### Path Parameters - **config** (const ModeConfig &) - Optional - Configuration settings for the sensor. Defaults to `ModeConfig{}`. ### Parameters (`ModeConfig`) #### Path Parameters - **rms_update_freq** (UpdateFrequency) - Optional - RMS update frequency (400ms or 800ms). Affects how often RMS is refreshed. Defaults to `UPDATE_FREQUENCY_400MS`. - **rms_waveform** (RmsWaveform) - Optional - Choose full or AC-only waveform for RMS measurement. Defaults to `RMS_WAVEFORM_FULL`. - **ac_freq** (LineFrequency) - Optional - Sets expected AC line frequency (50Hz or 60Hz). Defaults to `LINE_FREQUENCY_50HZ`. - **clear_mode** (ClearMode) - Optional - Whether the energy counter (`cf_cnt`) clears after each read. Defaults to `CNT_CLR_SEL_DISABLE`. - **accumulation_mode** (AccumulationMode) - Optional - Use algebraic (signed) or absolute energy accumulation. Defaults to `ACCUMULATION_MODE_ABSOLUTE`. - **uart_rate** (UartRate) - Optional - UART baud rate (4800, 9600, 19200, 38400). Must match your `Serial.begin()`. Defaults to `UART_RATE_4800`. ### Energy Reading Mode BL0942 supports two energy counting modes controlled by the `clear_mode` setting in `ModeConfig`: - `CNT_CLR_SEL_ENABLE`: The energy counter is **cleared after each read**. Sensor returns **ΔE** (delta energy) since last read. - `CNT_CLR_SEL_DISABLE`: The energy counter is **never cleared**, and value accumulates forever. Sensor returns **total ∫P** (integrated total energy). ### Request Example ```cpp bl0942::ModeConfig cfg; cfg.uart_rate = bl0942::UART_RATE_19200; cfg.ac_freq = bl0942::LINE_FREQUENCY_60HZ; blSensor.setup(cfg); ``` ``` -------------------------------- ### Setup BL0942 Sensor and Data Callback Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Initialize the BL0942 sensor and register a callback function to process received sensor data. The callback receives a SensorData object containing readings like power. ```cpp blSensor.setup(); // or setup(custom_config) blSensor.onDataReceived([](bl0942::SensorData &data) { Serial.printf("Power: %.2f", data.watt); }); ``` -------------------------------- ### Initialize BL0942 Sensor Instance Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Instantiate the BL0942 sensor driver, passing a reference to the HardwareSerial port to be used for communication. ```cpp bl0942::BL0942 blSensor(Serial1); ``` -------------------------------- ### Configure Serial Port for BL0942 Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Begin the serial communication with the desired baud rate, data bits, and parity. Ensure these settings match the sensor's configuration. ```cpp Serial1.begin(4800, SERIAL_8N1, BL0942_RX, BL0942_TX); ``` -------------------------------- ### Configure BL0942 Sensor with Custom Settings Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Set up the BL0942 sensor using a custom `ModeConfig` object to specify parameters like UART rate and AC line frequency. ```cpp bl0942::ModeConfig cfg; cfg.uart_rate = bl0942::UART_RATE_19200; cfg.ac_freq = bl0942::LINE_FREQUENCY_60HZ; blSensor.setup(cfg); ``` -------------------------------- ### BL0942 Constructor Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Constructs a new BL0942 driver instance, requiring a reference to a HardwareSerial port and an optional UART address. ```APIDOC ## BL0942(HardwareSerial &serial, uint8_t address = 0) ### Description Constructs a new BL0942 driver instance. ### Parameters #### Path Parameters - **serial** (HardwareSerial&) - Required - A reference to a HardwareSerial port (e.g. `Serial1`) - **address** (uint8_t) - Optional - UART address if your module uses addressable communication (default: `0`) > Note: You must call Serial1.begin() yourself before using this library. ``` -------------------------------- ### reset() Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Performs a soft reset of the BL0942 sensor, restoring internal configuration to factory defaults. ```APIDOC ## void reset() ### Description Performs a soft reset of the BL0942 sensor, restoring internal configuration to factory defaults. ``` -------------------------------- ### Configure BL0942 for Delta Energy Reading Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Set the `clear_mode` to `CNT_CLR_SEL_ENABLE` to configure the sensor to return delta energy (ΔE) since the last read. ```cpp bl0942::ModeConfig cfg; cfg.clear_mode = bl0942::CNT_CLR_SEL_ENABLE; // For delta energy // or cfg.clear_mode = bl0942::CNT_CLR_SEL_DISABLE; // For total energy blSensor.setup(cfg); ``` -------------------------------- ### loop Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Continuously checks the UART buffer for incoming packets from the BL0942 chip. It reads, validates checksums, and triggers registered callbacks upon successful data reception. ```APIDOC ## bool loop() ### Description Call this in your `loop()` function continuously. It checks the UART buffer, reads incoming packets, validates the checksum, and triggers the registered callback if data is received. ### Method Signature `bool loop()` ### Parameters None ### Return Value `true` if a packet was processed, `false` otherwise. ### Endpoint N/A (Method call) ### Usage ```cpp void loop() { blSensor.loop(); // other code... } ``` ``` -------------------------------- ### update Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Manually triggers the BL0942 chip to send its latest measurement data. This action requires subsequent handling in the `loop()` function to process the response. ```APIDOC ## void update() ### Description Manually sends a request to the BL0942 chip to send the latest measurement data. This triggers a response packet that must be handled in `loop()`. ### Method Signature `void update()` ### Parameters None ### Endpoint N/A (Method call) ### Request Example ```cpp blSensor.update(); ``` ### Response N/A (Triggers a response handled by `loop()`) ``` -------------------------------- ### Update and Loop BL0942 Sensor Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Regularly call `update()` to request new data from the sensor and `loop()` to process incoming data and trigger callbacks. ```cpp blSensor.update(); blSensor.loop(); ``` -------------------------------- ### Register Data Received Callback Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Registers a callback function to be executed when new, valid sensor data is received. The callback receives a SensorData struct containing voltage, current, power, energy, and frequency. ```cpp blSensor.onDataReceived([](bl0942::SensorData &data) { Serial.printf("U: %.2f V, I: %.2f A, P: %.2f W\n", data.voltage, data.current, data.watt); }); ``` -------------------------------- ### print_registers Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Reads and displays all critical registers of the BL0942 chip, including configuration settings, gain values, and sensor status, which is useful for debugging and diagnostics. ```APIDOC ## void print_registers() ### Description Reads and prints all key registers from the BL0942 for debugging and diagnostics. This includes mode configuration, gain, and sensor status values. ### Method Signature `void print_registers()` ### Parameters None ### Endpoint N/A (Method call) ### Usage ```cpp blSensor.print_registers(); ``` ``` -------------------------------- ### onDataReceived Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Registers a callback function to be executed when new, parsed sensor data is available. The callback receives a SensorData struct containing voltage, current, power, energy, and frequency. ```APIDOC ## void onDataReceived(OnDataReceivedCallback callback) ### Description Registers a callback function that will be triggered when new sensor data is received and successfully parsed. The `SensorData` struct is passed to your callback whenever new data is received from the BL0942 sensor. It contains the most recent measurement values. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method Signature `void onDataReceived(OnDataReceivedCallback callback)` ### Callback Signature `void callback(bl0942::SensorData &data)` ### SensorData Struct ```cpp struct SensorData { float voltage; // Voltage RMS in volts float current; // Current RMS in amperes float watt; // Active power in watts float energy; // Energy in kilowatt-hours (kWh) float frequency; // Line frequency in Hz }; ``` ### Example Usage ```cpp blSensor.onDataReceived([](bl0942::SensorData &data) { Serial.printf("U: %.2f V, I: %.2f A, P: %.2f W\n", data.voltage, data.current, data.watt); }); ``` ``` -------------------------------- ### BL0942 ModeConfig Structure Definition Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Defines the configuration options for the BL0942 sensor, including update frequency, waveform, line frequency, clear mode, accumulation mode, and UART rate. ```cpp struct ModeConfig { UpdateFrequency rms_update_freq = UPDATE_FREQUENCY_400MS; RmsWaveform rms_waveform = RMS_WAVEFORM_FULL; LineFrequency ac_freq = LINE_FREQUENCY_50HZ; ClearMode clear_mode = CNT_CLR_SEL_DISABLE; AccumulationMode accumulation_mode = ACCUMULATION_MODE_ABSOLUTE; UartRate uart_rate = UART_RATE_4800; }; ``` -------------------------------- ### Reset BL0942 Sensor Source: https://github.com/santerilindfors/bl0942/blob/main/README.md Perform a soft reset of the BL0942 sensor to restore its internal configuration to factory default settings. ```cpp blSensor.reset(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.