Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement power control over radio transmission #33

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ jobs:
docker:
- image: alpine
steps:
- run:
name: Install packages
command: apk add --no-cache curl docker-cli make openssh git
- checkout
- setup_remote_docker:
version: 20.10.7
docker_layer_caching: true
- run:
name: smoke-test
command: |
apk add --no-cache curl docker-cli make git
if [ "$(git log -1 --format=format:%H -- client/)" == "$CIRCLE_SHA1" ]
then
make client-test
Expand All @@ -25,12 +27,12 @@ jobs:
docker:
- image: alpine
steps:
- run:
name: Install packages
command: apk add --no-cache docker-cli make openssh git
- checkout
- setup_remote_docker:
version: 20.10.7
- run:
name: Install packages
command: apk add --no-cache docker-cli make
- run:
name: Docker image release
command: |
Expand Down
89 changes: 89 additions & 0 deletions include/Settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
*
* Settings.h
*
**/

/*
* !! WARNING !!
*
* !!! DO NOT USE pins 5(D1),4(D2)
* because they are reserved
* for I2C bus SCL,SDA !!!
*
* !! WARNING !!
*/

#ifndef SETTINGS_H
#define SETTINGS_H

#include <Arduino.h>

#ifndef CUSTOM_NETWORK
#define IP_ADDRESS "192.168.1.30"
#define SUBNET "255.255.255.0"
#define GATEWAY "192.168.1.1"
#define DNS1 "192.168.1.4"
#define DNS2 "192.168.1.1"
#define HOSTNAME "arduino-grower"
#else
#define IP_ADDRESS "192.168.1.31"
#define SUBNET "255.255.255.0"
#define GATEWAY "192.168.1.1"
#define DNS1 "192.168.1.4"
#define DNS2 "192.168.1.1"
#define HOSTNAME "test-arduino-grower"
#endif

#define WEB_SERVER_PORT 80
#define RADIO_POWER // Enable power control over radio transmission

#ifdef RADIO_POWER
#define TX_PIN 2 // D4 - orange
#define RCSWITCH_PROTOCOL 13
/*
* ORNO OR-AE-13132(GS) power extender codes
*/
#define MSG_LENGTH 24

#define OUTLET1_ON 0x15533
#define OUTLET1_OFF 0x1553C
#define OUTLET2_ON 0x155C3
#define OUTLET2_OFF 0x155CC
#define OUTLET3_ON 0x15703
#define OUTLET3_OFF 0x1570C
#define OUTLET4_ON 0x15D03
#define OUTLET4_OFF 0x15D0C
#define OUTLET5_ON 0x17503
#define OUTLET5_OFF 0x1750C
#define SSR_OUTLET_ON 0x17703
#define SSR_OUTLET_OFF 0x1770C

#define HUM_ON_CODE SSR_OUTLET_ON
#define HUM_OFF_CODE SSR_OUTLET_OFF
#define FAN_ON_CODE OUTLET3_ON
#define FAN_OFF_CODE OUTLET3_OFF
#define LAMP_ON_CODE OUTLET4_ON
#define LAMP_OFF_CODE OUTLET4_OFF
#else
#define LAMPRELAYPIN 0 // white/light-brown
#define HUMRELAYPIN 2 // violet
#define FANRELAYPIN 16 // white
#endif

#define PUMPPIN 14 // D5 - blue
#define LDRPIN A0 // dark-brown/blue

// TODO: replace const's with define's
/* monitoring constants */
const uint8_t MAX_TEMP = 40;
const uint8_t TEMP_HYSTERESIS = 10;
// Recommended RH values:
// - vegetative - 60%
// - flowering - 50%
const uint8_t MAX_RH = 70;
const uint8_t RH_HYSTERESIS = 30;
// lamp check interval in seconds
const uint8_t LIGHT_CHECK_INTERVAL = 10;

#endif
2 changes: 1 addition & 1 deletion include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
#ifndef VERSION_H
#define VERSION_H

#define VERSION "0.10.0"
#define VERSION "0.12.1"

#endif
12 changes: 11 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:d1_mini]
[platformio]
default_envs = prod

[env]
platform = espressif8266
board = d1_mini
framework = arduino
Expand All @@ -25,3 +28,10 @@ lib_deps =
DHT sensor library for [email protected]
https://github.com/jfturcot/SimpleTimer.git#b30890b8f7046bf3b0258e5870e5dda78ac5c71a
https://github.com/enjoyneering/HTU2xD_SHT2x_Si70xx.git#5a76484f6fd0504125851cc23791f0e2a73e6dae
https://github.com/vi7/rc-switch.git#149eba564821e586ef1c221ff8eef1582672bccd

[env:prod]

[env:test]
build_flags =
-D CUSTOM_NETWORK
8 changes: 8 additions & 0 deletions src/DHTDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ void DHTDevice::tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_H
Serial.printf((char*)F("DHT: Failed to read temperature! Device status: %s\n"), dht.getStatusString());
return;
}
#ifdef RADIO_POWER
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, &device->_onCode, &device->_offCode);
#else
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, &device->_pin);
#endif
}

void DHTDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERESIS) {
Expand All @@ -31,7 +35,11 @@ void DHTDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERE
Serial.printf((char*)F("DHT: Failed to read humidity! Device status: %s\n"), dht.getStatusString());
return;
}
#ifdef RADIO_POWER
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, &device->_onCode, &device->_offCode);
#else
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, &device->_pin);
#endif
}

