Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
bertmelis authored Nov 25, 2023
1 parent 921b7b9 commit 48de475
Show file tree
Hide file tree
Showing 18 changed files with 401 additions and 200 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/build_arduino_ide.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ jobs:
- name: esp8266:esp8266
source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json
sketch-paths: |
- examples/simple-read
- examples/simple-write
- examples/simple-read-VS1
- examples/simple-read-VS2
- examples/simple-write-VS1
- examples/simple-write-VS2
libraries: |
- name: VitoWiFi
source-path: ./
Expand All @@ -47,8 +49,10 @@ jobs:
- name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
sketch-paths: |
- examples/simple-read
- examples/simple-write
- examples/simple-read-VS1
- examples/simple-read-VS2
- examples/simple-write-VS1
- examples/simple-write-VS2
libraries: |
- name: VitoWiFi
source-path: ./
Expand Down
58 changes: 34 additions & 24 deletions .github/workflows/build_platformio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
example: [examples/simple-read/simple-read.ino, examples/simple-write/simple-write.ino]
example: [
examples/simple-read-VS1/simple-read-VS1.ino,
examples/simple-read-VS2/simple-read-VS2.ino,
examples/simple-write-VS1/simple-write-VS1.ino,
examples/simple-write-VS2/simple-write-VS2.ino
]
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
Expand All @@ -29,26 +34,31 @@ jobs:
PLATFORMIO_CI_SRC: ${{ matrix.example }}

build-for-esp32:
runs-on: ubuntu-latest
strategy:
matrix:
example: [examples/simple-read/simple-read.ino, examples/simple-write/simple-write.ino]
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v4
with:
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:
PLATFORMIO_CI_SRC: ${{ matrix.example }}
runs-on: ubuntu-latest
strategy:
matrix:
example: [
examples/simple-read-VS1/simple-read-VS1.ino,
examples/simple-read-VS2/simple-read-VS2.ino,
examples/simple-write-VS1/simple-write-VS1.ino,
examples/simple-write-VS2/simple-write-VS2.ino
]
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v4
with:
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:
PLATFORMIO_CI_SRC: ${{ matrix.example }}
109 changes: 109 additions & 0 deletions examples/simple-read-VS1/simple-read-VS1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
This example defines three datapoints.
The first two are TEMPL type datapoints and have their own callback.
When no specific callback is attached to a datapoint, it uses the global callback.
Note the difference in return value between the callbacks:
for tempCallback uses value.getFloat() as TEMPL datapoints return a float.
globalCallback uses value.getString(char*,size_t). This method is independent of the returned type.
*/

#include <Arduino.h>

#include <VitoWiFi.h>

#ifdef ESP8266
// optolink on full UART, logging output on secondary
#define SERIAL1 Serial
#define SERIAL2 Serial1
#define SERIALBAUDRATE 74880
#endif

#ifdef ESP32
// optolink on UART2, logging output on UART1 (connected to USB)
#define SERIAL1 Serial1
#define SERIAL2 Serial
#define SERIALBAUDRATE 115200
#endif

#ifndef SERIALBAUDRATE
#error Target platform not supported
#endif

VitoWiFi::VitoWiFi<VitoWiFi::VS1> vitoWiFi(&SERIAL1);
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
SERIAL2.print("Raw data received:");
for (uint8_t i = 0; i < length; ++i) {
SERIAL2.printf(" %02x", data[i]);
}
SERIAL2.print("\n");

// the raw data can be decoded using the datapoint. Be sure to use the correct type
SERIAL2.printf("%s: ", request.name());
if (request.converter() == VitoWiFi::div10) {
float value = request.decode(data, length);
SERIAL2.printf("%.1f\n", value);
} else if (request.converter() == VitoWiFi::noconv) {
// in this example, the response is one byte
SERIAL2.printf("%s\n", (data[0] > 0) ? "ON" : "OFF");
}
}

void onError(VitoWiFi::OptolinkResult error, const VitoWiFi::Datapoint& request) {
SERIAL2.printf("Datapoint \"%s\" error: ", request.name());
if (error == VitoWiFi::OptolinkResult::TIMEOUT) {
SERIAL2.print("timeout\n");
} else if (error == VitoWiFi::OptolinkResult::LENGTH) {
SERIAL2.print("length\n");
} else if (error == VitoWiFi::OptolinkResult::NACK) {
SERIAL2.print("nack\n");
} else if (error == VitoWiFi::OptolinkResult::CRC) {
SERIAL2.print("crc\n");
} else if (error == VitoWiFi::OptolinkResult::ERROR) {
SERIAL2.print("error\n");
}
}

