|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +#include <VitoWiFi.h> |
| 4 | + |
| 5 | +/* |
| 6 | +This example is to show how you could build your own |
| 7 | +interface and serves as a compilation test |
| 8 | +*/ |
| 9 | + |
| 10 | +// Dummy class that has all the methods for VitoWiFi to work |
| 11 | +// but doesn't do anything in this case. |
| 12 | +// Use as skeleton for your own implementation |
| 13 | +class DummyInterface { |
| 14 | + public: |
| 15 | + bool begin() { |
| 16 | + // prepare the interface |
| 17 | + // optolink comm at 4800 baud, 8 bits, even parity and 2 stop bits |
| 18 | + // called at VitoWiFi::begin() |
| 19 | + return true; |
| 20 | + } |
| 21 | + void end() { |
| 22 | + // stop the interface |
| 23 | + // called at VitoWiFi::end() |
| 24 | + } |
| 25 | + std::size_t write(const uint8_t* data, uint8_t length) { |
| 26 | + // tries to write `data` with length `length` to the interface |
| 27 | + // returns the actually written data |
| 28 | + return 0; |
| 29 | + } |
| 30 | + uint8_t read() { |
| 31 | + // read one byte from the interface |
| 32 | + // availability of data is checked first |
| 33 | + return 0; |
| 34 | + } |
| 35 | + size_t available() { |
| 36 | + // check if data is available |
| 37 | + return 0; |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +// optolink on Dummy Interface, logging output on UART1 (connected to USB) |
| 42 | +DummyInterface dummyInterface; |
| 43 | +#define SERIAL1 dummyInterface |
| 44 | +#define SERIAL2 Serial |
| 45 | +#define SERIALBAUDRATE 115200 |
| 46 | + |
| 47 | +VitoWiFi::VitoWiFi<VitoWiFi::VS2> vitoWiFi(&SERIAL1); |
| 48 | +bool readValues = false; |
| 49 | +uint8_t datapointIndex = 0; |
| 50 | + |
| 51 | +VitoWiFi::Datapoint datapoints[] = { |
| 52 | + VitoWiFi::Datapoint("outsidetemp", 0x5525, 2, VitoWiFi::div10), |
| 53 | + VitoWiFi::Datapoint("boilertemp", 0x0810, 2, VitoWiFi::div10), |
| 54 | + VitoWiFi::Datapoint("pump", 0x2906, 1, VitoWiFi::noconv) |
| 55 | +}; |
| 56 | + |
| 57 | +void onResponse(const VitoWiFi::PacketVS2& response, const VitoWiFi::Datapoint& request) { |
| 58 | + // raw data can be accessed through the 'response' argument |
| 59 | + SERIAL2.print("Raw data received:"); |
| 60 | + const uint8_t* data = response.data(); |
| 61 | + for (uint8_t i = 0; i < response.dataLength(); ++i) { |
| 62 | + SERIAL2.printf(" %02x", data[i]); |
| 63 | + } |
| 64 | + SERIAL2.print("\n"); |
| 65 | + |
| 66 | + // the raw data can be decoded using the datapoint. Be sure to use the correct type |
| 67 | + SERIAL2.printf("%s: ", request.name()); |
| 68 | + if (request.converter() == VitoWiFi::div10) { |
| 69 | + float value = request.decode(response); |
| 70 | + SERIAL2.printf("%.1f\n", value); |
| 71 | + } else if (request.converter() == VitoWiFi::noconv) { |
| 72 | + bool value = request.decode(response); |
| 73 | + // alternatively, we can just cast response.data()[0] to bool |
| 74 | + SERIAL2.printf("%s\n", value ? "ON" : "OFF"); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void onError(VitoWiFi::OptolinkResult error, const VitoWiFi::Datapoint& request) { |
| 79 | + SERIAL2.printf("Datapoint \"%s\" error: ", request.name()); |
| 80 | + if (error == VitoWiFi::OptolinkResult::TIMEOUT) { |
| 81 | + SERIAL2.print("timeout\n"); |
| 82 | + } else if (error == VitoWiFi::OptolinkResult::LENGTH) { |
| 83 | + SERIAL2.print("length\n"); |
| 84 | + } else if (error == VitoWiFi::OptolinkResult::NACK) { |
| 85 | + SERIAL2.print("nack\n"); |
| 86 | + } else if (error == VitoWiFi::OptolinkResult::CRC) { |
| 87 | + SERIAL2.print("crc\n"); |
| 88 | + } else if (error == VitoWiFi::OptolinkResult::ERROR) { |
| 89 | + SERIAL2.print("error\n"); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +void setup() { |
| 94 | + delay(1000); |
| 95 | + SERIAL2.begin(SERIALBAUDRATE); |
| 96 | + SERIAL2.print("Setting up vitoWiFi\n"); |
| 97 | + |
| 98 | + vitoWiFi.onResponse(onResponse); |
| 99 | + vitoWiFi.onError(onError); |
| 100 | + vitoWiFi.begin(); |
| 101 | + |
| 102 | + SERIAL2.print("Setup finished\n"); |
| 103 | +} |
| 104 | + |
| 105 | +void loop() { |
| 106 | + static uint32_t lastMillis = 0; |
| 107 | + if (millis() - lastMillis > 60000UL) { // read all values every 60 seconds |
| 108 | + lastMillis = millis(); |
| 109 | + readValues = true; |
| 110 | + datapointIndex = 0; |
| 111 | + } |
| 112 | + |
| 113 | + if (readValues) { |
| 114 | + if (vitoWiFi.read(datapoints[datapointIndex])) { |
| 115 | + ++datapointIndex; |
| 116 | + } |
| 117 | + if (datapointIndex == 3) { |
| 118 | + readValues = false; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + vitoWiFi.loop(); |
| 123 | +} |
0 commit comments