Advertisement
josefkyrian

arduino fan controller

Jun 2nd, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <config_sketch.h>
  2. #include <libespecko.h>
  3.  
  4.  
  5. float temperature = 0, optimalTemperature, fanPWM;
  6.  
  7. //pid settings and gains
  8. float Kp = 2, Ki = 5, Kd = 1;
  9. QuickPID inverterFanPID(&temperature, &fanPWM, &optimalTemperature, Kp, Ki, Kd, QuickPID::Action::reverse);
  10.  
  11.  
  12. class Application_t : public wattomic::core::Application_t {
  13. public:
  14.     double lastUpdatePIDTime = 0;
  15.  
  16.     virtual void setup()
  17.     {
  18.         /*//if temperature is more than 4 degrees below or above setpoint, OUTPUT will be set to min or max respectively
  19.         inverterFanPID.setBangBang(4);
  20.         //set PID update interval to 4000ms
  21.         inverterFanPID.setTimeStep(1000);
  22.         */
  23.         inverterFanPID.SetOutputLimits(0, 1024);
  24.         inverterFanPID.SetSampleTimeUs(100000);
  25.         inverterFanPID.SetMode(QuickPID::Control::automatic);
  26.  
  27.         optimalTemperature = 35.0;
  28.     }
  29.    
  30.     virtual void start()
  31.     {
  32.     }
  33.  
  34.     virtual void loop()
  35.     {
  36.         this->updatePID();
  37.     }
  38.  
  39.     void updatePID()
  40.     {
  41.         if (inverterFanPID.Compute()) {
  42.             LOG_INFO("PWM ", fanPWM);
  43.             analogWrite(D6, fanPWM);
  44.             analogWrite(D7, fanPWM);
  45.         }
  46.  
  47.         if (wattomic::common::seconds() - lastUpdatePIDTime >= 0.5) {
  48.             lastUpdatePIDTime = wattomic::common::seconds();
  49.             //inverterFanPID.run(); //call every loop, updates automatically at certain time interval
  50.             LOG_INFO("PWM ", fanPWM);
  51.             analogWrite(D6, fanPWM);
  52.             analogWrite(D7, fanPWM);
  53.         }
  54.     }
  55.  
  56.     virtual bool doAction(const String& action, JsonObject params, JsonObject result, std::function<void()> finishCallback) {
  57.         if (action == "setOptimalTemperature") {
  58.             if (params.containsKey("value")) {
  59.                 optimalTemperature = params["value"].as<double>();
  60.                 this->updatePID();
  61.                
  62.                 return wattomic::common::replyServerOK(finishCallback, result);
  63.             }else {
  64.                 return wattomic::common::replyServerError(finishCallback, result, "Missing mandatory param 'value'");
  65.             }
  66.         }else {
  67.             return false;
  68.         }
  69.         return true;
  70.     }
  71.  
  72.     virtual bool onAddNotification(wattomic::notification::EventNotification_t::Ptr notification)
  73.     {
  74.         if (notification->getEventName() == "pin.TEMPERATURE.DS18B20.temperatureChanged") {
  75.             auto *n = notification->as<::wattomic::pin::application::apps::SensorDS18B20::TemperatureChangedNotification_t>();
  76.             temperature = n->temperature;
  77.             LOG_INFO("temp ", temperature);
  78.             this->updatePID();
  79.         }
  80.         return true;
  81.     }
  82. };
  83.  
  84. void setup()
  85. {
  86.     especko::core::setup(new Application_t(),
  87.         R"(
  88.             {
  89.                 "config" : {
  90.                     "general" : {
  91.                         "hostname" : "smarthouse-controller-technical-room-cooling",
  92.                         "serialLoggingEnabled" : true,
  93.                         "logRpcRequests" : true,
  94.                         "logNotifications" : true,
  95.                         "logBufferSize" : 5000
  96.                     },
  97.                     "wifi" : {
  98.                         "ssid" : "SSID",
  99.                         "password" : "PWD"
  100.                     },
  101.                     "notification" : {
  102.                         "connection" : {
  103.                             "type" : "wifi",
  104.                             "wifi" : {
  105.                                 "url" : "http://HOSTNAME/notify-technical-room-cooling"
  106.                             }
  107.                         }
  108.                     },
  109.                     "PWM" : {
  110.                         "frequency" : 1,
  111.                         "range" : 1024
  112.                     }
  113.                 }
  114.             }
  115.         )",
  116.         R"(
  117.             {
  118.                 "FAN1" : {
  119.                     "pin" : "D6",
  120.                     "mode" : "PWM",
  121.                     "value" : 0,
  122.                     "application" : {
  123.                     }
  124.                 },
  125.                 "FAN2" : {
  126.                     "pin" : "D7",
  127.                     "mode" : "PWM",
  128.                     "value" : 0,
  129.                     "application" : {
  130.                     }
  131.                 },
  132.                 "TEMPERATURE" : {
  133.                     "pin" : "D1",
  134.                     "application" : {
  135.                         "DS18B20" : {
  136.                             "deviceIndex" : 0,
  137.                             "interval" : 5,
  138.                             "temperature" : {
  139.                                 "changeThreshold" : 0.05,
  140.                                 "forcedNotificationPeriod" : 60.0
  141.                             }
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.         )"
  147.     );
  148. }
  149.  
  150.  
  151. void loop()
  152. {
  153.     especko::core::loop();
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement