简介
硬件连接
D1------------红色LED正极(负极串联电阻接地)
D2------------绿色LED正极(负极串联电阻接地)
D4------------外部输入按钮正极
D5------------蓝色LED正极(负极串联电阻接地)
3V------------电源正极
G-------------地线
arduino代码
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h> // 添加这个库用于解析JSON
const char* ssid = "{YOUR_WIFI_SSID}";
const char* password = "{YOUR_WIFI_PASSWORD}";
const int successPin = 4; // 发送成功信号的GPIO
const int failurePin = 5; // 发送失败信号的GPIO
const int buttonPin = 2; // 连接外部按钮的GPIO
const int wifiPin = 14; // 监控WiFi连接状态的GPIO
const unsigned long notificationDuration = 3000; // Duration of notification in milliseconds
volatile bool buttonPressed = false;
unsigned long notificationStartTime = 0;
ICACHE_RAM_ATTR void handleButtonInterrupt() {
buttonPressed = true;
}
void setup() {
Serial.begin(115200);
pinMode(successPin, OUTPUT);
pinMode(wifiPin, OUTPUT);
pinMode(failurePin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
digitalWrite(wifiPin, HIGH);
// Configure button interrupt
attachInterrupt(digitalPinToInterrupt(buttonPin), handleButtonInterrupt, FALLING);
}
bool sendHttpPostRequest(String url, String formData, String checkField, String successValue) {
Serial.println("Sending HTTP POST request...");
// Send HTTP POST request
WiFiClient client; // Declare WiFiClient object
HTTPClient http;
http.begin(client, url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(formData);
// Print HTTP response code and content
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
if (httpResponseCode == 200) {
// Parse JSON data
DynamicJsonDocument jsonDoc(512); // Adjust the size based on your JSON data size
DeserializationError jsonError = deserializeJson(jsonDoc, http.getString());
// Check if parsing was successful
if (jsonError) {
Serial.print("JSON parsing failed! Error code: ");
Serial.println(jsonError.c_str());
http.end();
return false;
}
// Check the value of the specified checkField
String responseValue = jsonDoc[checkField].as<String>();
Serial.print("Response Value: ");
Serial.println(responseValue);
// Check if the response value matches the specified successValue
if (responseValue == successValue) {
http.end();
return true;
}
}
http.end();
return false;
}
void loop() {
if (buttonPressed) {
Serial.println("Button pressed!");
// Check if HTTP POST request was successful
bool postRequestSuccess = sendHttpPostRequest("EXAMPLTE.COM/API", "KEY=VALUE", "result", "success");
// Set output pins based on the request result
if (postRequestSuccess) {
digitalWrite(successPin, HIGH);
digitalWrite(failurePin, LOW);
} else {
digitalWrite(successPin, LOW);
digitalWrite(failurePin, HIGH);
}
// Record the start time of the notification
notificationStartTime = millis();
buttonPressed = false; // Reset button state
}
// Check if the notification should be turned off
if (millis() - notificationStartTime >= notificationDuration) {
digitalWrite(successPin, LOW);
digitalWrite(failurePin, LOW);
}
// 其他的循环代码...
}