-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZoho.ino
131 lines (101 loc) · 3.2 KB
/
Zoho.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
#include <HTTPClient.h>
#include <WiFi.h>
#include <WebServer.h>
#include <SPIFFS.h>
WebServer server(80);
String ssid = "";
String password = "";
//const char* apiKey = "https://www.zohoapis.com/crm/v2/functions/Customer_Feedback/actions/execute?auth_type=apikey&zapikey=1003.375bfdf09cba6ab0de8cd66a62085a7c.66ce1b40c27fa54e8b23f271fbde0af9";
const char* apiKey = "https://www.zohoapis.eu/crm/v2/functions/Sample/actions/execute?auth_type=apikey&zapikey=1003.bbb4158972effad1a500d591772c7d75.39665992c94420629e62c4b19f1da321";
const int debounceDelay = 500; // Adjust this value as needed
unsigned long lastDebounceTime = 0;
const char* htmlPage = R"=====(
<!DOCTYPE html>
<html>
<head>
<title>ESP32 Wi-Fi Setup</title>
</head>
<body>
<h2>ESP32 Wi-Fi Setup</h2>
<form action="/get">
SSID: <input type="text" name="ssid"><br><br>
Password: <input type="password" name="pass"><br><br>
<input type="submit" value="Connect">
</form>
</body>
</html>
)=====";
void handleRoot() {
server.send(200, "text/html", htmlPage);
}
void handleFormSubmit() {
ssid = server.arg("ssid");
password = server.arg("pass");
// Attempt to connect to the new Wi-Fi network
WiFi.begin(ssid.c_str(), password.c_str());
// Wait a bit for the connection to establish
int retries = 30;
while (WiFi.status() != WL_CONNECTED && retries--) {
delay(1000);
Serial.print(".");
}
// Check if connected
if(WiFi.status() == WL_CONNECTED) {
server.send(200, "text/plain", "Connected. IP Address: " + WiFi.localIP().toString());
} else {
server.send(200, "text/plain", "Connection failed.");
}
}
void setup() {
Serial.begin(115200);
pinMode(2, INPUT); // GPIO 4 for Button 1
pinMode(4, INPUT); // GPIO 5 for Button 2
pinMode(5, INPUT); // GPIO 6 for Button 3
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Set up the access point
WiFi.softAP("ESP32-Setup", "");
// Register web server handlers
server.on("/", handleRoot);
server.on("/get", handleFormSubmit);
// Start the server
server.begin();
}
void loop() {
// Check each button for button press with debounce
debounceButton(2, "1"); // Button 1
debounceButton(4, "2"); // Button 2
debounceButton(5, "3"); // Button 3
server.handleClient();
}
void debounceButton(int buttonPin, String data) {
// Read the state of the button
int buttonState = digitalRead(buttonPin);
// Check for button press with debounce
if ((buttonState == HIGH) && ((millis() - lastDebounceTime) > debounceDelay)) {
// Button is pressed
Serial.print("Button ");
Serial.print(buttonPin);
Serial.println(" is pressed");
// Send data via HTTP
sendData(data);
lastDebounceTime = millis();
}
}
void sendData(String data) {
String jsonData = "{\"State\":\"" + data + "}";
String url = String(apiKey) + "&State=" + data;
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.println("Error: " + http.errorToString(httpResponseCode));
}
http.end();
}