unn + 8266 下位机 +网络 1.0-4.0 刷卡 密码4位

UNO 4.0(直接用8266的1.0及以上):

 

#define SS_PIN 10 //522引脚
#define RST_PIN 9 //522引脚
#define offj 7 //门锁检测开关
int kaisuo=3;//输出开门继电器
int a=2;//按下保存卡的UID
int del=4;//5秒钟连续按下10次以上清空所有卡

int del_ci=0;//删除uid的判断按键次数
int del_temps=0;//判断 按下的时间计时
int addr = 10;//存储密码  其实位置
int addrb=0;//存储是否存入的密码

int keyon=0;//全局变量 判断key几个相等
int dingshi_s=10;//定时多少秒上传一次
const int del_s=5;

String val;//接收字符穿
int keys[17];//4位数组的密码

//___________
char mac_s[18];
#define mac_addr 300

#include <SPI.h>
#include <ArduinoJson.h>
#include <MFRC522.h>
#include <EEPROM.h>

const char* mac;
char mac_c[17];
int t_temp=0;//每隔多少时间临时变量
const int arraySize = 4;

 int arr[arraySize]={55,66,77,100};
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 

// Init array that will store new NUID 
int nuidPICC[4];
boolean panduan_uid(int arr[]);
void t_time10(void);
void baocun_mima(void);
void(* resetFunc) (void) = 0; //制造重启命令
void del_all(void);//删除数据判断
void del_allshuju(void);//删除数据步骤
void setup() 
{ 
  Serial.begin(115200);
   pinMode(a,INPUT_PULLUP);
   pinMode(offj,INPUT_PULLUP);
   pinMode(del,INPUT_PULLUP);
   pinMode(kaisuo,OUTPUT);
    SPI.begin(); // Init SPI bus
    rfid.PCD_Init(); // Init MFRC522 
// panduan_uid(arr,arraySize);

    for (byte i = 0; i < 6; i++) 
    {
      key.keyByte[i] = 0xFF;
    }
     
  //循环读取 4个4位的地址的内容
   for(int z=0;z<4*4;z=z+4)
   {
    Serial.println("_______________");
    for(int i=0;i<4;i++)
    
    { //先打印出地址 
      Serial.print(addr+i+z);
        Serial.print(":");
        //读取地址的内容 赋值到keys数组里面
       keys[z+i]=EEPROM.read(addr+i+z);
        Serial.println( keys[z+i]);
       //
       if(keys[z+i]==0xFF){Serial.println(addr+i+z);}
        
    }


   }

    do 
    {
      Serial.print("mac");
      rx();//接收字符
      rx_macok();//分析mac
      delay(500);
      val="";  
      }while(mac[2]!=':');
       
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  delay(300);
   
}
 
void loop() {

 rx();
  //rx_macok();
  
  val="";  
  del_all();//判断是否删除所以uid

  //每10秒执行一次
  t_time10();


  
  if ( ! rfid.PICC_IsNewCardPresent())
    return;
  if ( ! rfid.PICC_ReadCardSerial())
    return;



MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }

  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  
    if(panduan_uid(nuidPICC)==true)
  {
    keyon=4;panduan_kaisuo();
    }
  else 
  {
    keyon=0;panduan_kaisuo();
    }

     if(digitalRead(a)==LOW)
     {
              delay(15);
              if(digitalRead(a)==LOW)
              {
                  for (byte i = 0; i < 4; i++) 
                  {
                           nuidPICC[i] = rfid.uid.uidByte[i];
                 }
                baocun_mima();
 

              }
      }
  
}


void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}


void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

void t_time10(void)
{
   if(millis()/(dingshi_s*1000)!=t_temp)
  {
    t_temp=millis()/(dingshi_s*1000);
    int g=analogRead(A0);
    if(digitalRead(offj)==LOW)
    {
      Serial.print("&statu=off&mac=");
        Serial.print(mac);
        Serial.print("&off=");
         Serial.println('1');
        }
   else{
      //Serial.print("&statu=off&off=1&mac=");
        Serial.print("&statu=off&mac=");
       Serial.print(mac);
       Serial.print("&off=");
         Serial.println('0');
         }
   }
  }

void rx(void)
{
  while(Serial.available()>0){
    val += char(Serial.read());
    delay(2);    
  }   
}

void rx_macok()
{
  if (val.length() > 0  )
    {
     
     StaticJsonBuffer<200> jsonBuffer;
      JsonObject& jiexi = jsonBuffer.parseObject(val);
      if (!jiexi.success()) {
     //Serial.print("mac");
     
     // return;
        }
        else{
         mac = jiexi["macok"];
        }
      
    }
    //Serial.print(val);
    
  }

void baocun_mima(void)
{
      if(panduan_uid(nuidPICC)==true)
      {
        Serial.println("yicunzai!!!!!!!!!!");
        }
      else{
              
                  for(int z=0;z<4;z++)
                {

                    if('1'!=EEPROM.read(addrb+z))
                  {
                    EEPROM.write(addr+(z*4),rfid.uid.uidByte[0]);
                    EEPROM.write(addr+1+(z*4),rfid.uid.uidByte[1]);
                    EEPROM.write(addr+2+(z*4),rfid.uid.uidByte[2]);
                    EEPROM.write(addr+3+(z*4),rfid.uid.uidByte[3]);
                    EEPROM.write(addrb+z,'1');
                    z=4;
                    break;
                   }
                   
                  }
               Serial.println((String)"&statu=write&uid=" + rfid.uid.uidByte[0]+":"+ rfid.uid.uidByte[1]+":"+ rfid.uid.uidByte[2]+":"+ rfid.uid.uidByte[3] +"&mac="+mac);
               delay(1000);
               resetFunc();      //重启程序开始
        }
  }
boolean panduan_uid(int arr[] )
{
  int cishu=0;
      for(int z=0;z<4*4;z=z+4)
          {
           //   Serial.println("^^^^^^^^^^^^^^^^^^");
            for(int i=0;i<4;i++)
            { 
              if(i==0)cishu=0;//不清零以防累计等于3
              //如果读取的值 判断是否等于 key地址的值(addr+i + 倍数)
              if (arr[i] == keys[z+i])
                {
                  cishu++;
                  }
              else {
                  cishu=0;//如果有一个不想等就直接清零,反正不会等于3了
                }
             
            }
            if(cishu==4){break;z=16;}
          }
          if(cishu==4){ return true;}
          else {return false;}
       
          
       
  }
  void panduan_kaisuo(void)
  {
     if(keyon==4)
        {
        Serial.println(String("&statu=on&uid=") + rfid.uid.uidByte[0]+":"+ rfid.uid.uidByte[1]+":"+ rfid.uid.uidByte[2]+":"+ rfid.uid.uidByte[3] +"&mac="+mac);
          digitalWrite(kaisuo,LOW);
          delay(800);
           digitalWrite(kaisuo,HIGH);
        }
        else  
            Serial.println(String("&statu=error&uid=") + rfid.uid.uidByte[0]+":"+ rfid.uid.uidByte[1]+":"+ rfid.uid.uidByte[2]+":"+ rfid.uid.uidByte[3] +"&mac="+mac);
        keyon=0;//每次判断后清理 
    
    }
void del_all(void)
{
  if(digitalRead(del)==LOW)
  {
    delay(10);
    if(digitalRead(del)==LOW)
      { if(del_ci==0)
        {
          del_temps=millis()/1000;
          }
        del_ci++;//删除uid的判断按键次数
              if(del_ci>10)
                {
                    if(millis()/1000-del_temps<del_s)
                    {
                    
                          del_allshuju();
                           del_ci=0;
                   }
                   else
                   {
                    del_temps=millis()/1000;
                    del_ci=0;
                   }
                }
        }
    }
  }
void del_allshuju(void)
{
   for(int z=0;z<4*4;z=z+4)
   {
    for(int i=0;i<4;i++)
    { 
       EEPROM.write(addr+i+z,0xff);
       if(z==0)
      EEPROM.write(addrb+i,0xff);
    }
    
   }
   Serial.println("del_alluid_ok!");
  }

 

 

 

UNO 3.0:

 

#define SS_PIN 10 //522引脚
#define RST_PIN 9 //522引脚
#define offj 7 //门锁检测开关
int kaisuo=3;//输出开门继电器
int a=2;//按下保存卡的UID
int del=4;//5秒钟连续按下10次以上清空所有卡

int del_ci=0;//删除uid的判断按键次数
int del_temps=0;//判断 按下的时间计时
int addr = 10;//存储密码  其实位置
int addrb=0;//存储是否存入的密码

int keyon=0;//全局变量 判断key几个相等
int dingshi_s=10;//定时多少秒上传一次
const int del_s=5;

String val;//接收字符穿
int keys[17];//4位数组的密码

//___________
char mac_s[18];
#define mac_addr 300

#include <SPI.h>
#include <ArduinoJson.h>
#include <MFRC522.h>
#include <EEPROM.h>

const char* mac;
char mac_c[17];
int t_temp=0;//每隔多少时间临时变量
const int arraySize = 4;

 int arr[arraySize]={55,66,77,100};
 
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 

// Init array that will store new NUID 
int nuidPICC[4];
//byte nuidPICC[4];
boolean panduan_uid(int arr[]);
void t_time10(void);
void baocun_mima(void);
void(* resetFunc) (void) = 0; //制造重启命令
void del_all(void);//删除数据判断
void del_allshuju(void);//删除数据步骤
void setup() 
{ 
  Serial.begin(115200);
   pinMode(a,INPUT_PULLUP);
   pinMode(offj,INPUT_PULLUP);
   pinMode(del,INPUT_PULLUP);
   pinMode(kaisuo,OUTPUT);
    SPI.begin(); // Init SPI bus
    rfid.PCD_Init(); // Init MFRC522 
// panduan_uid(arr,arraySize);

    for (byte i = 0; i < 6; i++) 
    {
      key.keyByte[i] = 0xFF;
    }
     
  //循环读取 4个4位的地址的内容
   for(int z=0;z<4*4;z=z+4)
   {
    Serial.println("_______________");
    for(int i=0;i<4;i++)
    
    { //先打印出地址 
      Serial.print(addr+i+z);
        Serial.print(":");
        //读取地址的内容 赋值到keys数组里面
       keys[z+i]=EEPROM.read(addr+i+z);
        Serial.println( keys[z+i]);
       //
       if(keys[z+i]==0xFF){Serial.println(addr+i+z);}
        
    }


   }
    /*
    for(int i =0; i<17;i++)
    {
      Serial.print(addr+i);
      Serial.print(":");
      Serial.println(EEPROM.read(addr+i));
      }
      */
    /*
    for(int i =0; i<17;i++)//读取的mac 然后打印出来
    {
      mac_s[i]=EEPROM.read(mac_addr+i);
      }
      
    Serial.println(mac_s);
    */
    do 
    {
      Serial.print("mac");
      rx();//接收字符
      rx_macok();//分析mac
      delay(500);
      val="";  
      }while(mac[2]!=':');
       
     
 /*
    Serial.print(v0);
    Serial.print(v1);
    Serial.print(v2);
    Serial.print(v3);
   */ 
    
 // Serial.println(F("This code scan the MIFARE Classsic NUID."));
 // Serial.print(F("Using the following key:"));
 
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  delay(300);
   
}
 
void loop() {

 rx();
  //rx_macok();
  
  val="";  
  del_all();//判断是否删除所以uid

  //每10秒执行一次
  t_time10();


  
  if ( ! rfid.PICC_IsNewCardPresent())
    return;
  if ( ! rfid.PICC_ReadCardSerial())
    return;



MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  //Serial.println(rfid.PICC_GetTypeName(piccType));
/*
  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }
*/

      


 // Store NUID into nuidPICC array

    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   

  //printHex(rfid.uid.uidByte, rfid.uid.size);

  //  printDec(rfid.uid.uidByte, rfid.uid.size);
    //Serial.println();
  //  panduan_uid(nuidPICC);
  if(panduan_uid(nuidPICC)==true)
  {keyon=4;panduan_kaisuo();}
  else {keyon=0;panduan_kaisuo();}

  
 

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  




     if(digitalRead(a)==LOW)
     {
              delay(15);
              if(digitalRead(a)==LOW)
              {
                  for (byte i = 0; i < 4; i++) 
                  {
                           nuidPICC[i] = rfid.uid.uidByte[i];
                 }
                baocun_mima();
 

              }
      }
  
}


void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}


void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

void t_time10(void)
{
   if(millis()/(dingshi_s*1000)!=t_temp)
  {
    t_temp=millis()/(dingshi_s*1000);
    int g=analogRead(A0);
    if(digitalRead(offj)==LOW)
    {
      Serial.print("&statu=off&mac=");
        Serial.print(mac);
        Serial.print("&off=");
         Serial.println('1');
        }
   else{
      //Serial.print("&statu=off&off=1&mac=");
        Serial.print("&statu=off&mac=");
       Serial.print(mac);
       Serial.print("&off=");
         Serial.println('0');
         }
   }
  }

void rx(void)
{
  while(Serial.available()>0){
    val += char(Serial.read());
    delay(2);    
  }   
}

void rx_macok()
{
  if (val.length() > 0  )
    {
     
     StaticJsonBuffer<200> jsonBuffer;
      JsonObject& jiexi = jsonBuffer.parseObject(val);
      if (!jiexi.success()) {
     //Serial.print("mac");
     
     // return;
        }
        else{
         mac = jiexi["macok"];
       /*
        for(int i=0;17>i;i++)
        {
          EEPROM.write(mac_addr+i,mac[i]);
          delay(3);
        }
        */
        }
      
    }
    //Serial.print(val);
    
  }

void baocun_mima(void)
{
      if(panduan_uid(nuidPICC)==true)
      {
        Serial.println("yicunzai!!!!!!!!!!");
        }
      else{
              
                  for(int z=0;z<4;z++)
                {
                     Serial.print(addrb+z);
                      Serial.print(":");
                  Serial.println(EEPROM.read(addrb+z));
                    if('1'!=EEPROM.read(addrb+z))
                  {
                    EEPROM.write(addr+(z*4),rfid.uid.uidByte[0]);
                    EEPROM.write(addr+1+(z*4),rfid.uid.uidByte[1]);
                    EEPROM.write(addr+2+(z*4),rfid.uid.uidByte[2]);
                    EEPROM.write(addr+3+(z*4),rfid.uid.uidByte[3]);
                    EEPROM.write(addrb+z,'1');
                    z=4;
                    break;
                   }
                   
                  }
               Serial.println((String)"&statu=write&uid=" + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);
               resetFunc();      //重启程序开始
        }
  }
boolean panduan_uid(int arr[] )
{
  int cishu=0;
      for(int z=0;z<4*4;z=z+4)
          {
              Serial.println("^^^^^^^^^^^^^^^^^^");
            for(int i=0;i<4;i++)
            { 
              //Serial.println(arr[i]);
              if(i==0)cishu=0;//不清零以防累计等于3
              //如果读取的值 判断是否等于 key地址的值(addr+i + 倍数)
              if (arr[i] == keys[z+i])
                {
                  cishu++;Serial.println(cishu);
                  }
              else {
                  cishu=0;//如果有一个不想等就直接清零,反正不会等于3了
                }
            Serial.print(rfid.uid.uidByte[i]); Serial.print("_"); Serial.print(keys[z+i]);Serial.println();
             
            }
            if(cishu==4){break;z=16;}
          }
          if(cishu==4){ return true;}
          else {return false;}
       
          
       
  }
  void panduan_kaisuo(void)
  {
     if(keyon==4)
        {
          Serial.println(String("&statu=on&uid=") + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);
          digitalWrite(kaisuo,LOW);
          delay(800);
           digitalWrite(kaisuo,HIGH);
        }
        else  
            Serial.println(String("&statu=error&uid=") + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);
        keyon=0;//每次判断后清理 
    
    }
void del_all(void)
{
  if(digitalRead(del)==LOW)
  {
    delay(10);
    if(digitalRead(del)==LOW)
      { if(del_ci==0)
        {
          del_temps=millis()/1000;
          }
        del_ci++;//删除uid的判断按键次数
              if(del_ci>10)
                {
                    if(millis()/1000-del_temps<del_s)
                    {
                    
                          del_allshuju();
                           del_ci=0;
                   }
                   else
                   {
                    del_temps=millis()/1000;
                    del_ci=0;
                   }
                }
        }
    }
  }
void del_allshuju(void)
{
   for(int z=0;z<4*4;z=z+4)
   {
    for(int i=0;i<4;i++)
    { 
       EEPROM.write(addr+i+z,0xff);
       if(z==0)
      EEPROM.write(addrb+i,0xff);
    }
    
   }
   Serial.println("del_alluid_ok!");
  }

 

 

 

 

UNO 2.0:

#define SS_PIN 10 //522引脚
#define RST_PIN 9 //522引脚
#define offj 7 //门锁检测开关
int kaisuo=3;//输出开门继电器
int a=2;//按下保存卡的UID


int addr = 10;//存储密码  其实位置
int addrb=0;//存储是否存入的密码
int keyon=0;//全局变量 判断key几个相等
int dingshi_s=10;//定时多少秒上传一次
String val;//接收字符穿
int keys[17];//4位数组的密码

char mac_s[18];
#define mac_addr 300

#include <SPI.h>
#include <ArduinoJson.h>
#include <MFRC522.h>
#include <EEPROM.h>

const char* mac;
char mac_c[17];
int t_temp=0;//每隔多少时间临时变量

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key; 

// Init array that will store new NUID 
byte nuidPICC[4];
void panduan_mima(void);
void t_time10(void);
void baocun_mima(void);
void(* resetFunc) (void) = 0; //制造重启命令
void setup() 
{ 
  Serial.begin(115200);
   pinMode(a,INPUT_PULLUP);
   pinMode(offj,INPUT_PULLUP);
   pinMode(kaisuo,OUTPUT);
    SPI.begin(); // Init SPI bus
    rfid.PCD_Init(); // Init MFRC522 
   
    for (byte i = 0; i < 6; i++) 
    {
      key.keyByte[i] = 0xFF;
    }
     
  //循环读取 4个4位的地址的内容
   for(int z=0;z<4*4;z=z+4)
   {
    Serial.println("_______________");
    for(int i=0;i<4;i++)
    
    { //先打印出地址 
      Serial.print(addr+i+z);
        Serial.print(":");
        //读取地址的内容 赋值到keys数组里面
       keys[z+i]=EEPROM.read(addr+i+z);
        Serial.println( keys[z+i]);
       //
       if(keys[z+i]==0xFF){Serial.println(addr+i+z);}
        
    }


   }
    /*
    for(int i =0; i<17;i++)
    {
      Serial.print(addr+i);
      Serial.print(":");
      Serial.println(EEPROM.read(addr+i));
      }
      */
    /*
    for(int i =0; i<17;i++)//读取的mac 然后打印出来
    {
      mac_s[i]=EEPROM.read(mac_addr+i);
      }
      
    Serial.println(mac_s);
    */
    do 
    {
      Serial.print("mac");
      rx();//接收字符
      rx_macok();//分析mac
      delay(500);
      val="";  
      }while(mac[2]!=':');
       
     
 /*
    Serial.print(v0);
    Serial.print(v1);
    Serial.print(v2);
    Serial.print(v3);
   */ 
    
 // Serial.println(F("This code scan the MIFARE Classsic NUID."));
 // Serial.print(F("Using the following key:"));
 
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  delay(300);
   
}
 
void loop() {

 rx();
  //rx_macok();
  
  val="";  


  //每10秒执行一次
  t_time10();


  
  if ( ! rfid.PICC_IsNewCardPresent())
    return;
  if ( ! rfid.PICC_ReadCardSerial())
    return;



MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  //Serial.println(rfid.PICC_GetTypeName(piccType));
/*
  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }
*/

      


 // Store NUID into nuidPICC array

    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   

  //printHex(rfid.uid.uidByte, rfid.uid.size);

  //  printDec(rfid.uid.uidByte, rfid.uid.size);
    //Serial.println();
    panduan_mima();
      

  
 

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  




     if(digitalRead(a)==LOW)
     {
              delay(15);
              if(digitalRead(a)==LOW)
              {
                baocun_mima();
                
                /*
                if('0'==EEPROM.read(addrb))
                {
                   EEPROM.write(addr,rfid.uid.uidByte[0]);
                  EEPROM.write(addr+1,rfid.uid.uidByte[1]);
                  EEPROM.write(addr+2,rfid.uid.uidByte[2]);
                  EEPROM.write(addr+3,rfid.uid.uidByte[3]);
                  EEPROM.write(addrb,'1');
                 }
                 else if('0'==EEPROM.read(addrb+1))
                  {
                    EEPROM.write(addr,rfid+4.uid.uidByte[0]);
                    EEPROM.write(addr+5,rfid.uid.uidByte[1]);
                    EEPROM.write(addr+6,rfid.uid.uidByte[2]);
                    EEPROM.write(addr+7,rfid.uid.uidByte[3]);
                    EEPROM.write(addrb+1,'1');
                    }
                    */
             

                
               
                Serial.println((String)"&statu=write&uid=" + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);
               resetFunc();      //重启程序开始
              }
      }
  
}


void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}


void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

void t_time10(void)
{
   if(millis()/(dingshi_s*1000)!=t_temp)
  {
    t_temp=millis()/(dingshi_s*1000);
    int g=analogRead(A0);
    if(digitalRead(offj)==LOW)
    {
      Serial.print("&statu=off&mac=");
        Serial.print(mac);
        Serial.print("&off=");
         Serial.println('1');
        }
   else{
      //Serial.print("&statu=off&off=1&mac=");
        Serial.print("&statu=off&mac=");
       Serial.print(mac);
       Serial.print("&off=");
         Serial.println('0');
         }
   }
  }

void rx(void)
{
  while(Serial.available()>0){
    val += char(Serial.read());
    delay(2);    
  }   
}

void rx_macok()
{
  if (val.length() > 0  )
    {
     
     StaticJsonBuffer<200> jsonBuffer;
      JsonObject& jiexi = jsonBuffer.parseObject(val);
      if (!jiexi.success()) {
     //Serial.print("mac");
     
     // return;
        }
        else{
         mac = jiexi["macok"];
       /*
        for(int i=0;17>i;i++)
        {
          EEPROM.write(mac_addr+i,mac[i]);
          delay(3);
        }
        */
        }
      
    }
    //Serial.print(val);
    
  }

void baocun_mima(void)
{
                  for(int z=0;z<4;z++)
                {
                     Serial.print(addrb+z);
                      Serial.print(":");
                  Serial.println(EEPROM.read(addrb+z));
                    if('1'!=EEPROM.read(addrb+z))
                  {
                    EEPROM.write(addr+(z*4),rfid.uid.uidByte[0]);
                    EEPROM.write(addr+1+(z*4),rfid.uid.uidByte[1]);
                    EEPROM.write(addr+2+(z*4),rfid.uid.uidByte[2]);
                    EEPROM.write(addr+3+(z*4),rfid.uid.uidByte[3]);
                    EEPROM.write(addrb+z,'1');
                    z=4;
                    break;
                   }
                   
                  }
  }
void panduan_mima(void)
{
  for(int z=0;z<4*4;z=z+4)
          {
              Serial.println("^^^^^^^^^^^^^^^^^^");
            for(int i=0;i<4;i++)
            { 
              if(i==0)keyon=0;//不清零以防累计等于3
              //如果读取的值 判断是否等于 key地址的值(addr+i + 倍数)
              if (rfid.uid.uidByte[i] == keys[z+i])
                {
                  keyon++;Serial.println(keyon);
                  }
              else {
                  keyon=0;//如果有一个不想等就直接清零,反正不会等于3了
                }
           // Serial.print(rfid.uid.uidByte[i]); Serial.print("_"); Serial.print(keys[z+i]);Serial.println();
             
            }
            if(keyon==4){break;z=16;}
          }
        if(keyon==4)
        {
          Serial.println(String("&statu=on&uid=") + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);
          digitalWrite(kaisuo,LOW);
          delay(800);
           digitalWrite(kaisuo,HIGH);
        }
        else  
            Serial.println(String("&statu=error&uid=") + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);
        keyon=0;//每次判断后清理 
  }

 

 

uno1.0:

int a=2;
int s=3;
int addr = 0;
String val;
int v0=0;
int v1=0;
int v2=0;
int v3=0;
char mac_s[18];
#define mac_addr 300
#define offj 7
#include <SPI.h>
#include <ArduinoJson.h>
#include <MFRC522.h>
#include <EEPROM.h>
#define SS_PIN 10
#define RST_PIN 9
const char* mac;
char mac_c[17];
int t_temp=0;
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key; 

// Init array that will store new NUID 
byte nuidPICC[4];
void t_time10(void);
void setup() { 
  Serial.begin(115200);
  pinMode(a,INPUT_PULLUP);
   pinMode(offj,INPUT_PULLUP);
  SPI.begin(); // Init SPI bus
  rfid.PCD_Init(); // Init MFRC522 

  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
    
  }
v0=EEPROM.read(addr);
    v1=EEPROM.read(addr+1);
    v2=EEPROM.read(addr+2);
    v3=EEPROM.read(addr+3);
    for(int i =0; i<17;i++)
    {
      mac_s[i]=EEPROM.read(mac_addr+i);
      }
      
    Serial.println(mac_s);
    
    do 
    {
      Serial.print("mac");
      rx();
      rx_macok();
      delay(1000);
      }while(mac[2]!=':');
      
     
 /*
    Serial.print(v0);
    Serial.print(v1);
    Serial.print(v2);
    Serial.print(v3);
   */ 
    
 // Serial.println(F("This code scan the MIFARE Classsic NUID."));
 // Serial.print(F("Using the following key:"));
 
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  delay(300);
    Serial.print("mac");  
}
 
void loop() {

 rx();
  rx_macok();
  
  val="";  


  //每10秒执行一次
  t_time10();


  
  if ( ! rfid.PICC_IsNewCardPresent())
    return;
  if ( ! rfid.PICC_ReadCardSerial())
    return;



MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  //Serial.println(rfid.PICC_GetTypeName(piccType));
/*
  // Check is the PICC of Classic MIFARE type
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));
    return;
  }
*/

      


 // Store NUID into nuidPICC array

    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];
    }
   

  //printHex(rfid.uid.uidByte, rfid.uid.size);

  //  printDec(rfid.uid.uidByte, rfid.uid.size);
    //Serial.println();
  if (rfid.uid.uidByte[0] == v0 &&
    rfid.uid.uidByte[1] == v1 &&
    rfid.uid.uidByte[2] ==v2 &&
    rfid.uid.uidByte[3] == v3 )
    {
      Serial.println(String("&statu=on&uid=") + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);
      delay(100);
       Serial.println("ok");

     }
      else
             Serial.println(String("&statu=error&uid=") + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);

  // Halt PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD
  rfid.PCD_StopCrypto1();
  




     if(digitalRead(a)==LOW)
     {
              delay(15);
              if(digitalRead(a)==LOW)
              {
                EEPROM.write(addr,rfid.uid.uidByte[0]);
                EEPROM.write(addr+1,rfid.uid.uidByte[1]);
                EEPROM.write(addr+2,rfid.uid.uidByte[2]);
                EEPROM.write(addr+3,rfid.uid.uidByte[3]);
                Serial.println((String)"&statu=write&uid=" + rfid.uid.uidByte[0]+ rfid.uid.uidByte[1]+ rfid.uid.uidByte[2]+ rfid.uid.uidByte[3] +"&mac="+mac);
              
              }
      }
  
}


void printHex(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}


void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

void t_time10(void)
{
   if(millis()/10000!=t_temp)
  {
    t_temp=millis()/10000;
    int g=analogRead(A0);
    if(digitalRead(offj)==LOW)
    {
      Serial.print("&statu=off&mac=");
        Serial.print(mac);
        Serial.print("&off=");
         Serial.println(g);
        }
   else{
      //Serial.print("&statu=off&off=1&mac=");
        Serial.print("&statu=off&mac=");
       Serial.print(mac);
       Serial.print("&off=");
         Serial.println(g);
         }
   }
  }

void rx(void)
{
  while(Serial.available()>0){
    val += char(Serial.read());
    delay(2);    
  }   
}

