-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.ino
132 lines (106 loc) · 3.23 KB
/
sketch.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
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 <DHT.h>
#include "WiFiEsp.h"
#include "SoftwareSerial.h"
// Pins for SoftwareSerial
SoftwareSerial softserial(A9, A8); // RX, TX
// WiFi network credentials
char ssid[] = "ssid";
char pass[] = "12345678";
// Server details
const char* serverName = "192.168.100.132"; // Replace with your machine's local network IP address
const int serverPort = 3000;
int status = WL_IDLE_STATUS;
WiFiEspClient client;
#define PIR_SENSOR_PIN 2 // Digital pin 2 for the PIR sensor
// Define the DHT sensor pin and type
#define DHTPIN 3 // Digital pin 3
#define DHTTYPE DHT11 // DHT 11
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
softserial.begin(115200);
softserial.write("AT+CIOBAUD=9600\r\n");
softserial.write("AT+RST\r\n");
softserial.begin(9600);
// Initialize ESP module
WiFi.init(&softserial);
// Check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true);
}
// Attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
}
Serial.println("You're connected to the network");
printWifiStatus();
// Initialize the PIR sensor pin
pinMode(PIR_SENSOR_PIN, INPUT);
// Initialize the DHT sensor
dht.begin();
}
void loop() {
// Read sensor values
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int pirValue = digitalRead(PIR_SENSOR_PIN);
// Send sensor data to the server
sendSensorData(temperature, humidity, pirValue);
// Wait for 5 seconds before sending next data
delay(5000);
}
void sendSensorData(float temperature, float humidity, int pirValue) {
if (client.connect(serverName, serverPort)) {
Serial.println("Connected to server");
// Create the URL with sensor values
String url = "/api/sensor-data?";
url += "roomNumber=10&temperature=";
url += temperature;
url += "&humidity=";
url += humidity;
url += "&soundLevel=";
url += pirValue;
url += "&airQuality=Good&comfort=4";
// Make a HTTP GET request
client.print("GET ");
client.print(url);
client.print(" HTTP/1.1\r\n");
client.print("Host: ");
client.print(serverName);
client.print(":");
client.print(serverPort);
client.print("\r\n");
client.print("Connection: close\r\n");
client.print("\r\n");
// Wait for a response from the server
while (client.available()) {
char c = client.read();
Serial.print(c);
}
// Close the connection
client.stop();
Serial.println("Disconnected from server");
} else {
Serial.println("Connection to server failed");
}
}
void printWifiStatus() {
// Print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// Print where to go in the browser
Serial.println();
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
Serial.println();
}