Sie sind auf Seite 1von 22

WIRELESS READER USING ESP8266

OBJECTIVE :-
The objective of this project is to make SMART I’s, ‘RFID readers’
and ‘biometric scanners’ wireless using TCP and UDP protocol, also
making the setup of system easier.

SETUP:-

This setup is required in case you don’t have a USB-TTL that provides 5V
power.
But in case you have a 3.3V powered USB-TTL then you shouldn’t use
78xxl. A direct connection should be done from 3.3V supply to VCC of
ESP8266 and the other connection connected to it should be kept the
same way.
The SMART I reader need a 5V connection. So a 5V connection should
be provided to the reader and the TX of reader should be connected to
RX of ESP8266 and RX of reader should be connected to TX of ESP8266.
In the above given case the left switch is FLASH switch and the right
switch is RESET switch.
There are two modes in which the ESP8266 works:-
1) The flash mode
2) The normal mode

1) The flash mode: - In this mode you can burn the code.
To get ESP into this mode press the RESET button and to then press the
FLASH button and the release the RESET BUTTON and then release the
hold button.
2) The normal mode: - In this mode ESP runs the code.
To get into this mode just hold and release the RESET button.
So once the system is connected put it in flash mode and burn the
code in it and then put it in normal mode so that it can function
smoothly.
FLOW OF HOW THE SYSTEM WORKS :-
First of all the new wireless RFID readers will be mounted in the
office. Once the power is supplied to it, it will create its own hotspot.
The User connects to the hotspot created by ESP8266.
Once connected :-
1) The login page will open in which we have to enter the default
login id and password set by the company.
2) The second page will ask the User the following points:- (all the
variables are set to ‘smarti’ in the start except for the hotspot
password which should be minimum 8 characters so the
password is smarti12345)
1) New login id and password
2) The new name for hotspot and password
3) The login id and password of WIFI network to which the
controller is connected.
4) The authentication type of that network.
5) The ip address of controller
6) The port via which communication to controller will be
done.
7) Controller id.
8) The type of communication
9) Reader no
10) Channel no
After all this information is provided the reader will connect to the
controller.
And send whatever the data it gets from the reader and send the
reader back whatever data is collected from microcontroller.
SETTING UP THE SOFTWARE:-
First download the Arduino IDE from the official website:-
https://www.arduino.cc/en/Main/Software
Then select the preferences option in the file menu and copy the
following link in Additional Board Managers Url
http://arduino.esp8266.com/stable/package_esp8266com_index.json

Go to tools>boards>boards manager
in it select esp8266 by ESP8266 community and install it.

Once the board is installed select Generic ESP8266 Module in


tools>boards and you are ready to go.
Connect the esp8266 to you PC burn the code given in “ESP8266”
To serially communicate with esp8266 select the icon at the top of
Arduino ide and set the baud rate to 115200.
The code is explained with comments.
WHAT IS DONE:-
The ESP8266 module takes the required information from the user and
will connect to any microcontroller or pc connected on the specified
port and Ip address.
It will obtain the data from the microcontroller and send it to the
reader and data obtained from the reader will be sent to
microcontroller.
WHAT IS LEFT:-
The data obtained by reader should be converted in a format that
microcontroller will understand.
This has to be done for:-
1) RFID reader.
2) Biometric sensor.
The code must be modified a bit so that if ESP is connected to the door
it should signal the actuator when to open and close the door
dependent on the signal given from the controller.

CODE:-
// login - page 1 login id pass - page 1 password

// wlogin - esp hotspot name wpass - esp hotspot password

// nlogin - name of the wifi to be connected npass- password nauth- authenication type

// cip - controller ip cport- controller port cid - controller id ctype- connection type

// rdno - reader no ch no - channel no

int c = 0;

int m=0; //m governs the program - for example the udp or tcp connection program has to be used can be decided
by the value of m
int q=3; //q is used to restrict the movement of user from page to page

// all the variables initialisedd

int udp_port=0;

String my_ip="not yet available";

String login2 = "";

String pass2 = "";

String wlogin2 = "";

String wpass2 = "";

String nlogin2 = "";

String npass2= "";

String nauth2 = "";

String cip2 = "";

String cport2 = "";

String ctype2 = "";

String cid2 = "";

String rdno2 = "";

String chno2 = "";

String login1 = "";

String pass1 = "";

String wlogin1 = "";

String wpass1 = "";

String nlogin1 = "";

String npass1= "";

String nauth1 = "";

String cip1 = "";

String cport1 = "";

String ctype1 = "";

String cid1 = "";

String rdno1 = "";

String chno1 = "";

char incomingPacket[255];
#include <ESP8266WiFi.h>

#include <WiFiClient.h>

#include <ESP8266WebServer.h>

#include <ESP8266mDNS.h>

#include <EEPROM.h>

#include <String.h>

#include "SoftwareSerial.h"

IPAddress apIP(10, 10, 10, 1); // the user can acess the login and other pages on 10.10.10.1

//SSID and Password to your ESP Access Point

const char* ssid = "ESPWebServer";

const char* password = "12345678";

WiFiClient client; //used to handle cliet

WiFiUDP Udp; // used for udp connection

IPAddress udp_ip(0, 0, 0, 0); // intialising variable

struct CONFIG_PARA //intialsing characters

char isnew[20];

char login[20];

char pass[20];

char wlogin[20];

char wpass[20];

char nlogin[20];

char npass[20];

char nauth[20];

char cip[20];

char cport[20];

char cid[20];

char ctype[20];

char rdno[20];
char chno[20];

}CF;

ESP8266WebServer server(80); //Server on port 80

int Debug = 0x0F; // used in dprint

#define INITIALISE 0X00 // when m is set to 0 it initialses the program

#define TCP_CONNECT 0x02// when m is set to 2 esp tries to create a tcp conection

#define TCP_WORKING 0x03 // when m is set to 3 esp recieves and sends data using tcp

#define UDP_CONNECT 0x04 //// when m is set to 4 esp connects to udp

#define UDP_WORKING 0X05// when m is set to 5 esp recieves and sends data using udp

#define D_INFO 0x08 //used to decide if DPrint should print or not

void DPrint(int dbg,String data,String value) //function created so the serial print can be on or off. serial print is
used for serial communication

if(Debug & dbg)

Serial.println(data + " :" + value);

void PrintAllPara() //used to print all parameters for debugging

DPrint(D_INFO,"LOGIN :",String(CF.login));

DPrint(D_INFO,"PASSWORD :",String(CF.pass));

DPrint(D_INFO,"HOTSPOT LOGIN :",String(CF.wlogin));

DPrint(D_INFO,"HOTSPOT PASSWORD :",String(CF.wpass));

DPrint(D_INFO,"WIFI LOGIN :",String(CF.nlogin));

DPrint(D_INFO,"WIFI PASSWORD :",String(CF.npass));

DPrint(D_INFO,"WIFI AUTHENTICATION TYPE :",String(CF.nauth));

DPrint(D_INFO,"CONTOROLLER IP :",String(CF.cip));

DPrint(D_INFO,"CONTROLLER PORT :",String(CF.cport));

DPrint(D_INFO,"CONTROLLER ID :",String(CF.cid));

DPrint(D_INFO,"CONTROLLER TYPE :",String(CF.ctype));


DPrint(D_INFO,"READER NO :",String(CF.rdno));

DPrint(D_INFO,"CHANNEL NO :",String(CF.chno));

void EStrWrite(unsigned int loc,unsigned char *data,int s) // used to read and write in eeprom

for(int i=0;i<s;i++)

EEPROM.write(loc+i,data[i]); //Write one by one with starting address of 0x0F

EEPROM.commit();

void EStrRead(unsigned int loc ,unsigned char *data,int s) // used to read from eeprom

for(int i=0;i<s;i++)

data[i] = char(EEPROM.read(loc+i)); //Read one by one with starting address of 0x0F

void IniDefault(void) // initialise esp8266

EStrRead(0,(unsigned char *) &CF,sizeof(CF)); // read from eeprom

String ho= String(CF.isnew);

if (!ho.equals("smart1")) //if the esp is running for the first time after its burnt

DPrint(D_INFO,"SMART NO MATCH!!","");

DPrint(D_INFO,"SMART KEY:","smart1");

DPrint(D_INFO,"SMART KEY STORED :",String(CF.isnew));

memcpy(CF.isnew,"smarti",20);
memcpy(CF.login,"smarti",20);

memcpy(CF.pass,"smarti",20);

memcpy(CF.wlogin,"smarti",20);

memcpy(CF.wpass,"smarti12345",20);

memcpy(CF.nlogin,"smarti",20);

memcpy(CF.npass,"smarti",20);

memcpy(CF.cip,"smarti",20);

memcpy(CF.cport,"smarti",20);

memcpy(CF.cid,"smarti",20);

