Reading a voltage - I don't know the syntax for displaying it in an object #928
-
I've compiled my own OpenHasp for a Guition 4848S040 using GitPod. It reads the Battery voltage using an ADC on GPIO 1. It seems to work as I can see the voltage in MQTT Explorer (vbat_Fraction and vbat_Level). Here's the my_custom.cpp code. /* MIT License - Copyright (c) 2019-2024 Francis Van Roie
For full license information read the LICENSE file in the project folder */
// USAGE: - Copy this file and rename it to my_custom.cpp
// - Change false to true on line 9
#include "hasplib.h"
#if defined(HASP_USE_CUSTOM) && HASP_USE_CUSTOM > 0 && true // <-- set this to true in your code
#include "hasp_debug.h"
unsigned long last_blink = 0;
const int voltage_read = 1; // GPIO 1 can do ADC
const int blink_speed = 10000; // Every 10 seconds
float batteryFraction;
float voltageLevel;
int vbat;
// Voltage read
const int MAX_ANALOG_VAL = 4095;
const float MAX_BATTERY_VOLTAGE = 4.2; // Typical Max of a LiPo
void custom_setup()
{
// Initialization code here
last_blink = millis();
pinMode(voltage_read, INPUT);
randomSeed(millis());
}
void custom_loop()
{
// Non-blocking code here, this should execute very fast!
if(blink_speed && (millis() - last_blink > blink_speed)){
vbat = analogRead(voltage_read);
voltageLevel = (vbat / 4095.0 * 4.2);
batteryFraction = voltageLevel / MAX_BATTERY_VOLTAGE;
last_blink = millis();
}
}
void custom_every_second()
{
Serial.print("#");
}
void custom_every_5seconds()
{
LOG_VERBOSE(TAG_CUSTOM, "5 seconds have passsed...");
String vbatFraction = String(batteryFraction);
String vbatLevel = String(voltageLevel);
String jsonStringL = "{\"vbat_Level\":" + vbatLevel + "}";
String jsonStringF = "{\"vbat_Fraction\":" + vbatFraction + "}";
const char* jsonCharL = jsonStringL.c_str();
const char* jsonCharF = jsonStringF.c_str();
dispatch_state_subtopic("vbat_Fraction", jsonCharF);
dispatch_state_subtopic("vbat_Level", jsonCharL);
}
bool custom_pin_in_use(uint8_t pin)
{
if(pin == 1) return true; // This is the only GPIO I use here
// otherwise the pin is not used
return false;
}
void custom_get_sensors(JsonDocument& doc)
{
/* Sensor Name */
JsonObject sensor = doc.createNestedObject(F("Custom"));
/* Key-Value pair of the sensor value */
sensor[F("Random")] = HASP_RANDOM(256);
}
void custom_topic_payload(const char* topic, const char* payload, uint8_t source){
// Not used
}
void custom_state_subtopic(const char* subtopic, const char* payload){
// Not used
}
#endif // HASP_USE_CUSTOM I don't know what some of it does or why; I just copied and modified examples I found. A few questions:
Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You can include a function to update a label or gauge object like this: static void custom_label_update(uint8_t page, uint8_t id, String text)
{
lv_obj_t* label = hasp_find_obj_from_page_id(page, id);
if(!label) return; // object doesn't exist
lv_label_set_text(label, text.c_str()); // Use c string function the get the text data only
}
static void custom_gauge_update(uint8_t page, uint8_t id, int32_t val)
{
lv_obj_t* gauge = hasp_find_obj_from_page_id(page, id);
if(!gauge) return; // object doesn't exist
lv_gauge_set_value(gauge, 0, val); // 0 is the needle id (always use 0)
}
static void custom_read_battery() {
if(millis() - last_blink >= blink_speed) { // only update the values every 10 seconds
vbat = analogRead(voltage_read);
voltageLevel = (vbat / 4095.0 * 4.2);
batteryFraction = voltageLevel / MAX_BATTERY_VOLTAGE;
last_blink = millis();
}
} Then you can call them as needed from void custom_every_5seconds()
{
LOG_VERBOSE(TAG_CUSTOM, "5 seconds have passsed...");
custom_read_battery();
String vbatFraction = String(batteryFraction);
String vbatLevel = String(voltageLevel);
// This will update the objects on screen using page number and object id
custom_label_update(1, 150, vbatFraction);
custom_label_update(1, 151, vbatLevel );
int32_t val = vbat *100 / 4095; // calculate the correct val for the gauge needle
custom_gauge_update(1, 160, val); // val depends on the min and max value of the gauge
// This will publish the data to MQTT, if you don't need that remove this code
String jsonStringL = "{\"vbat_Level\":" + vbatLevel + "}";
String jsonStringF = "{\"vbat_Fraction\":" + vbatFraction + "}";
const char* jsonCharL = jsonStringL.c_str();
const char* jsonCharF = jsonStringF.c_str();
dispatch_state_subtopic("vbat_Fraction", jsonCharF);
dispatch_state_subtopic("vbat_Level", jsonCharL);
} To make the code run as fast as possible, remove the updating in void custom_loop()
{
}
void custom_every_second()
{
} Only read the GPIO before it's output is actually being used... openHASP will post to the MQTT sensors subtopic every 30 seconds, you can add the data to that JSON if needed: void custom_get_sensors(JsonDocument& doc)
{
custom_read_battery();
String vbatFraction = String(batteryFraction);
String vbatLevel = String(voltageLevel);
/* Sensor Name */
JsonObject sensor = doc.createNestedObject(F("Battery"));
/* Key-Value pair of the sensor value */
sensor[F("Fraction")] = vbatFraction;
sensor[F("Level")] = vbatLevel;
} I'm not a Home Assistant expert, but you can verify that the sensor values are available in MQTT using a tool like MQTT Explorer. Then check the Home Assistant guide to use the MQTT values... Note: This code has not been tested and might need testing or tweaking to get it to compile and work properly, but this should get you on your way to update the objects and post sensor data to MQTT. |
Beta Was this translation helpful? Give feedback.
You can include a function to update a label or gauge object like this: