ESP32 HttpClient get.string()

mkucukturkmen
Posts: 1
Joined: Tue Mar 08, 2022 10:49 am

ESP32 HttpClient get.string()

Postby mkucukturkmen » Tue Mar 08, 2022 11:05 am

Hello ım working on a project rfid mysql and php project, my arduino (esp32) .It connects to a site ( getUID.php) with http.begin and transmits rfid tag information, then php mysql platforms perform operations in the background and show the tag information on a separate php page (readtag2.php). I want to connect with http.begin again for this site and then save the tag information on that page to the payload variable with get.string(), but I see the source string of that site, so actually I can transfer the html codes to my variable. How can I transfer the information from that site to my variable?.

My arduino codes :

#include <WebServer.h>
#include <HTTPClient.h>
#include <WiFi.h>
#include <WiFiClient.h>
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <SPI.h>
#include <MFRC522.h>
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

#define SS_PIN 5 //–> SDA
#define RST_PIN 27 //–> RST
MFRC522 mfrc522(SS_PIN, RST_PIN);

#define ON_Board_LED 2 //–> On Board LED

//-----------------------------------------SSID and Password of your WiFi router-------------------------------------------------------------------------------------------------------------//
const char* ssid = “”;
const char* password = “”;
//---------------------------------------------------------------------------------------------
WebServer server(80); //–> Server on port 80

int readsuccess;
byte readcard[4];
char str[32] = “”;
String StrUID;

String httpGETRequest(const char* serverName) {
HTTPClient http;

// Your IP address with path or Domain name with URL path
http.begin(serverName);
http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”);

// Send HTTP POST request
int httpResponseCode = http.GET();

String payload = “{}”;

if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}

http.end();

return payload;
}
//-----------------------------------------------------------------------------------------------SETUP--------------------------------------------------------------------------------------//
void setup() {
Serial.begin(115200); //–> Initialize serial communications with the PC
SPI.begin(); //–> Init SPI bus
mfrc522.PCD_Init(); //–> Init MFRC522 card

delay(500);

WiFi.begin(ssid, password); //–> Connect to your WiFi router
Serial.println("");

pinMode(ON_Board_LED,OUTPUT);
digitalWrite(ON_Board_LED, HIGH); //–> Turn off Led On Board

//----------------------------------------Wait for connection
Serial.print(“Connecting”);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
//----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
digitalWrite(ON_Board_LED, LOW);
delay(250);
digitalWrite(ON_Board_LED, HIGH);
delay(250);
}
digitalWrite(ON_Board_LED, HIGH); //–> Turn off the On Board LED when it is connected to the wifi router.
//----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
Serial.println("");
Serial.print("Successfully connected to : ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

Serial.println(“Please tag a card or keychain to see the UID !”);
Serial.println("");
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
void loop() {
// put your main code here, to run repeatedly
readsuccess = getid();

if(readsuccess) {
digitalWrite(ON_Board_LED, LOW);
HTTPClient http; //Declare object of class HTTPClient
String UIDresultSend, postData;
UIDresultSend = StrUID;

//Post Data
postData = “UIDresult=” + UIDresultSend;

http.begin(“http://192.168…/…/getUID.php”); //Specify request destination

http.addHeader(“Content-Type”, “application/x-www-form-urlencoded”); //Specify content-type header

int httpCode = http.POST(postData); //Send the request
String payload = http.getString(); //Get the response payload

Serial.println(UIDresultSend);
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
delay(1000);

httpGETRequest(“http://192.168…/…/readtag2.php”);
digitalWrite(ON_Board_LED, HIGH);
}

}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

//----------------------------------------Procedure for reading and obtaining a UID from a card or keychain---------------------------------------------------------------------------------//
int getid() {
if(!mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
if(!mfrc522.PICC_ReadCardSerial()) {
return 0;
}

Serial.print("THE UID OF THE SCANNED CARD IS : ");

for(int i=0;i<4;i++){
readcard=mfrc522.uid.uidByte; //storing the UID of the tag in readcard
array_to_string(readcard, 4, str);
StrUID = str;
}
mfrc522.PICC_HaltA();
return 1;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

//----------------------------------------Procedure to change the result of reading an array UID into a string------------------------------------------------------------------------------//
void array_to_string(byte array, unsigned int len, char buffer) {
for (unsigned int i = 0; i < len; i++)
{
byte nib1 = (array >> 4) & 0x0F;
byte nib2 = (array >> 0) & 0x0F;
buffer[i*2+0] = nib1 < 0xA ? ‘0’ + nib1 : ‘A’ + nib1 - 0xA;
buffer[i*2+1] = nib2 < 0xA ? ‘0’ + nib2 : ‘A’ + nib2 - 0xA;
}
buffer[len*2] = ‘\0’;
}
//-------------------------

My Serial Monitor

Please tag a card or keychain to see the UID !
23:18:48.137 →
23:18:59.428 → THE UID OF THE SCANNED CARD IS : 421F0DD4
23:19:00.340 → 200
23:19:00.340 →
23:19:01.355 → HTTP Response code: 200
23:19:01.355 → alinan deger
23:19:01.355 → <?php
23:19:01.355 → $Write="<?php $" . "UIDresult=''; " . "echo $" . "UIDresult;" . " ?>";
23:19:01.355 → file_put_contents(‘UIDContainer.php’,$Write);
23:19:01.355 → ?>
23:19:01.355 →
23:19:01.355 →
23:19:01.355 →
23:19:01.355 → Http body script meta etc. all about web site source code, not database
datas. Returns an incorrect String.
23:19:01.355 →
23:19:01.355 →
23:19:01.355 →
23:19:01.355 →
23:19:01.355 →

Who is online

Users browsing this forum: No registered users and 89 guests