-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat-dht11-relay
112 lines (93 loc) · 2.61 KB
/
thermostat-dht11-relay
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
107
108
109
110
111
112
/* Sketch for Exhasut Fan Thermostat
* Author: Nick Cross 24/01/2021
*
* Ver A - build and dht code 23/01/2021
* Wer B - addedd relay code and tested 24/01/2021
*
*/
// Import libraries
#include <DHT.h>
#include <Adafruit_Sensor.h>
#include <ESP8266WiFi.h>
#include <InfluxDbClient.h>
// Set dht parameters
#define DHTPIN D4
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
//Set Relay parameters
int RELAYPIN = D7;
// Define application constants and variables
// --------------------------------------------------
// dht
float temperatureData = 0.0;
float humidityData = 0.0;
// relay
int fan = 0;
float triggerTemperature = 25.0;
// Wifi details
const char* ssid = "enarceeoit";
const char* password = "********";
// InfluxDB
//influxdb url
#define INFLUXDB_URL "http://192.168.0.35:8086"
// influxdb database
#define INFLUXDB_DB_NAME "ttndb"
// influxdb user
#define INFLUXDB_USER "ttndb"
// influxdb password
#define INFLUXDB_PASSWORD "5*******"
// influxdb client instance
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD);
// InfluxDBClient client;
Point sensor("thermostat");
void setup(){
// Open perial port
Serial.begin(115200);
// Start DHT
dht.begin();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(".");
}
// Print ESP8266 Local IP Address
Serial.print("Wifi connected. IP Addr: ");
Serial.println(WiFi.localIP());
// Set InfluxDB 1 authentication params
client.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD);
// sensor.addTag("temp", "temp");
// check influxdb connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
// Set relay pin out
pinMode(RELAYPIN, OUTPUT);
}
void loop()
{
humidityData = dht.readHumidity();
temperatureData = dht.readTemperature();
Serial.print("Temp: ");
Serial.println(temperatureData);
Serial.print("Hum: ");
Serial.println(humidityData);
sensor.clearFields();
if (temperatureData > triggerTemperature) {
digitalWrite(RELAYPIN, HIGH);
sensor.addField("fan", 1);
} else {
digitalWrite(RELAYPIN, LOW);
sensor.addField("fan", 0);
}
// Send data to influxdb
sensor.addField("temperature", temperatureData);
sensor.addField("humidity", humidityData);
client.writePoint(sensor);
delay(300000);
}