From 66a666d79266d255aead31967fc6164c2853a3c8 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 21 Jan 2021 16:07:43 +0100 Subject: [PATCH] first commit --- .gitignore | 19 +++ README.md | 1 + keywords.txt | 18 +++ library.properties | 11 ++ src/YoulessMonitor.cpp | 254 +++++++++++++++++++++++++++++++++++++++++ src/YoulessMonitor.h | 64 +++++++++++ 6 files changed, 367 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 keywords.txt create mode 100644 library.properties create mode 100644 src/YoulessMonitor.cpp create mode 100644 src/YoulessMonitor.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc47415 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Created by https://www.toptal.com/developers/gitignore/api/vscode,platformio +# Edit at https://www.toptal.com/developers/gitignore?templates=vscode,platformio + +### PlatformIO ### +.pioenvs +.piolibdeps +.clang_complete +.gcc-flags.json +.pio + +### vscode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# End of https://www.toptal.com/developers/gitignore/api/vscode,platformio \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f395431 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Readme diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..0b66d48 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,18 @@ +# Syntax Coloring Map for YoulessMonitor + +# Classes +YoulessMonitor KEYWORD1 + +# Methods and Functions +getBasicStatus KEYWORD2 +readCounter KEYWORD2 +readPower KEYWORD2 +readConnectionStatus KEYWORD2 +getUploadedValues KEYWORD2 +readConsumptionCounter KEYWORD2 +readProductionCounter KEYWORD2 +readGasCounter KEYWORD2 + +# Constants +WIRELESS LITERAL1 +WIRED LITERAL1 \ No newline at end of file diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..11d9d99 --- /dev/null +++ b/library.properties @@ -0,0 +1,11 @@ +name=Youless Arduino Library +version=1.0.0 +author=Nick van Tholen +maintainer=Nick van Tholen +sentence=A library for use with Youless Energy Monitor. +paragraph=This library provides an usefull connection between a development-board and the Youless Energy Monitor. It uses a direct TCP link to the Youless monitor to get the measured data, the library also handles the formating of this data. For more information about this library and documentation check out the github repository. +category=Communication +url= +architectures=* +includes=YoulessMonitor.h +depends=ArduinoJson \ No newline at end of file diff --git a/src/YoulessMonitor.cpp b/src/YoulessMonitor.cpp new file mode 100644 index 0000000..4f7d655 --- /dev/null +++ b/src/YoulessMonitor.cpp @@ -0,0 +1,254 @@ +// MY OWN LICENSE // + +// INCLUDED LICENSES // +/* +The MIT License (MIT) +--------------------- + +Copyright © 2014-2020 Benoit BLANCHON + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#define ARDUINOJSON_USE_LONG_LONG 1 +#include "Arduino.h" +#include "WiFiClient.h" +#include "Ethernet.h" +#include "ArduinoJson.h" +#include "YoulessMonitor.h" + +/* CONSTRUCTOR */ +YoulessMonitor::YoulessMonitor(IPAddress host, uint8_t conType, uint8_t port) +{ + _host = host; + _port = port; + switch (conType) + { + case 0: + _client = new WiFiClient; + break; + + case 1: + _client = new EthernetClient; + break; + } + this->_Connect(); +} + +/* DESTRUCTOR */ +YoulessMonitor::~YoulessMonitor() +{ + /* Not Implemented */ +} + +/* Public Functions*/ +// Get basic status from youless +DynamicJsonDocument YoulessMonitor::getBasicStatus() +{ + unsigned long now = millis(); + String Jsonbuffer; + DynamicJsonDocument doc(256); + if (now - _lastBasicStatus >= _updateTimeOut) + { + Jsonbuffer = this->_GetResponse("/a?f=j"); + } + else + { + Jsonbuffer = _basicStatusString; + } + deserializeJson(doc, Jsonbuffer); + if (doc["Status"] == false) + { + deserializeJson(doc, _basicStatusString); + } + else + { + _basicStatusString = Jsonbuffer; + } + return doc; +} + +// Get energy count from youless +float YoulessMonitor::readCounter() +{ + DynamicJsonDocument doc(256); + doc = this->getBasicStatus(); + String tempCount = doc["cnt"]; + tempCount.trim(); + tempCount.replace(",", "."); + return tempCount.toFloat(); +} + +// Get power from youless +long YoulessMonitor::readPower() +{ + DynamicJsonDocument doc(256); + doc = this->getBasicStatus(); + return doc["pwr"]; +} + +// Check if connection between youless en p1-port is OK +bool YoulessMonitor::readConnectionStatus() +{ + DynamicJsonDocument doc(256); + doc = this->getBasicStatus(); + if (doc["con"] == "OK") + { + return true; + } + else + { + return false; + } +} + +// Get uploaded values +DynamicJsonDocument YoulessMonitor::getUploadedValues() +{ + unsigned long now = millis(); + String Jsonbuffer; + DynamicJsonDocument doc(256); + if (now - _lastUploadedValues >= _uploadTimeOut) + { + Jsonbuffer = this->_GetResponse("/e"); + } + else + { + Jsonbuffer = _uploadedValuesString; + } + deserializeJson(doc, Jsonbuffer); + if (doc["Status"] == false) + { + deserializeJson(doc, _uploadedValuesString); + } + else + { + _uploadedValuesString = Jsonbuffer; + } + return doc[0]; +} + +// Read the consumption counter +float YoulessMonitor::readConsumptionCounter(int tarrif) +{ + return this->_readTarrifCounter(tarrif, false); +} + +// Read the production counter +float YoulessMonitor::readProductionCounter(int tarrif) +{ + return this->_readTarrifCounter(tarrif, true); +} + +// Read the gas counter +float YoulessMonitor::readGasCounter() +{ + DynamicJsonDocument doc(256); + doc = this->getUploadedValues(); + return doc["gas"]; +} + +/* Private Functions */ +// Check for connection, if not connected try to connect +bool YoulessMonitor::_Connect() +{ + if (_client->connected()) + { + return true; + } + else + { + _client->connect(_host, _port); + if (_client->connected()) + { + return true; + } + else + { + return false; + } + } +} + +// Try to get a response from the server +String YoulessMonitor::_GetResponse(String input) +{ + DynamicJsonDocument doc(128); + if (this->_Connect()) + { + String line; + _client->print(String("GET ") + input + " HTTP/1.1\r\n" + "Host: " + _host.toString() + "\r\n" + "Connection: close\r\n\r\n"); + delay(10); + while (_client->available()) + { + line = _client->readStringUntil('\r'); + } + line.trim(); + if (line != "") + { + return line; + } + } + //Serial.println("No connection available!"); + doc["Status"] = false; + String retData; + serializeJson(doc, retData); + return retData; +} + +// Read the tarrif counter false = consumption, true = production +float YoulessMonitor::_readTarrifCounter(int tarrif, bool select) +{ + float retValue = 0; + DynamicJsonDocument doc(256); + doc = this->getUploadedValues(); + switch (tarrif) + { + case -1: + float temp; + if (select) + { + temp = doc["n1"]; + retValue = doc["n2"]; + retValue += temp; + } + else + { + temp = doc["p1"]; + retValue = doc["p2"]; + retValue += temp; + } + break; + + case 0: + if (select) + { + retValue = doc["n1"]; + } + else + { + retValue = doc["p1"]; + } + break; + + case 1: + if (select) + { + retValue = doc["n2"]; + } + else + { + retValue = doc["p2"]; + } + break; + + default: + retValue = -1; + break; + } + return retValue; +} \ No newline at end of file diff --git a/src/YoulessMonitor.h b/src/YoulessMonitor.h new file mode 100644 index 0000000..17b3c1b --- /dev/null +++ b/src/YoulessMonitor.h @@ -0,0 +1,64 @@ +// MY OWN LICENSE // + +// INCLUDED LICENSES // +/* +The MIT License (MIT) +--------------------- + +Copyright © 2014-2020 Benoit BLANCHON + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef YOULESSMONITOR_H +#define YOULESSMONITOR_H + +class Client; + +#include +#include +#include + +#define WIRELESS 0 +#define WIRED 1 + +class YoulessMonitor +{ +public: + // Constructor and Destructor + YoulessMonitor(IPAddress host, uint8_t conType, uint8_t port = 80); + ~YoulessMonitor(); + // Public variables + // Public functions + DynamicJsonDocument getBasicStatus(); + float readCounter(); + long readPower(); + bool readConnectionStatus(); + DynamicJsonDocument getUploadedValues(); + float readConsumptionCounter(int tarrif = -1); + float readProductionCounter(int tarrif = -1); + float readGasCounter(); + +private: + // Private variables + IPAddress _host; + uint8_t _port; + String _basicStatusString; + String _uploadedValuesString; + const unsigned long _updateTimeOut = 5000; + const unsigned long _uploadTimeOut = 30 * 1000; + unsigned long _lastBasicStatus = 0; + unsigned long _lastUploadedValues = 0; + // Private functions + bool _Connect(); + String _GetResponse(String input); + float _readTarrifCounter(int tarrif, bool select); + // Private classes + Client *_client; +}; + +#endif \ No newline at end of file