sensor lib
Dependents: gather_sensor_data
Revision 0:cbe8cd32b8d9, committed 2016-09-08
- Comitter:
- readysteadygo2006
- Date:
- Thu Sep 08 14:05:15 2016 +0000
- Commit message:
- test;
Changed in this revision
diff -r 000000000000 -r cbe8cd32b8d9 BH1750Sensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BH1750Sensor.cpp Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,45 @@ +#include "BH1750Sensor.h" + +BH1750Sensor::BH1750Sensor(PinName scaPin, PinName sclPin): BH1750_sensor(scaPin, sclPin) { + reading_type = BH1750_ALL; + BH1750_sensor.init(); +} + + +sensorStatus BH1750Sensor::readSensor(string &sensor_reading) { + + //add start to reading + sensor_reading = "{"; + + //Read Visible Light Intensity + if ((reading_type & BH1750_VL) == BH1750_VL) { + float vl = BH1750_sensor.readIntensity(); + //check required here + + char reading_type_string[10]; + sprintf(reading_type_string, "%05X", BH1750_VL); + char vl_string[10]; + sprintf(vl_string, "%2.2f", vl); + sensor_reading = sensor_reading + reading_type_string + ":" + vl_string; + } + + //add end to reading + sensor_reading = sensor_reading + "}"; + + return SENSOR_SUCCESS; +} + + +sensorStatus BH1750Sensor::setReadingType(sensorReadingType sensor_reading_types) { + if ((sensor_reading_types > BH1750_FIRST) && (sensor_reading_types <= BH1750_ALL)) { + reading_type = sensor_reading_types; + return SENSOR_SUCCESS; + } else { + return SENSOR_PARAM_OUT_RANGE; + } +} + +sensorReadingType BH1750Sensor::getReadingType() { + return reading_type; +} +
diff -r 000000000000 -r cbe8cd32b8d9 BH1750Sensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BH1750Sensor.h Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,24 @@ +#ifndef MBED_BH1750_SENSOR_H +#define MBED_BH1750_SENSOR_H + +#include "mbed.h" +#include <string> +#include "BH1750.h" +#include "sensorTypes.h" + + +class BH1750Sensor { + +public: + BH1750Sensor(PinName scaPin, PinName sclPin); + + sensorStatus readSensor(string &sensor_reading); + sensorStatus setReadingType(sensorReadingType sensor_reading_types = BH1750_ALL); + sensorReadingType getReadingType(); + +private: + BH1750 BH1750_sensor; + sensorReadingType reading_type; +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r cbe8cd32b8d9 DHTSensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHTSensor.cpp Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,60 @@ +#include "DHTSensor.h" + +DHTSensor::DHTSensor(PinName pn, int dht_type): dht_sensor(pn, dht_type) { + reading_type = DHT_ALL; +} + + +sensorStatus DHTSensor::readSensor(string &sensor_reading) { + + //add start to reading + int err = dht_sensor.readData(); + //Read Temp + if (err == 0) { + sensor_reading = "{"; + if ((reading_type & DHT_TEMP) == DHT_TEMP) { + float temp = dht_sensor.ReadTemperature(CELCIUS); + //check required here + + char reading_type_string[10]; + sprintf(reading_type_string, "%05X", DHT_TEMP); + char temp_string[10]; + sprintf(temp_string, "%2.2f", temp); + sensor_reading = sensor_reading + reading_type_string + ":" + temp_string; + } + + if ((reading_type & DHT_HUMID) == DHT_HUMID) { + float humid = dht_sensor.ReadHumidity(); + //check required here + char reading_type_string[10]; + sprintf(reading_type_string, "%05X", DHT_HUMID); + char humid_string[10]; + sprintf(humid_string, "%2.2f", humid); + sensor_reading = sensor_reading + ", " + reading_type_string + ":" + humid_string; + } + + //add end to reading + sensor_reading = sensor_reading + "}"; + return SENSOR_SUCCESS; + + } else { + sensor_reading = sensor_reading + ""; + return SENSOR_READING_ERROR; + } + +} + + +sensorStatus DHTSensor::setReadingType(sensorReadingType sensor_reading_types) { + if ((sensor_reading_types > DHT_FIRST) && (sensor_reading_types <= DHT_ALL)) { + reading_type = sensor_reading_types; + return SENSOR_SUCCESS; + } else { + return SENSOR_PARAM_OUT_RANGE; + } +} + +sensorReadingType DHTSensor::getReadingType() { + return reading_type; +} +
diff -r 000000000000 -r cbe8cd32b8d9 DHTSensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHTSensor.h Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,24 @@ +#ifndef MBED_DHT_SENSOR_H +#define MBED_DHT_SENSOR_H + +#include "mbed.h" +#include <string> +#include "DHT.h" +#include "sensorTypes.h" + + +class DHTSensor { + +public: + DHTSensor(PinName pn, int dht_type); + + sensorStatus readSensor(string &sensor_reading); + sensorStatus setReadingType(sensorReadingType sensor_reading_types = DHT_ALL); + sensorReadingType getReadingType(); + +private: + DHT dht_sensor; + sensorReadingType reading_type; +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r cbe8cd32b8d9 DS1820Sensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1820Sensor.cpp Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,44 @@ +#include "DS1820Sensor.h" + +DS1820Sensor::DS1820Sensor(PinName one_wire_pinname): DS1820_sensor(one_wire_pinname) { + reading_type = DS1820_ALL; +} + + +sensorStatus DS1820Sensor::readSensor(string &sensor_reading) { + + //add start to reading + sensor_reading = "{"; + + //Read Temp + if ((reading_type & DS1820_TEMP) == DS1820_TEMP) { + float temp = DS1820_sensor.read(); + //check required here + + char reading_type_string[10]; + sprintf(reading_type_string, "%05X", DS1820_TEMP); + char temp_string[10]; + sprintf(temp_string, "%2.2f", temp); + sensor_reading = sensor_reading + reading_type_string + ":" + temp_string; + } + + //add end to reading + sensor_reading = sensor_reading + "}"; + + return SENSOR_SUCCESS; +} + + +sensorStatus DS1820Sensor::setReadingType(sensorReadingType sensor_reading_types) { + if ((sensor_reading_types > DS1820_FIRST) && (sensor_reading_types <= DS1820_ALL)) { + reading_type = sensor_reading_types; + return SENSOR_SUCCESS; + } else { + return SENSOR_PARAM_OUT_RANGE; + } +} + +sensorReadingType DS1820Sensor::getReadingType() { + return reading_type; +} +
diff -r 000000000000 -r cbe8cd32b8d9 DS1820Sensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1820Sensor.h Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,24 @@ +#ifndef MBED_DS1820_SENSOR_H +#define MBED_DS1820_SENSOR_H + +#include "mbed.h" +#include <string> +#include "DS1820.h" +#include "sensorTypes.h" + + +class DS1820Sensor { + +public: + DS1820Sensor(PinName one_wire_pinname); + + sensorStatus readSensor(string &sensor_reading); + sensorStatus setReadingType(sensorReadingType sensor_reading_types = DS1820_ALL); + sensorReadingType getReadingType(); + +private: + DS1820 DS1820_sensor; + sensorReadingType reading_type; +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r cbe8cd32b8d9 ML8511Sensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ML8511Sensor.cpp Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,48 @@ +#include "ML8511Sensor.h" + +ML8511Sensor::ML8511Sensor(PinName analog_in_pin_name): ML8511_sensor(analog_in_pin_name) { + reading_type = ML8511_ALL; + analog_in_pname = analog_in_pin_name; +} + + +sensorStatus ML8511Sensor::readSensor(string &sensor_reading) { + + //add start to reading + sensor_reading = "{"; + + //Read + if ((reading_type & ML8511_UV) == ML8511_UV) { + // UV index + // 1V=0 + // 2V=10 + float uv = (ML8511_sensor.read()*3.3-1.0)/(2.0-1.0)*10.0; + //check required here + + char reading_type_string[10]; + sprintf(reading_type_string, "%05X", ML8511_UV); + char uv_string[10]; + sprintf(uv_string, "%2.2f", uv); + sensor_reading = sensor_reading + reading_type_string + ":" + uv_string; + } + + //add end to reading + sensor_reading = sensor_reading + "}"; + + return SENSOR_SUCCESS; +} + + +sensorStatus ML8511Sensor::setReadingType(sensorReadingType sensor_reading_types) { + if ((sensor_reading_types > ML8511_FIRST) && (sensor_reading_types <= ML8511_ALL)) { + reading_type = sensor_reading_types; + return SENSOR_SUCCESS; + } else { + return SENSOR_PARAM_OUT_RANGE; + } +} + +sensorReadingType ML8511Sensor::getReadingType() { + return reading_type; +} +
diff -r 000000000000 -r cbe8cd32b8d9 ML8511Sensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ML8511Sensor.h Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,24 @@ +#ifndef MBED_ML8511_SENSOR_H +#define MBED_ML8511_SENSOR_H + +#include "mbed.h" +#include <string> +#include "sensorTypes.h" + + +class ML8511Sensor { + +public: + ML8511Sensor(PinName one_wire_pinname); + + sensorStatus readSensor(string &sensor_reading); + sensorStatus setReadingType(sensorReadingType sensor_reading_types = ML8511_ALL); + sensorReadingType getReadingType(); + +private: + AnalogIn ML8511_sensor; + sensorReadingType reading_type; + PinName analog_in_pname; +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r cbe8cd32b8d9 MQ135Sensor.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQ135Sensor.cpp Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,55 @@ +#include "MQ135Sensor.h" + +MQ135Sensor::MQ135Sensor(PinName analog_out_pin_name): MQ135_sensor(analog_out_pin_name) { + analog_out_pname = analog_out_pin_name; + reading_type = MQ135_ALL; +} + + +sensorStatus MQ135Sensor::readSensor(string &sensor_reading) { + + //add start to reading + sensor_reading = "{"; + + //Read Temp + if ((reading_type & MQ135_CO2) == MQ135_CO2) { + float co2 = MQ135_sensor.getResistance(); + //check required here + + char reading_type_string[10]; + sprintf(reading_type_string, "%05X", MQ135_CO2); + char co2_string[10]; + sprintf(co2_string, "%2.2f", co2); + sensor_reading = sensor_reading + reading_type_string + ":" + co2_string; + } + + if ((reading_type & MQ135_NH3) == MQ135_NH3) { + float nh3 = MQ135_sensor.getResistance(); + //check required here + char reading_type_string[10]; + sprintf(reading_type_string, "%05X", MQ135_NH3); + char nh3_string[10]; + sprintf(nh3_string, "%2.2f", nh3); + sensor_reading = sensor_reading + ", " + reading_type_string + ":" + nh3_string; + } + + //add end to reading + sensor_reading = sensor_reading + "}"; + + return SENSOR_SUCCESS; +} + + +sensorStatus MQ135Sensor::setReadingType(sensorReadingType sensor_reading_types) { + if ((sensor_reading_types > MQ135_FIRST) && (sensor_reading_types <= MQ135_ALL)) { + reading_type = sensor_reading_types; + return SENSOR_SUCCESS; + } else { + return SENSOR_PARAM_OUT_RANGE; + } +} + +sensorReadingType MQ135Sensor::getReadingType() { + return reading_type; +} +
diff -r 000000000000 -r cbe8cd32b8d9 MQ135Sensor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQ135Sensor.h Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,25 @@ +#ifndef MBED_MQ135_SENSOR_H +#define MBED_MQ135_SENSOR_H + +#include "mbed.h" +#include <string> +#include "MQ135.h" +#include "sensorTypes.h" + + +class MQ135Sensor { + +public: + MQ135Sensor(PinName analog_out_pin_name); + + sensorStatus readSensor(string &sensor_reading); + sensorStatus setReadingType(sensorReadingType sensor_reading_types = MQ135_ALL); + sensorReadingType getReadingType(); + +private: + PinName analog_out_pname; + MQ135 MQ135_sensor; + sensorReadingType reading_type; +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r cbe8cd32b8d9 SENSOR.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SENSOR.cpp Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,133 @@ +#include "SENSOR.h" + + +SENSOR::SENSOR ( + PinName io_pin_name1, + PinName io_pin_name2, + PinName io_pin_name3, + PinName io_pin_name4, + PinName io_pin_name5, + PinName sense_pin_name1 +): sense_pin1(sense_pin_name1) { + io_type = NONE; + io_pname1 = io_pin_name1; + io_pname2 = io_pin_name2; + io_pname3 = io_pin_name3; + io_pname4 = io_pin_name4; + io_pname5 = io_pin_name5; + sense_pname1 = sense_pname1; +} + + +SENSOR::~SENSOR() { + io_type = NONE; + io_pname1 = NC; + io_pname2 = NC; + io_pname3 = NC; + io_pname4 = NC; + io_pname5 = NC; + sense_pname1 = NC; +} + + +sensorType SENSOR::detectSensorType() { + return NO_SENSOR; +} + +sensorType SENSOR::getSensorType() { + return sensor_type; +} + +sensorStatus SENSOR::getSensorStatus() { + return sensor_status; +} + +sensorType SENSOR::detectSensor() { + sensor_type = detectSensorType(); + sensor_type = detectSensor(sensor_type); + return sensor_type; +} + +sensorType SENSOR::detectSensor(sensorType stype) { + + //Set sensor_type + sensor_type = stype; + + //handle detected sensor + switch (stype) { + case DS1820_SENSOR: + ds1820_sensor_ptr = new DS1820Sensor(io_pname1); + break; + case DHT11_SENSOR: + dht11_sensor_ptr = new DHTSensor(io_pname1, DHT11); + break; + case DHT22_SENSOR: + dht22_sensor_ptr = new DHTSensor(io_pname1, DHT22); + break; + case ML8511_SENSOR: + ml8511_sensor_ptr = new ML8511Sensor(io_pname5); + break; + case MQ135_SENSOR: + mq135_sensor_ptr = new MQ135Sensor(io_pname5); + break; + case BH1750_SENSOR: +// bh1750_sensor_ptr = new BH1750Sensor(io_pname1, io_pname2); + bh1750_sensor_ptr = new BH1750Sensor(io_pname1, io_pname2); + break; + case UNSUPPORTED_SENSOR: + sensor_status = SENSOR_DETECTION_ERROR; + return(sensor_type); + break; + case NO_SENSOR: + sensor_status = SENSOR_UNCONNECTED; + return(sensor_type); + break; + default: +// printf("Error, should not get here!\r\n"); + break; + } + + sensor_status = SENSOR_CONNECTED; + return sensor_type; +} + +sensorStatus SENSOR::readSensor(string &sensor_reading) { + sensorStatus status; + switch (sensor_type) { + case DS1820_SENSOR: + status = ds1820_sensor_ptr->readSensor(sensor_reading); + break; + case DHT11_SENSOR: + status = dht11_sensor_ptr->readSensor(sensor_reading); + break; + case DHT22_SENSOR: + status = dht22_sensor_ptr->readSensor(sensor_reading); + break; + case ML8511_SENSOR: + status = ml8511_sensor_ptr->readSensor(sensor_reading); + break; + case MQ135_SENSOR: + status = mq135_sensor_ptr->readSensor(sensor_reading); + break; + case BH1750_SENSOR: + status = bh1750_sensor_ptr->readSensor(sensor_reading); + break; + case UNSUPPORTED_SENSOR: + sensor_status = SENSOR_TYPE_UNSUPPORTED; + sensor_reading = ""; + return(sensor_status); + break; + case NO_SENSOR: + sensor_status = SENSOR_UNCONNECTED; + sensor_reading = ""; + return(sensor_status); + break; + default: +// printf("Error, should not get here!\r\n"); + break; + } + + return status; +} + +
diff -r 000000000000 -r cbe8cd32b8d9 SENSOR.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SENSOR.h Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,56 @@ +#ifndef MBED_SENSOR_H +#define MBED_SENSOR_H + +#include "mbed.h" +#include <string> +#include "SupportedDevices.h" +#include "sensorTypes.h" +#include "DHTSensor.h" +#include "BH1750Sensor.h" +#include "DS1820Sensor.h" +#include "MQ135Sensor.h" +#include "ML8511Sensor.h" + + + +class SENSOR { + +public: + SENSOR ( + PinName io_pin_name1, + PinName io_pin_name2, + PinName io_pin_name3, + PinName io_pin_name4, + PinName io_pin_name5, + PinName sense_pin_name1 + ); + ~SENSOR(); + + sensorType detectSensor(); + sensorType getSensorType(); + sensorStatus getSensorStatus(); + sensorType detectSensor(sensorType stype); + sensorStatus readSensor(string &sensor_reading); + + +private: + sensorType detectSensorType(); + PinName io_pname1; + PinName io_pname2; + PinName io_pname3; + PinName io_pname4; + PinName io_pname5; + PinName sense_pname1; + sensorIOType io_type; + sensorType sensor_type; + sensorStatus sensor_status; + DigitalInOut sense_pin1; + DS1820Sensor *ds1820_sensor_ptr; + DHTSensor *dht11_sensor_ptr; + DHTSensor *dht22_sensor_ptr; + MQ135Sensor *mq135_sensor_ptr; + ML8511Sensor *ml8511_sensor_ptr; + BH1750Sensor *bh1750_sensor_ptr; +}; + +#endif
diff -r 000000000000 -r cbe8cd32b8d9 SupportedDevices.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SupportedDevices.h Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,14 @@ +#ifndef MBED_SUPPORTED_SENSOR_H +#define MBED_SUPPORTED_SENSOR_H + +#include "mbed.h" +#include "DHT.h" +#include "BH1750.h" +#include "DS1820.h" +#include "MQ135.h" + + + + + +#endif \ No newline at end of file
diff -r 000000000000 -r cbe8cd32b8d9 sensorTypes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensorTypes.h Thu Sep 08 14:05:15 2016 +0000 @@ -0,0 +1,60 @@ +#ifndef MBED_sensorTypes_H +#define MBED_sensorTypes_H + + +enum sensorIOType{ + NONE = 0, + I2CIO = 11, + SERIALIO = 12, + ONEWIREIO = 13, + ANALOGIO = 14 +}; + +enum sensorStatus { + SENSOR_READING_ERROR = 0, + SENSOR_DETECTION_ERROR = 1, + SENSOR_SUCCESS = 2, + SENSOR_PARAM_OUT_RANGE = 3, + SENSOR_CONNECTED = 10, + SENSOR_UNCONNECTED = 11, + SENSOR_TYPE_UNSUPPORTED = 12 +}; + +enum sensorType { + NO_SENSOR = 01, + UNSUPPORTED_SENSOR = 02, + DS1820_SENSOR = 10, + DHT11_SENSOR = 11, + DHT22_SENSOR = 12, + ML8511_SENSOR = 20, + BH1750_SENSOR = 21, + MQ135_SENSOR = 30 +}; + + +enum sensorReadingType { + //DHT=01 Reading types + DHT_FIRST = 0x01003, + DHT_TEMP = 0x01001, + DHT_HUMID = 0x01002, + DHT_ALL = 0x01FFF, + //ML8511=02 Reading types + ML8511_FIRST = 0x02002, + ML8511_UV = 0x02001, + ML8511_ALL = 0x02FFF, + //BH1750=03 Reading types + BH1750_FIRST = 0x03002, + BH1750_VL = 0x03001, + BH1750_ALL = 0x03FFF, + //MQ135=04 Reading types + MQ135_FIRST = 0x04003, + MQ135_CO2 = 0x04001, + MQ135_NH3 = 0x04002, + MQ135_ALL = 0x04FFF, + //BH1750=03 Reading types + DS1820_FIRST = 0x05002, + DS1820_TEMP = 0x05001, + DS1820_ALL = 0x05FFF, +}; + +#endif \ No newline at end of file