add commands
This commit is contained in:
parent
5a99550e9c
commit
a3b0eea2c6
63
src/main.cpp
63
src/main.cpp
|
@ -1,7 +1,6 @@
|
|||
#include <Arduino.h>
|
||||
#include <Homie.h>
|
||||
|
||||
|
||||
/*
|
||||
* Wemos d1 mini
|
||||
* Flash Size: 4M (1M SPIFFS)
|
||||
|
@ -10,7 +9,9 @@
|
|||
//Upload config: platformio run --target uploadfs
|
||||
|
||||
|
||||
|
||||
#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)
|
||||
|
||||
// HX711 circuit wiring
|
||||
const int LOADCELL_DOUT_PIN = D2;
|
||||
|
@ -46,6 +47,8 @@ int sort_desc(const void *cmp1, const void *cmp2);
|
|||
float getFilteredWeight();
|
||||
float getWeightSpread();
|
||||
void sendWeight(float w);
|
||||
bool cmdHandler(const HomieRange& range, const String& value);
|
||||
void powerOff();
|
||||
|
||||
|
||||
void setup() {
|
||||
|
@ -68,7 +71,7 @@ void setup() {
|
|||
scaleNode.advertise("raw");
|
||||
scaleNode.advertise("max");
|
||||
|
||||
|
||||
scaleNode.advertise("CMD").settable(cmdHandler); //function inputHandler gets called on new message on topic/input/set
|
||||
|
||||
Homie.setup();
|
||||
|
||||
|
@ -78,7 +81,8 @@ void setup() {
|
|||
|
||||
//calibration
|
||||
Serial.println("setup");
|
||||
scale.set_scale(23805); //calibrated with 2.25kg weight
|
||||
|
||||
scale.set_scale(SCALE_CALIBRATION);
|
||||
delay(500);
|
||||
scale.tare();
|
||||
delay(2000);
|
||||
|
@ -144,6 +148,7 @@ void loopHandler() {
|
|||
weight_read[weight_read_pos]=weight_current; //one reading takes 91ms
|
||||
} else {
|
||||
Serial.println("HX711 not found.");
|
||||
scaleNode.setProperty("cmd").send("HX711 not found"); //can be done in main loop
|
||||
}
|
||||
|
||||
float weight_filtered=getFilteredWeight();
|
||||
|
@ -184,16 +189,11 @@ void loopHandler() {
|
|||
sendWeight(weight_max-weight_tare);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#define STAYONTIME_AFTER_SENT 5000
|
||||
if (millis() > MAXONTIME || (weight_sent && millis()>weight_sent_time+STAYONTIME_AFTER_SENT)) {
|
||||
Serial.println("Turning Off");
|
||||
Serial.flush();
|
||||
delay(100);
|
||||
digitalWrite(PIN_SELFENABLE, LOW);
|
||||
powerOff();
|
||||
}
|
||||
|
||||
|
||||
|
@ -254,3 +254,48 @@ void sendWeight(float w) {
|
|||
weight_sent_time=millis();
|
||||
Serial.print("Weight sent="); Serial.println(w,3);
|
||||
}
|
||||
|
||||
|
||||
bool cmdHandler(const HomieRange& range, const String& value) {
|
||||
if (range.isRange) {
|
||||
return false; //if range is given but index is not in allowed range
|
||||
}
|
||||
Homie.getLogger() << "cmd" << ": " << value << endl;
|
||||
|
||||
|
||||
//boolean value
|
||||
if (value=="reset") {
|
||||
if (scale.wait_ready_timeout(1000)) { //for non blocking mode
|
||||
scale.set_scale(0);
|
||||
scale.tare();
|
||||
scaleNode.setProperty("cmd").send("tared");
|
||||
} else {
|
||||
Serial.println("HX711 not found.");
|
||||
scaleNode.setProperty("cmd").send("HX711 not found");
|
||||
}
|
||||
|
||||
}if (value=="adc") {
|
||||
if (scale.wait_ready_timeout(1000)) { //for non blocking mode
|
||||
scaleNode.setProperty("cmd").send("adc="+scale.read_average(20));
|
||||
} else {
|
||||
Serial.println("HX711 not found.");
|
||||
scaleNode.setProperty("cmd").send("HX711 not found");
|
||||
}
|
||||
|
||||
}if (value=="off") {
|
||||
powerOff();
|
||||
scaleNode.setProperty("cmd").send("shutting down");
|
||||
} else {
|
||||
Homie.getLogger() << "cmd not recognized" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void powerOff() {
|
||||
Serial.println("Turning Off");
|
||||
Serial.flush();
|
||||
delay(100);
|
||||
digitalWrite(PIN_SELFENABLE, LOW);
|
||||
}
|
Loading…
Reference in New Issue