#include <ESP8266WiFi.h> //引入ESP8266Wifi库
#include <SPI.h>
#include <MFRC522.h>
#define OUT D3
const char* ssid = "Tenda_261AD0";
const char* password = "ghq13336261416";
const char* httpHost = "192.168.0.105";//服务器ip
const int httpPort = 8080;//服务器端口号
IPAddress local(192,168,0,104);//本地arduino ip
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0);
MFRC522 mfrc522(D10, D9); // 创建MFRC实例
void setup()
{
Serial.begin(115200);//端口初始化
Serial.println();
WiFi.begin(ssid, password); //就只需要这一句就可以连接到一个网络了,括号内参数分别为网络名称(SSID)和网络密码
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) //等待网络连接成功
{
delay(300);
Serial.print(".");
}
WiFi.config(local, gateway, subnet);
Serial.println();
Serial.print("Wifi OK! Arduino IP address: ");
Serial.println(WiFi.localIP()); //通过串口打印输出模块在网络中的IP地址
pinMode(OUT,OUTPUT);//蜂鸣器两声表示wifi连接成功
digitalWrite(OUT,HIGH);
delay(300);
digitalWrite(OUT,LOW);
delay(300);
digitalWrite(OUT,HIGH);
delay(300);
digitalWrite(OUT,LOW);
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("系统开启,请将你的卡片放置感应区...");
pinMode(D3,OUTPUT);
//pinMode(D2,OUTPUT);
}
void loop() {
// 寻找新卡
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//显示卡号 b6a5eebc 182165238188
//Serial.print("UID tag :");
String content= "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
//Serial.println(mfrc522.uid.uidByte[i]);
//Serial.print(mfrc522.uid.uidByte[i], HEX);
content+=mfrc522.uid.uidByte[i];
}
Serial.print("Card ID : ");
Serial.println(content);
if(content!="")
{
digitalWrite(OUT,HIGH);
delay(300);
digitalWrite(OUT,LOW);
}
String url = "/LIMS/ArduinoKQ?ID=";
url+=content;
WiFiClient client;
client.connect(httpHost, httpPort);
client.print(String("GET ")+url+"HTTP/1.1\r\n" +
"Host: " + httpHost + "\r\n" +
"Connection: Keep Alive\r\n");
unsigned long timeout = millis();
while(client.available() == 0){
if(millis() - timeout > 1000){
client.stop();
return;
}
}
while(client.available()){
String r = client.readStringUntil('\r');
if(r.equals("Yes"))
{
digitalWrite(D3,HIGH);
delay(300);
digitalWrite(D3,LOW);
Serial.println("考勤成功!");
}
else
{
digitalWrite(D3,HIGH);
delay(300);
digitalWrite(D3,LOW);
delay(300);
digitalWrite(D3,HIGH);
delay(300);
digitalWrite(D3,LOW);
delay(300);
Serial.println("考勤失败!");
}
}
}