Advertisement
Otisburgh_its

custom_hx711.h

Jun 1st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include "esphome.h"
  2. #include "HX711.h"
  3.  
  4. #define DOUT  D2
  5. #define CLK  D3
  6.  
  7. class CustomHX711 : public PollingComponent, public Sensor {
  8.  public:
  9.  
  10.   CustomHX711(int poll=5000) : PollingComponent(poll){}
  11.   CustomHX711(int poll=5000, float offset = -7050) : PollingComponent(poll)
  12.   {
  13.      calibration = offset;
  14.   }
  15.  
  16.   float calibration; // = -7050; //-11000
  17.   HX711 scale;
  18.   int polltime;
  19.  
  20.   void setup(int poll, float offset){
  21.     calibration = offset;
  22.   }
  23.  
  24.   void setup() override {
  25.     scale.begin(DOUT, CLK);
  26.     scale.set_scale(calibration);
  27.     scale.tare();
  28.   }
  29.  
  30.   void update() override {
  31.  
  32.      //Serial.println(scale.read());                 // print a raw reading from the ADC
  33.      //Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC
  34.      //Serial.println(scale.get_value(5));      // print the average of 5 readings from the ADC minus the tare weight, set with tare()
  35.      //Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
  36.  
  37.      float w = scale.get_units(10);
  38.  
  39.  
  40.      w = roundf(w * 100) / 100;
  41.      publish_state(w);
  42.   }
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement