mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-15 23:06:30 +00:00
Add hardware serial option to MHZ-19
Add hardware serial option to MHZ-19 (#2659)
This commit is contained in:
parent
016b1dd029
commit
0aea9ba572
@ -8,6 +8,7 @@
|
||||
* Fix sensor MHZ-19 vanishing data over time (#2659)
|
||||
* Add Portuguese in Brazil language file
|
||||
* Add rule state test for On/Off in addition to 0/1 (#2613)
|
||||
* Add hardware serial option to MHZ-19 sensor (#2659)
|
||||
* Updated Italian language file (#2618)
|
||||
* Optimize command handling
|
||||
*
|
||||
|
@ -100,6 +100,7 @@ const char kTasmotaCommands[] PROGMEM =
|
||||
int baudrate = APP_BAUDRATE; // Serial interface baud rate
|
||||
SerialConfig serial_config = SERIAL_8N1; // Serial interface configuration 8 data bits, No parity, 1 stop bit
|
||||
byte serial_in_byte; // Received byte
|
||||
uint8_t serial_local = 0; // Handle serial locally;
|
||||
unsigned long serial_polling_window = 0; // Serial polling window
|
||||
int serial_in_byte_counter = 0; // Index in receive buffer
|
||||
byte dual_hex_code = 0; // Sonoff dual input flag
|
||||
@ -2431,7 +2432,7 @@ void loop()
|
||||
|
||||
if (millis() >= state_loop_timer) StateLoop();
|
||||
|
||||
SerialInput();
|
||||
if (!serial_local) SerialInput();
|
||||
|
||||
#ifdef USE_ARDUINO_OTA
|
||||
ArduinoOTA.handle();
|
||||
|
@ -486,6 +486,14 @@ void SetSerialBaudrate(int baudrate)
|
||||
}
|
||||
}
|
||||
|
||||
void SetSerialLocal(bool slocal)
|
||||
{
|
||||
serial_local = slocal;
|
||||
if (slocal) {
|
||||
SetSeriallog(LOG_LEVEL_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t GetHash(const char *buffer, size_t size)
|
||||
{
|
||||
uint32_t hash = 0;
|
||||
|
@ -91,6 +91,46 @@ uint8_t mhz_retry = MHZ19_RETRY_COUNT;
|
||||
uint8_t mhz_received = 0;
|
||||
uint8_t mhz_state = 0;
|
||||
|
||||
uint8_t mhz_hard_serial = 0;
|
||||
|
||||
/*********************************************************************************************/
|
||||
|
||||
size_t MhzSerialAvailable()
|
||||
{
|
||||
if (mhz_hard_serial) {
|
||||
return Serial.available();
|
||||
} else {
|
||||
return MhzSerial->available();
|
||||
}
|
||||
}
|
||||
|
||||
void MhzSerialFlush()
|
||||
{
|
||||
if (mhz_hard_serial) {
|
||||
Serial.flush();
|
||||
} else {
|
||||
MhzSerial->flush();
|
||||
}
|
||||
}
|
||||
|
||||
size_t MhzSerialWrite(byte *array, size_t size)
|
||||
{
|
||||
if (mhz_hard_serial) {
|
||||
return Serial.write(array, size);
|
||||
} else {
|
||||
return MhzSerial->write(array, size);
|
||||
}
|
||||
}
|
||||
|
||||
int MhzSerialRead()
|
||||
{
|
||||
if (mhz_hard_serial) {
|
||||
return Serial.read();
|
||||
} else {
|
||||
return MhzSerial->read();
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************/
|
||||
|
||||
byte MhzCalculateChecksum(byte *array)
|
||||
@ -117,7 +157,8 @@ size_t MhzSendCmd(byte command_id)
|
||||
mhz_send[7] = 0x00;
|
||||
*/
|
||||
mhz_send[8] = MhzCalculateChecksum(mhz_send);
|
||||
return MhzSerial->write(mhz_send, sizeof(mhz_send));
|
||||
|
||||
return MhzSerialWrite(mhz_send, sizeof(mhz_send));
|
||||
}
|
||||
|
||||
/*********************************************************************************************/
|
||||
@ -160,7 +201,7 @@ bool MhzCheckAndApplyFilter(uint16_t ppm, uint8_t s)
|
||||
void MhzEverySecond()
|
||||
{
|
||||
mhz_state++;
|
||||
if (8 == mhz_state) { // Every 8 sec start a MH-Z19 measuring cycle (which takes 1005 +5% ms)
|
||||
if (8 == mhz_state) { // Every 8 sec start a MH-Z19 measuring cycle (which takes 1005 +5% ms)
|
||||
mhz_state = 0;
|
||||
|
||||
if (mhz_retry) {
|
||||
@ -171,7 +212,7 @@ void MhzEverySecond()
|
||||
}
|
||||
}
|
||||
|
||||
MhzSerial->flush(); // Sync reception
|
||||
MhzSerialFlush(); // Sync reception
|
||||
MhzSendCmd(MHZ_CMND_READPPM);
|
||||
mhz_received = 0;
|
||||
}
|
||||
@ -182,8 +223,8 @@ void MhzEverySecond()
|
||||
unsigned long start = millis();
|
||||
uint8_t counter = 0;
|
||||
while (((millis() - start) < MHZ19_READ_TIMEOUT) && (counter < 9)) {
|
||||
if (MhzSerial->available() > 0) {
|
||||
mhz_response[counter++] = MhzSerial->read();
|
||||
if (MhzSerialAvailable() > 0) {
|
||||
mhz_response[counter++] = MhzSerialRead();
|
||||
} else {
|
||||
delay(5);
|
||||
}
|
||||
@ -281,9 +322,19 @@ void MhzInit()
|
||||
{
|
||||
mhz_type = 0;
|
||||
if ((pin[GPIO_MHZ_RXD] < 99) && (pin[GPIO_MHZ_TXD] < 99)) {
|
||||
MhzSerial = new TasmotaSerial(pin[GPIO_MHZ_RXD], pin[GPIO_MHZ_TXD]);
|
||||
if (MhzSerial->begin(9600)) {
|
||||
if ((1 == pin[GPIO_MHZ_RXD]) && (3 == pin[GPIO_MHZ_TXD])) {
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("MHZ: Hardware serial"));
|
||||
baudrate = 9600;
|
||||
SetSerialBaudrate(baudrate);
|
||||
SetSerialLocal(true);
|
||||
mhz_hard_serial = 1;
|
||||
mhz_type = 1;
|
||||
} else {
|
||||
MhzSerial = new TasmotaSerial(pin[GPIO_MHZ_RXD], pin[GPIO_MHZ_TXD]);
|
||||
if (MhzSerial->begin(9600)) {
|
||||
AddLog_P(LOG_LEVEL_DEBUG, PSTR("MHZ: Software serial"));
|
||||
mhz_type = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user