I want to display the RFID tag value to a html file.
I am using ESP8266 ESP-01 module, EM-18 Reader Module, Arduino Uno and Xampp for hosting PHP.
Right now, I am getting a value of "255" in Arduino whenever I scan a RFID tag. But I am supposed to get the 12-digit alphanumeric RFID tag value.
I have attached the Arduino and also the output screen of the Serial Monitor.
I have my college submission next week so it's urgent. Any help would be appreciated.
#include "SoftwareSerial.h"String ssid ="Home";String password="****";SoftwareSerial esp(2, 3);// RX, TXString data;String server = "192.168.0.182"; // www.example.comString uri = "/testget.php";// our example is /esppost.phpint DHpin = 0;//sensor pinbyte dat [12];String tag;void setup() { pinMode (DHpin, OUTPUT); esp.begin(9600); Serial.begin(9600); reset(); connectWifi();}//reset the esp8266 modulevoid reset() { esp.println("AT+RST"); delay(1000); if(esp.find("OK") ) Serial.println("Module Reset");}//connect to your wifi networkvoid connectWifi() { String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\""; esp.println(cmd); delay(4000); /* if(esp.find("OK")) { Serial.println("Connected!"); } else { connectWifi(); Serial.println("Cannot connect to wifi"); }*/} byte read_data () { byte data; for (int i = 0; i < 13; i ++) { if (digitalRead (DHpin) == LOW) { while (digitalRead (DHpin) == LOW); // wait for 50us delayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1' if (digitalRead (DHpin) == HIGH) data |= (1 << (12-i)); // high front and low in the post while (digitalRead (DHpin) == HIGH);// data '1 ', wait for the next one receiver } } return data; }void start_test () { digitalWrite (DHpin, LOW); // bus down, send start signal delay (30); // delay greater than 18ms, so DHT11 start signal can be detected digitalWrite (DHpin, HIGH); delayMicroseconds (40); // Wait for DHT11 response pinMode (DHpin, INPUT); while (digitalRead (DHpin) == HIGH); delayMicroseconds (80); // DHT11 response, pulled the bus 80us if (digitalRead (DHpin) == LOW); delayMicroseconds (80); // DHT11 80us after the bus pulled to start sending data for (int i = 0; i < 13; i ++) // receive tag value data, the parity bit is not considered dat[i] = read_data (); pinMode (DHpin, OUTPUT); digitalWrite (DHpin, HIGH); // send data once after releasing the bus, wait for the host to open the next Start signal}void loop () { start_test (); // convert the bit data to string form tag= String(dat[12]); data = "tag=" + tag;// data sent must be under this form //name1=value1&name2=value2. Serial.println(data); httppost(); delay(1000);}void httppost () { esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection. if( esp.find("OK")) { Serial.println("TCP connection ready"); } delay(1000); String postRequest = "POST " + uri + " HTTP/1.0\r\n" + "Host: " + server + "\r\n" + "Accept: *" + "/" + "*\r\n" + "Content-Length: " + data.length() + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "\r\n" + data; String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent. esp.print(sendCmd); esp.println(postRequest.length() ); delay(500); if(esp.find(">")) { Serial.println("Sending.."); esp.print(postRequest); if( esp.find("SEND OK")) { Serial.println("Packet sent"); while (esp.available()) { String tmpResp = esp.readString(); Serial.println(tmpResp); } // close the connection esp.println("AT+CIPCLOSE"); } }}Output :
- 2
tag= String(dat[12]);doesn't do what you think it does. Also you are trying to read 13 bytes into an array of 12 bytes and cram 13 bits into a byte. And for some reason using pin 0 for the sensor while still using serial.gre_gor– gre_gor2018-04-13 17:47:11 +00:00CommentedApr 13, 2018 at 17:47 - Please can you help me with the code? I am new to Arduino and I'm really struggling with my grades on the line!curiously77– curiously772018-04-13 17:55:06 +00:00CommentedApr 13, 2018 at 17:55
- change
<13to<12and properly convert the byte(/char)array to a StringGerben– Gerben2018-04-13 18:47:38 +00:00CommentedApr 13, 2018 at 18:47 - I did that and I'm getting 254 now. I wrote this: String myString = String(myByteArray); to convert.curiously77– curiously772018-04-13 18:59:20 +00:00CommentedApr 13, 2018 at 18:59
- Your example code doesn't have
myByteArray, so saying you tried that doesn't help anyone understand the context in which you tried it. You can edit your question to include updated code you have tried, etc.jose can u c– jose can u c2018-04-13 19:49:58 +00:00CommentedApr 13, 2018 at 19:49
2 Answers2
I got it working using the code below:
#include "SoftwareSerial.h"String ssid ="Home";String password="%Qwerty@123%";char input[12]; int count = 0; SoftwareSerial esp(2, 3);// RX, TXString data;String server = "192.168.0.182"; // www.example.comString uri = "/testget.php";// our example is /esppost.phpint DHpin = 0;//sensor pinbyte dat [12];String temp;void setup() {pinMode (DHpin, OUTPUT);esp.begin(9600);Serial.begin(9600);reset();connectWifi();}//reset the esp8266 modulevoid reset() {esp.println("AT+RST");delay(1000);if(esp.find("OK") ) Serial.println("Module Reset");}//connect to your wifi networkvoid connectWifi() {String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";esp.println(cmd);delay(4000);}byte read_data () {byte data;for (int i = 0; i < 8; i ++) {if (digitalRead (DHpin) == LOW) {while (digitalRead (DHpin) == LOW); // wait for 50usdelayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1'if (digitalRead (DHpin) == HIGH){data |= (1 << (7-i)); // high front and low in the post }while (digitalRead (DHpin) == HIGH);// data '1 ', wait for the next one receiver}} return data; }void start_test () {digitalWrite (DHpin, LOW); // bus down, send start signaldelay (30); // delay greater than 18ms, so DHT11 start signal can be detecteddigitalWrite (DHpin, HIGH);delayMicroseconds (40); // Wait for DHT11 responsepinMode (DHpin, INPUT);while (digitalRead (DHpin) == HIGH);delayMicroseconds (80);// DHT11 response, pulled the bus 80usif (digitalRead (DHpin) == LOW);delayMicroseconds (80);while(count<12){input[count] = Serial.read(); // Read 1 Byte of data and store it in the input[] variable count++; // increment counter delay(5);}// DHT11 80us after the bus pulled to start sending data//for (int i = 0; i < 4; i ++)// receive temperature and humidity data, the parity bit is not considered/*for(int x=0;x<12;x++)for(int i=0;i<12;i++)//dat[i] = read_data ();dat[x]=input[i];*/pinMode (DHpin, OUTPUT);digitalWrite (DHpin, HIGH);// send data once after releasing the bus, wait for the host to open the next Start signal}void loop () {start_test (); count = 0; // Reset the counter to zero temp = ""; // convert the bit data to string formfor(int i=0;i<13;i++)temp += input[i];Serial.println(temp);//Serial.println(temp);data = "tag=" + temp;// data sent must be under this form //name1=value1&name2=value2.httppost();delay(200);}void httppost () {esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.if( esp.find("OK")) {Serial.println("TCP connection ready");} delay(100);String postRequest ="POST " + uri + " HTTP/1.0\r\n" +"Host: " + server + "\r\n" +"Accept: *" + "/" + "*\r\n" +"Content-Length: " + data.length() + "\r\n" +"Content-Type: application/x-www-form-urlencoded\r\n" +"\r\n" + data;String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.esp.print(sendCmd);esp.println(postRequest.length() );delay(500);if(esp.find(">")) { Serial.println("Sending.."); esp.print(postRequest);if( esp.find("SEND OK")) { Serial.println("Packet sent");//while (esp.available()) {//String tmpResp = esp.readString();//Serial.println(tmpResp);//}// close the connectionesp.println("AT+CIPCLOSE");}}}You can follow this awesome tutorial if you want to connect your RFID RC522 module to PHP. Source Code and a step by step tutorial are provided. You only need NodeMcu and RC522 RFID module.https://devcraze.com/tutorials/connecting-rfid-scanner-to-php-using-nodemcu-wifi-module/
Explore related questions
See similar questions with these tags.

