diff --git a/examples/EnergyMonitor/EnergyMonitor.h b/examples/EnergyMonitor/EnergyMonitor.h new file mode 100644 index 00000000..927bfbcc --- /dev/null +++ b/examples/EnergyMonitor/EnergyMonitor.h @@ -0,0 +1,66 @@ +/* + * This is a part of the Uniot project. + * Copyright (C) 2016-2024 Uniot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include +#include +#include + +#define RX_PIN 4 // GPIO4 +#define TX_PIN 5 // GPIO5 + + +SoftwareSerial pzemSWSerial(RX_PIN, TX_PIN); + +class EnergyMonitor { + public: + EnergyMonitor() { + _setup(); + } + + uint16_t getPower() { + auto power = pzem.power(); + if (isnan(power)) { + UNIOT_LOG_PRINT("EnergyMonitor: Power reading failed"); + return 0; + } + return (uint16_t)power; + } + + uint16_t getVoltage() { + auto voltage = pzem.voltage(); + if (isnan(voltage)) { + UNIOT_LOG_PRINT("EnergyMonitor: Voltage reading failed"); + return 0; + } + return (uint16_t)voltage; + } + + private: + void _setup() { + // GPIO setup. + pinMode(RX_PIN, INPUT); + pinMode(TX_PIN, OUTPUT); + + pzem = PZEM004Tv30(pzemSWSerial); + } + + PZEM004Tv30 pzem; +}; diff --git a/examples/EnergyMonitor/EnergyMonitor.ino b/examples/EnergyMonitor/EnergyMonitor.ino new file mode 100644 index 00000000..7ba6527a --- /dev/null +++ b/examples/EnergyMonitor/EnergyMonitor.ino @@ -0,0 +1,74 @@ +/* + * This is a part of the Uniot project. + * Copyright (C) 2016-2023 Uniot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "EnergyMonitor.h" +#include + +using namespace uniot; +using namespace std; + +EnergyMonitor Monitor; + +Object power_read(Root root, VarObject env, VarObject list) { + auto expeditor = PrimitiveExpeditor::describe(getPrimitiveName(), Lisp::Int, 0) + .init(root, env, list); + auto power = Monitor.getPower(); + return expeditor.makeInt(power); +} +Object voltage_read(Root root, VarObject env, VarObject list) { + auto expeditor = PrimitiveExpeditor::describe(getPrimitiveName(), Lisp::Int, 0) + .init(root, env, list); + auto voltage = Monitor.getVoltage(); + return expeditor.makeInt(voltage); +} + +auto taskPrintHeap = TaskScheduler::make([](short t) { + Serial.println(ESP.getFreeHeap()); +}); + +auto taskPrintTime = TaskScheduler::make([](short t) { + Serial.println(Date::getFormattedTime()); +}); + +void inject() { + auto &MainAppKit = AppKit::getInstance(0, LOW, 2); + + MainAppKit.getLisp().pushPrimitive(power_read); + MainAppKit.getLisp().pushPrimitive(voltage_read); + + MainEventBus.registerKit(&MainAppKit); + + MainScheduler.push(&MainAppKit) + ->push(taskPrintTime) + ->push(taskPrintHeap); + + taskPrintHeap->attach(500); + taskPrintTime->attach(500); + + MainAppKit.begin(); + + UNIOT_LOG_INFO("%s: %s", "CHIP_ID", String(ESP.getChipId(), HEX).c_str()); + UNIOT_LOG_INFO("%s: %s", "DEVICE_ID", MainAppKit.getCredentials().getDeviceId().c_str()); + UNIOT_LOG_INFO("%s: %s", "OWNER_ID", MainAppKit.getCredentials().getOwnerId().c_str()); +} diff --git a/examples/Tomzn1PRelay/Tomzn1PRelay.h b/examples/Tomzn1PRelay/Tomzn1PRelay.h new file mode 100644 index 00000000..76fbf0a0 --- /dev/null +++ b/examples/Tomzn1PRelay/Tomzn1PRelay.h @@ -0,0 +1,59 @@ +/* + * This is a part of the Uniot project. + * Copyright (C) 2016-2024 Uniot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include "CSE7766.h" +#include +#include + +#define RELAY_PIN 13 + +class Tomzn1PRelay { + public: + Tomzn1PRelay() { + _setup(); + } + + auto getPower() { + auto power = cse7766.getActivePower(); + return (uint16_t)power; + } + + auto getVoltage() { + auto voltage = cse7766.getVoltage(); + return (uint16_t)voltage; + } + + void setSupply(bool status) { + digitalWrite(RELAY_PIN, status ? HIGH : LOW); + } + + private: + void _setup() { + cse7766.setRX(1); + cse7766.begin(); + + // GPIO setup. + pinMode(RELAY_PIN, OUTPUT); + digitalWrite(RELAY_PIN, HIGH); + } + + CSE7766 cse7766; +}; diff --git a/examples/Tomzn1PRelay/Tomzn1PRelay.ino b/examples/Tomzn1PRelay/Tomzn1PRelay.ino new file mode 100644 index 00000000..d094320e --- /dev/null +++ b/examples/Tomzn1PRelay/Tomzn1PRelay.ino @@ -0,0 +1,83 @@ +/* + * This is a part of the Uniot project. + * Copyright (C) 2016-2023 Uniot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "Tomzn1PRelay.h" +#include + +using namespace uniot; +using namespace std; + +Tomzn1PRelay Relay; + +Object power_read(Root root, VarObject env, VarObject list) { + auto expeditor = PrimitiveExpeditor::describe(getPrimitiveName(), Lisp::Int, 0) + .init(root, env, list); + auto power = Relay.getPower(); + return expeditor.makeInt(power); +} +Object voltage_read(Root root, VarObject env, VarObject list) { + auto expeditor = PrimitiveExpeditor::describe(getPrimitiveName(), Lisp::Int, 0) + .init(root, env, list); + auto voltage = Relay.getVoltage(); + return expeditor.makeInt(voltage); +} +Object supply_set(Root root, VarObject env, VarObject list) { + auto expeditor = PrimitiveExpeditor::describe(getPrimitiveName(), Lisp::Bool, 1, Lisp::Bool) + .init(root, env, list); + expeditor.assertDescribedArgs(); + auto status = expeditor.getArgBool(0); + Relay.setSupply(status); + return expeditor.makeBool(true); +} + +auto taskPrintHeap = TaskScheduler::make([](short t) { + Serial.println(ESP.getFreeHeap()); +}); + +auto taskPrintTime = TaskScheduler::make([](short t) { + Serial.println(Date::getFormattedTime()); +}); + +void inject() { + auto &MainAppKit = AppKit::getInstance(0, LOW, 2); + + MainAppKit.getLisp().pushPrimitive(power_read); + MainAppKit.getLisp().pushPrimitive(voltage_read); + MainAppKit.getLisp().pushPrimitive(supply_set); + + MainEventBus.registerKit(&MainAppKit); + + MainScheduler.push(&MainAppKit) + ->push(taskPrintTime) + ->push(taskPrintHeap); + + taskPrintHeap->attach(500); + taskPrintTime->attach(500); + + MainAppKit.begin(); + + UNIOT_LOG_INFO("%s: %s", "CHIP_ID", String(ESP.getChipId(), HEX).c_str()); + UNIOT_LOG_INFO("%s: %s", "DEVICE_ID", MainAppKit.getCredentials().getDeviceId().c_str()); + UNIOT_LOG_INFO("%s: %s", "OWNER_ID", MainAppKit.getCredentials().getOwnerId().c_str()); +} diff --git a/platformio.ini b/platformio.ini index ec04112d..01cc08c2 100755 --- a/platformio.ini +++ b/platformio.ini @@ -21,6 +21,8 @@ lib_deps = ./../uniot-lisp ./../uniot-pubsubclient ./../uniot-crypto + ./../PZEM + ./../CSE7766 build_flags = -D UNIOT_CREATOR_ID=\"UNIOT\" -D UNIOT_LOG_ENABLED=1