Skip to content

Commit

Permalink
Get rid of Blynk + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristty committed May 4, 2021
1 parent 5d7c43f commit 59f8951
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 310 deletions.
1 change: 0 additions & 1 deletion include/secrets.h.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define ORANGE_SSID "orange-tram"
#define ASUS_SSID "tram4-2g"

const char BLYNK_AUTH[] = "blabla124324234234234223dsfassa";
const char WIFI_SSID[] = ASUS_SSID;
const char WIFI_PSK[] = "access-point-password";

Expand Down
1 change: 0 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ monitor_dtr = 0

lib_deps =
ezTime@0.8.3
https://github.com/Kristty/blynk-library.git#v0.6.1-linker-patch
DHT sensor library for ESPx@1.17
https://github.com/jfturcot/SimpleTimer.git#b30890b8f7046bf3b0258e5870e5dda78ac5c71a
53 changes: 0 additions & 53 deletions src/BlynkManager.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions src/BlynkManager.h

This file was deleted.

41 changes: 11 additions & 30 deletions src/DHTDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

#include "DHTDevice.h"

void DHTDevice::init(uint8_t pin, uint8_t blynkTempPin) {
DHTDevice::DHTDevice(uint8_t pin) {
this->pin=pin;
pinMode(pin, INPUT_PULLUP);
dht.setup(pin, DHT_MODEL);
dhtReadInterval = (dht.getMinimumSamplingPeriod());
Serial.printf((char*)F("DHT sensor read interval is: %u\n"), dhtReadInterval);
Serial.printf((char*)F("DHT sensor read interval is: %ums\n"), dhtReadInterval);
}

void DHTDevice::init(uint8_t pin, uint8_t blynkTempPin, uint8_t blynkRhPin) {
init(pin, blynkTempPin);
this->blynkTempPin = blynkTempPin;
this->blynkRhPin = blynkRhPin;
};

/************ LOGGING ************/
// TODO: candidate for debug logging
// Serial.print("DHT " + String(dht.getStatusString()));
Expand All @@ -28,37 +23,23 @@ void DHTDevice::init(uint8_t pin, uint8_t blynkTempPin, uint8_t blynkRhPin) {
// Serial.println(rH);
/************ END LOGGING ************/
void DHTDevice::tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_HYSTERESIS) {
float currTemp;
currTemp = dht.getTemperature();
if (isnan(currTemp)) {
temp = dht.getTemperature();
if (isnan(temp)) {
_tempReadErrs++;
Serial.printf((char*)F("DHT: Failed to read temperature!\n\tcurrent value:\t\t\t%f\n\tlast valid value:\t\t%f\n"), currTemp, temp);
Serial.printf((char*)F("DHT: Failed to read temperature! Device status: %s\n"), dht.getStatusString());
return;
}
temp = currTemp;
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, device->pin, device->led);
// BlynkManager::sendTempToBlynk(temp, blynkTempPin);
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, this->pin);
}

void DHTDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERESIS) {
float currRH;
currRH = dht.getHumidity();
if (isnan(currRH)) {
rH = dht.getHumidity();
if (isnan(rH)) {
_rHReadErrs++;
Serial.printf((char*)F("DHT: Failed to read humidity!\n\tcurrent value:\t\t\t%f\n\tlast valid value:\t\t%f\n"), currRH, rH);
Serial.printf((char*)F("DHT: Failed to read humidity! Device status: %s\n"), dht.getStatusString());
return;
}
rH = currRH;
PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, device->pin, device->led);
// BlynkManager::sendRhToBlynk(rH, blynkRhPin);
}

void DHTDevice::powerOn() {

}

void DHTDevice::powerOff() {

PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, this->pin);
}

