Thanks Deva_Rishi. The code I have been following is made under platformio so its different to the arduino format.
But this is the core of the code. I have build up a LillyGo TTGO ESP32 slightly different display but its working and measuring Watts via CT clamp. I have it connected to Home Assistant via MQTT and have not enabled AWS.
config.h
#define LOCAL_MEASUREMENTS 30
#define HA_ENABLED true
#define HA_ADDRESS "x.x.x.x"
#define HA_PORT 1883
#define HA_USER "username"
#define HA_PASSWORD "password"
main.cpp this runs a task called measureElectricity
unsigned short measurements[LOCAL_MEASUREMENTS];
unsigned char measureIndex = 0;
xTaskCreate(
measureElectricity,
"Measure electricity", // Task name
5000, // Stack size (bytes)
NULL, // Parameter
4, // Task priority
NULL // Task handle
);
If set to true Home Assistant enabled
#if HA_ENABLED == true
xTaskCreate(
HADiscovery,
"MQTT-HA Discovery", // Task name
5000, // Stack size (bytes)
NULL, // Parameter
5, // Task priority
NULL // Task handle
);
xTaskCreate(
keepHAConnectionAlive,
"MQTT-HA Connect",
5000,
NULL,
4,
NULL
);
#endif
measure-electricty.h task sendEnergyToHA
#ifndef TASK_MEASURE_ELECTRICITY
#define TASK_MEASURE_ELECTRICITY
#include <Arduino.h>
#include "EmonLib.h"
#include "../config/config.h"
#include "../config/enums.h"
#include "mqtt-aws.h"
#include "mqtt-home-assistant.h"
extern DisplayValues gDisplayValues;
extern EnergyMonitor emon1;
extern unsigned short measurements[];
extern unsigned char measureIndex;
void measureElectricity(void * parameter)
{
for(;;){
serial_println("[ENERGY] Measuring...");
long start = millis();
double amps = emon1.calcIrms(1480);
double watts = amps * HOME_VOLTAGE;
gDisplayValues.amps = amps;
gDisplayValues.watt = watts;
measurements[measureIndex] = watts;
measureIndex++;
if(measureIndex == LOCAL_MEASUREMENTS){
#if AWS_ENABLED == true
xTaskCreate(
uploadMeasurementsToAWS,
"Upload measurements to AWS",
10000, // Stack size (bytes)
NULL, // Parameter
5, // Task priority
NULL // Task handle
);
#endif
#if HA_ENABLED == true
xTaskCreate(
sendEnergyToHA,
"HA-MQTT Upload",
10000, // Stack size (bytes)
NULL, // Parameter
5, // Task priority
NULL // Task handle
);
#endif
}
measureIndex = 0;
long end = millis();
// Schedule the task to run again in 1 second (while
// taking into account how long measurement took)
vTaskDelay((1000-(end-start)) / portTICK_PERIOD_MS);
}
}
#endif
this calls mqtt-home-assistant.h sendEnergyToHA this is where my question starts from
#ifndef TASK_HOME_ASSISTANT
#define TASK_HOME_ASSISTANT
#if HA_ENABLED == true
#include <Arduino.h>
//#include <WiFiClientSecure.h>
#include <WiFiClient.h>
#include <MQTTClient.h>
#include "../config/config.h"
//WiFiClientSecure HA_net;
WiFiClient HA_net;
MQTTClient HA_mqtt(1024);
extern unsigned short measurements[];
const char* PROGMEM HA_discovery_msg = "{"
"\"name\":\"" DEVICE_NAME "\","
"\"device_class\":\"power\","
"\"unit_of_measurement\":\"W\","
"\"icon\":\"mdi:transmission-tower\","
"\"state_topic\":\"homeassistant/sensor/" DEVICE_NAME "/state\","
"\"value_template\":\"{{ value_json.power}}\","
"\"device\": {"
"\"name\":\"" DEVICE_NAME "\","
"\"sw_version\":\"2.0\","
"\"model\":\"HW V2\","
"\"manufacturer\":\"Xavier Decuyper\","
"\"identifiers\":[\"" DEVICE_NAME "\"]"
"}"
"}";
/**
* Established a connection to Home Assistant MQTT broker.
*
* This task should run continously. It will check if an
* MQTT connection is active and if so, will sleep for 1
* minute. If not, a new connection will be established.
*/
void keepHAConnectionAlive(void * parameter){
for(;;){
// When we are connected, loop the MQTT client and sleep for 0,5s
if(HA_mqtt.connected()){
HA_mqtt.loop();
vTaskDelay(250 / portTICK_PERIOD_MS);
continue;
}
if(!WiFi.isConnected()){
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
serial_println(F("[MQTT] Connecting to HA..."));
HA_mqtt.begin(HA_ADDRESS, HA_PORT, HA_net);
long startAttemptTime = millis();
while (!HA_mqtt.connect(DEVICE_NAME, HA_USER, HA_PASSWORD) &&
millis() - startAttemptTime < MQTT_CONNECT_TIMEOUT)
{
vTaskDelay(MQTT_CONNECT_DELAY / portTICK_PERIOD_MS);
}
if(!HA_mqtt.connected()){
serial_println(F("[MQTT] HA connection failed. Waiting 30s.."));
vTaskDelay(30000 / portTICK_PERIOD_MS);
}
serial_println(F("[MQTT] HA Connected!"));
}
}
/**
* TASK: Every 15 minutes we send Home Assistant a discovery message
* so that the energy monitor shows up in the device registry.
*/
void HADiscovery(void * parameter){
for(;;){
if(!HA_mqtt.connected()){
serial_println("[MQTT] HA: no MQTT connection.");
vTaskDelay(30 * 1000 / portTICK_PERIOD_MS);
continue;
}
serial_println("[MQTT] HA sending auto discovery");
HA_mqtt.publish("homeassistant/sensor/" DEVICE_NAME "/config", HA_discovery_msg);
vTaskDelay(15 * 60 * 1000 / portTICK_PERIOD_MS);
}
}
void sendEnergyToHA(void * parameter){
if(!HA_mqtt.connected()){
serial_println("[MQTT] Can't send to HA without MQTT. Abort.");
vTaskDelete(NULL);
}
char msg[30];
strcpy(msg, "{\"power\":");
strcat(msg, String(measurements[LOCAL_MEASUREMENTS-1]).c_str());
strcat(msg, "}");
serial_print("[MQTT] HA publish: ");
serial_println(msg);
HA_mqtt.publish("homeassistant/sensor/" DEVICE_NAME "/state", msg);
// Task is done!
vTaskDelete(NULL);
}
#endif
#endif