-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.c
151 lines (123 loc) · 4.38 KB
/
temp.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <Wire.h>
#include <Adafruit_AHTX0.h>
#include <WiFi.h>
#include <HTTPClient.h>
// Initialize the AHT10 sensor
Adafruit_AHTX0 aht;
// Wi-Fi credentials
const char* ssid = "Hackathon"; // Replace with your WiFi SSID
const char* password = "Intel@Kp6!3t"; // Replace with your WiFi password
// Flask server URL
const char* serverUrl = "http://172.168.72.89:5000/upload_data"; // Replace with your Flask server IP and port
// Optional: API Key for authentication (set on Flask server)
const char* apiKey = "jobin"; // Replace with your actual API key
// Timing variables
unsigned long previousMillis = 0;
const long interval = 5000; // Interval to send data (milliseconds)
// Retry mechanism variables
const int maxRetries = 3;
const long retryDelay = 2000; // Delay between retries (milliseconds)
void setup() {
// Start serial communication
Serial.begin(115200);
delay(1000); // Allow time for serial monitor to initialize
// Initialize the AHT10 sensor
if (!aht.begin()) {
Serial.println("Could not find AHT10 sensor!");
while (1) {
delay(10); // Halt if sensor is not found
}
}
Serial.println("AHT10 sensor initialized.");
// Connect to Wi-Fi
connectToWiFi();
}
void loop() {
unsigned long currentMillis = millis();
// Check if it's time to send data
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Ensure Wi-Fi is connected
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected. Attempting to reconnect...");
connectToWiFi();
// If reconnection fails, skip sending data this cycle
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Reconnection failed. Will retry in the next cycle.");
return;
}
}
// Read temperature data
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp); // Populate temp with temperature event data
float temperature = temp.temperature;
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Send data to Flask server with retry mechanism
sendTemperatureData(temperature);
}
// Other non-blocking code can be placed here
}
/**
* Connects to the specified Wi-Fi network.
*/
void connectToWiFi() {
Serial.printf("Connecting to WiFi SSID: %s\n", ssid);
WiFi.begin(ssid, password);
unsigned long startAttemptTime = millis();
// Keep trying to connect for 10 seconds
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) {
Serial.print(".");
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to WiFi!");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("\nFailed to connect to WiFi.");
}
}
/**
* Sends temperature data to the Flask server with retries.
* @param temperature The temperature value to send.
*/
void sendTemperatureData(float temperature) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected. Cannot send data.");
return;
}
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Optional: Include API key in the POST data for authentication
String postData = "sensor=temperature&temperature=" + String(temperature) + "&api_key=" + String(apiKey);
bool success = false;
int attempt = 0;
while (attempt < maxRetries && !success) {
Serial.printf("Sending data to server (Attempt %d)...\n", attempt + 1);
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
Serial.printf("HTTP Response code: %d\n", httpResponseCode);
if (httpResponseCode == 200) {
Serial.println("Data sent successfully.");
success = true;
} else {
Serial.printf("Server responded with code: %d\n", httpResponseCode);
}
} else {
Serial.printf("Error sending POST request: %s\n", http.errorToString(httpResponseCode).c_str());
}
if (!success) {
attempt++;
if (attempt < maxRetries) {
Serial.printf("Retrying in %ld ms...\n", retryDelay);
delay(retryDelay);
} else {
Serial.println("Max retries reached. Failed to send data.");
}
}
}
http.end();
}