add mqtt display functions
This commit is contained in:
parent
2457d6ee51
commit
9fab6f4d16
79
src/main.cpp
79
src/main.cpp
|
@ -26,12 +26,20 @@ const uint8_t SEG_POINT[] = { SEG_DP};
|
||||||
|
|
||||||
uint8_t display_data[] = { 0xff, 0xff, 0xff, 0xff };
|
uint8_t display_data[] = { 0xff, 0xff, 0xff, 0xff };
|
||||||
uint8_t display_blank[] = { 0x00, 0x00, 0x00, 0x00 };
|
uint8_t display_blank[] = { 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
uint8_t display_custom[] = { 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
unsigned long last_displayupdate=0;
|
unsigned long last_displayupdate=0;
|
||||||
#define DISPLAYUPDATEINTERVAL 100 //maximum time to update display
|
#define DISPLAYUPDATEINTERVAL 100 //maximum time to update display
|
||||||
#define DISPLAYUPDATEINTERVAL_MIN 10 //minimum display update time
|
#define DISPLAYUPDATEINTERVAL_MIN 10 //minimum display update time
|
||||||
bool update_display=true;
|
bool update_display=true;
|
||||||
|
|
||||||
|
uint8_t displaybrightness = 7; //0 to 7
|
||||||
|
|
||||||
|
unsigned long last_new_display_custom=0;
|
||||||
|
unsigned long display_custom_duration=1000*3;
|
||||||
|
|
||||||
|
unsigned long last_display_blink=0; //for turning off shortly
|
||||||
|
unsigned long display_blink_duration=100;
|
||||||
|
|
||||||
#include "HX711.h"
|
#include "HX711.h"
|
||||||
//#define SCALE_CALIBRATION 23805 //calibrated with 2.25kg weight. devide adc reading by calibration weight (in kg) to get this value (or other way around)
|
//#define SCALE_CALIBRATION 23805 //calibrated with 2.25kg weight. devide adc reading by calibration weight (in kg) to get this value (or other way around)
|
||||||
|
@ -46,6 +54,8 @@ const int PIN_SELFENABLE = D1;
|
||||||
HX711 scale;
|
HX711 scale;
|
||||||
|
|
||||||
float weight_current=0; //last weight reading
|
float weight_current=0; //last weight reading
|
||||||
|
float weight_filtered=0;
|
||||||
|
float spread=0;
|
||||||
|
|
||||||
#define MEASURE_INTERVAL 100 //ms
|
#define MEASURE_INTERVAL 100 //ms
|
||||||
#define READING_FILTER_SIZE 40 //latency is about READING_FILTER_SIZE/2*MEASURE_INTERVAL
|
#define READING_FILTER_SIZE 40 //latency is about READING_FILTER_SIZE/2*MEASURE_INTERVAL
|
||||||
|
@ -72,16 +82,19 @@ bool livesend=false; //if true, sends continuous data over mqtt
|
||||||
void loopHandler();
|
void loopHandler();
|
||||||
|
|
||||||
HomieNode scaleNode("weight", "Scale", "scale"); //paramters: topic, $name, $type
|
HomieNode scaleNode("weight", "Scale", "scale"); //paramters: topic, $name, $type
|
||||||
|
HomieNode displayNode("display", "Scale", "scale"); //paramters: topic, $name, $type
|
||||||
|
|
||||||
int sort_desc(const void *cmp1, const void *cmp2);
|
int sort_desc(const void *cmp1, const void *cmp2);
|
||||||
float getFilteredWeight();
|
float getFilteredWeight();
|
||||||
float getWeightSpread();
|
float getWeightSpread();
|
||||||
void sendWeight(float w);
|
void sendWeight(float w);
|
||||||
bool cmdHandler(const HomieRange& range, const String& value);
|
bool cmdHandler(const HomieRange& range, const String& value);
|
||||||
|
bool displayNodeHandler(const HomieRange& range, const String& value);
|
||||||
void powerOff();
|
void powerOff();
|
||||||
void displayNumber(float numberdisplay);
|
void displayNumber(float numberdisplay);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(PIN_SELFENABLE,OUTPUT);
|
pinMode(PIN_SELFENABLE,OUTPUT);
|
||||||
digitalWrite(PIN_SELFENABLE, HIGH);
|
digitalWrite(PIN_SELFENABLE, HIGH);
|
||||||
|
@ -91,7 +104,7 @@ void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
|
||||||
display.setBrightness(7, true); //brightness 0 to 7
|
display.setBrightness(displaybrightness, true); //brightness 0 to 7
|
||||||
|
|
||||||
|
|
||||||
Homie.disableResetTrigger(); //disable config reset if pin 1 (D3) is low on startup
|
Homie.disableResetTrigger(); //disable config reset if pin 1 (D3) is low on startup
|
||||||
|
@ -107,7 +120,7 @@ void setup() {
|
||||||
scaleNode.advertise("raw");
|
scaleNode.advertise("raw");
|
||||||
scaleNode.advertise("max");
|
scaleNode.advertise("max");
|
||||||
|
|
||||||
|
displayNode.advertise("segments").settable(displayNodeHandler);
|
||||||
|
|
||||||
Homie.setup();
|
Homie.setup();
|
||||||
|
|
||||||
|
@ -135,6 +148,8 @@ void loop() {
|
||||||
void loopHandler() {
|
void loopHandler() {
|
||||||
unsigned long loopmillis=millis();
|
unsigned long loopmillis=millis();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static unsigned long last_measure=0;
|
static unsigned long last_measure=0;
|
||||||
if (loopmillis>last_measure+MEASURE_INTERVAL) {
|
if (loopmillis>last_measure+MEASURE_INTERVAL) {
|
||||||
last_measure=loopmillis;
|
last_measure=loopmillis;
|
||||||
|
@ -151,8 +166,8 @@ void loopHandler() {
|
||||||
scaleNode.setProperty("cmd").send("HX711 not found"); //can be done in main loop
|
scaleNode.setProperty("cmd").send("HX711 not found"); //can be done in main loop
|
||||||
}
|
}
|
||||||
|
|
||||||
float weight_filtered=getFilteredWeight();
|
weight_filtered=getFilteredWeight();
|
||||||
float spread=getWeightSpread();
|
spread=getWeightSpread();
|
||||||
|
|
||||||
//Serial.println(weight_current);
|
//Serial.println(weight_current);
|
||||||
//Serial.print("spread="); Serial.println(spread,3);
|
//Serial.print("spread="); Serial.println(spread,3);
|
||||||
|
@ -166,6 +181,7 @@ void loopHandler() {
|
||||||
weight_tare=weight_filtered;
|
weight_tare=weight_filtered;
|
||||||
update_display=true;
|
update_display=true;
|
||||||
Serial.print("new tare="); Serial.println(weight_tare,3);
|
Serial.print("new tare="); Serial.println(weight_tare,3);
|
||||||
|
last_display_blink=loopmillis; //blink
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#define MAXSPREAD_MEASURE 0.5 //in kg
|
#define MAXSPREAD_MEASURE 0.5 //in kg
|
||||||
|
@ -174,6 +190,7 @@ void loopHandler() {
|
||||||
weight_max=weight_filtered;
|
weight_max=weight_filtered;
|
||||||
update_display=true;
|
update_display=true;
|
||||||
Serial.print("new max="); Serial.println(weight_max,3);
|
Serial.print("new max="); Serial.println(weight_max,3);
|
||||||
|
last_display_blink=loopmillis; //blink
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,7 +220,6 @@ void loopHandler() {
|
||||||
|
|
||||||
dtostrf(weight_max-weight_tare,4, 3, charBuf);
|
dtostrf(weight_max-weight_tare,4, 3, charBuf);
|
||||||
scaleNode.setProperty("max").send(charBuf); //filtered and auto tared
|
scaleNode.setProperty("max").send(charBuf); //filtered and auto tared
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define STAYONTIME_AFTER_SENT 5000
|
#define STAYONTIME_AFTER_SENT 5000
|
||||||
|
@ -214,7 +230,19 @@ void loopHandler() {
|
||||||
if ( (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL) | (update_display && (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL_MIN) )) {
|
if ( (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL) | (update_display && (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL_MIN) )) {
|
||||||
last_displayupdate=loopmillis;
|
last_displayupdate=loopmillis;
|
||||||
update_display=false; //reset flag
|
update_display=false; //reset flag
|
||||||
displayNumber(weight_current);
|
if ((loopmillis >= last_new_display_custom) && (loopmillis < last_new_display_custom+display_custom_duration)) {
|
||||||
|
display.setBrightness(displaybrightness,true); //enable display
|
||||||
|
display.setSegments(display_custom);
|
||||||
|
}else{
|
||||||
|
displayNumber(weight_filtered);
|
||||||
|
display.setSegments(display_data);
|
||||||
|
|
||||||
|
if ((loopmillis >= last_display_blink) && (loopmillis < last_display_blink+display_blink_duration)) {
|
||||||
|
display.setBrightness(displaybrightness,false);
|
||||||
|
}else{
|
||||||
|
display.setBrightness(displaybrightness,true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -322,6 +350,42 @@ bool cmdHandler(const HomieRange& range, const String& value) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool displayNodeHandler(const HomieRange& range, const String& value) {
|
||||||
|
if (range.isRange) {
|
||||||
|
return false; //if range is given but index is not in allowed range
|
||||||
|
}
|
||||||
|
Homie.getLogger() << "displayset" << ": " << value << endl;
|
||||||
|
|
||||||
|
if (value.length()!=8*4) { //treat as number to show
|
||||||
|
|
||||||
|
displayNumber(value.toFloat());
|
||||||
|
for (uint8_t i=0;i<4;i++) {
|
||||||
|
display_custom[i]=display_data[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
uint8_t number = 0;
|
||||||
|
for (uint8_t i=0; i< value.length(); i++) // for every character in the string strlen(s) returns the length of a char array
|
||||||
|
{
|
||||||
|
number *= 2; // double the result so far
|
||||||
|
if (value[i] == '1'){
|
||||||
|
number++; //add 1 if needed
|
||||||
|
Serial.print("1("); Serial.print(i); Serial.print(")");
|
||||||
|
}
|
||||||
|
if ((i+1)%8==0 && i!=0) { //was the last bit
|
||||||
|
display_custom[i/8] = number; //use this number
|
||||||
|
Serial.print(" ->");Serial.print(i/8); Serial.print("="); Serial.println(number);
|
||||||
|
number=0; //reset number for next byte
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
last_new_display_custom=millis(); //save time when message arrived
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void powerOff() {
|
void powerOff() {
|
||||||
Serial.println("Turning Off");
|
Serial.println("Turning Off");
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
|
@ -393,8 +457,7 @@ void displayNumber(float numberdisplay) {
|
||||||
display_data[3] |= SEG_DP;
|
display_data[3] |= SEG_DP;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println();
|
|
||||||
display.setSegments(display_data);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue