|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +#include <VitoWiFi.h> |
| 4 | +#include <SoftwareSerial.h> |
| 5 | +const int sRX = 4; // software RX on D2(GPIO 4) |
| 6 | +const int sTX = 5; // software TX on D1(GPIO 5) |
| 7 | + |
| 8 | +EspSoftwareSerial::UART swSer(sRX, sTX); |
| 9 | + |
| 10 | +// optolink on softwareserial, logging output on hardware UART |
| 11 | + |
| 12 | +VitoWiFi::VitoWiFi<VitoWiFi::VS1> vitoWiFi(&swSer); |
| 13 | +bool readValues = false; |
| 14 | +uint8_t datapointIndex = 0; |
| 15 | + |
| 16 | +VitoWiFi::Datapoint datapoints[] = { |
| 17 | + VitoWiFi::Datapoint("outsidetemp", 0x5525, 2, VitoWiFi::div10), |
| 18 | + VitoWiFi::Datapoint("boilertemp", 0x0810, 2, VitoWiFi::div10), |
| 19 | + VitoWiFi::Datapoint("pump", 0x2906, 1, VitoWiFi::noconv) |
| 20 | +}; |
| 21 | + |
| 22 | +void onResponse(const uint8_t* data, uint8_t length, const VitoWiFi::Datapoint& request) { |
| 23 | + // raw data can be accessed through the 'response' argument |
| 24 | + Serial.print("Raw data received:"); |
| 25 | + for (uint8_t i = 0; i < length; ++i) { |
| 26 | + Serial.printf(" %02x", data[i]); |
| 27 | + } |
| 28 | + Serial.print("\n"); |
| 29 | + |
| 30 | + // the raw data can be decoded using the datapoint. Be sure to use the correct type |
| 31 | + Serial.printf("%s: ", request.name()); |
| 32 | + if (request.converter() == VitoWiFi::div10) { |
| 33 | + float value = request.decode(data, length); |
| 34 | + Serial.printf("%.1f\n", value); |
| 35 | + } else if (request.converter() == VitoWiFi::noconv) { |
| 36 | + // in this example, the response is one byte |
| 37 | + Serial.printf("%s\n", (data[0] > 0) ? "ON" : "OFF"); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void onError(VitoWiFi::OptolinkResult error, const VitoWiFi::Datapoint& request) { |
| 42 | + Serial.printf("Datapoint \"%s\" error: ", request.name()); |
| 43 | + if (error == VitoWiFi::OptolinkResult::TIMEOUT) { |
| 44 | + Serial.print("timeout\n"); |
| 45 | + } else if (error == VitoWiFi::OptolinkResult::LENGTH) { |
| 46 | + Serial.print("length\n"); |
| 47 | + } else if (error == VitoWiFi::OptolinkResult::NACK) { |
| 48 | + Serial.print("nack\n"); |
| 49 | + } else if (error == VitoWiFi::OptolinkResult::CRC) { |
| 50 | + Serial.print("crc\n"); |
| 51 | + } else if (error == VitoWiFi::OptolinkResult::ERROR) { |
| 52 | + Serial.print("error\n"); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void setup() { |
| 57 | + delay(1000); |
| 58 | + Serial.begin(74880); |
| 59 | + Serial.print("Setting up vitoWiFi\n"); |
| 60 | + |
| 61 | + vitoWiFi.onResponse(onResponse); |
| 62 | + vitoWiFi.onError(onError); |
| 63 | + vitoWiFi.begin(); |
| 64 | + |
| 65 | + Serial.print("Setup finished\n"); |
| 66 | +} |
| 67 | + |
| 68 | +void loop() { |
| 69 | + static uint32_t lastMillis = 0; |
| 70 | + if (millis() - lastMillis > 60000UL) { // read all values every 60 seconds |
| 71 | + lastMillis = millis(); |
| 72 | + readValues = true; |
| 73 | + datapointIndex = 0; |
| 74 | + } |
| 75 | + |
| 76 | + if (readValues) { |
| 77 | + if (vitoWiFi.read(datapoints[datapointIndex])) { |
| 78 | + ++datapointIndex; |
| 79 | + } |
| 80 | + if (datapointIndex == 3) { |
| 81 | + readValues = false; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + vitoWiFi.loop(); |
| 86 | +} |
0 commit comments