From efccd2c25c678ccb081552e772df08044195e3ce Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Tue, 24 Sep 2024 13:45:39 +0200 Subject: [PATCH] limit swserial to esp8266 --- .github/workflows/build_arduino_ide.yml | 13 +--- .github/workflows/build_platformio.yml | 7 +- README.md | 4 +- examples/softwareserial/softwareserial.ino | 86 ++++++++++++++++++++++ src/GWG/GWG.cpp | 3 + src/GWG/GWG.h | 4 + src/Interface/SoftwareSerialInterface.cpp | 2 +- src/Interface/SoftwareSerialInterface.h | 2 +- src/VS1/VS1.cpp | 3 + src/VS1/VS1.h | 4 + src/VS2/VS2.cpp | 3 + src/VS2/VS2.h | 4 + 12 files changed, 115 insertions(+), 20 deletions(-) create mode 100644 examples/softwareserial/softwareserial.ino diff --git a/.github/workflows/build_arduino_ide.yml b/.github/workflows/build_arduino_ide.yml index 83b039f..7dd637c 100644 --- a/.github/workflows/build_arduino_ide.yml +++ b/.github/workflows/build_arduino_ide.yml @@ -27,11 +27,7 @@ jobs: - examples/simple-write-VS1 - examples/simple-write-VS2 - examples/simple-read-GWG - libraries: | - - name: VitoWiFi - source-path: ./ - - name: EspSoftwareSerial - source-url: https://github.com/plerup/espsoftwareserial.git + - examples/softwareserial build-for-esp32: runs-on: ubuntu-latest @@ -54,9 +50,4 @@ jobs: - examples/simple-read-VS2 - examples/simple-write-VS1 - examples/simple-write-VS2 - - examples/simple-read-GWG - libraries: | - - name: VitoWiFi - source-path: ./ - - name: EspSoftwareSerial - source-url: https://github.com/plerup/espsoftwareserial.git \ No newline at end of file + - examples/simple-read-GWG \ No newline at end of file diff --git a/.github/workflows/build_platformio.yml b/.github/workflows/build_platformio.yml index 0c962d8..48d2fa6 100644 --- a/.github/workflows/build_platformio.yml +++ b/.github/workflows/build_platformio.yml @@ -12,7 +12,8 @@ jobs: examples/simple-read-VS2/simple-read-VS2.ino, examples/simple-write-VS1/simple-write-VS1.ino, examples/simple-write-VS2/simple-write-VS2.ino, - examples/simple-read-GWG/simple-read-GWG.ino + examples/simple-read-GWG/simple-read-GWG.ino, + examples/softwareserial/softwareserial.ino ] steps: - uses: actions/checkout@v3 @@ -27,8 +28,6 @@ jobs: python-version: '3.9' - name: Install PlatformIO Core run: pip install --upgrade platformio - - name: Download external libraries - run: pio pkg install --global --library plerup/EspSoftwareSerial - name: Build PlatformIO examples run: pio ci --lib="." --board=d1_mini env: @@ -58,8 +57,6 @@ jobs: python-version: '3.9' - name: Install PlatformIO Core run: pip install --upgrade platformio - - name: Download external libraries - run: pio pkg install --global --library plerup/EspSoftwareSerial - name: Build PlatformIO examples run: pio ci --lib="." --board=lolin32 env: diff --git a/README.md b/README.md index 8c7076b..000133c 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Based on the fantastic work on [openv](https://github.com/openv/openv/wiki). - VS1 (KW) and VS2 (P300) support. Older systems using the GWG protocol are not supported - Non-blocking API calls - For the Arduino framework and POSIX systems (Linux, tested on a Raspberry Pi 1B) -- Possible to use `SoftwareSerial` +- Possible to use `SoftwareSerial` on ESP8266 ## Contents @@ -254,7 +254,7 @@ Returns a pointer to the payload. ##### `VitoWiFi(IFACE* interface)` Constructor of the VitoWiFi class. `PROTOCOL_VERSION` can be `GWG`, `VS1` or `VS2`. If your Viessmann device is somewhat modern, you should use `VS2`. -`interface` can be any of the `HardwareSerial` interfaces (`Serial`, `Serial1`...), `SoftwareSerial` or if you are on Linux, pass the c-string depicting your device (for example `"/dev/ttyUSB0"`). +`interface` can be any of the `HardwareSerial` interfaces (`Serial`, `Serial1`...) on Arduino boards, `SoftwareSerial` (on ESP8266) or if you are on Linux, pass the c-string depicting your device (for example `"/dev/ttyUSB0"`). ##### `void onResponse(typename PROTOCOLVERSION::OnResponseCallback callback)` diff --git a/examples/softwareserial/softwareserial.ino b/examples/softwareserial/softwareserial.ino new file mode 100644 index 0000000..6f684ec --- /dev/null +++ b/examples/softwareserial/softwareserial.ino @@ -0,0 +1,86 @@ +#include + +#include +#include +const int sRX = 4; // software RX on D2(GPIO 4) +const int sTX = 5; // software TX on D1(GPIO 5) + +EspSoftwareSerial::UART swSer(sRX, sTX); + +// optolink on softwareserial, logging output on hardware UART + +VitoWiFi::VitoWiFi vitoWiFi(&swSer); +bool readValues = false; +uint8_t datapointIndex = 0; + +VitoWiFi::Datapoint datapoints[] = { + VitoWiFi::Datapoint("outsidetemp", 0x5525, 2, VitoWiFi::div10), + VitoWiFi::Datapoint("boilertemp", 0x0810, 2, VitoWiFi::div10), + VitoWiFi::Datapoint("pump", 0x2906, 1, VitoWiFi::noconv) +}; + +void onResponse(const uint8_t* data, uint8_t length, const VitoWiFi::Datapoint& request) { + // raw data can be accessed through the 'response' argument + Serial.print("Raw data received:"); + for (uint8_t i = 0; i < length; ++i) { + Serial.printf(" %02x", data[i]); + } + Serial.print("\n"); + + // the raw data can be decoded using the datapoint. Be sure to use the correct type + Serial.printf("%s: ", request.name()); + if (request.converter() == VitoWiFi::div10) { + float value = request.decode(data, length); + Serial.printf("%.1f\n", value); + } else if (request.converter() == VitoWiFi::noconv) { + // in this example, the response is one byte + Serial.printf("%s\n", (data[0] > 0) ? "ON" : "OFF"); + } +} + +void onError(VitoWiFi::OptolinkResult error, const VitoWiFi::Datapoint& request) { + Serial.printf("Datapoint \"%s\" error: ", request.name()); + if (error == VitoWiFi::OptolinkResult::TIMEOUT) { + Serial.print("timeout\n"); + } else if (error == VitoWiFi::OptolinkResult::LENGTH) { + Serial.print("length\n"); + } else if (error == VitoWiFi::OptolinkResult::NACK) { + Serial.print("nack\n"); + } else if (error == VitoWiFi::OptolinkResult::CRC) { + Serial.print("crc\n"); + } else if (error == VitoWiFi::OptolinkResult::ERROR) { + Serial.print("error\n"); + } +} + +void setup() { + delay(1000); + Serial.begin(74880); + Serial.print("Setting up vitoWiFi\n"); + + vitoWiFi.onResponse(onResponse); + vitoWiFi.onError(onError); + vitoWiFi.begin(); + + Serial.print("Setup finished\n"); +} + +void loop() { + static uint32_t lastMillis = 0; + if (millis() - lastMillis > 60000UL) { // read all values every 60 seconds + lastMillis = millis(); + readValues = true; + datapointIndex = 0; + } + + if (readValues) { + if (vitoWiFi.read(datapoints[datapointIndex])) { + ++datapointIndex; + } + if (datapointIndex == 3) { + readValues = false; + } + } + + vitoWiFi.loop(); +} diff --git a/src/GWG/GWG.cpp b/src/GWG/GWG.cpp index 4d00cea..83e7041 100644 --- a/src/GWG/GWG.cpp +++ b/src/GWG/GWG.cpp @@ -37,6 +37,7 @@ GWG::GWG(HardwareSerial* interface) } } +#if defined(ARDUINO_ARCH_ESP8266) GWG::GWG(SoftwareSerial* interface) : _state(State::UNDEFINED) , _currentMillis(vw_millis()) @@ -62,6 +63,8 @@ GWG::GWG(SoftwareSerial* interface) vw_abort(); } } +#endif + #else GWG::GWG(const char* interface) : _state(State::UNDEFINED) diff --git a/src/GWG/GWG.h b/src/GWG/GWG.h index a28e7b1..b327316 100644 --- a/src/GWG/GWG.h +++ b/src/GWG/GWG.h @@ -17,7 +17,9 @@ the LICENSE file. #include "../Datapoint/Datapoint.h" #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #include "../Interface/HardwareSerialInterface.h" +#if defined(ARDUINO_ARCH_ESP8266) #include "../Interface/SoftwareSerialInterface.h" +#endif #elif defined(__linux__) #include "../Interface/LinuxSerialInterface.h" #else @@ -33,7 +35,9 @@ class GWG { #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) explicit GWG(HardwareSerial* interface); + #if defined(ARDUINO_ARCH_ESP8266) explicit GWG(SoftwareSerial* interface); + #endif #else explicit GWG(const char* interface); #endif diff --git a/src/Interface/SoftwareSerialInterface.cpp b/src/Interface/SoftwareSerialInterface.cpp index bea3993..b7fb655 100644 --- a/src/Interface/SoftwareSerialInterface.cpp +++ b/src/Interface/SoftwareSerialInterface.cpp @@ -6,7 +6,7 @@ For a copy, see or the LICENSE file. */ -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP8266) #include "SoftwareSerialInterface.h" diff --git a/src/Interface/SoftwareSerialInterface.h b/src/Interface/SoftwareSerialInterface.h index 006164e..f46d46b 100644 --- a/src/Interface/SoftwareSerialInterface.h +++ b/src/Interface/SoftwareSerialInterface.h @@ -8,7 +8,7 @@ the LICENSE file. #pragma once -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP8266) #include diff --git a/src/VS1/VS1.cpp b/src/VS1/VS1.cpp index 6050890..27ba562 100644 --- a/src/VS1/VS1.cpp +++ b/src/VS1/VS1.cpp @@ -37,6 +37,7 @@ VS1::VS1(HardwareSerial* interface) } } +#if defined(ARDUINO_ARCH_ESP8266) VS1::VS1(SoftwareSerial* interface) : _state(State::UNDEFINED) , _currentMillis(vw_millis()) @@ -62,6 +63,8 @@ VS1::VS1(SoftwareSerial* interface) vw_abort(); } } +#endif + #else VS1::VS1(const char* interface) : _state(State::UNDEFINED) diff --git a/src/VS1/VS1.h b/src/VS1/VS1.h index bfd8fba..89c94f1 100644 --- a/src/VS1/VS1.h +++ b/src/VS1/VS1.h @@ -17,7 +17,9 @@ the LICENSE file. #include "../Datapoint/Datapoint.h" #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #include "../Interface/HardwareSerialInterface.h" +#if defined(ARDUINO_ARCH_ESP8266) #include "../Interface/SoftwareSerialInterface.h" +#endif #elif defined(__linux__) #include "../Interface/LinuxSerialInterface.h" #else @@ -33,7 +35,9 @@ class VS1 { #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) explicit VS1(HardwareSerial* interface); + #if defined(ARDUINO_ARCH_ESP8266) explicit VS1(SoftwareSerial* interface); + #endif #else explicit VS1(const char* interface); #endif diff --git a/src/VS2/VS2.cpp b/src/VS2/VS2.cpp index 478d430..3069700 100644 --- a/src/VS2/VS2.cpp +++ b/src/VS2/VS2.cpp @@ -31,6 +31,7 @@ VS2::VS2(HardwareSerial* interface) } } +#if defined(ARDUINO_ARCH_ESP8266) VS2::VS2(SoftwareSerial* interface) : _state(State::UNDEFINED) , _currentMillis(vw_millis()) @@ -50,6 +51,8 @@ VS2::VS2(SoftwareSerial* interface) vw_abort(); } } +#endif + #else VS2::VS2(const char* interface) : _state(State::UNDEFINED) diff --git a/src/VS2/VS2.h b/src/VS2/VS2.h index 1de7d8f..4abff4a 100644 --- a/src/VS2/VS2.h +++ b/src/VS2/VS2.h @@ -17,7 +17,9 @@ the LICENSE file. #include "../Datapoint/Datapoint.h" #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #include "../Interface/HardwareSerialInterface.h" +#if defined(ARDUINO_ARCH_ESP8266) #include "../Interface/SoftwareSerialInterface.h" +#endif #elif defined(__linux__) #include "../Interface/LinuxSerialInterface.h" #else @@ -33,7 +35,9 @@ class VS2 { #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) explicit VS2(HardwareSerial* interface); + #if defined(ARDUINO_ARCH_ESP8266) explicit VS2(SoftwareSerial* interface); + #endif #else explicit VS2(const char* interface); #endif