1

I'm trying to build an attendance system based on RFID and NODEMCU, the NODEMCU send POST request to PHP file but PHP file doesn't receive any data, so I decided to intercept network traffic and I saw that server response correctly.

POST request

Server response

this is arduino code

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <MFRC522.h>
#include <SPI.h>

const char* ssid = "Bam";
const char* password = "123456789";
const char* target = "http://192.168.1.109/RFID/getuid.php";

MFRC522 reader(D4, D3);
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  SPI.begin();
  reader.PCD_Init();
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED){
    delay(1000);
    Serial.println("Connecting to " + String(ssid) + "...");
  }
  Serial.println("Connected to "+String(ssid)+"!");

}

void loop() {
  // put your main code here, to run repeatedly:
  if(reader.PICC_IsNewCardPresent() && reader.PICC_ReadCardSerial()){
    String uid = "";
    for(byte i=0; i<reader.uid.size;i++){
      uid+=String(reader.uid.uidByte[i] < 0x10 ? "0" : "");
      uid+=String(reader.uid.uidByte[i], HEX);
    }
    Serial.println("Card UID: "+uid);
    sendUid(uid);
    delay(1000);
  }

}
void sendUid(String uid){
  if(WiFi.status() == WL_CONNECTED){
    WiFiClient client;
    HTTPClient http;
    http.begin(client, target);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    String data = "uid="+uid;
    int httpResCode = http.POST(data);
    if(httpResCode == HTTP_CODE_OK){
      String res = http.getString();
      Serial.println("Server response: "+res);
    }else{
      Serial.println("HTTP error code: "+ String(httpResCode));
    }
    http.end();
  }else{
    Serial.println("WiFi not connected");
  }
}

this is php code <?php echo $data = isset($_POST['uid']) ? $_POST['uid'] : 'no data'; ?>

php file output after submitting RF card

the php supposed to print data received while the server response correctly with write output

m0rbot
  • 11
  • 4
  • Try echoing out $rawPost in PHP where $rawPost = file_get_contents('php://input'); - I always do this first when getting data from a difference language / source - it lets you see what the data looks like before trying to cast it or read it. – Enigma Plus Apr 04 '23 at 12:26
  • @EnigmaPlus I tried this but no changes, when I intercept traffic the server response with data I want to be echoed in the browser but when I refresh the page there is no changes – m0rbot Apr 04 '23 at 12:40
  • Sorry - I don't follow what you are doing. Are you posting to the PHP script and then refreshing it in a browser? $_POST won't still be populated from your earlier post from Arduino. – Enigma Plus Apr 04 '23 at 12:45
  • 1
    _"php file output after submitting RF card"_ - that screenshot shows you requesting the script apparently from within your browser. What did you think _that_ request would have to do, with whatever you are sending from your NODEMCU? There is _no_ connection whatsoever between those two requests. – CBroe Apr 04 '23 at 12:51
  • @CBroe I believe there is a misunderstanding, I want to build an student attendance system, so the first thing is to send UID to PHP file so I can handle this UID to insert it into database or check if this UID in the database, and then check if PHP script receive this UID so I can move to the next step. – m0rbot Apr 04 '23 at 13:11
  • @EnigmaPlus What I supposed to do, so I cant able to see if data was received successfully. I know the data was received successfully. – m0rbot Apr 04 '23 at 13:13
  • 1
    I think it is safe to assume that your only issue here is your understanding of PHP / HTTP. Go ahead and assume that you have got your data available in PHP - do whatever you had to planned with it and it should be fine. – Enigma Plus Apr 04 '23 at 13:14
  • _"I know the data was received successfully."_ - but you appear to be unclear about _where_ it was actually received. Again: If your NodeMCU device makes this request, then that has absolutely nothing whatsoever to do with what you are looking at _in your browser_. Requesting this script in your browser, is a _second_ request, that has no connection to the first one at all. If you want to be able to "share" data across multiple requests, then you need to store it somewhere on the server side. – CBroe Apr 04 '23 at 13:23
  • To expand on Enigma Plus's last comment, it looks like the issue you have is that you cannot just load the PHP script in your browser to see if the data was received. It is the same script but it runs 2 times separately, the first one with the POST data (but you can't see the result in your arduino) and the second time from your browser with no POST data. You must find another way to debug (for example insert in DB with first script, and call a second script in broswer checking what's in DB). Hope this is clearer – Kaddath Apr 04 '23 at 13:25
  • @EnigmaPlus I've updated the script so it insert data into table, but when I opened mysql database and refresh the table it show nothing(empty field with id 1 --because id is auto increment of course--), so the data was not received successfully by the script – m0rbot Apr 04 '23 at 13:28
  • Try inserting $rawPost into a text field to get started - rem $rawPost = file_get_contents('php://input'); – Enigma Plus Apr 04 '23 at 13:31
  • @Kaddath The NODEMCU response with 200 OK and intercepting network traffic i found that the server response with {200 OK + body}, I've tried to INSERT data into table in database, but I found an empty field with id:1 – m0rbot Apr 04 '23 at 13:31
  • @EnigmaPlus Thank you, the problem is solved the now I can see data changed into table – m0rbot Apr 04 '23 at 13:39
  • Thank you all for your replays – m0rbot Apr 04 '23 at 13:39

0 Answers0