Added voltage and power calculation functions
This commit is contained in:
parent
02ad9b7596
commit
c5952386ea
@ -1,43 +1,96 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "driver/uart.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "string.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "inttypes.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
#define UART_NUM UART_NUM_1
|
||||
#define TXD_PIN GPIO_NUM_17
|
||||
#define RXD_PIN GPIO_NUM_16
|
||||
#define BUF_SIZE 1024
|
||||
#define UART_NUM UART_NUM_1 // Use UART1
|
||||
#define TXD_PIN GPIO_NUM_17 // TX pin for MCP39F501
|
||||
#define RXD_PIN GPIO_NUM_16 // RX pin for MCP39F501
|
||||
#define BUF_SIZE 256 // UART buffer size
|
||||
|
||||
static const char *TAG = "UART";
|
||||
static const char *TAG = "MCP39F501";
|
||||
|
||||
// Initialize UART
|
||||
void uart_init(void) {
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = 4800,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.baud_rate = 4800, // Set baud rate
|
||||
.data_bits = UART_DATA_8_BITS, // 8-bit data
|
||||
.parity = UART_PARITY_DISABLE, // No parity
|
||||
.stop_bits = UART_STOP_BITS_1, // 1 stop bit
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE // No flow control
|
||||
};
|
||||
|
||||
uart_param_config(UART_NUM, &uart_config);
|
||||
uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
|
||||
uart_driver_install(UART_NUM, BUF_SIZE * 2, 0, 0, NULL, 0);
|
||||
}
|
||||
|
||||
// Send a command byte by byte
|
||||
void uart_send_command(const uint8_t *command, size_t length) {
|
||||
uart_write_bytes(UART_NUM, (const char *)command, length);
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
uart_write_bytes(UART_NUM, (const char *)&command[i], 1); // Send one byte
|
||||
vTaskDelay(pdMS_TO_TICKS(10)); // Small delay for MCP39F501 to process
|
||||
}
|
||||
}
|
||||
|
||||
// Receive response from MCP39F501
|
||||
int uart_receive_response(uint8_t *data, size_t length) {
|
||||
return uart_read_bytes(UART_NUM, data, length, pdMS_TO_TICKS(1000));
|
||||
return uart_read_bytes(UART_NUM, data, length, pdMS_TO_TICKS(500)); // Wait 500ms for response
|
||||
}
|
||||
|
||||
void request_current_value(void) {
|
||||
uint8_t command[] = {0xA5, 0x05,0x4C, 0x4C, 0x42}; // Example command to request current value
|
||||
uart_send_command(command, sizeof(command));
|
||||
// Read VRMS (Voltage RMS)
|
||||
uint32_t read_vrms() {
|
||||
uint8_t vrms_cmd[] = { 0xA5, 0x07, 0x41, 0x00, 0x08, 0x52, 0x47 }; // Command for VRMS
|
||||
uint8_t response[5];
|
||||
uint32_t vrms_value = 0;
|
||||
|
||||
uart_send_command(vrms_cmd, sizeof(vrms_cmd));
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
uint8_t response[31];
|
||||
|
||||
int len = uart_receive_response(response, sizeof(response));
|
||||
if (len > 0 ) {
|
||||
vrms_value = (response[2] << 8) | response[3]; // Extract VRMS value
|
||||
ESP_LOGI(TAG, "VRMS: %lu", vrms_value);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to receive complete VRMS data");
|
||||
}
|
||||
|
||||
return vrms_value;
|
||||
}
|
||||
|
||||
// Read Active Power
|
||||
uint32_t read_active_power() {
|
||||
uint8_t power_cmd[] = { 0xA5, 0x07, 0x41, 0x00, 0x0A, 0x44, 0x03B }; // Command for Active Power
|
||||
uint8_t response[8];
|
||||
uint32_t power_value = 0;
|
||||
|
||||
uart_send_command(power_cmd, sizeof(power_cmd));
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
|
||||
int len = uart_receive_response(response, sizeof(response));
|
||||
if (len >= 7) {
|
||||
power_value = (response[2] << 24) | (response[3] << 16) | (response[4] << 8) | response[5]; // Extract Power
|
||||
ESP_LOGI(TAG, "Active Power: %d", power_value);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to receive complete power data");
|
||||
}
|
||||
|
||||
return power_value;
|
||||
}
|
||||
|
||||
void idle_message() {
|
||||
uint8_t command[] = {0xA4}; // Example command to check acknowledge
|
||||
// uint8_t command[] = { 0xA5, 0x07, 0x41, 0x00, 0x02, 0x52, 0x41 }; // find System version
|
||||
uart_send_command(command, sizeof(command));
|
||||
|
||||
uint8_t response[1];
|
||||
int len = uart_receive_response(response, sizeof(response));
|
||||
if (len > 0) {
|
||||
ESP_LOGI(TAG, "Received %d bytes", len);
|
||||
@ -47,10 +100,23 @@ void request_current_value(void) {
|
||||
}
|
||||
}
|
||||
|
||||
// Main application
|
||||
void app_main(void) {
|
||||
uart_init();
|
||||
/*
|
||||
for(uint8_t j =0; j<=3; j++) {
|
||||
idle_message();
|
||||
vTaskDelay(pdMS_TO_TICKS(210));
|
||||
}
|
||||
*/
|
||||
|
||||
while (1) {
|
||||
request_current_value();
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
uint32_t vrms = read_vrms();
|
||||
uint32_t power = read_active_power();
|
||||
|
||||
ESP_LOGI(TAG, "Voltage RMS: %.2f V", vrms / 10.0);
|
||||
ESP_LOGI(TAG, "Active Power: %.3f W", power / 100.0);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(2000)); // Read every 2 seconds
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user