supla-device
Loading...
Searching...
No Matches
HX711.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_SENSOR_HX711_H_
5#define SRC_SUPLA_SENSOR_HX711_H_
6
7/*
8Dependency: https://github.com/olkal/HX711_ADC,
9use library manager to install it
10*/
11
12#include <HX711_ADC.h>
13#include <supla/log_wrapper.h>
14#include <supla/time.h>
15
16#include "weight.h"
17#include "../storage/storage.h"
18
19namespace Supla {
20namespace Sensor {
21
22#pragma pack(push, 1)
24 uint32_t tareOffset;
25 float calValue;
26};
27#pragma pack(pop)
28
29class HX711 : public Weight {
30 public:
31 HX711(int sdaPin, int sclPin, float defCalValue = 696.0)
32 : hx711Sensor(sdaPin, sclPin), defCalValue(defCalValue) {
33 }
34
35 double getValue() {
36 if ((hx711Sensor.getSPS() < 7) || (hx711Sensor.getSPS() > 100)) {
37 value = WEIGHT_NOT_AVAILABLE;
38 SUPLA_LOG_DEBUG(
39 "HX711 measured sampling rate: %.2f HZ is abnormal, check connection"
40 , hx711Sensor.getSPS());
41 } else {
42 value = (value < 0 && value > -1) ? 0 : value;
43 // Displaying one decimal in Cloud
44 int tmp = static_cast<int>(value*10);
45 value = static_cast<double>(tmp/10.0);
46 }
47 return value;
48 }
49
50 void onInit() {
51 hx711Sensor.begin();
52 hx711Sensor.setTareOffset(tareOffset);
53 uint64_t stabilizingTime = 2000;
54 bool _tare = false;
55 hx711Sensor.start(stabilizingTime, _tare);
56 if (hx711Sensor.getTareTimeoutFlag()) {
57 SUPLA_LOG_DEBUG("HX711 timeout, check connection");
58 } else {
59 calFactor = (calValue > 0) ? calValue : defCalValue;
60 hx711Sensor.setCalFactor(calFactor);
61 SUPLA_LOG_DEBUG("HX711 startup is complete");
62 }
63 uint32_t wait = millis();
64 while (!hx711Sensor.update() && millis() - wait <= 100) {}
65 SUPLA_LOG_INFO("HX711 calibration value: %.2f", hx711Sensor.getCalFactor());
66 SUPLA_LOG_INFO(
67 "HX711 measured conversion time ms: %.2f",
68 hx711Sensor.getConversionTime());
69 SUPLA_LOG_DEBUG(
70 "HX711 measured sampling rate: %.2f HZ", hx711Sensor.getSPS());
71 SUPLA_LOG_INFO(
72 "HX711 measured settling time ms: %d", hx711Sensor.getSettlingTime());
73 channel.setNewValue(getValue());
74 }
75
76 void calibrateScales(float knownMass = 0) {
77 if (!readyToCalibration) {
78 SUPLA_LOG_DEBUG("HX711 please do tare");
79 } else {
80 hx711Sensor.update();
81 hx711Sensor.refreshDataSet();
82 if (knownMass > 0) {
83 calValue = hx711Sensor.getNewCalibration(knownMass);
84 SUPLA_LOG_DEBUG("HX711 new calibration value: %.2f", calValue);
86 } else {
87 SUPLA_LOG_DEBUG("HX711 known mass must be greater than zero");
88 }
89 }
90 readyToCalibration = false;
91 }
92
93 void tareScales() {
94 hx711Sensor.tare();
95 tareOffset = hx711Sensor.getTareOffset();
96 SUPLA_LOG_DEBUG("HX711 new tare offset: %d", tareOffset);
98 readyToCalibration = true;
99 }
100
102 // According to the library, the sampling frequency
103 // must be in the range 7 - 100 Hz. Now, it's around 10.5 Hz.
104 if (hx711Sensor.update()) {
105 value = hx711Sensor.getData();
106 }
107 if (millis() - lastReadTime > 500) {
108 lastReadTime = millis();
109 channel.setNewValue(getValue());
110 }
111 }
112
113 void onLoadState() {
115 if (Supla::Storage::ReadState((unsigned char *)&data, sizeof(data))) {
116 tareOffset = data.tareOffset;
117 calValue = data.calValue;
118 }
119 SUPLA_LOG_DEBUG(
120 "HX711 settings restored from storage. Tare offset: %d; "
121 "calibration value: %.2f", tareOffset, calValue);
122 }
123
124 void onSaveState() {
125 HX711ConfigData data;
126 data.tareOffset = tareOffset;
127 data.calValue = calValue;
128
129 Supla::Storage::WriteState((unsigned char *)&data, sizeof(data));
130 }
131
132 float getTareOffset() {
133 return tareOffset;
134 }
135
136 float getCalFactor() {
137 return calFactor;
138 }
139
140 protected:
141 ::HX711_ADC hx711Sensor;
142 double value = 0;
143 uint32_t tareOffset = 0;
144 float calFactor = 0;
145 float defCalValue;
146 float calValue = 0;
147 bool readyToCalibration = false;
148};
149
150}; // namespace Sensor
151}; // namespace Supla
152
153#endif // SRC_SUPLA_SENSOR_HX711_H_
Definition HX711.h:29
void onSaveState()
Method called periodically during SuplaDevice iteration.
Definition HX711.h:124
void onInit()
Third method called on element in SuplaDevice.begin()
Definition HX711.h:50
void iterateAlways()
Method called on each SuplaDevice iteration.
Definition HX711.h:101
void onLoadState()
Second method called on element in SuplaDevice.begin().
Definition HX711.h:113
Definition weight.h:17
static void ScheduleSave(uint32_t delayMsMax, uint32_t delayMsMin=0)
Schedules save of state storage in given time range.
Definition storage.cpp:93
Definition HX711.h:23