EIoT LoRa node 2 - Working ok

Dependencies:   BufferedSerial SX1276GenericLib-node1 mbed

Fork of DISCO-L072CZ-LRWAN1_LoRa_node by Santiago Gil

Committer:
sagilar
Date:
Mon Oct 08 16:11:05 2018 +0000
Revision:
13:c2c91cc0467d
EIoT LoRa node 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sagilar 13:c2c91cc0467d 1 #include <mbed.h>
sagilar 13:c2c91cc0467d 2 /** A Hall-Effect sensor for measuring current levels in a given path
sagilar 13:c2c91cc0467d 3 *
sagilar 13:c2c91cc0467d 4 * Can be used as a current detector
sagilar 13:c2c91cc0467d 5 *
sagilar 13:c2c91cc0467d 6 * Example:
sagilar 13:c2c91cc0467d 7 * @code
sagilar 13:c2c91cc0467d 8 * // Periodically read current levels in a circuit and
sagilar 13:c2c91cc0467d 9 * // send output to PC terminal
sagilar 13:c2c91cc0467d 10 *
sagilar 13:c2c91cc0467d 11 * #include "mbed.h"
sagilar 13:c2c91cc0467d 12 * #include "ACS712.h"
sagilar 13:c2c91cc0467d 13 *
sagilar 13:c2c91cc0467d 14 * // Connect the sensor analog output pin to mbed's AnalogIn pin
sagilar 13:c2c91cc0467d 15 * ACS712 dev(p18);
sagilar 13:c2c91cc0467d 16 * // Connect mbed to pc's USB port
sagilar 13:c2c91cc0467d 17 * Serial pc(USBTX, USBRX);
sagilar 13:c2c91cc0467d 18 *
sagilar 13:c2c91cc0467d 19 * int main() {
sagilar 13:c2c91cc0467d 20 * pc.printf("Sensor Log: \n\n\r");
sagilar 13:c2c91cc0467d 21 * while (1) {
sagilar 13:c2c91cc0467d 22 * // Read current from sensor and output to pc terminal
sagilar 13:c2c91cc0467d 23 * pc.printf("Sensor Value: %2.2f A\n\r", dev);
sagilar 13:c2c91cc0467d 24 * wait(0.200);
sagilar 13:c2c91cc0467d 25 * }
sagilar 13:c2c91cc0467d 26 * }
sagilar 13:c2c91cc0467d 27 * @endcode
sagilar 13:c2c91cc0467d 28 */
sagilar 13:c2c91cc0467d 29
sagilar 13:c2c91cc0467d 30 class ACS712 {
sagilar 13:c2c91cc0467d 31
sagilar 13:c2c91cc0467d 32 public:
sagilar 13:c2c91cc0467d 33 /** Create a hall-effect sensor of the specified type
sagilar 13:c2c91cc0467d 34 *
sagilar 13:c2c91cc0467d 35 * @param _pin mbed AnalogIn pin where the analog output of sensor is connected
sagilar 13:c2c91cc0467d 36 * @param voltDivRatio resistor voltage division ratio at output of the sensor
sagilar 13:c2c91cc0467d 37 * @param type type of ACS712 sensor used
sagilar 13:c2c91cc0467d 38 *
sagilar 13:c2c91cc0467d 39 * @note Supported types of sensors:
sagilar 13:c2c91cc0467d 40 */
sagilar 13:c2c91cc0467d 41 ACS712(PinName _pin, float voltDivRatio = 1, short type = 5);
sagilar 13:c2c91cc0467d 42
sagilar 13:c2c91cc0467d 43 /** Read the value of the measured current in amps
sagilar 13:c2c91cc0467d 44 *
sagilar 13:c2c91cc0467d 45 * @return current value in amps
sagilar 13:c2c91cc0467d 46 */
sagilar 13:c2c91cc0467d 47 float read();
sagilar 13:c2c91cc0467d 48 ACS712& operator=(const ACS712&);
sagilar 13:c2c91cc0467d 49
sagilar 13:c2c91cc0467d 50 /** Read the value of the measured current in amps
sagilar 13:c2c91cc0467d 51 * Allows the ACS712 object to be used in a float context
sagilar 13:c2c91cc0467d 52 *
sagilar 13:c2c91cc0467d 53 * @return current value in amps
sagilar 13:c2c91cc0467d 54 */
sagilar 13:c2c91cc0467d 55 operator float() { return read(); }
sagilar 13:c2c91cc0467d 56
sagilar 13:c2c91cc0467d 57 private:
sagilar 13:c2c91cc0467d 58 AnalogIn sensor;
sagilar 13:c2c91cc0467d 59 float translate(float);
sagilar 13:c2c91cc0467d 60 float ratio;
sagilar 13:c2c91cc0467d 61 short type;
sagilar 13:c2c91cc0467d 62
sagilar 13:c2c91cc0467d 63 };
sagilar 13:c2c91cc0467d 64
sagilar 13:c2c91cc0467d 65 ACS712::ACS712(PinName _pin, float voltDivRatio, short sensorType) : sensor(_pin){
sagilar 13:c2c91cc0467d 66 ratio = voltDivRatio;
sagilar 13:c2c91cc0467d 67 type = sensorType;
sagilar 13:c2c91cc0467d 68 }
sagilar 13:c2c91cc0467d 69
sagilar 13:c2c91cc0467d 70 float ACS712::translate(float val){
sagilar 13:c2c91cc0467d 71 switch(type){
sagilar 13:c2c91cc0467d 72 case 5:
sagilar 13:c2c91cc0467d 73 return (val*ratio - 2.46*ratio)/(.185*ratio);
sagilar 13:c2c91cc0467d 74 case 20:
sagilar 13:c2c91cc0467d 75 return (val*ratio - 2.46*ratio)/(.1*ratio);
sagilar 13:c2c91cc0467d 76 case 30:
sagilar 13:c2c91cc0467d 77 return (val*ratio - 2.46*ratio)/(.066*ratio);
sagilar 13:c2c91cc0467d 78 default:
sagilar 13:c2c91cc0467d 79 return 999;
sagilar 13:c2c91cc0467d 80 }
sagilar 13:c2c91cc0467d 81 }
sagilar 13:c2c91cc0467d 82
sagilar 13:c2c91cc0467d 83
sagilar 13:c2c91cc0467d 84 float ACS712::read(){
sagilar 13:c2c91cc0467d 85 return ACS712::translate((sensor) * 3.3);
sagilar 13:c2c91cc0467d 86 }
sagilar 13:c2c91cc0467d 87
sagilar 13:c2c91cc0467d 88 ACS712& ACS712::operator=(const ACS712& rhs){
sagilar 13:c2c91cc0467d 89 sensor = rhs.sensor;
sagilar 13:c2c91cc0467d 90 ratio = rhs.ratio;
sagilar 13:c2c91cc0467d 91 type = rhs.type;
sagilar 13:c2c91cc0467d 92 return *this;
sagilar 13:c2c91cc0467d 93 }