我最近一直在做项目,我很难解决这个问题。在我点击 esp8266 中的 rfid 卡后,它发送数据正常,但之后串行监视器显示代码 302 问题,现在我无法再次点击我的卡,它完全停止程序。我一直在工作的逻辑是,如果用户或卡点击 rfid 传感器,它将放入 4 列。在 A 列中,它将生成日期和 B 列时间或超时 我希望如果用户点击一次,它将像这样输入“TIME IN (8:30 AM)”,如果是下午“TIME IN (8:30 PM) )”它会像这样,如果用户再次点击我想发送另一个数据以使其在另一行中成为超时逻辑。
//Arduino Code
#include <SPI.h>
#include <MFRC522.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <TimeLib.h> // Include library to work with time
#define RST_PIN D3
#define SS_PIN D4
#define BUZZER D8
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
bool isTimeIn = true; // Variable to track if it's a Time In or Time Out tap
byte readBlockData[18]; // Declare byte array for reading block data
byte bufferLen = 18; // Buffer length for reading block data
String card_holder_name;
const String sheet_url = "https://script.google.com/macros/s//exec";
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
void setup()
{
Serial.begin(9600);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(200);
}
pinMode(BUZZER, OUTPUT);
SPI.begin();
mfrc522.PCD_Init(); // Initialize the RFID reader once during setup
}
void loop()
{
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
ReadDataFromBlock(2, readBlockData); // Read block 2 from the RFID card
String current_time = getFormattedTime(); // Get the formatted time
String time_status = (isTimeIn) ? "TIME IN (" + current_time + ")" : "TIME OUT (" + current_time + ")";
isTimeIn = !isTimeIn; // Toggle between Time In and Time Out
if (WiFi.status() == WL_CONNECTED) {
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
client->setInsecure();
HTTPClient https;
String postData = "{\"name\":\"" + String((char*)readBlockData) + "\", \"time_status\":\"" + time_status + "\", \"location\":\"Computer Lab\"}";
if (https.begin(*client, sheet_url)) {
https.addHeader("Content-Type", "application/json");
int httpCode = https.POST(postData);
if (httpCode > 0) {
Serial.printf("[HTTPS] POST... code: %d\n", httpCode);
}
https.end();
}
}
delay(1000);
}
void ReadDataFromBlock(int blockNum, byte readBlockData[]) {
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF; // Default key for RFID cards
}
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
if (status == MFRC522::STATUS_OK) {
status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen); // Use bufferLen for the size of data
if (status != MFRC522::STATUS_OK) {
Serial.println("Failed to read block.");
}
} else {
Serial.println("Authentication failed.");
}
}
String getFormattedTime() {
int hour = hourFormat12(); // 12-hour format
String meridiem = isAM() ? "AM" : "PM";
String minuteString = (minute() < 10) ? "0" + String(minute()) : String(minute()); // Correct use of minute()
return String(hour) + ":" + minuteString + " " + meridiem;
}
//Google Script Code
function doPost(e) {
try {
// Open the Google Sheet using its ID
var sheet = SpreadsheetApp.openById("1cPUd1xJwk_RXOlhZQZJUkFp6dK-dOfYOOUgVKEJzmCU").getSheetByName("Sheet1");
// Parse the incoming request payload
var data = JSON.parse(e.postData.contents);
// Get the current date
var currentDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy");
// Get the time status (TIME IN or TIME OUT), location, and name from the request
var timeStatus = data.time_status; // Format: TIME IN (08:26 AM) or TIME OUT (08:26 PM)
var location = data.location; // Example: "Computer Lab"
var name = data.name; // Name from RFID card data
// Append a new row with the data: [Date, Time Status, Location, Name]
sheet.appendRow([currentDate, timeStatus, location, name]);
// Send a success response back to the ESP8266
return ContentService.createTextOutput("Success");
} catch (error) {
// If an error occurs, return it in the response
return ContentService.createTextOutput("Error: " + error.message);
}
}
HTTP Code 302 不报告错误,它是服务器重定向到新位置。