-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHT11.ino
87 lines (70 loc) · 1.9 KB
/
DHT11.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
#include "DHT.h"
#include "Servo.h"
#include "WiFi.h"
#include "MQTT.h"
const char ssid[] = "Wi-Fi";
const char pass[] = "t4np4k4bel";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.println("wifi check ");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect("ESP32", "f191b033", "c870232af9647138")) {
Serial.println("connecting ");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("/DHTData");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}
const int pinDHT = 4;
int servoPin = 25;
Servo newServo;
#define DHTTYPE DHT11 // DHT 11
DHT dht(pinDHT, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
dht.begin();
client.begin("broker.shiftr.io", net);
client.onMessage(messageReceived);
Serial.println(F("DHT11 test!"));
newServo.attach(servoPin);
connect();
}
void loop() {
client.loop();
delay(10);
if(!client.connected()){
connect();
}
int t = dht.readTemperature();
if (isnan(t)) {
Serial.println(F("Gagal membaca suhu !"));
return;
}
if(t<35){
newServo.write(0);
client.publish("/DHTData/Alert", "Suhu dibawah 35 derajat celcius");
}
else if(t>=35 && t<45){
newServo.write(90);
client.publish("/DHTData/Alert", "Suhu diatas 35 derajat celcius");
}
else if(t>=45){
newServo.write(180);
client.publish("/DHTData/Alert", "Suhu diatas 45 derajat celcius");
}
Serial.print(t);
Serial.println(F("°C "));
char tString[10];
itoa(t, tString, 10);
client.publish("/DHTData/Suhu", tString);
}