void setup() {
delay(1000);
SERIAL2.begin(SERIALBAUDRATE);
SERIAL2.print("Setting up vitoWiFi\n");

vitoWiFi.onResponse(onResponse);
vitoWiFi.onError(onError);
vitoWiFi.begin();

SERIAL2.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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ void onResponse(const VitoWiFi::PacketVS2& response, const VitoWiFi::Datapoint&
SERIAL2.print("\n");

// the raw data can be decoded using the datapoint. Be sure to use the correct type
if (strcmp(request.name(), datapoints[0].name()) == 0) {
float outsideTemp = request.decode(response);
SERIAL2.printf("Outside temperature is %.1f\n", outsideTemp);
} else if (strcmp(request.name(), datapoints[1].name()) == 0) {
float outsideTemp = request.decode(response);
SERIAL2.printf("Boiler temperature is %.1f\n", outsideTemp);
} else if (strcmp(request.name(), datapoints[2].name()) == 0) {
bool pumpStatus = request.decode(response);
SERIAL2.printf("Heating pump status is %s\n", pumpStatus ? "ON" : "OFF");
SERIAL2.printf("%s: ", request.name());
if (request.converter() == VitoWiFi::div10) {
float value = request.decode(response);
SERIAL2.printf("%.1f\n", value);
} else if (request.converter() == VitoWiFi::noconv) {
bool value = request.decode(response);
// alternatively, we can just cast response.data()[0] to bool
SERIAL2.printf("%s\n", value ? "ON" : "OFF");
}
}

Expand Down
121 changes: 121 additions & 0 deletions examples/simple-write-VS1/simple-write-VS1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
This example defines three datapoints.
The first two are TEMPL type datapoints and have their own callback.
When no specific callback is attached to a datapoint, it uses the global callback.
Note the difference in return value between the callbacks:
for tempCallback uses value.getFloat() as TEMPL datapoints return a float.
globalCallback uses value.getString(char*,size_t). This method is independent of the returned type.
*/

#include <Arduino.h>

#include <VitoWiFi.h>

#ifdef ESP8266
// optolink on full UART, logging output on secondary
#define SERIAL1 Serial
#define SERIAL2 Serial1
#define SERIALBAUDRATE 74880
#endif

#ifdef ESP32
// optolink on UART2, logging output on UART1 (connected to USB)
#define SERIAL1 Serial1
#define SERIAL2 Serial
#define SERIALBAUDRATE 115200
#endif

#ifndef SERIALBAUDRATE
#error Target platform not supported
#endif

VitoWiFi::VitoWiFi<VitoWiFi::VS1> vitoWiFi(&SERIAL1);
bool readValues = false;
uint8_t datapointIndex = 0;
uint8_t roomTemperature = 20;
bool writeRoomTemp = false;

VitoWiFi::Datapoint datapoints[] = {
VitoWiFi::Datapoint("roomtemp", 0x2306, 1, VitoWiFi::noconv),
VitoWiFi::Datapoint("boilertemp", 0x0810, 2, VitoWiFi::div10)
};

void setRoomTemp(uint8_t value) {
roomTemperature = value;
writeRoomTemp = true;
}

void onResponse(const uint8_t* data, uint8_t length, const VitoWiFi::Datapoint& request) {
// raw data can be accessed through the 'response' argument
SERIAL2.print("Raw data received:");
for (uint8_t i = 0; i < length; ++i) {
SERIAL2.printf(" %02x", data[i]);
}
SERIAL2.print("\n");

// the raw data can be decoded using the datapoint. Be sure to use the correct type
SERIAL2.printf("%s: ", request.name());
if (request.converter() == VitoWiFi::div10) {
float value = request.decode(data, length);
SERIAL2.printf("%.1f\n", value);
} else if (request.converter() == VitoWiFi::noconv) {
// in this example, the response is one byte
SERIAL2.printf("%s\n", (data[0] > 0) ? "ON" : "OFF");
}
}

void onError(VitoWiFi::OptolinkResult error, const VitoWiFi::Datapoint& request) {
SERIAL2.printf("Datapoint \"%s\" error: ", request.name());
if (error == VitoWiFi::OptolinkResult::TIMEOUT) {
SERIAL2.print("timeout\n");
} else if (error == VitoWiFi::OptolinkResult::LENGTH) {
SERIAL2.print("length\n");
} else if (error == VitoWiFi::OptolinkResult::NACK) {
SERIAL2.print("nack\n");
} else if (error == VitoWiFi::OptolinkResult::CRC) {
SERIAL2.print("crc\n");
} else if (error == VitoWiFi::OptolinkResult::ERROR) {
SERIAL2.print("error\n");
}
}

void setup() {
delay(1000);
SERIAL2.begin(SERIALBAUDRATE);
SERIAL2.print("Setting up vitoWiFi\n");

vitoWiFi.onResponse(onResponse);
vitoWiFi.onError(onError);
vitoWiFi.begin();

SERIAL2.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;
}
}

if (writeRoomTemp) {
if (vitoWiFi.write(datapoints[0], roomTemperature)) {
writeRoomTemp = false;
}
}

vitoWiFi.loop();
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ void onResponse(const VitoWiFi::PacketVS2& response, const VitoWiFi::Datapoint&
SERIAL2.print("\n");

// the raw data can be decoded using the datapoint. Be sure to use the correct type
if (strcmp(request.name(), datapoints[0].name()) == 0) {
uint8_t roomSetTemp = request.decode(response);
SERIAL2.printf("Room set temperature is %u\n", roomSetTemp);
} else if (strcmp(request.name(), datapoints[1].name()) == 0) {
float outsideTemp = request.decode(response);
SERIAL2.printf("Boiler temperature is %.1f\n", outsideTemp);
SERIAL2.printf("%s: ", request.name());
if (request.converter() == VitoWiFi::div10) {
float value = request.decode(response);
SERIAL2.printf("%.1f\n", value);
} else if (request.converter() == VitoWiFi::noconv) {
bool value = request.decode(response);
// alternatively, we can just cast response.data()[0] to bool
SERIAL2.printf("%s\n", value ? "ON" : "OFF");
}
}

Expand Down
Loading

0 comments on commit 48de475

Please sign in to comment.