2020-08-16 21:46:43 +01:00
|
|
|
/*
|
|
|
|
xsns_76_dyp.ino - DYP ME007 serial interface
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Janusz Kostorz
|
2020-08-16 21:46:43 +01:00
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
2020-08-16 21:46:43 +01:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
2020-08-16 21:46:43 +01:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_DYP
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
2020-08-17 15:25:24 +01:00
|
|
|
* DYP ME007 ultrasonic distance sensor (300...4000mm), serial version
|
2020-08-16 21:46:43 +01:00
|
|
|
*
|
|
|
|
* Every one second code check last received value from sensor via serial port and return:
|
|
|
|
* -1 for checksum error
|
|
|
|
* 0 for distance below 300mm
|
|
|
|
* 300...4000 for measured distance
|
|
|
|
* 4999 for distance above 4000mm
|
|
|
|
* 5999 for not connected sensor
|
|
|
|
*
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_76 76
|
|
|
|
|
|
|
|
#include <TasmotaSerial.h>
|
|
|
|
|
|
|
|
TasmotaSerial *DYPSerial = nullptr;
|
|
|
|
|
|
|
|
#define DYP_CRCERROR -1
|
|
|
|
#define DYP_BELOWMIN 0
|
|
|
|
#define DYP_MIN 300
|
|
|
|
#define DYP_MAX 4000
|
|
|
|
#define DYP_ABOVEMAX 4999
|
|
|
|
#define DYP_NOSENSOR 5999
|
|
|
|
|
|
|
|
uint16_t DYPDistance = 0; // distance in milimeters
|
|
|
|
bool DYPSensor = false; // sensor available
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
void DYPInit(void) {
|
|
|
|
DYPSensor = false;
|
|
|
|
if (PinUsed(GPIO_DYP_RX)) {
|
|
|
|
DYPSerial = new TasmotaSerial(Pin(GPIO_DYP_RX), -1, 1);
|
|
|
|
if (DYPSerial->begin(9600)) {
|
|
|
|
if (DYPSerial->hardwareSerial()) {
|
|
|
|
ClaimSerial();
|
|
|
|
}
|
|
|
|
DYPSensor = true;
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
2020-08-17 15:25:24 +01:00
|
|
|
}
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
void DYPEverySecond(void) {
|
|
|
|
if (!DYPSensor) { return; }
|
|
|
|
|
|
|
|
// check for serial data
|
|
|
|
if (DYPSerial->available() < 6) {
|
|
|
|
DYPDistance = DYP_NOSENSOR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// trash old data (only 7 last bytes are needed for calculate distance)
|
|
|
|
while (DYPSerial->available() > 7) {
|
|
|
|
DYPSerial->read();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read data from serial port - * 0xFF MSB LSB CRC *
|
|
|
|
while (DYPSerial->available() > 3) {
|
|
|
|
// check for start byte signature
|
|
|
|
if (DYPSerial->read() != 0xFF) {
|
|
|
|
continue;
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
// check for data bytes
|
|
|
|
if (DYPSerial->available() > 2) {
|
|
|
|
uint8_t msb = DYPSerial->read();
|
|
|
|
uint8_t lsb = DYPSerial->read();
|
|
|
|
if (((uint16_t)(0xFF + msb + lsb) & 0xFF) == DYPSerial->read()) {
|
|
|
|
uint16_t data = (msb << 8) | lsb;
|
|
|
|
if (data < DYP_MIN) {
|
|
|
|
data = DYP_BELOWMIN;
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
2020-08-17 15:25:24 +01:00
|
|
|
if (data > DYP_MAX) {
|
|
|
|
data = DYP_ABOVEMAX;
|
|
|
|
}
|
|
|
|
DYPDistance = data;
|
|
|
|
} else {
|
|
|
|
DYPDistance = DYP_CRCERROR;
|
|
|
|
}
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
2020-08-17 15:25:24 +01:00
|
|
|
}
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
void DYPShow(bool json) {
|
|
|
|
char types[5] = "DYP";
|
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(PSTR(",\"%s\":{\"" D_DISTANCE "\":%d}"), types, DYPDistance);
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_RANGE, types, DYPDistance);
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
bool Xsns76(uint8_t function) {
|
|
|
|
if (!PinUsed(GPIO_DYP_RX)) { return false; }
|
2020-08-16 21:46:43 +01:00
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
switch (function) {
|
2020-08-16 21:46:43 +01:00
|
|
|
case FUNC_INIT:
|
|
|
|
DYPInit();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
DYPEverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
DYPShow(1);
|
|
|
|
break;
|
2020-08-17 15:25:24 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2020-08-16 21:46:43 +01:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
DYPShow(0);
|
|
|
|
break;
|
2020-08-17 15:25:24 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
return false;
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DYP
|