Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1535

Esp8266 hTTP-Post 302 Problem in Rfid Sensor [closed]

$
0
0

I've been making project lately and im hard stuck to this problem. After i tap the rfid card in esp8266 it send the data okay but after that the serial monitor displaying a code 302 problem and now i can't tap my card again and it stop the program completely. The logic i've been working was if the user or card tap in rfid sensor it will in put in 4 columns. In Column A it will generate the Date and Column B Time in or Time Out I want that if the user tap once it will input like this "TIME IN (8:30 AM)" and if afternoon "TIME IN (8:30 PM)" it will like this and if the the user tap again I want to send another data to make it in another row the TIME OUT logic.

//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   D8MFRC522 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 tapbyte readBlockData[18];  // Declare byte array for reading block databyte bufferLen = 18;     // Buffer length for reading block dataString 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 Codefunction 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);  }}

This was only the result after i tap the card once and recieved in the serial monitor a code 302The Code 302 in Serial Monitor


Viewing all articles
Browse latest Browse all 1535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>