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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-08 14:27:40 +00:00
|
|
|
#define XSNS_76 76
|
2020-08-16 21:46:43 +01:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2022-11-08 15:16:15 +00:00
|
|
|
uint16_t DYPDistance = 0; // distance in millimeters
|
2020-08-16 21:46:43 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2020-08-17 15:25:24 +01:00
|
|
|
void DYPInit(void) {
|
|
|
|
if (PinUsed(GPIO_DYP_RX)) {
|
|
|
|
DYPSerial = new TasmotaSerial(Pin(GPIO_DYP_RX), -1, 1);
|
|
|
|
if (DYPSerial->begin(9600)) {
|
|
|
|
if (DYPSerial->hardwareSerial()) {
|
|
|
|
ClaimSerial();
|
|
|
|
}
|
2023-12-28 16:25:01 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR("DYP: Serial UART%d"), DYPSerial->getUart());
|
|
|
|
#endif
|
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) {
|
|
|
|
// 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;
|
|
|
|
}
|
2022-11-08 15:16:15 +00:00
|
|
|
DYPDistance = data; // mm
|
2020-08-17 15:25:24 +01:00
|
|
|
} 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) {
|
2022-11-08 14:27:40 +00:00
|
|
|
char types[4] = "DYP";
|
2022-11-08 15:16:15 +00:00
|
|
|
float distance = (float)DYPDistance / 10; // cm
|
2020-08-17 15:25:24 +01:00
|
|
|
if (json) {
|
2022-11-08 15:16:15 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%1_f}"), types, &distance);
|
2020-08-17 15:25:24 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2022-11-08 15:16:15 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_F_DISTANCE_CM, types, &distance);
|
2020-08-17 15:25:24 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns76(uint32_t function) {
|
2022-11-08 14:27:40 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
DYPInit();
|
|
|
|
}
|
|
|
|
else if (DYPSerial) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
DYPEverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
DYPShow(1);
|
|
|
|
break;
|
2020-08-17 15:25:24 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2022-11-08 14:27:40 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
DYPShow(0);
|
|
|
|
break;
|
2020-08-17 15:25:24 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2022-11-08 14:27:40 +00:00
|
|
|
}
|
2020-08-17 15:25:24 +01:00
|
|
|
}
|
|
|
|
return false;
|
2020-08-16 21:46:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DYP
|