-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino Code(C++)
More file actions
85 lines (61 loc) · 1.72 KB
/
Copy pathArduino Code(C++)
File metadata and controls
85 lines (61 loc) · 1.72 KB
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
#include <DHT.h>
#include <SoftwareSerial.h>
#define DHTPIN 2
#define DHTTYPE DHT11
#define VIBRATION_PIN A0
#define SOUND_PIN A1
DHT dht(DHTPIN, DHTTYPE);
// WiFi module pins
SoftwareSerial esp(9, 10); // RX, TX
String apiKey = "YOUR_THINGSPEAK_API_KEY";
String ssid = "YOUR_WIFI_NAME";
String password = "YOUR_WIFI_PASSWORD";
void setup() {
Serial.begin(9600);
esp.begin(9600);
dht.begin();
connectWiFi();
}
void loop() {
float temperature = dht.readTemperature();
int vibration = analogRead(VIBRATION_PIN);
int sound = analogRead(SOUND_PIN);
String status = checkStatus(temperature, vibration, sound);
sendData(temperature, vibration, sound, status);
delay(15000); // ThingSpeak allows update every 15 sec
}
String checkStatus(float temp, int vib, int sound) {
int alertCount = 0;
if (temp > 50) alertCount++;
if (vib > 600) alertCount++;
if (sound > 500) alertCount++;
if (alertCount >= 2)
return "Critical";
else if (alertCount == 1)
return "Warning";
else
return "Healthy";
}
void connectWiFi() {
esp.println("AT");
delay(2000);
esp.println("AT+CWMODE=1");
delay(2000);
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
esp.println(cmd);
delay(5000);
}
void sendData(float temp, int vib, int sound, String status) {
String data = "GET /update?api_key=" + apiKey +
"&field1=" + String(temp) +
"&field2=" + String(vib) +
"&field3=" + String(sound) +
"&field4=" + status;
esp.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80");
delay(2000);
esp.println("AT+CIPSEND=" + String(data.length() + 4));
delay(2000);
esp.println(data);
delay(2000);
esp.println("AT+CIPCLOSE");
}