1

This program is a program which readings of rfid are saved to myphp database. I have a problem when running the code The wifi is connected but when the code doesn't save im assuming that the problem is in this part of the code client.print("GET /rfid/nodemcu_rfid/rfid_read.php?allow="); Since i have done a wire shark capture and notice a Bad Request error 400 as shown in the screen shot. enter image description here

IP Addresses: 192.168.0.14 Is the php server , 192.168.0.19 is the nodemcu

ALL THE CODE :

#include<SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <SPI.h>
#include <MFRC522.h>

const char* ssid = "ARRIS-84AB";
const char* password = "your SSID password ";
//WiFiClient client;
char server[] = "192.168.0.14";   //eg: 192.168.0.222
#define SS_PIN 2 //FOR RFID SS PIN BECASUSE WE ARE USING BOTH ETHERNET SHIELD AND RS-522
#define RST_PIN 15
#define No_Of_Card 3

 WiFiClient client;

//WiFiServer server(80);
SoftwareSerial mySerial(8,9);     
MFRC522 rfid(SS_PIN,RST_PIN);
MFRC522::MIFARE_Key key; 
byte id[No_Of_Card][4]={
  {44,153,22,219},             //RFID NO-1
  {112,224,72,84},             //RFID NO-2
  {151,94,80,84}              //RFID NO-3
};
byte id_temp[3][3];
byte i;
int j=0;
void setup()
{
 Serial.begin(115200);
  delay(10);
  mySerial.begin(115200);
  SPI.begin();
  rfid.PCD_Init();

    for(byte i=0;i<6;i++)
  {
    key.keyByte[i]=0xFF;
  }

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
//  server.begin();
  Serial.println("Server started");
  Serial.print(WiFi.localIP());
  delay(1000);
  Serial.println("connecting...");
 }
void loop()
{  // Check if a client has connected
  int m=0;
  if(!rfid.PICC_IsNewCardPresent())
  return;
  if(!rfid.PICC_ReadCardSerial())
  return;
  for(i=0;i<4;i++)
  {
   id_temp[0][i]=rfid.uid.uidByte[i]; 
             delay(50);
  }

   for(i=0;i<No_Of_Card;i++)
  {
          if(id[i][0]==id_temp[0][0])
          {
            if(id[i][1]==id_temp[0][1])
            {
              if(id[i][2]==id_temp[0][2])
              {
                if(id[i][3]==id_temp[0][3])
                {
                  Serial.print("your card no :");
                  for(int s=0;s<4;s++)
                  {
                    Serial.print(rfid.uid.uidByte[s]);
                    Serial.print(" ");

                  }
                  Serial.println("\nVALID");
                  Sending_To_DB();
                  j=0;

                            rfid.PICC_HaltA(); rfid.PCD_StopCrypto1();   return; 
                }
              }
            }
          }
   else
   {j++;
    if(j==No_Of_Card)
    {
      Serial.println("inVALID");
      Sending_To_DB();
      j=0;
    }
   }
  }

     // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
 }

 void Sending_To_DB()   //CONNECTING WITH MYSQL
 {
   if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    Serial.println("GET /rfid/rfidread.php?allow=");     //YOUR URL /rfid/rfidread.php?allow
    client.print("GET /rfid/nodemcurfid/rfidread.php?allow=");     //YOUR URL /rfid/rfidread.php?allow  /var/www/html/rfid/rfid_read.php
    if(j!=No_Of_Card)
    {
      Serial.println('1');
      client.print('1');
    }
    else
    {
      Serial.println('0');
      client.print('0');
    }
    Serial.println("&id=");
    client.print("&id=");
    for(int s=0;s<4;s++)
                  {
                    Serial.println(rfid.uid.uidByte[s]);
                    client.print(rfid.uid.uidByte[s]);

                  }
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.print("Host: ");
     client.println(server);
    client.println("Host: 192.168.0.14");//eg: 192.168.0.222
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  client.stop();
 }
  • client.println("Host: YOUR SERVER IP");//eg: 192.168.0.222 -- Is that really what you have in your running code? – Majenko May 12 at 20:44
  • no my that is a sample my code is client.println("Host: 192.168.0.14"); – wen02 May 12 at 20:45
  • 1
    Please show us your real code. We can't tell you what is wrong with your code if you show us something that is not your code. – Majenko May 12 at 20:46
  • Edited with all configs (the real code) – wen02 May 12 at 20:48
1

Your problem is here:

client.print(" ");      //SPACE BEFORE HTTP/1.1
client.print("HTTP/1.1");
client.print("Host: ");
 client.println(server);
client.println("Host: 192.168.0.14");//eg: 192.168.0.222
client.println("Connection: close");
client.println();

That ends up with:

....(your request)... HTTP/1.1Host: 192.168.0.14
Host: 193.168.0.14
Connection: close

Not only are you sending the Host header twice, but you're not adding the line ending after the HTTP version.

Instead it should look more like:

client.print(" ");      //SPACE BEFORE HTTP/1.1
client.println("HTTP/1.1"); // <-- Note the use of println not print
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
  • I have fixed the code like this but still no luck client.print(" "); //SPACE BEFORE HTTP/1.1 client.print("HTTP/1.1"); client.println(" Host: 192.168.0.14");//eg: 192.168.0.222 client.println("Connection: close"); client.println(); What is a line ending after HTTP version please ? – wen02 May 12 at 20:57
  • See my edit.... – Majenko May 12 at 21:10
  • Thanks it worked !!! – wen02 May 12 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.