-
Notifications
You must be signed in to change notification settings - Fork 0
/
code
132 lines (114 loc) · 2.84 KB
/
code
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
const int DHT_PIN = 15;
#define DHTTYPE DHT11
const char *ssid = "wifi"; // WiFi ssid
const char *password = "alfan12345";
const char *mqtt_server = "test.mosquitto.org";
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
DHT dht(DHT_PIN, DHTTYPE);
const int LED_1_PIN = 2; // Connect LED 1 to pin 2
const int LED_2_PIN = 4; // Connect LED 2 to pin 3
void setup_wifi()
{
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
}
void reconnect()
{
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str()))
{
Serial.println("Connected");
client.publish("/ThinkIOT/Publish", "Welcome");
client.subscribe("/ThinkIOT/Subscribe");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup()
{
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
dht.begin();
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 2000)
{
lastMsg = now;
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
String tempStr = String(temperature, 2);
client.publish("/Thinkitive/temp", tempStr.c_str());
String humStr = String(humidity, 1);
client.publish("/Thinkitive/hum", humStr.c_str());
Serial.print("Temperature: ");
Serial.println(tempStr);
Serial.print("Humidity: ");
Serial.println(humStr);
// Control LEDs based on temperature conditions
if (temperature <= 31)
{
analogWrite(LED_1_PIN, 1); // Turn on LED 1
analogWrite(LED_2_PIN, 0); // Turn off LED 2
}
if (temperature > 32 && temperature < 35)
{
analogWrite(LED_1_PIN, 0); // Turn off LED 1
analogWrite(LED_2_PIN, 1); // Turn on LED 2
}
else
{
analogWrite(LED_1_PIN, 0); // Turn off LED 1
analogWrite(LED_2_PIN, 0); // Turn off LED 2
}
}
}