String DHTDevice::status() {
Expand Down
6 changes: 1 addition & 5 deletions src/DHTDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@
#include <DHTesp.h>
#include "Device.h"
#include "MetricsCollectable.h"
// #include "BlynkManager.h"

#define DHT_MODEL DHTesp::DHT22
#define DHT_NAME "DHT22"

class DHTDevice: public Device, MetricsCollectable {

public:
uint16_t blynkTempPin, blynkRhPin;
uint16_t dhtReadInterval;
float temp, rH;

void init(uint8_t pin, uint8_t blynkTempPin);

void init(uint8_t pin, uint8_t blynkTempPin, uint8_t blynkRhPin);
DHTDevice(uint8_t pin);

void tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_HYSTERESIS);

Expand Down
25 changes: 6 additions & 19 deletions src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,23 @@

#include "Device.h"

Device::Device() {
};

void Device::init(uint8_t pin, uint8_t blynkPin) {
this->pin = pin;
this->led = new WidgetLED(blynkPin);

pinMode(pin, OUTPUT);

isPowerOn = PowerManager::manualPower(true, pin, led);
isAutoPowerOn = true;
};

void Device::powerOn() {
isPowerOn = PowerManager::manualPower(true, pin, led);
isAutoPowerOn = true;
this->isPowerOn = PowerManager::manualPowerOn(pin);
this->isAutoPowerOn = true;
}

void Device::powerOff() {
this->isPowerOn = PowerManager::manualPower(false, pin, led);
isAutoPowerOn = false;
this->isPowerOn = PowerManager::manualPowerOff(pin);
this->isAutoPowerOn = false;
}

void Device::scheduledPowerOn(Scheduler scheduler) {
powerOn();
this->powerOn();
scheduler.setNextEvent();
}

void Device::scheduledPowerOff(Scheduler scheduler) {
powerOff();
this->powerOff();
scheduler.setNextEvent();
}

Expand Down
15 changes: 8 additions & 7 deletions src/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
#define DEVICE_H

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

class Device {

public:

Device();

uint8_t pin;
uint8_t blynkPin;
WidgetLED *led;
bool isPowerOn;
bool isAutoPowerOn;

void init(uint8_t pin, uint8_t blynkPin);
Device(){};

Device(uint8_t pin):
pin(pin),
isAutoPowerOn(true) {
pinMode(pin, OUTPUT);
this->isPowerOn = PowerManager::manualPowerOn(pin);
};

void powerOn();

Expand Down
10 changes: 0 additions & 10 deletions src/LDRDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@

#include "LDRDevice.h"

void LDRDevice::init(uint8_t pin, uint8_t blynkPin) {
this->pin = pin;
this->blynkPin = blynkPin;
this->led = new WidgetLED(blynkPin);
uint16_t lightVal = getLightValue();
this->isPowerOn = lightVal < LAMP_ON_VALUE ? true : false;
}

uint16_t LDRDevice::getLightValue() {
return analogRead(pin);
}

void LDRDevice::lampStatus() {
uint16_t lightVal = getLightValue();
uint8_t ledBrightness = map(lightVal, 0, 1023, 255, 0);
// TODO: candidate for debug logging
// Serial.println("LDR sensor: " + String(lightVal));
if (lightVal < LAMP_ON_VALUE && !isPowerOn) {
Expand All @@ -28,7 +19,6 @@ void LDRDevice::lampStatus() {
Serial.println(F("LDR sensor: lamp is off"));
isPowerOn = false;
}
// BlynkManager::sendLampToBlynk(ledBrightness, led);
}


Expand Down
8 changes: 6 additions & 2 deletions src/LDRDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@

#include <Arduino.h>
#include "Device.h"
// #include "BlynkManager.h"

// LDR sensor values. The lower value is the brighter is light.
const uint8_t LAMP_ON_VALUE = 200;
const uint16_t LAMP_OFF_VALUE = 900;

class LDRDevice: public Device {
public:
void init(uint8_t pin, uint8_t blynkPin);

LDRDevice(uint8_t pin) {
this->pin = pin;
uint16_t lightVal = getLightValue();
this->isPowerOn = lightVal < LAMP_ON_VALUE ? true : false;
};

void lampStatus();

Expand Down
33 changes: 13 additions & 20 deletions src/PowerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,22 @@

#include "PowerManager.h"


void PowerManager::autoPower(bool *autoControl, bool *isOn, float *currVal, float maxVal, float valHyst, uint8_t pin, WidgetLED *led) {
void PowerManager::autoPower(bool *autoControl, bool *currentState, float *currVal, float maxVal, float valHyst, uint8_t pin) {
if (!*autoControl) return;
if (*currVal >= maxVal && *isOn) {
*isOn = manualPower(false, pin, led);
if (*currVal >= maxVal && *currentState) {
*currentState = manualPowerOff(pin);
}
else if (*currVal < maxVal - valHyst && !*isOn) {
*isOn = manualPower(true, pin, led);
else if (*currVal < maxVal - valHyst && !*currentState) {
*currentState = manualPowerOn(pin);
}
}

bool PowerManager::manualPower(bool enabled, uint8_t pin, WidgetLED *led) {
bool isOn;
if (enabled) {
digitalWrite(pin, RELAY_ON);
isOn = true;
Serial.println("Blynk: enabling led for pin: " + String(pin));
led->on();
} else {
digitalWrite(pin, RELAY_OFF);
isOn = false;
Serial.println("Blynk: disabling led for pin: " + String(pin));
led->off();
}
return isOn;
bool PowerManager::manualPowerOn(uint8_t pin) {
digitalWrite(pin, RELAY_ON);
return true;
}

bool PowerManager::manualPowerOff(uint8_t pin) {
digitalWrite(pin, RELAY_OFF);
return false;
}
9 changes: 3 additions & 6 deletions src/PowerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
#define RELAY_OFF HIGH

#include <Arduino.h>
#include <BlynkSimpleEsp8266_SSL.h>

class PowerManager {
public:

static void autoPower(bool *autoControl, bool *isOn, float *currVal, float maxVal, float valHyst, uint8_t pin, WidgetLED *led);

static bool manualPower(bool enabled, uint8_t pin, WidgetLED *led);
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
Loading

0 comments on commit 59f8951

Please sign in to comment.