arduino - 一段时间后以太网屏蔽停止
问题描述
我在我的 Arduino Uno 上使用以太网屏蔽 (W5100) 和 RC522。它工作 1 或 2 小时(有时 15 分钟 - 有时 2 天) 在这个随机时间之后,它停止工作。我的意思是停止,RC-522模块不读卡,以太网屏蔽无法连接服务器。当我拔下电源(1.5A - 12 V 电源)并重新插入时,它开始成功工作。
我需要这个系统永远工作......这个系统读取mifare卡,并发送到服务器,然后检查回复,如果回复为“1”,则触发中继。(继电器为5V简易继电器)
有人说“换你的适配器”,我换了,没有任何改变。有人说“在 rst 和 gnd 引脚之间使用 10 微法拉电容器”,但没有任何改变。有人说,“这是arduino伙计,只是学生放弃并使用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();
}
}
我希望它运行时不会冻结
实际结果是一段时间后以太网屏蔽冻结
解决方案
我在一些脚本中使用看门狗。如果您在间隔期间不重置看门狗,此功能可以自动重置您的 arduino。
您在 setup() 中执行 wdt_enable() 并在循环开始时执行 wd_reset()
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
}
推荐阅读
- reporting-services - SSRS - 折线图超过最大 Y 值
- react-native - 如何在 react native 中使用 react-native-azure-ad-2 从 Microsoft 获取 idToken?
- python - 如何在视图中删除带有条件的obj
- mysql - SQLSTATE[HY000]: errno: 150 "外键约束格式不正确
- ios - 当整个屏幕设置为 RTL 时,我们如何将语义更改为 tableview 单元格的 LTR?
- java - Java:如何生成 KeyPair 形式的私钥,只使用没有 BouncyCastle 的纯 Java.Security?
- string - 使用应用程序语言(最好是批处理脚本)拆分出现次数可变的字符串
- css - 无法使用 Flexbox 使 div 右对齐
- mongodb - Loopback 4:当与 MongoDB 建立连接时,测试永远不会结束
- javascript - 当我增加 textcontent 值时,减少按钮仍然被禁用