Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <config_sketch.h>
- #include <libespecko.h>
- float temperature = 0, optimalTemperature, fanPWM;
- //pid settings and gains
- float Kp = 2, Ki = 5, Kd = 1;
- QuickPID inverterFanPID(&temperature, &fanPWM, &optimalTemperature, Kp, Ki, Kd, QuickPID::Action::reverse);
- class Application_t : public wattomic::core::Application_t {
- public:
- double lastUpdatePIDTime = 0;
- virtual void setup()
- {
- /*//if temperature is more than 4 degrees below or above setpoint, OUTPUT will be set to min or max respectively
- inverterFanPID.setBangBang(4);
- //set PID update interval to 4000ms
- inverterFanPID.setTimeStep(1000);
- */
- inverterFanPID.SetOutputLimits(0, 1024);
- inverterFanPID.SetSampleTimeUs(100000);
- inverterFanPID.SetMode(QuickPID::Control::automatic);
- optimalTemperature = 35.0;
- }
- virtual void start()
- {
- }
- virtual void loop()
- {
- this->updatePID();
- }
- void updatePID()
- {
- if (inverterFanPID.Compute()) {
- LOG_INFO("PWM ", fanPWM);
- analogWrite(D6, fanPWM);
- analogWrite(D7, fanPWM);
- }
- if (wattomic::common::seconds() - lastUpdatePIDTime >= 0.5) {
- lastUpdatePIDTime = wattomic::common::seconds();
- //inverterFanPID.run(); //call every loop, updates automatically at certain time interval
- LOG_INFO("PWM ", fanPWM);
- analogWrite(D6, fanPWM);
- analogWrite(D7, fanPWM);
- }
- }
- virtual bool doAction(const String& action, JsonObject params, JsonObject result, std::function<void()> finishCallback) {
- if (action == "setOptimalTemperature") {
- if (params.containsKey("value")) {
- optimalTemperature = params["value"].as<double>();
- this->updatePID();
- return wattomic::common::replyServerOK(finishCallback, result);
- }else {
- return wattomic::common::replyServerError(finishCallback, result, "Missing mandatory param 'value'");
- }
- }else {
- return false;
- }
- return true;
- }
- virtual bool onAddNotification(wattomic::notification::EventNotification_t::Ptr notification)
- {
- if (notification->getEventName() == "pin.TEMPERATURE.DS18B20.temperatureChanged") {
- auto *n = notification->as<::wattomic::pin::application::apps::SensorDS18B20::TemperatureChangedNotification_t>();
- temperature = n->temperature;
- LOG_INFO("temp ", temperature);
- this->updatePID();
- }
- return true;
- }
- };
- void setup()
- {
- especko::core::setup(new Application_t(),
- R"(
- {
- "config" : {
- "general" : {
- "hostname" : "smarthouse-controller-technical-room-cooling",
- "serialLoggingEnabled" : true,
- "logRpcRequests" : true,
- "logNotifications" : true,
- "logBufferSize" : 5000
- },
- "wifi" : {
- "ssid" : "SSID",
- "password" : "PWD"
- },
- "notification" : {
- "connection" : {
- "type" : "wifi",
- "wifi" : {
- "url" : "http://HOSTNAME/notify-technical-room-cooling"
- }
- }
- },
- "PWM" : {
- "frequency" : 1,
- "range" : 1024
- }
- }
- }
- )",
- R"(
- {
- "FAN1" : {
- "pin" : "D6",
- "mode" : "PWM",
- "value" : 0,
- "application" : {
- }
- },
- "FAN2" : {
- "pin" : "D7",
- "mode" : "PWM",
- "value" : 0,
- "application" : {
- }
- },
- "TEMPERATURE" : {
- "pin" : "D1",
- "application" : {
- "DS18B20" : {
- "deviceIndex" : 0,
- "interval" : 5,
- "temperature" : {
- "changeThreshold" : 0.05,
- "forcedNotificationPeriod" : 60.0
- }
- }
- }
- }
- }
- )"
- );
- }
- void loop()
- {
- especko::core::loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement