How to measure water in a reservoir
Dependencies: Cayenne-MQTT-mbed Cayenne-LPP
inc/dust_sensor.h@0:7bfeb237e600, 2019-03-08 (annotated)
- Committer:
- wamae
- Date:
- Fri Mar 08 11:34:56 2019 +0000
- Revision:
- 0:7bfeb237e600
working code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wamae | 0:7bfeb237e600 | 1 | #ifndef _DUST_SENSOR_H |
wamae | 0:7bfeb237e600 | 2 | #define _DUST_SENSOR_H |
wamae | 0:7bfeb237e600 | 3 | |
wamae | 0:7bfeb237e600 | 4 | #include "mbed.h" |
wamae | 0:7bfeb237e600 | 5 | |
wamae | 0:7bfeb237e600 | 6 | class DustSensor : public NonCopyable<DustSensor> { |
wamae | 0:7bfeb237e600 | 7 | public: |
wamae | 0:7bfeb237e600 | 8 | DustSensor(PinName pin) : _input(pin), _cb(NULL), _busy(false) { |
wamae | 0:7bfeb237e600 | 9 | |
wamae | 0:7bfeb237e600 | 10 | } |
wamae | 0:7bfeb237e600 | 11 | |
wamae | 0:7bfeb237e600 | 12 | bool measure(Callback<void(int, float, float)> cb) { |
wamae | 0:7bfeb237e600 | 13 | if (_busy) { |
wamae | 0:7bfeb237e600 | 14 | return false; |
wamae | 0:7bfeb237e600 | 15 | } |
wamae | 0:7bfeb237e600 | 16 | |
wamae | 0:7bfeb237e600 | 17 | _cb = cb; |
wamae | 0:7bfeb237e600 | 18 | |
wamae | 0:7bfeb237e600 | 19 | _busy = true; |
wamae | 0:7bfeb237e600 | 20 | |
wamae | 0:7bfeb237e600 | 21 | m.attach(callback(this, &DustSensor::calculate), 30.0f); |
wamae | 0:7bfeb237e600 | 22 | t.reset(); // reset mesurement every 30 seconds |
wamae | 0:7bfeb237e600 | 23 | _input.fall(callback(this, &DustSensor::start)); // start measuring when signal is low |
wamae | 0:7bfeb237e600 | 24 | _input.rise(callback(this, &DustSensor::stop)); // stop measuring when signal is high |
wamae | 0:7bfeb237e600 | 25 | |
wamae | 0:7bfeb237e600 | 26 | return true; |
wamae | 0:7bfeb237e600 | 27 | } |
wamae | 0:7bfeb237e600 | 28 | |
wamae | 0:7bfeb237e600 | 29 | bool is_busy() { |
wamae | 0:7bfeb237e600 | 30 | return _busy; |
wamae | 0:7bfeb237e600 | 31 | } |
wamae | 0:7bfeb237e600 | 32 | |
wamae | 0:7bfeb237e600 | 33 | private: |
wamae | 0:7bfeb237e600 | 34 | // Start Timer |
wamae | 0:7bfeb237e600 | 35 | void start(void) { |
wamae | 0:7bfeb237e600 | 36 | t.start(); |
wamae | 0:7bfeb237e600 | 37 | } |
wamae | 0:7bfeb237e600 | 38 | |
wamae | 0:7bfeb237e600 | 39 | // Stop Timer |
wamae | 0:7bfeb237e600 | 40 | void stop(void) { |
wamae | 0:7bfeb237e600 | 41 | t.stop(); |
wamae | 0:7bfeb237e600 | 42 | } |
wamae | 0:7bfeb237e600 | 43 | |
wamae | 0:7bfeb237e600 | 44 | // calculate sensor value |
wamae | 0:7bfeb237e600 | 45 | void calculate() { |
wamae | 0:7bfeb237e600 | 46 | // run measurements |
wamae | 0:7bfeb237e600 | 47 | int lpo = t.read_us(); |
wamae | 0:7bfeb237e600 | 48 | float ratio = t.read_us() / (30000 * 10.0); |
wamae | 0:7bfeb237e600 | 49 | float concentration = 1.1 * pow(ratio,3) -3.8 * pow(ratio, 2) + 520 * ratio + 0.62; |
wamae | 0:7bfeb237e600 | 50 | _cb(lpo, ratio, concentration); |
wamae | 0:7bfeb237e600 | 51 | |
wamae | 0:7bfeb237e600 | 52 | _busy = false; |
wamae | 0:7bfeb237e600 | 53 | } |
wamae | 0:7bfeb237e600 | 54 | |
wamae | 0:7bfeb237e600 | 55 | InterruptIn _input; |
wamae | 0:7bfeb237e600 | 56 | Callback<void(int, float, float)> _cb; |
wamae | 0:7bfeb237e600 | 57 | |
wamae | 0:7bfeb237e600 | 58 | bool _busy; |
wamae | 0:7bfeb237e600 | 59 | |
wamae | 0:7bfeb237e600 | 60 | Timer t; // measure amount of time signal is low |
wamae | 0:7bfeb237e600 | 61 | Timeout m; // every 30 seconds calculate measurement |
wamae | 0:7bfeb237e600 | 62 | }; |
wamae | 0:7bfeb237e600 | 63 | |
wamae | 0:7bfeb237e600 | 64 | #endif // _DUST_SENSOR_H |