一段时间后,以太网屏蔽将停止

时间:2019-01-16 16:35:44

标签: arduino ethernet rfid

我在Arduino Uno上使用了以太网屏蔽(W5100)和RC522。它可以工作1或2个小时(有时15分钟-有时2天),经过此随机时间后,它将停止工作。我的意思是,RC-522模块不读取卡,并且以太网屏蔽无法连接服务器。当我拔下电源(应提供1.5A-12 V电源)并重新插上电源后,它将开始成功工作。

我需要此系统永久运行...该系统读取mifare卡,并将其发送到服务器,然后检查答复,如果答复为“ 1”,则触发中继。 (继电器是5V简单继电器)

有人说“更换您的适配器”,而我更改了它,没有任何改变。 有人说“在rst和gnd引脚之间使用10微法拉电容器”并没有改变。 有人说:“这是arduino dude,它只是为了让studend放弃并使用stm32”,而我尚未应用此建议。我想知道为什么会这样。

#include <SPI.h>
#include <Ethernet.h>
#include <MFRC522.h>

//Mac address of ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEA }; 

//My Network info, i use static ip
byte ip[] = { 172, 16, 64, 78 }; 
byte gateway[] = { 172, 16, 64, 1 }; 
byte myserver[] = { 172, 16, 64, 46 }; 
byte subnet[] = { 255, 255, 255, 0 }; 

String CardInfo = "";
EthernetClient client;
String GateNo = "0";
String DeviceNo = "100";

String Answer = "";


MFRC522 mfrc522;
byte Key[] = { 0xff,0xff,0xff,0xff,0xff,0xff };
MFRC522::MIFARE_Key key;


void setup(){
  //Disabling SD Card
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, subnet, gateway);

  KeyCreate();

  mfrc522.PCD_Init(2, 8); 
  mfrc522.PCD_DumpVersionToSerial();

}



void sendGET()
{
  //I used this line to guarantee the disconnect from server
  client.stop();
  Answer = "";
  if (client.connect(myserver, 81)) {  
    client.println("GET /AccessCheck/CardNo=" + CardInfo + "&GateNo=" + GateNo + "&DeviceNo=" + DeviceNo + " HTTP/1.0");
    client.println("Authorization: Basic xxxxxxxxxxxx");
    client.println(); 
  } 
  else {
    //Ethernet.begin(mac, ip, subnet, gateway);
    return;
  }

  int connectLoop = 0;
  while(client.connected())
  {
    while(client.available())
    {
      char c = client.read();
      Answer = Answer + c;
      connectLoop = 0;
    }

    delay(1);
    connectLoop++;
    if(connectLoop > 5000)
    {
      client.stop();
      return;
    }
  }
  client.stop();
}

//This function disables eth and enable rc522 (and reverse)
void Switch(int i)
{
  switch (i)
  {
    case 0:
      digitalWrite(10, HIGH);
      digitalWrite(2, LOW);
      break;
    case 1:
      digitalWrite(2, HIGH);
      digitalWrite(10, LOW);
      break;
  }
}

void AccessControl()
{
  int AnswerLength = Answer.length();

  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);

  if(Answer[AnswerLength-1] == 49)
  {
    digitalWrite(5, LOW);
    delay(100);
    digitalWrite(5, HIGH);
  }
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  delay(1000);
}

void ReadCard()
{
  byte len = 18;
  MFRC522::StatusCode status;

  byte MyBuffer[18];
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 10, &key, &(mfrc522.uid));
  status = mfrc522.MIFARE_Read(10, MyBuffer, &len);
  int counter = 0;
  //This line is check for turkish character
  if(MyBuffer[0] == 221)
  {
    CardInfo = "X";
    for (int i = 1; i < 16; i++)
    {
      if (MyBuffer[i] != 32)
      {
        CardInfo = CardInfo + (char)MyBuffer[i];
      }
    }
  }
  else
  {
    CardInfo = "";
    for (int i = 0; i < 16; i++)
    {
      if (MyBuffer[i] != 32)
      {
        CardInfo = CardInfo + (char)MyBuffer[i];
      }
    }
  }

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
  return;
}

void KeyCreate()
{
  for (int i = 0; i < 6; i++)
  {
    key.keyByte[i] = Key[i];
  }
}

void loop(){
  Switch(0);
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
        ReadCard(); 
        Switch(1);
        sendGET();
        AccessControl();
  }
} 

我希望它运行时不会冻结

实际结果是一段时间后以太网屏蔽冻结

1 个答案:

答案 0 :(得分:0)

我在某些脚本中使用看门狗。如果您在重置过程中不重置看门狗,则此功能可以自动重置您的arduino。

您可以在循环开始时在setup()和wd_reset()中执行wdt_enable()

time before watchdog firing    argument of wdt_enable()
-------------------------------------------------------
15mS                           WDTO_15MS
30mS                           WDTO_30MS
60mS                           WDTO_60MS
120mS                          WDTO_120MS
250mS                          WDTO_250MS
500mS                          WDTO_500MS
1S                             WDTO_1S            
2S                             WDTO_2S
4S                             WDTO_4S
8S                             WDTO_8S

使用示例:

#include <avr/wdt.h>

void setup()
{
  wdt_enable(WDTO_4S);       // enable the watchdog
                             // will fire after 4s without reset
}

void loop(){
 wdt_reset();              // resets the watchdog timer count
     :
     :
                     // if program hangs more than 4s, launch the reset of arduino
}