memcpy(CF.rdno,"smarti",20);

memcpy(CF.chno,"smarti",20);

memcpy(CF.ctype,"smarti",20);

login1 =String (CF.login) ;

pass1 = String (CF.pass);

wlogin1 =String (CF.wlogin);

wpass1 = String (CF.wpass);

nlogin1 = String (CF.nlogin);

npass1 = String (CF.npass);

nauth1 = String (CF.nauth);

cip1 = String (CF.cip);

cport1 = String (CF.cport);

ctype1 = String (CF.ctype);

cid1 = String (CF.cid);

rdno1 = String (CF.rdno);

chno1 = String (CF.chno);

void handleRoot() { // first page

q=0;

String e= "esp htspot";

String set="/login";
String content= "<html><body><p align=\"center\"><font size=\"5\">" +e+ "</font></p><br><br>";

content += "<form action=\""+set+"\" method='POST'><p align=\"center\">";

content += "<font size=\"5\">USER ID:<br><input type=\"text\" name=\"login1\" size=\"50\" ><br>";

content += "PASSWORD:<br><input type=\"password\" name=\"pass1\" value="" size=\"50\"><br><br>";

content += "<input type=\"submit\" value=\"Submit\"></font></p></form></body></html>";

server.send(200, "text/html", content);

DPrint(D_INFO,"login: ",login1);

DPrint(D_INFO,"password: ",pass2);

q=1;

void handleFinish(){ //last page

if(q==2)

login2 = server.arg("login");

pass2 = server.arg("pass");

wlogin2 = server.arg("wlogin1");

wpass2 = server.arg("wpass1");

nlogin2 = server.arg("nlogin1");

npass2 = server.arg("npass1");

nauth2 = server.arg("nauth1");

cip2 = server.arg("cip1");

cport2 = server.arg("cport1");

ctype2 = server.arg("ctype1");

cid2 = server.arg("cid1");

rdno2 = server.arg("rdno1");

chno2 = server.arg("chno1");

server.send(200, "text/html", "FINISH");

login2.toCharArray( CF.login,20);

pass2.toCharArray( CF.pass,20);

wlogin2.toCharArray( CF.wlogin,20);

wpass2.toCharArray(CF.wpass,20);
nlogin2.toCharArray( CF.nlogin,20);

npass2.toCharArray( CF.npass,20);

nauth2.toCharArray( CF.nauth,20);

cip2.toCharArray( CF.cip,20);

cport2.toCharArray( CF.cport,20);

cid2.toCharArray( CF.cid,20);

ctype2.toCharArray( CF.ctype,20);

rdno2.toCharArray( CF.rdno,20);

chno2.toCharArray( CF.chno,20);

//Serial.println("all data incoming");

PrintAllPara();

EStrWrite(0,(unsigned char *)&CF,sizeof(CF));

m=INITIALISE;

q=3;

WiFi.disconnect();

else

String content="<html><body><p align=\"center\"><font size=\"5\">no no no no!!!</font></p><br><br>";

server.send(200, "text/html", content);

void handleLogin2(){

if(q==2) //second page

login2 = String(CF.login);

pass2 = String(CF.pass);

wlogin2 = String(CF.wlogin);

wpass2 = String(CF.wpass);
nlogin2 = String(CF.nlogin);

npass2 = String(CF.npass);

nauth2 = String(CF.nauth);

cip2 = String(CF.cip);

cport2 = String(CF.cport);

ctype2 = String(CF.cid);

rdno2 = String(CF.rdno);

chno2 = String(CF.chno);

String content= "<html><body><p align><font size=\"5\"> IP ADRESS OF THIS DEVICE:"+my_ip+"


</font></p><br><br><form method=\"POST\" action=\"/finish\"><p ><font size=\"5\">";

content+="MY NAME:<input type=\"text\" name=\"login\" value=\""+login2+"\" size=\"50\" ><br>";

content+="MY PASSWORD:<input type=\"text\" name=\"pass\" value=\""+pass2+"\" size=\"50\"><br><font


size=\"5\">" ;

content+="LOGIN NAME:<input type=\"text\" name=\"wlogin1\" value=\""+wlogin2+"\" size=\"50\" ><br><font


size=\"5\">";

content+="LOGIN PASSWORD:<input type=\"text\" name=\"wpass1\" value=\""+wpass2+"\" size=\"50\"


><br><br>";

content+="NETWORK NAME:<input type=\"text\" name=\"nlogin1\" value=\""+nlogin2+"\" size=\"50\" ><br>";

content+="PASSWORD:<input type=\"text\" name=\"npass1\" value=\""+ npass2+"\" size=\"50\"><br>";

content+="AUTHENTICTION TYPE:<select name=\"nauth1\" ><option value=\"WPA\">WPA</option><option


value=\"AES\">AES</option><option value=\"OPEN\">OPEN</option></select><br><br>";

content+="CONTROLLER IP:<input type=\"text\" name=\"cip1\" value=\""+ cip2+"\" size=\"50\" ><br>";

content+="CONTROLLER PORT:<input type=\"text\" name=\"cport1\" value=\""+cport2+"\" size=\"50\"><br><font


size=\"5\">CONTROLLER ID:<input type=\"text\" name=\"cid1\" value=\"\" size=\"50\" ><br>";

content+="CONNECTION TYPE:<select name=\"ctype1\" ><option value=\"UDP\">UDP</option><option


value=\"TCP\">TCP</option><option value=\"AUTHUDP\">AUTH UDP</option><option
value=\"AUTHTCP\">AUTH TCP</option></select><br><br>";

content+="READER NO:<input type=\"text\" name=\"rdno\" value=\""+rdno2+"\" size=\"50\" ><br>";

content+="CHANNEL NO:<input type=\"text\" name=\"chno\" value=\""+chno2+"\"


size=\"50\"><br></select><br><br><input type=\"submit\" value=\"Submit\"><br></font></p></form>";

server.send(200, "text/html", content);

c=c+1;
DPrint(D_INFO,"login: ",login2);

DPrint(D_INFO,"pass: ",pass2);

DPrint(D_INFO,"HOTSPOT login: ",wlogin2);

DPrint(D_INFO,"HOTSPOT pass: ",wpass2);

DPrint(D_INFO,"WIFI NAME: ",nlogin2);

DPrint(D_INFO,"WIFI PASS:: ",npass2);

DPrint(D_INFO,"CONTROLLER IP: ",cip2);

DPrint(D_INFO,"CONTROLLER PORT: ",cport2);

DPrint(D_INFO,"CONTROLLER ID: ",cid2);

DPrint(D_INFO,"CONTROLLER TYPPE: ",ctype2);

DPrint(D_INFO,"READER NO: ",rdno2);

DPrint(D_INFO,"CHANNEL NO: ",chno2);

else

String wrong="<html><body><p align=\"center\"><font size=\"5\">PLEASE LOGIN


FIRST!!!</font></p><br><br>";

server.send(200, "text/html", wrong);

void handleLogin(){

if(q==1) //second page

String login = server.arg("login1");

String pass= server.arg("pass1");

DPrint(D_INFO,"LOGIN: ",login);

DPrint(D_INFO,"PASSWORD: ",pass);

if (login.equals(login1) && pass.equals(pass1))

{
String content= "<html><body><p align><font size=\"5\"> < ESP Hot spot name> </font></p><br><br><form
method=\"POST\" action=\"/login2\"><p ><font size=\"5\">";

content+="YOU HAVE SUCESFULLY LOGGED IN";

content += "<input type=\"submit\" value=\"Submit\"></font></p></form></body></html>";

server.send(200, "text/html", content);

q=2;

else

String e= "esp htspot";

String set="/";

String wrong="<html><body><p align=\"center\"><font size=\"5\"> WRONG LOGIN ID


PASSWORD!</font></p><br><br>";

String content= e;

content += "<form action=\""+set+"\" method='POST'><p align=\"center\">";

content += "<input type=\"submit\" value=\"Submit\"></font></p></form></body></html>"+wrong;

server.send(200, "text/html", content);

q=0;

else

String wrong="<html><body><p align=\"center\"><font size=\"5\">PLEASE LOGIN FIRST!!!</font></p><br><br>";

server.send(200, "text/html", wrong);

void setup(void){
delay(5000);

Serial.begin(38400); // serial port runs at baud rate 38400 because the reader runs at baudrate 38400

EEPROM.begin(512);// required for eeprom read- write

WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); // the user can acess the login and other pages on
value of apIP

DPrint(D_INFO,"Started : ",String(m));

IniDefault();

WiFi.mode(WIFI_AP); //Only Access point

WiFi.softAP(CF.wlogin,CF.wpass); //Start HOTspot removing password will disable security

IPAddress myIP = WiFi.softAPIP(); //Get IP address

DPrint(D_INFO,"HotSpt IP:" ,String(myIP));

DPrint(D_INFO,"login1",login1);

DPrint(D_INFO,"pass1",pass1);

server.on("/", handleRoot);

server.on("/login", handleLogin);

server.on("/finish", handleFinish);

server.on("/login2", handleLogin2);

server.begin();

DPrint(D_INFO,"HTTP server started","");

String a="";

String b="";

void loop(void)

int count;

server.handleClient(); // required to handle the client


if (m==INITIALISE)

IniDefault(); // defined above

if(!(count++ %100))

{ PrintAllPara();

//ESP8266WebServer server(80);

char c[10];

if(WiFi.status() != WL_CONNECTED)

WiFi.disconnect();

WiFi.begin(CF.nlogin,CF.npass);

DPrint(D_INFO,"TRYING TO CONNECT ","");

DPrint(D_INFO,"",String(CF.nlogin));

int i =100000;

while(i--)

if(i == 100000-10000) //wait till i=10000 so esp 8266 tries to connect to wifi

//if it isnt able to connect still then wait for the user to put in new wifi login and password till i=100000

DPrint(D_INFO,"TRY CONNECTING ","");

WiFi.mode(WIFI_AP); //Only Access point

WiFi.softAP(CF.wlogin,CF.wpass); //Start HOTspot removing password will disable security

IPAddress myIP = WiFi.softAPIP(); //Get IP address

server.handleClient();

delay(1);

if(WiFi.status() == WL_CONNECTED) // if wifi is connected in between break loop


{

DPrint(D_INFO,"Local IP :",String(WiFi.localIP()));

DPrint(D_INFO,"CONNECTED TO : ",String(CF.wlogin));

break;

else

my_ip=WiFi.localIP().toString();

DPrint(D_INFO,"Local IP :",my_ip);

DPrint(D_INFO,"CONNECTED TO : ",String(CF.wlogin));

m=1;

if(m == 1) // once esp is connected to wifi it jumps to this step

if (ctype1.equals("TCP"))

m= TCP_CONNECT ;

DPrint(D_INFO,"TRYING TO CONNECT TO TCP ","");

if (ctype1.equals("UDP"))

m=UDP_CONNECT;

DPrint(D_INFO,"TRYING TO CONNECT TO UDP","");

if( m == TCP_CONNECT )
{

server.handleClient();

int port = atoi(CF.cport);

if (!client.connect(CF.cip, port))

DPrint(D_INFO,"TCP connection failed","");

DPrint(D_INFO,"CONTROLLER IP",String(CF.cip));

DPrint(D_INFO,"CONTROLLER PORT",String(port));

return;

server.handleClient();

else

m=TCP_WORKING ;

DPrint(D_INFO,"TCP connected","");

if(m==TCP_WORKING )

while(client.available())

//Serial.print(client.read());

String line = client.readString();

DPrint(D_INFO,line,"");

}
while(Serial.available())

// send data to server

String old = Serial.readString();

client.print(old);

if(m==UDP_CONNECT )

DPrint(D_INFO,"UDP WORKING","");

int port = atoi(CF.cport);

Udp.begin(port);

DPrint(D_INFO,"Now listening at IP", WiFi.localIP().toString().c_str());

DPrint(D_INFO,"PORT", String(port));

// Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_s0tr(), port);

m=UDP_WORKING;

if(m==UDP_WORKING)

// IPAddress udp_ip(0, 0, 0, 0);

// int udp_port=0;

//Serial.print("in udp loop");

int packetSize = Udp.parsePacket();

if (packetSize)

// receive incoming UDP packets


DPrint(D_INFO,Udp.remoteIP().toString().c_str(),"");

DPrint(D_INFO,"FROM PORT",String(Udp.remotePort()));

//Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(),


Udp.remotePort());

int len = Udp.read(incomingPacket, 255);

udp_ip=Udp.remoteIP();

//memcpy(udp_ip,Udp.remoteIP(),20);

udp_port=Udp.remotePort();

//char *udp_ip[20];

//char *udp_port[20];

if (len > 0)

incomingPacket[len] = 0;

Serial.printf(incomingPacket);

}// send back a reply, to the IP address and port we got the packet from

while(Serial.available())

Serial.print("udp printing");

Udp.beginPacket(udp_ip,udp_port);

char singham[40];

Serial.println("check this data out");

Serial.println(udp_ip);

Serial.println(udp_port);

Serial.readString().toCharArray(singham,40);

Udp.write(singham);

Udp.endPacket();

}
}

Das könnte Ihnen auch gefallen