Commit effa2568 authored by user2684's avatar user2684 Committed by GitHub
Browse files

Revert "Enhanced remote API #102 #20 #28 #53 #78"

This reverts commit 34e073fc.
parent 34e073fc
......@@ -195,53 +195,6 @@ int Timer::getUnit() {
return _unit;
}
/******************************************
Request
*/
Request::Request(const char* string) {
char str[10];
char* ptr;
strcpy(str,string);
// tokenize the string and split function from value
strtok_r(str,",",&ptr);
_function = atoi(str);
strcpy(_value,ptr);
#if DEBUG == 1
Serial.print(F("REQ F="));
Serial.print(getFunction());
Serial.print(F(" I="));
Serial.print(getValueInt());
Serial.print(F(" F="));
Serial.print(getValueFloat());
Serial.print(F(" S="));
Serial.println(getValueString());
#endif
}
// return the parsed function
int Request::getFunction() {
return _function;
}
// return the value as an int
int Request::getValueInt() {
return atoi(_value);
}
// return the value as a float
float Request::getValueFloat() {
return atof(_value);
}
// return the value as a string
char* Request::getValueString() {
return _value;
}
/******************************************
Sensors
*/
......@@ -255,7 +208,6 @@ Sensor::Sensor(NodeManager* node_manager, int child_id, int pin) {
_child_id = child_id;
_pin = pin;
_msg = MyMessage(_child_id, _type);
_msg_service = MyMessage(_child_id, V_CUSTOM);
_report_timer = new Timer(_node_manager);
_force_update_timer = new Timer(_node_manager);
}
......@@ -464,50 +416,11 @@ void Sensor::loop(const MyMessage & message) {
// receive a message from the radio network
void Sensor::receive(const MyMessage &message) {
// return if not for this sensor
if (message.sensor != _child_id) return;
// check if it is a request for the API
if (message.getCommand() == C_REQ && message.type == V_CUSTOM) {
#if REMOTE_CONFIGURATION == 1
// parse the request
Request request = Request(message.getString());
// if it is for a sensor-generic function, call process(), otherwise the sensor-specific onProcess();
if (request.getFunction() < 100) process(request);
else onProcess(request);
#endif
}
// return if the type is not correct
if (message.type != _type) return;
if (message.sensor != _child_id || message.type != _type) return;
// a request would make the sensor executing its main task passing along the message
loop(message);
}
// process a remote configuration request message
void Sensor::process(Request & request) {
int function = request.getFunction();
switch(function) {
case 1: setPin(request.getValueInt()); break;
case 2: setChildId(request.getValueInt()); break;
case 3: setType(request.getValueInt()); break;
case 4: setDescription(request.getValueString()); break;
case 5: setSamples(request.getValueInt()); break;
case 6: setSamplesInterval(request.getValueInt()); break;
case 7: setTrackLastValue(request.getValueInt()); break;
case 8: setForceUpdateCycles(request.getValueInt()); break;
case 9: setForceUpdateMinutes(request.getValueInt()); break;
case 10: setValueType(request.getValueInt()); break;
case 11: setFloatPrecision(request.getValueInt()); break;
#if POWER_MANAGER == 1
case 12: setAutoPowerPins(request.getValueInt()); break;
case 13: powerOn(); break;
case 14: powerOff(); break;
#endif
case 15: setReportIntervalCycles(request.getValueInt()); break;
case 16: setReportIntervalMinutes(request.getValueInt()); break;
default: return;
}
_send(_msg_service.set(function));
}
// send a message to the network
void Sensor::_send(MyMessage & message) {
// send the message, multiple times if requested
......@@ -555,7 +468,7 @@ bool Sensor::_isWorthSending(bool comparison) {
return false;
}
#if MODULE_ANALOG_INPUT == 1
/*
SensorAnalogInput
*/
......@@ -615,20 +528,6 @@ void SensorAnalogInput::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorAnalogInput::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 101: setReference(request.getValueInt()); break;
case 102: setReverse(request.getValueInt()); break;
case 103: setOutputPercentage(request.getValueInt()); break;
case 104: setRangeMin(request.getValueInt()); break;
case 105: setRangeMax(request.getValueInt()); break;
default: return;
}
_send(_msg_service.set(function));
}
// read the analog input
int SensorAnalogInput::_getAnalogRead() {
#ifndef MY_GATEWAY_ESP8266
......@@ -740,19 +639,6 @@ void SensorThermistor::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorThermistor::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 101: setNominalResistor((long)request.getValueInt()); break;
case 102: setNominalTemperature(request.getValueInt()); break;
case 103: setBCoefficient(request.getValueInt()); break;
case 104: setSeriesResistor((long)request.getValueString()); break;
case 105: setOffset(request.getValueFloat()); break;
default: return;
}
_send(_msg_service.set(function));
}
/*
SensorML8511
......@@ -802,10 +688,6 @@ void SensorML8511::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorML8511::onProcess(Request & request) {
}
// The Arduino Map function but for floats
float SensorML8511::_mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
......@@ -861,17 +743,6 @@ void SensorACS712::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorACS712::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 100: setmVPerAmp(request.getValueInt()); break;
case 102: setOffset(request.getValueInt()); break;
default: return;
}
_send(_msg_service.set(function));
}
/*
SensorRainGauge
*/
......@@ -954,17 +825,6 @@ void SensorRainGauge::onReceive(const MyMessage & message) {
}
}
// what to do when receiving a remote message
void SensorRainGauge::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 101: setReportInterval(request.getValueInt()); break;
case 102: setSingleTip(request.getValueFloat()); break;
default: return;
}
_send(_msg_service.set(function));
}
/*
SensorRain
*/
......@@ -995,9 +855,151 @@ SensorSoilMoisture::SensorSoilMoisture(NodeManager* node_manager, int child_id,
setRangeMin(100);
}
#endif
#if MODULE_DIGITAL_INPUT == 1
/*
* SensorMQ
*/
SensorMQ::SensorMQ(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager,child_id,pin) {
setPresentation(S_AIR_QUALITY);
setType(V_LEVEL);
}
//setter/getter
void SensorMQ::setRlValue(float value) {
_rl_value = value;
}
void SensorMQ::setRoValue(float value) {
_ro = value;
}
void SensorMQ::setCleanAirFactor(float value) {
_ro_clean_air_factor = value;
}
void SensorMQ::setCalibrationSampleTimes(int value) {
_calibration_sample_times = value;
}
void SensorMQ::setCalibrationSampleInterval(int value){
_calibration_sample_interval = value;
}
void SensorMQ::setReadSampleTimes(int value) {
_read_sample_times = value;
}
void SensorMQ::setReadSampleInterval(int value) {
_read_sample_interval = value;
}
void SensorMQ::setLPGCurve(float *value) {
_LPGCurve[0] = value[0];
_LPGCurve[2] = value[1];
_LPGCurve[2] = value[2];
}
void SensorMQ::setCOCurve(float *value) {
_COCurve[0] = value[0];
_COCurve[2] = value[1];
_COCurve[2] = value[2];
}
void SensorMQ::setSmokeCurve(float *value) {
_SmokeCurve[0] = value[0];
_SmokeCurve[2] = value[1];
_SmokeCurve[2] = value[2];
}
// what to do during before
void SensorMQ::onBefore() {
// prepare the pin for input
pinMode(_pin, INPUT);
}
// what to do during setup
void SensorMQ::onSetup() {
_ro = _MQCalibration();
}
// what to do during loop
void SensorMQ::onLoop() {
if (_pin == -1) return;
// calculate rs/ro
float mq = _MQRead()/_ro;
// calculate the ppm
float lpg = _MQGetGasPercentage(mq,_gas_lpg);
float co = _MQGetGasPercentage(mq,_gas_co);
float smoke = _MQGetGasPercentage(mq,_gas_smoke);
// assign to the value the requested gas
uint16_t value;
if (_target_gas == _gas_lpg) value = lpg;
if (_target_gas == _gas_co) value = co;
if (_target_gas == _gas_smoke) value = smoke;
#if DEBUG == 1
Serial.print(F("MQ I="));
Serial.print(_child_id);
Serial.print(F(" V="));
Serial.print(value);
Serial.print(F(" LPG="));
Serial.print(lpg);
Serial.print(F(" CO="));
Serial.print(co);
Serial.print(F(" SMOKE="));
Serial.println(smoke);
#endif
// store the value
_value_int = (int16_t)ceil(value);
}
// what to do as the main task when receiving a message
void SensorMQ::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// returns the calculated sensor resistance
float SensorMQ::_MQResistanceCalculation(int raw_adc) {
return ( ((float)_rl_value*(1023-raw_adc)/raw_adc));
}
// This function assumes that the sensor is in clean air
float SensorMQ::_MQCalibration() {
int i;
float val=0;
//take multiple samples
for (i=0; i< _calibration_sample_times; i++) {
val += _MQResistanceCalculation(analogRead(_pin));
wait(_calibration_sample_interval);
}
//calculate the average value
val = val/_calibration_sample_times;
//divided by RO_CLEAN_AIR_FACTOR yields the Ro
val = val/_ro_clean_air_factor;
//according to the chart in the datasheet
return val;
}
// This function use MQResistanceCalculation to caculate the sensor resistenc (Rs).
float SensorMQ::_MQRead() {
int i;
float rs=0;
for (i=0; i<_read_sample_times; i++) {
rs += _MQResistanceCalculation(analogRead(_pin));
wait(_read_sample_interval);
}
rs = rs/_read_sample_times;
return rs;
}
// This function passes different curves to the MQGetPercentage function which calculates the ppm (parts per million) of the target gas.
int SensorMQ::_MQGetGasPercentage(float rs_ro_ratio, int gas_id) {
if ( gas_id == _gas_lpg ) {
return _MQGetPercentage(rs_ro_ratio,_LPGCurve);
} else if ( gas_id == _gas_co) {
return _MQGetPercentage(rs_ro_ratio,_COCurve);
} else if ( gas_id == _gas_smoke) {
return _MQGetPercentage(rs_ro_ratio,_SmokeCurve);
}
return 0;
}
// returns ppm of the target gas
int SensorMQ::_MQGetPercentage(float rs_ro_ratio, float *pcurve) {
return (pow(10,( ((log10(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
}
/*
SensorDigitalInput
*/
......@@ -1037,16 +1039,12 @@ void SensorDigitalInput::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorDigitalInput::onProcess(Request & request) {
}
#endif
#if MODULE_DIGITAL_OUTPUT == 1
/*
SensorDigitalOutput
*/
// contructor
SensorDigitalOutput::SensorDigitalOutput(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager,child_id, pin) {
_safeguard_timer = new Timer(node_manager);
}
......@@ -1116,21 +1114,6 @@ void SensorDigitalOutput::onReceive(const MyMessage & message) {
}
}
// what to do when receiving a remote message
void SensorDigitalOutput::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 101: setInitialValue(request.getValueInt()); break;
case 102: setPulseWidth(request.getValueInt()); break;
case 103: setOnValue(request.getValueInt()); break;
case 104: setLegacyMode(request.getValueInt()); break;
case 105: setSafeguard(request.getValueInt()); break;
case 106: setInputIsElapsed(request.getValueInt()); break;
default: return;
}
_send(_msg_service.set(function));
}
// write the value to the output
void SensorDigitalOutput::set(int value) {
if (_input_is_elapsed) {
......@@ -1186,7 +1169,14 @@ SensorRelay::SensorRelay(NodeManager* node_manager, int child_id, int pin): Sens
setPresentation(S_BINARY);
setType(V_STATUS);
}
/*
// define what to do during loop
void SensorRelay::onLoop() {
// set the value to -1 so to avoid reporting to the gateway during loop
_value_int = -1;
_last_value_int = -1;
}
*/
/*
SensorLatchingRelay
*/
......@@ -1197,7 +1187,6 @@ SensorLatchingRelay::SensorLatchingRelay(NodeManager* node_manager, int child_id
setPulseWidth(50);
}
#endif
/*
SensorDHT
*/
......@@ -1269,10 +1258,6 @@ void SensorDHT::onLoop() {
void SensorDHT::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorDHT::onProcess(Request & request) {
}
#endif
/*
......@@ -1344,10 +1329,6 @@ void SensorSHT21::onLoop() {
void SensorSHT21::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorSHT21::onProcess(Request & request) {
}
#endif
/*
......@@ -1359,7 +1340,6 @@ SensorHTU21D::SensorHTU21D(NodeManager* node_manager, int child_id, int pin): Se
}
#endif
#if MODULE_SWITCH == 1
/*
* SensorSwitch
*/
......@@ -1427,19 +1407,6 @@ void SensorSwitch::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorSwitch::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 101: setMode(request.getValueInt()); break;
case 102: setDebounce(request.getValueInt()); break;
case 103: setTriggerTime(request.getValueInt()); break;
case 104: setInitial(request.getValueInt()); break;
default: return;
}
_send(_msg_service.set(function));
}
/*
* SensorDoor
*/
......@@ -1457,7 +1424,6 @@ SensorMotion::SensorMotion(NodeManager* node_manager, int child_id, int pin): Se
// set initial value to LOW
setInitial(LOW);
}
#endif
/*
SensorDs18b20
......@@ -1512,17 +1478,6 @@ void SensorDs18b20::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorDs18b20::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 101: setResolution(request.getValueInt()); break;
case 102: setSleepDuringConversion(request.getValueInt()); break;
default: return;
}
_send(_msg_service.set(function));
}
// function to print a device address
DeviceAddress* SensorDs18b20::getDeviceAddress() {
return &_device_address;
......@@ -1581,10 +1536,6 @@ void SensorBH1750::onLoop() {
void SensorBH1750::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorBH1750::onProcess(Request & request) {
}
#endif
/*
......@@ -1629,10 +1580,6 @@ void SensorMLX90614::onLoop() {
void SensorMLX90614::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorMLX90614::onProcess(Request & request) {
}
#endif
......@@ -1693,17 +1640,7 @@ void SensorBosch::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorBosch::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 101: setForecastSamplesCount(request.getValueInt()); break;
default: return;
}
_send(_msg_service.set(function));
}
// calculate and send the forecast back
void SensorBosch::_forecast(float pressure) {
if (isnan(pressure)) return;
// Calculate the average of the last n minutes.
......@@ -1923,93 +1860,30 @@ void SensorBMP085::onLoop() {
}
#endif
/*
SensorHCSR04
SensorSonoff
*/
#if MODULE_HCSR04 == 1
#if MODULE_SONOFF == 1
// contructor
SensorHCSR04::SensorHCSR04(NodeManager* node_manager, int child_id, int pin): Sensor(node_manager, child_id, pin) {
// set presentation and type
setPresentation(S_DISTANCE);
setType(V_DISTANCE);
_trigger_pin = pin;
_echo_pin = pin;
}
// what to do during before
void SensorHCSR04::onBefore() {
// initialize the library
_sonar = new NewPing(_trigger_pin,_echo_pin,_max_distance);
SensorSonoff::SensorSonoff(NodeManager* node_manager, int child_id): Sensor(node_manager, child_id,1) {
setPresentation(S_BINARY);
setType(V_STATUS);
}
// setter/getter
void SensorHCSR04::setTriggerPin(int value) {
_trigger_pin = value;
void SensorSonoff::setButtonPin(int value) {
_button_pin = value;
}
void SensorHCSR04::setEchoPin(int value) {
_echo_pin = value;
void SensorSonoff::setRelayPin(int value) {
_relay_pin = value;
}
void SensorHCSR04::setMaxDistance(int value) {
_max_distance = value;
void SensorSonoff::setLedPin(int value) {
_led_pin = value;
}
// what to do during setup
void SensorHCSR04::onSetup() {
}
// what to do during loop
void SensorHCSR04::onLoop() {
int distance = _node_manager->getIsMetric() ? _sonar->ping_cm() : _sonar->ping_in();
#if DEBUG == 1
Serial.print(F("HC I="));
Serial.print(_child_id);
Serial.print(F(" D="));
Serial.println(distance);
#endif
_value_int = distance;
}
// what to do as the main task when receiving a message
void SensorHCSR04::onReceive(const MyMessage & message) {
if (message.getCommand() == C_REQ) onLoop();
}
// what to do when receiving a remote message
void SensorHCSR04::onProcess(Request & request) {
int function = request.getFunction();
switch(function) {
case 101: setTriggerPin(request.getValueInt()); break;
case 102: setEchoPin(request.getValueInt()); break;
case 103: setMaxDistance(request.getValueInt()); break;
default: return;
}
_send(_msg_service.set(function));
}
#endif
/*
SensorSonoff
*/
#if MODULE_SONOFF == 1
// contructor
SensorSonoff::SensorSonoff(NodeManager* node_manager, int child_id): Sensor(node_manager, child_id,1) {
setPresentation(S_BINARY);
setType(V_STATUS);
}
// setter/getter
void SensorSonoff::setButtonPin(int value) {
_button_pin = value;
}
void SensorSonoff::setRelayPin(int value) {
_relay_pin = value;
}