I'm trying to do a project RFID Reader door open with arduino mega and esp8266-01, in this part of my project i was able to check if my iD exists on my database, the problem is that my return from esp-01 is not appearing on my arduino console(serial), it's just say "Waiting for response from ESP-01...."
"Finished processing response from ESP-01...."
above is the site where i saw the code
my arduino code
#include <StreamUtils.h>
#include <SPI.h>
#include <MFRC522.h>
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(23, 22); // TX, RX
MFRC522 mfrc522(53, 5); // Create Instance of MFRC522 mfrc522(SS_PIN, RST_PIN)
const int ledPinGreen = A0;
const int ledPin = A1;
bool isRead = false;
bool isNewCard = false;
String tagContent = "";
String currentUID = "";
// Interval before we process the same RFID
int INTERVAL = 2000;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
void setup()
{
Serial.begin(115200);
mySerial.begin(4800);
mySerial.setTimeout(5000);
// Setup the led
pinMode(ledPinGreen, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPinGreen, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPinGreen, LOW);
SPI.begin();
mfrc522.PCD_Init();
Serial.println("Detecting RFID Tags");
}
void loop()
{
// Look for new cards
if (mfrc522.PICC_IsNewCardPresent())
{
// Select one of the cards
if (mfrc522.PICC_ReadCardSerial())
{
isRead = true;
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
tagContent.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
tagContent.concat(String(mfrc522.uid.uidByte[i], HEX));
}
tagContent.toUpperCase();
}
if (isRead)
{
currentMillis = millis();
// If current UID is not equal to current UID..meaning new card, then validate if it has access
if (currentUID != tagContent) {
currentUID = tagContent;
isNewCard = true;
} else {
// If it is the same RFID UID then we wait for interval to pass before we process the same RFID
if (currentMillis - previousMillis >= INTERVAL) {
isNewCard = true;
} else {
isNewCard = false;
}
}
if (isNewCard)
{
if (tagContent != "") //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
{
turnOffLED();
previousMillis = currentMillis;
//Send the RFID Uid code to the ESP-01 for validation
Serial.print("Sending data to ESP-01 :: ");
Serial.println(tagContent);
//TODO Process read from serial here
mySerial.println(tagContent);
Serial.println("Waiting for response from ESP-01....");
int iCtr = 0;
while (!mySerial.available())
{
iCtr++;
if (iCtr >= 100)
break;
delay(50);
}
//รงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรงรง
if (mySerial.available()) {
bool isAuthorized = isUserAuthorized(tagContent);
// If not authorized then sound the buzzer
if (!isAuthorized)
{
playNotAuthorized();
}
else{ //light up the Green LED
turnOnLED();
}
}
Serial.println("Finished processing response from ESP-01....");
}
}
}
else {
Serial.println("No card details was read!");
}
tagContent = "";
isNewCard = false;
}
}
bool isUserAuthorized(String tagContent)
{
const size_t capacity = JSON_OBJECT_SIZE(1) + 30;
DynamicJsonDocument doc(capacity);
// Use during debugging
//ReadLoggingStream loggingStream(mySerial, Serial);
// DeserializationError error = deserializeJson(doc, loggingStream);
// Use during actual running
DeserializationError error = deserializeJson(doc, mySerial);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return error.c_str();
}
bool is_authorized = doc["is_authorized"] == "true";
Serial.print("is_authorized :: ");
Serial.println(is_authorized);
return is_authorized;
}
void playNotAuthorized()
{
Serial.println("TESTE10");
digitalWrite(ledPin, HIGH);
}
void turnOffLED()
{
digitalWrite(ledPin, LOW);
Serial.println("cartao detetado");
}
void turnOnLED()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
Serial.println("TESTE12");
}
my esp-01 code
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// NOTE: CHANGE THIS TO YOUR WIFI SSID AND PASSWORD
const char* ssid = "Noira2";
const char* password = "12345678";
//#define DEBUG //If you comment this line, the DPRINT & DPRINTLN lines are defined as blank.
#ifdef DEBUG //Macros are usually in all capital letters.
#define DPRINT(...) Serial.print(__VA_ARGS__) //DPRINT is a macro, debug print
#define DPRINTLN(...) Serial.println(__VA_ARGS__) //DPRINTLN is a macro, debug print with new line
#else
#define DPRINT(...) //now defines a blank line
#define DPRINTLN(...) //now defines a blank line
#endif
void setup() {
Serial.begin(4800);
delay(10);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
const byte numChars = 20;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void loop() {
recvWithEndMarker();
processRFIDCode();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void processRFIDCode() {
if (newData == true) {
DPRINT("This just in ... ");
DPRINTLN(receivedChars);
newData = false;
isUserAuthorized(receivedChars);
}
}
void isUserAuthorized(String rfIdCode){
// wait for WiFi connection
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
// NOTE: CHANGE THIS TO THE IP ADDRESS WHERE YOUR APPLICATION IS RUNNING
String url = "http://172.19.0.89:8080/student/isauthorized?rf_id_code=";
String encodedRFIDCode = urlencode(rfIdCode);
url+=encodedRFIDCode;
DPRINT("url :: ");
DPRINTLN(url);
http.setTimeout(20000);
if (http.begin(client, url)) { // HTTP
// start connection and send HTTP header
int httpCode = http.GET();
DPRINT("httpCode :: ");
DPRINTLN(httpCode);
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
DPRINTLN(payload);
sendDataBackToArduino(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
}
String urlencode(String str)
{
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
void sendDataBackToArduino(String payload)
{
const size_t capacity = JSON_OBJECT_SIZE(1) + 30;
DynamicJsonDocument doc(capacity);
// Parse JSON object
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
serializeJson(doc, Serial);
}
Python code
#NOTE: CHANGE THIS TO WHERE YOU DOWNLOAD YOUR GIT REPO
db_folder = Path("C:/git/database-project/StudentDB.db")
application = default_app()
@get('/student/isauthorized')
def message():
rf_id_code = request.query.rf_id_code.lstrip().rstrip()
length = len(rf_id_code)
print(f"Received the following query parameter rf_id_code={rf_id_code}, len={length}")
conn = sqlite3.connect(db_folder)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM STUDENTS WHERE RF_ID_CODE=?", (rf_id_code,))
result = cursor.fetchone()
row_count = result[0]
print(f"query result :: ", row_count);
cursor.close()
#Set Response Header to JSON
response.headers['Content-Type'] = 'application/json'
response.headers['Cache-Control'] = 'no-cache'
if(row_count > 0):
message_result = {"is_authorized" : "true"}
else:
message_result = {"is_authorized": "false"}
print(f"message_result :: {message_result}")
return json.dumps(message_result)
httpserver.serve(application, host='0.0.0.0', port=8080)
the result "i want my arduino to write on console "authorized"