void rx_macok()
{
  if (val.length() > 0  )
    {
     
     StaticJsonBuffer<200> jsonBuffer;
      JsonObject& jiexi = jsonBuffer.parseObject(val);
      if (!jiexi.success()) {
     //Serial.print("mac");
     
     // return;
        }
        else{
         mac = jiexi["macok"];
       /*
        for(int i=0;17>i;i++)
        {
          EEPROM.write(mac_addr+i,mac[i]);
          delay(3);
        }
        */
        }
      
    }
    //Serial.print(val);
    
  }

 

 

 

 

8266  1.0:

 

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
int num=0;
char pp[128];
String val="";
//自行修改wifi名和密码
const char* ssid     = "WiFi";
const char* password = "23456789";
const char* host = "xiaohe4966.zonezhi.com";
uint8_t MAC_array_STA[6];//MAC地址
char MAC_char_STA[18];
String statu="on";
String uid="uid";
String off=""; 
void mac(void);
void get_val(String val);
void setup() {
  Serial.begin(115200);//端口波特率
  Serial.println(ssid);
  //连接wifi
  WiFi.begin(ssid, password);
  //等待wifi连接
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  mac();
}

void loop() {
 
while(Serial.available()>0){
    val += char(Serial.read());
        delay(2);    
  } 
  if (val.length()> 0)
    {
        //Serial.println(val);
        if(val=="mac")
        {
          Serial.println(String("{\"macok\":")+"\""+ MAC_char_STA[0]+ MAC_char_STA[1]+":"+ MAC_char_STA[3]+  MAC_char_STA[4]+":"+ MAC_char_STA[6]+  MAC_char_STA[7]+":"+ MAC_char_STA[9]+  MAC_char_STA[10]+":"+MAC_char_STA[12]+  MAC_char_STA[13]+":"+MAC_char_STA[15]+  MAC_char_STA[16]+"\"}");
          }
         else
         {
          if(val.length() > 4)
    {
     // Serial.println(val);
      if(val[0]=='&')
            {
              get_val(val);
              }
      }
          }
        /*
        for(int i=0;i<val.length();i++)
        {
          pp[i]=val[i];
          }
        JsonObject& jiexi = jsonBuffer.parseObject(pp);
        if (!jiexi.success()) 
        {
           Serial.println("parseObject() failed");
          return;
           }
           const char* statu = jiexi["statu"];
        */
        
       
         //  Serial.println(MAC_char_STA);
          
    }
   
     val="";
 
}
void mac(void)
{
   WiFi.macAddress(MAC_array_STA);
   for (int i = 0; i < sizeof(MAC_array_STA); ++i) {
    sprintf(MAC_char_STA, "%s%02X ", MAC_char_STA, MAC_array_STA[i]);
   }
}

void get_val(String val)
{



  
   Serial.print("************* ");
  Serial.println(millis());//打印系统运行毫秒
  WiFiClient client;
  num++;
  if (!client.connect(host,80)) {
    Serial.println("connection failed");//连接服务器失败
    return;
  }
  //GET提交网络  可自己百度GET提交方式
  String url = "http://xiaohe4966.zonezhi.com/8266/door.php?";
  client.print(String("GET ") + url + val+ " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

 //val +"&uid="+uid + "&off=" + off + "&mac="+ MAC_char_STA[0]+ MAC_char_STA[1]+":"+ MAC_char_STA[3]+  MAC_char_STA[4]+":"+ MAC_char_STA[6]+  MAC_char_STA[7]+":"+ MAC_char_STA[9]+  MAC_char_STA[10]+":"+MAC_char_STA[12]+  MAC_char_STA[13]+":"+MAC_char_STA[15]+  MAC_char_STA[16]
  delay(5);
  Serial.println(millis());//打印系统运行毫秒
  //获取服务器返回的信息给line变量
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  //结束  
  Serial.println();
  Serial.println(millis());
  Serial.println("________");
  
}

 其实get访问只需要发送这样就可以成功(来自丁丁串口调试):GET http://xiaohe4966.zonezhi.com/ip.php \r\n

posted @ 2017-10-02 22:23  xiaohe520  阅读(144)  评论(0编辑  收藏  举报