String DHTDevice::status() {
Expand Down
1 change: 1 addition & 0 deletions src/DHTDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <Arduino.h>
#include <DHTesp.h>
#include "Settings.h"
#include "Device.h"
#include "MetricsCollectable.h"

Expand Down
8 changes: 8 additions & 0 deletions src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
#include "Device.h"

void Device::powerOn() {
#ifdef RADIO_POWER
this->isPowerOn = PowerManager::manualPowerOn(&_onCode);
#else
this->isPowerOn = PowerManager::manualPowerOn(_pin);
#endif
this->isAutoPowerOn = true;
}

void Device::powerOff() {
#ifdef RADIO_POWER
this->isPowerOn = PowerManager::manualPowerOff(&_offCode);
#else
this->isPowerOn = PowerManager::manualPowerOff(_pin);
#endif
this->isAutoPowerOn = false;
}

Expand Down
15 changes: 14 additions & 1 deletion src/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,37 @@
#define DEVICE_H

#include <Arduino.h>
#include "Settings.h"
#include "PowerManager.h"
#include "scheduler.h"

class Device {

public:
uint8_t _pin;
bool isPowerOn;
bool isAutoPowerOn;
uint8_t _pin;

Device(){};

#ifdef RADIO_POWER
uint32_t _onCode;
uint32_t _offCode;

Device(uint32_t onCode, uint32_t offCode):
isAutoPowerOn(true),
_onCode(onCode),
_offCode(offCode) {
this->isPowerOn = PowerManager::manualPowerOn(&onCode);
};
#else
Device(uint8_t pin):
_pin(pin),
isAutoPowerOn(true) {
pinMode(pin, OUTPUT);
this->isPowerOn = PowerManager::manualPowerOn(pin);
};
#endif

void powerOn();

Expand Down
9 changes: 8 additions & 1 deletion src/HTU2xDDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ void HTU2xDDevice::tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEM
htu2xD->setResolution(HUMD_12BIT_TEMP_14BIT); //humidity 12-bit, temperature 14-bit
return;
}
#ifdef RADIO_POWER
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, &device->_onCode, &device->_offCode);
#else
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, &device->_pin);
// TODO: add delay(500) as in the HTU2xD_SHT2x_Si70xx lib example?
#endif
}

void HTU2xDDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERESIS) {
Expand All @@ -29,7 +32,11 @@ void HTU2xDDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYST
Serial.printf((char*)F("%s: Failed to read humidity! CRC8 or communication error occurred\n"), HTU2XD_NAME);
return;
}
#ifdef RADIO_POWER
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, &device->_onCode, &device->_offCode);
#else
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, &device->_pin);
#endif
}

String HTU2xDDevice::status() {
Expand Down
1 change: 1 addition & 0 deletions src/HTU2xDDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <Arduino.h>
#include <HTU2xD_SHT2x_Si70xx.h>
#include "Settings.h"
#include "Device.h"
#include "MetricsCollectable.h"

Expand Down
28 changes: 25 additions & 3 deletions src/PowerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@


#include "PowerManager.h"
#ifdef RADIO_POWER
void PowerManager::autoPower(bool *autoControl, bool *currentState, float *currVal, float maxVal, float valHyst,
uint32_t *onCode, uint32_t *offCode) {
if (!*autoControl) return;
if (*currVal >= maxVal) {
*currentState = manualPowerOff(offCode);
}
else if (*currVal < maxVal - valHyst) {
*currentState = manualPowerOn(onCode);
}
}

bool PowerManager::manualPowerOn(uint32_t *onCode) {
PowerManager::transmitter.send(*onCode, MSG_LENGTH);
return true;
}

bool PowerManager::manualPowerOff(uint32_t *offCode) {
PowerManager::transmitter.send(*offCode, MSG_LENGTH);
return false;
}
#else
void PowerManager::autoPower(bool *autoControl, bool *currentState, float *currVal, float maxVal, float valHyst, uint8_t *pin) {
if (!*autoControl) return;
if (*currVal >= maxVal && *currentState) {
if (*currVal >= maxVal) {
*currentState = manualPowerOff(*pin);
}
else if (*currVal < maxVal - valHyst && !*currentState) {
else if (*currVal < maxVal - valHyst) {
*currentState = manualPowerOn(*pin);
}
}
Expand All @@ -23,4 +44,5 @@ bool PowerManager::manualPowerOn(uint8_t pin) {
bool PowerManager::manualPowerOff(uint8_t pin) {
digitalWrite(pin, RELAY_OFF);
return false;
}
}
#endif
11 changes: 11 additions & 0 deletions src/PowerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@
#define RELAY_OFF HIGH

#include <Arduino.h>
#include <RCSwitch.h>

#include "Settings.h"

class PowerManager {
public:
static RCSwitch transmitter;
#ifdef RADIO_POWER
static void autoPower(bool *autoControl, bool *isOn, float *currVal, float maxVal, float valHyst,
uint32_t *onCode, uint32_t *offCode);
static bool manualPowerOn(uint32_t *onCode);
static bool manualPowerOff(uint32_t *offCode);
#else
static void autoPower(bool *autoControl, bool *isOn, float *currVal, float maxVal, float valHyst, uint8_t *pin);
static bool manualPowerOn(uint8_t pin);
static bool manualPowerOff(uint8_t pin);
#endif

};

Expand Down
Loading