Hi! I am only a newbie but our group is creating a monitoring system with SMS notifications. We are using NODEMCU and MYSQL database and RFID to store the information for the registered card tags. One of the information to put is Contact Number. The code below, which we got from a source and tweaked it for our components for the system, will read and display the CardID of the card tag to the serial monitor whenever it is scanned. Can someone help us on how to get the Contact Number from the database and read it and display as well in the serial monitor. we will use this contact number of the registered cards to send SMS notifications to them whenever it is scanned. I have yet to put the needed libraries for the GSM module and code for this. We just want to know if how to get this contact number from database and send it to the nodemcu. Thank You!!
/*This code was created by H.I Electronic Tech channel and was further modified by the researchers
*/
#include <ESP8266WiFi.h> //Include Esp library
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <MFRC522.h> //include RFID library
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define SS_PIN D4 //RX slave select
#define RST_PIN D3
#define gLed D0
#define buzzer D8
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
LiquidCrystal_I2C lcd(0x27, 16, 2);
/* Set these to your desired credentials. */
const char *ssid = "xxxxx"; //ENTER YOUR WIFI SETTINGS
const char *password = "xxxxx";
//Web/Server address to read/write from
const char *host = "192.168.xxx.xxx"; //IP address of server
int dservo=3500;
String getData ,Link;
String CardID="";
void setup() {
lcd.init();
pinMode(gLed,OUTPUT);
pinMode(buzzer,OUTPUT);
noTone(buzzer);
delay(1000);
Serial.begin(115200);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Attemping to Connect with ");
Serial.print(ssid);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected Successfully to ");
Serial.print(ssid);
Serial.println("!");
Serial.print("IP address of NodeMCU: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
Serial.println("");
Serial.println("Ready To Scan Card");
Wire.begin(D2,D1);
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Good Day! Please");
lcd.setCursor(1,1);
lcd.print("Scan Your Card");
digitalWrite(gLed,HIGH);
tone(buzzer, 1000);
delay(300);
noTone(buzzer);
delay(300);
digitalWrite(gLed,LOW);
}
void loop() {
if(WiFi.status() != WL_CONNECTED){
WiFi.disconnect();
WiFi.mode(WIFI_STA);
Serial.print("Attempting to Reconnect with ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected Successfully to ");
Serial.print(ssid);
Serial.print("IP address of NodeMCU: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
//look for new card
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;//got to start of loop if there is no card present
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;//if read card serial(0) returns 1, the uid struct contians the ID of the read card.
}
for (byte i = 0; i < mfrc522.uid.size; i++) {
CardID += mfrc522.uid.uidByte[i];
}
digitalWrite(gLed,LOW);
Serial.println("Sending the Card ID");
WiFiClient client;
HTTPClient http; //Declare object of class HTTPClient
//GET Data
getData = "?CardID=" + CardID; //Note "?" added at front
Link = "http://192.168.xxx.xxx/loginsystem/postdemo.php" + getData;
http.begin(client, Link);
int httpCode = http.GET(); //Send the request
delay(10);
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
Serial.println(CardID); //Print Card ID
if(payload == "login"){
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Welcome!");
lcd.setCursor(0,1);
lcd.print("Have A Nice Day");
digitalWrite(gLed,HIGH);
tone(buzzer, 600);
delay(100);
digitalWrite(gLed,LOW);
delay(100);
digitalWrite(gLed,HIGH);
delay(100);
noTone(buzzer);
digitalWrite(gLed,LOW);
delay(500); //Post Data at every 5 seconds
}
else if(payload == "logout"){
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Goodbye!");
lcd.setCursor(0,1);
lcd.print("Have A Nice Day");
digitalWrite(gLed,HIGH);
tone(buzzer, 400);
delay(100);
digitalWrite(gLed,LOW);
delay(100);
digitalWrite(gLed,HIGH);
delay(100);
noTone(buzzer);
digitalWrite(gLed,LOW);
delay(500); //Post Data at every 5 seconds
}
else if(payload == "succesful" || payload == "Cardavailable"){
lcd.clear();
lcd.print("Please Register");
lcd.setCursor(3,1);
lcd.print("Your Card");
digitalWrite(gLed,HIGH);
tone(buzzer, 800);
delay(100);
digitalWrite(gLed,LOW);
delay(100);
digitalWrite(gLed,HIGH);
delay(100);
digitalWrite(gLed,LOW);
delay(100);
digitalWrite(gLed,HIGH);
delay(100);
digitalWrite(gLed,LOW);
delay(100);
digitalWrite(gLed,HIGH);
delay(100);
digitalWrite(gLed,LOW);
delay(100);
digitalWrite(gLed,HIGH);
delay(100);
digitalWrite(gLed,LOW);
delay(100);
digitalWrite(gLed,HIGH);
noTone(buzzer);
digitalWrite(gLed,LOW);
delay(500);
}
delay(500);
CardID = "";
getData = "";
Link = "";
http.end(); //Close connection
Serial.println("");
Serial.println("Ready To Scan Next Card");
delay(750);
lcd.clear();
delay(200);
lcd.print("Good Day! Please");
lcd.setCursor(1,1);
lcd.print("Scan Your Card");
digitalWrite(gLed,HIGH);
}
//=======================================================================