I'm reading RFID cards' UID
with an ESP8266 and trying to send that information to my web server hosted on a Raspberry Pi. I'm currently storing the UID
in an int array, but the http.POST();
function takes a string input. I tried adding each UID
digit together, but that doesn't work either.
How would you go about sending the uid in the request?
My code:
/*
Created by TheCircuit
*/
#define SS_PIN 4 //D2
#define RST_PIN 5 //D1
#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "****";
const char* password = "****";
const char* serverName = "****";
int RELAY = 2; //D6
int UIDs[1][4] = {
{83, 111, 39, 47}
};
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
boolean compareUIDs(int a[4], int b[4]) {
for (int i = 0; i < 4; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
void loop()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
// mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
int UID[4];
for (int i = 0; i < 4; i++) {
UID[i] = mfrc522.uid.uidByte[i];
}
Serial.print(UID[0]);
Serial.print(" ");
Serial.print(UID[1]);
Serial.print(" ");
Serial.print(UID[2]);
Serial.print(" ");
Serial.print(UID[3]);
Serial.println();
for (int i = 0; i < sizeof(UIDs); i++) {
boolean success = compareUIDs(UIDs[i], UID);
if (success) {
Serial.println("ACCESS GRANTED");
digitalWrite(RELAY, LOW);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("UID GOES HERE");
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
delay(2000);
digitalWrite(RELAY, HIGH);
return;
}
}
}
print
to string same way as you print to Serial