首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ethernet Shield会在一段时间后停止

Ethernet Shield会在一段时间后停止
EN

Stack Overflow用户
提问于 2019-01-17 00:35:44
回答 1查看 794关注 0票数 0

我在我的Arduino Uno上使用以太网盾(W5100)和RC522。它工作1到2个小时(有时15分钟,有时2天),在这个随机时间之后,它停止工作。我的意思是停止,RC-522模块不读卡,以太网屏不能连接服务器。当我拔下电源(1.5A -12V电源)并重新插入时,它开始正常工作。

我需要这个系统永远有效..。本系统读取MIFARE卡,发送到服务器,检查应答,如果应答为"1",则触发中继。(继电器为5V简单继电器)

有人说“换个适配器”,我换了,什么都没变。有些人说“在rst和gnd引脚之间使用10微法拉电容”,但什么也没改变。有些人说,“这是阿杜伊诺兄弟,只是为了学生放弃和使用stm32",我还没有应用这个建议。我想知道为什么会这样。

代码语言:javascript
复制
#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();
  }
} 

我希望它运行时不会冻结

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

EN

回答 1

Stack Overflow用户

发布于 2019-01-17 04:12:57

我在一些脚本中使用了watchdog。如果你在失效期间不重置看门狗,这个功能可以自动重置你的arduino。

在setup()中执行wdt_enable(),在循环开始时执行wd_reset()

代码语言:javascript
复制
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

使用示例:

代码语言:javascript
复制
#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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54221477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档