-
Notifications
You must be signed in to change notification settings - Fork 2
/
PID-DTH11.ino
106 lines (75 loc) · 2.23 KB
/
PID-DTH11.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* PID USING DTH11 and ESP8266
* ----------------------------
* This a sketch to control a heater using PID with aggressive and conservative tunings.
*
*/
//needed for library
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <DHT.h>
#include <PID_v1.h>
int hu;
int tp;
int sp;
int mpd;
int op;
#define DHTPIN 4
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
#define RelayPin 5
float temp, humi;
//Define the aggressive and conservative Tuning Parameters
double aggKp = 4, aggKi = 10, aggKd = 2;
double consKp = 2, consKi = 5, consKd = 1;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
void setup()
{
Serial.begin(9600);
pinMode(RelayPin, OUTPUT);
dht.begin();
//this is the temperature we want to reach
Setpoint = 26;
//tell the PID to range between 0 and 255
myPID.SetOutputLimits(0, 255);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
humi = dht.readHumidity();
temp = dht.readTemperature();
sp = (Setpoint * 100) / 100; //changes humi to a 2 digit intiger
hu = (humi * 10) / 10; //changes humi to a 2 digit intiger
tp = (temp * 10) / 10; //changes temp to a 2 digit integer
Input = tp;
double gap = abs(Setpoint - Input); //distance away from setpoint
if (gap < 10)
{ //we're close to setpoint, use conservative tuning parameters
myPID.SetTunings(consKp, consKi, consKd);
}
else
{
//we're far from setpoint, use aggressive tuning parameters
myPID.SetTunings(aggKp, aggKi, aggKd);
}
myPID.Compute();
//Serial.print("humidity:");
//Serial.print(hu);
//Serial.print("\t");
Serial.print("temp:");
Serial.print(tp);
Serial.print("\t");
Serial.print("relay pin:");
Serial.print(Output);
Serial.print("\n");
//Serial.print("kp: "); Serial.print(myPID.GetKp()); Serial.print(" ");
//Serial.print("ki: "); Serial.print(myPID.GetKi()); Serial.print(" ");
//Serial.print("kd: "); Serial.print(myPID.GetKd()); Serial.println();
delay(10);
}