Merge branch 'main' into lcd_upload

This commit is contained in:
Siwat Sirichai 2023-12-07 12:20:53 +07:00
commit 6ba2a1a0f7
15 changed files with 1619 additions and 137 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,12 @@
/**
* @brief Header file for the ESPMega IoT Core library.
*
* This library provides functions for controlling various IoT modules such as climate, IR, and input/output.
* It also includes functions for MQTT communication, LCD display, and OTA updates.
*
* @note This library requires the ESPMegaPRO library to be installed.
*
*/
#pragma once
#include <ESPMegaPRO.h>
@ -37,6 +46,7 @@
void mqtt_callback(char* topic, byte* payload, unsigned int length);
void virtual_interrupt_loop();
void virtual_interrupt_callback(int pin, int state);
void virtual_interrupt_preload();
void network_begin();
void mqtt_connect();
void mqtt_subscribe();
@ -95,8 +105,8 @@ void trigger13();
void trigger14();
void trigger15();
void eeprom_retrieve_init();
void eeprom_pwm_update();
void fram_retrieve_init();
void fram_pwm_update();
void lcd_ac_refresh_fan();
void lcd_ac_refresh_mode();
@ -106,20 +116,45 @@ void set_netmask(String address);
void set_dns(String address);
void set_gw(String address);
void set_mqtt_server(String address);
void eeprom_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4);
IPAddress eeprom_ip_retrieve(uint16_t rom_address);
void fram_ip_update(uint16_t rom_address, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4);
IPAddress fram_ip_retrieve(uint16_t rom_address);
void set_hostname(String hostname);
void eeprom_hostname_retrieve();
void fram_hostname_retrieve();
void set_basetopic(String topic);
void eeprom_basetopic_retrieve();
void fram_basetopic_retrieve();
void mqtt_port_set(uint16_t port);
void eeprom_mqtt_port_retrieve();
void eeprom_mqtt_username_retrieve();
void eeprom_mqtt_password_retrieve();
void fram_mqtt_port_retrieve();
void fram_mqtt_username_retrieve();
void fram_mqtt_password_retrieve();
void set_mqtt_username(String username);
void set_mqtt_password(String password);
void eeprom_mqtt_useauth_retrieve();
void fram_mqtt_useauth_retrieve();
void set_mqtt_useauth(bool use_auth);
void factory_reset();
void check_boot_reset();
void check_boot_reset();
void enable_adc(int id);
void disable_adc(int id);
void adc_update(int id);
void adc_update_force(int id);
void adc_update_all();
void adc_loop();
void publish_adc_value(int id);
void publish_adc_values();
uint16_t get_adc_value(int id);
void adc_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length);
void dac_set_value(int id, int value);
void dac_set_state(int id, bool state);
void dac_set_value_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length);
void dac_set_state_callback(char *topic, uint8_t topic_length, char *payload, unsigned int payload_length);
void publish_dac_value(int id);
void publish_dac_state(int id);
void publish_dac_values();
void publish_dac_states();
void publish_adc_state(int id);
void publish_adc_states();
uint16_t adc_get_value(int id);
bool adc_get_state(int id);
uint16_t dac_get_value(int id);
bool dac_get_state(int id);

View file

@ -1,5 +1,14 @@
#include "espmega_iot_emon.hpp"
/**
* @brief Constructor for ESPMega_CT class.
*
* @param analog_pin The analog pin to read the current sensor from.
* @param adc_to_watts A function pointer to a function that converts ADC value to watts.
* @param fram_address The address of the FRAM to store energy data.
*/
ESPMega_CT::ESPMega_CT(uint8_t analog_pin, float (*adc_to_watts)(uint16_t adc_value), uint32_t fram_address)
{
this->analog_pin = analog_pin;
@ -7,6 +16,11 @@ ESPMega_CT::ESPMega_CT(uint8_t analog_pin, float (*adc_to_watts)(uint16_t adc_va
this->adc_to_watts = adc_to_watts;
}
/**
* @brief Initializes the ESPMega_CT object.
*
* Reads the energy data from FRAM, sets the last conversion timestamp to current time, and calculates the current power.
*/
void ESPMega_CT::begin()
{
this->last_conversion_timestamp = millis();
@ -14,6 +28,11 @@ void ESPMega_CT::begin()
this->power = adc_to_watts(ESPMega_analogRead(this->analog_pin));
}
/**
* @brief The main loop function of the ESPMega_CT object.
*
* Calculates the energy consumed since the last loop iteration, updates the current power, and writes the energy data to FRAM.
*/
void ESPMega_CT::loop()
{
this->energy += (millis() - this->last_conversion_timestamp) / 3600000 * this->power;
@ -22,22 +41,41 @@ void ESPMega_CT::loop()
ESPMega_FRAM.write(fram_address, (uint8_t *)&this->energy, 16);
}
/**
* @brief Resets the energy data stored in FRAM and the energy variable.
*/
void ESPMega_CT::reset_energy()
{
this->energy = 0;
ESPMega_FRAM.write16(fram_address, 0);
}
/**
* @brief Returns the energy consumed since the object was initialized.
*
* @return The energy consumed since the object was initialized.
*/
long double ESPMega_CT::get_energy()
{
return this->energy;
}
/**
* @brief Returns the current power consumption.
*
* @return The current power consumption.
*/
float ESPMega_CT::get_power()
{
return this->power;
}
/**
* @brief A built-in function to convert ADC value to watts.
*
* @param adc_value The ADC value to convert to watts.
* @return The power in watts.
*/
float ESPMega_CT::adc_to_watts_builtin(uint16_t adc_value)
{
const float RATIO = 0.1;

View file

@ -1,13 +1,54 @@
/**
* @brief Class for measuring current and power consumption using a current transformer and an ADC.
*
*/
#pragma once
#include <ESPMegaPRO.h>
class ESPMega_CT {
public:
/**
* @brief Construct a new ESPMega_CT object
*
* @param analog_pin The analog pin to which the current transformer is connected.
* @param adc_to_watts A function pointer to a function that converts ADC values to watts.
* @param fram_address The address in FRAM where the energy consumption data is stored.
*/
ESPMega_CT(uint8_t analog_pin,float(*adc_to_watts)(uint16_t adc_value), uint32_t fram_address);
/**
* @brief Initializes the object.
*
*/
void begin();
/**
* @brief Updates the power and energy consumption values.
*
*/
void loop();
/**
* @brief Returns the current power consumption in watts.
*
* @return float The current power consumption in watts.
*/
float get_power();
/**
* @brief Returns the total energy consumption in watt-hours.
*
* @return long double The total energy consumption in watt-hours.
*/
long double get_energy();
/**
* @brief Resets the energy consumption value to zero.
*
*/
void reset_energy();
private:
uint8_t analog_pin;
uint32_t fram_address;

View file

@ -0,0 +1,28 @@
#include "espmega_iot_homeassistant.hpp"
void publishDiscoveryPayload(const char* component, const char* config) {
String topic = "homeassistant/" + String(component) + "/config";
mqttClient.publish(topic.c_str(), config);
}
void publishDiscoveryPayload(const char* component, const char* name, const char* deviceClass, const char* unitOfMeasurement, const char* stateTopic, const char* commandTopic, const char* availabilityTopic, const char* payloadAvailable, const char* payloadNotAvailable, const char* uniqueId) {
String config = "{";
config += "\"name\":\"" + String(name) + "\",";
config += "\"device_class\":\"" + String(deviceClass) + "\",";
config += "\"unit_of_measurement\":\"" + String(unitOfMeasurement) + "\",";
config += "\"state_topic\":\"" + String(stateTopic) + "\",";
config += "\"command_topic\":\"" + String(commandTopic) + "\",";
config += "\"availability_topic\":\"" + String(availabilityTopic) + "\",";
config += "\"payload_available\":\"" + String(payloadAvailable) + "\",";
config += "\"payload_not_available\":\"" + String(payloadNotAvailable) + "\",";
config += "\"unique_id\":\"" + String(uniqueId) + "\"";
config += "}";
String topic = "homeassistant/" + String(component) + "/config";
mqttClient.publish(topic.c_str(), config.c_str());
}
// Example usage:
publishDiscoveryPayload("sensor", "temperature", "temperature", "°C", "home/temperature", "home/temperature/set", "home/availability", "online", "offline", "temperature_sensor");
publishDiscoveryPayload("binary_sensor", "motion", "motion", "", "home/motion", "home/motion/set", "home/availability", "online", "offline", "motion_sensor");
publishDiscoveryPayload("climate", "thermostat", "temperature", "°C", "home/thermostat", "home/thermostat/set", "home/availability", "online", "offline", "thermostat");
publishDiscoveryPayload("switch", "light", "switch", "", "home/light", "home/light/set", "home/availability", "online", "offline", "light_switch");

View file

@ -0,0 +1,2 @@
#pragma once
#include <ESPMegaPRO.h>

View file

@ -1,6 +1,11 @@
#include <user_code.hpp>
#include <espmega_iot_lcd.hpp>
/**
* @brief Sends stop bit to LCD.
*
* This function sends a stop bit to the LCD. It is only enabled if the ENABLE_INTERNAL_LCD macro is defined.
*/
void lcd_send_stop_bit()
{
#ifdef ENABLE_INTERNAL_LCD
@ -10,6 +15,11 @@ void lcd_send_stop_bit()
#endif
}
/**
* Sends a command to the LCD display.
*
* @param command The command to send to the LCD display.
*/
void lcd_send_command(String command)
{
#ifdef ENABLE_INTERNAL_LCD
@ -19,6 +29,12 @@ void lcd_send_command(String command)
#endif
}
/**
* @brief Resets the LCD display.
*
* If ENABLE_INTERNAL_LCD is defined, sends a reset command to the LCD display.
*
*/
void lcd_reset()
{
#ifdef ENABLE_INTERNAL_LCD
@ -28,6 +44,9 @@ void lcd_reset()
#endif
}
/**
* Initializes the LCD display.
*/
void lcd_init()
{
#ifdef ENABLE_INTERNAL_LCD
@ -36,6 +55,12 @@ void lcd_init()
#endif
}
/**
* @brief Starts the upload process to the LCD.
*
* @param size The size of the data to be uploaded.
* @return true if the upload process started successfully, false otherwise.
*/
bool lcd_upload_start(size_t size)
{
Serial.begin(115200);
@ -49,6 +74,14 @@ bool lcd_upload_start(size_t size)
lcd_send_stop_bit();
lcd_wait_ack();
}
/**
* Writes data to the LCD upload buffer.
*
* @param data Pointer to the data to be written.
* @param size Size of the data to be written.
* @return True if the write was successful, false otherwise.
*/
bool lcd_upload_write(uint8_t *data, size_t size)
{
for (int i = 0; i < size; i++)
@ -57,6 +90,10 @@ bool lcd_upload_write(uint8_t *data, size_t size)
}
}
/**
* Waits for an acknowledgement signal from the LCD.
* @return true if acknowledgement signal is received, false otherwise.
*/
bool lcd_wait_ack()
{
bool data_ok = false;

View file

@ -1,5 +1,9 @@
#include "espmega_iot_timer.hpp"
/**
* @brief Loop function that checks if the timer should run and calls the timer callback function.
*
*/
void ESPMega_Timer::loop() {
rtctime_t curtime = ESPMega_getTime();
if(today!=curtime.day) {
@ -14,6 +18,14 @@ void ESPMega_Timer::loop() {
}
}
/**
* @brief Constructor for ESPMega_Timer class.
*
* @param hour The hour at which the timer should run.
* @param minute The minute at which the timer should run.
* @param timer_callback The function to be called when the timer runs.
* @param fram_address The address in FRAM where the timer state is stored.
*/
ESPMega_Timer::ESPMega_Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address) {
this->hr = hour;
this->min = minute;
@ -21,6 +33,10 @@ ESPMega_Timer::ESPMega_Timer(uint8_t hour,uint8_t minute,void(*timer_callback)()
this->fram_address = fram_address;
}
/**
* @brief Begins the timer and sets the initial timer state.
*
*/
void ESPMega_Timer::begin() {
rtctime_t curtime = ESPMega_getTime();
this-> today = curtime.day;
@ -28,6 +44,12 @@ void ESPMega_Timer::begin() {
loop();
}
/**
* @brief Sets the hour and minute at which the timer should run.
*
* @param hour The hour at which the timer should run.
* @param minute The minute at which the timer should run.
*/
void ESPMega_Timer::set(uint8_t hour,uint8_t minute) {
rtctime_t curtime = ESPMega_getTime();
if ((hr < curtime.hours || (hr == curtime.hours && min <= curtime.minutes))) {

View file

@ -1,12 +1,43 @@
#pragma once
#include <ESPMegaPRO.h>
/**
* @brief Class representing a timer for ESPMega board.
*
*/
class ESPMega_Timer {
public:
/**
* @brief Loop function to be called in the main loop.
*
*/
void loop();
ESPMega_Timer(uint8_t hour,uint8_t minute,void(*timer_callback)(), uint32_t fram_address);
void set(uint8_t hour,uint8_t minute);
/**
* @brief Constructor for ESPMega_Timer class.
*
* @param hour The hour at which the timer should trigger.
* @param minute The minute at which the timer should trigger.
* @param timer_callback The function to be called when the timer triggers.
* @param fram_address The address of the FRAM memory to store the timer state.
*/
ESPMega_Timer(uint8_t hour, uint8_t minute, void(*timer_callback)(), uint32_t fram_address);
/**
* @brief Set the hour and minute at which the timer should trigger.
*
* @param hour The hour at which the timer should trigger.
* @param minute The minute at which the timer should trigger.
*/
void set(uint8_t hour, uint8_t minute);
/**
* @brief Begin the timer.
*
*/
void begin();
private:
uint8_t today;
uint8_t timer_ran_today;

View file

@ -6,19 +6,31 @@
#include "espmega_iot_timer.hpp"
#include "espmega_iot_external_lcd.hpp"
// Enable Software Module(s)
#define ENABLE_INTERNAL_LCD
#define ENABLE_IR_MODULE
#define ENABLE_CLIMATE_MODULE // Require IR Module
#define ENABLE_WEBUI
// Bus Overclocking Configuration
// Do not enable if you are using external I/O cards as it will cause signal integrity issues.
// Choose only one mode
//#define OVERCLOCK_FM
//#define OVERCLOCK_FM2
// Infrared Transciever
// I/O Configuration
#define VIRTUAL_INTERRUPT_PRELOAD // Preload Virtual Interrupts buffer
// Enable Software Module(s)
// Deprecated. Use Build Flags instead.
// #define ENABLE_INTERNAL_LCD
// #define ENABLE_IR_MODULE
// #define ENABLE_CLIMATE_MODULE // Require IR Module
// #define ENABLE_ANALOG_MODULE
// #define ENABLE_WEBUI
// IR Kit Configuration
#define IR_RECIEVE_PIN 35
#define IR_SEND_PIN 17
#define MARK_EXCESS_MICROS 20
#define RAW_BUFFER_LENGTH 750
#define IR_RAW_BUFFER_LENGTH 750
#define AC_MAX_TEMPERATURE 30
#define AC_MIN_TEMPERATURE 15
#define DHT22_PIN 32
// External LCD Configuration
#define ENABLE_EXTERNAL_LCD
@ -29,19 +41,24 @@
#define ESPMega_EXTLCD Serial2
#endif
// Analog Module Configuration
#define ANALOG_REPORTING_INTERVAL 500
// User Defined Functions
void timer1_callback();
void bt0PopCallback(void *ptr);
// User Defined IoT Core Callback Functions (Required)
void user_mqtt_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length);
void user_state_request_callback();
void mqtt_connected_user_callback();
void user_pre_init();
void user_init();
void user_loop();
void virtual_interrupt_user_callback(int pin, int state);
void pwm_changed_user_callback(int pin);
void ac_changed_user_callback(int mode, int temperature, int fan_speed);
void timer_tick_callback();
void timer1_callback();
void mqtt_connected_user_callback();
void bt0PopCallback(void *ptr);
void user_state_request_callback();
void user_mqtt_callback(char* topic, uint8_t topic_length, char* payload, unsigned int payload_length);
void virtual_interrupt_user_callback(int pin, int state);
// ESPMega IoT Core Build-in Functions
extern void pwm_set_state(int id, int state);
@ -53,9 +70,26 @@ extern bool pwm_get_state(int id);
extern uint16_t pwm_get_value(int id);
extern boolean pwm_group_state(int id1, int id2);
extern bool input_get_state(int id);
extern bool standalone;
extern PubSubClient mqtt;
// IR Kit Build-in Functions
#ifdef ENABLE_IR_MODULE
extern void ac_set_state(int mode, int temperature, int fan_speed);
extern uint8_t ac_get_temperature();
extern uint8_t ac_get_mode();
extern uint8_t ac_get_fan_speed();
extern bool standalone;
extern PubSubClient mqtt;
#endif
//Analog Expansion Card Build-in Functions
#ifdef ENABLE_ANALOG_MODULE
extern void dac_set_value(int id, int value);
extern void dac_set_state(int id, bool state);
extern void enable_adc(int id);
extern void disable_adc(int id);
extern void enable_dac(int id);
extern uint16_t adc_get_value(int id);
extern bool adc_get_state(int id);
extern uint16_t dac_get_value(int id);
extern bool dac_get_state(int id);
#endif