Hello everyone. I come from a small company in China. Now I have developed a small project. The code is as follows.
My question is: why this code will crash and trigger the watchdog program when executed only 65 to 66 times?
I use arduino Uno development board .
My English is not very good, I need help from friends urgently. Thank you all
#include <Ethernet.h> #include <SPI.h> #include <avr/wdt.h> int pin[] = {2, 3, 4, 5, 6, 7, 8, 9, A0, A1, A2, A3, A4, A5}; bool state[14]; String value = ""; byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x02}; IPAddress ip(192, 168, 0, 31); char cloudDn[] = "cloud.caffz.com"; char server[] = "www.caffz.com"; EthernetClient client; void setup() { Serial.begin(4800); Serial.println("Hello CAFFZ!"); initialize(); wdt_enable(WDTO_8S); } void loop() { for (int i = 0; i < sizeof(state) / sizeof(state[0]); i++) { if (state[i]) { value.concat('1'); } else { value.concat('0'); } } request(); } void initialize() { String command = ""; for (int i = 0; i < sizeof(pin) / sizeof(pin[0]); i++) { pinMode(pin[i], OUTPUT); command.concat("0"); } execute(command); } void execute(String command) { for (int i = 0; i < command.length(); i++) { if (command.charAt(i) == '0') { digitalWrite(pin[i], LOW); state[i] = false; } else if (command.charAt(i) == '1') { digitalWrite(pin[i], HIGH); state[i] = true; } //Serial.print("No."); //Serial.print(i + 1); //Serial.print(" "); //Serial.println(command.charAt(i)); } } void check() { if (!Ethernet.begin(mac)) { Ethernet.begin(mac, ip); } Serial.print("local IP: "); Serial.println(Ethernet.localIP()); } void request() { check(); if (!client.connect(cloudDn, 12345)) { String str = getCloudIp(); if (str != "") { int num[4]; for (int i = 0; i < 4; i++) { int idx = str.indexOf("."); if (idx != -1) { num[i] = str.substring(0, idx).toInt(); str = str.substring(idx + 1); } else { num[i] = str.toInt(); } } IPAddress cloudIp(num[0], num[1], num[2], num[3]); if (!client.connect(cloudIp, 12345)) { return; } } else { return; } } client.print("GET /smartfarm2/a2268.php?c=control&a=select&number=226.8.3.1.1-8&value="); client.print(value); client.println("&token=www.caffz.com HTTP/1.1"); client.println("Host: cloud.caffz.com"); client.println("Connection: keep-alive"); //client.println("Connection: close"); client.println(); unsigned long start = millis(); String reply = ""; int sign = 0; while (true) { while (client.available()) { char c = client.read(); reply.concat(c); if (c == ' ') { if (sign == 0) { if (reply.indexOf("200") != -1) { sign = 1; } else { sign = -1; } } else if (sign == 1) { if (reply == " ") { sign = 2; delay(500); } } Serial.print(reply); reply = ""; } } if (sign == 2 || millis() - start > 5000) { break; } } Serial.println(reply); client.stop(); if (sign == 2) { execute(reply); wdt_reset(); } } String getCloudIp() { if (client.connect(server, 80)) { client.println("GET /czip/ip.php HTTP/1.1"); client.println("Host: www.caffz.com"); client.println("Connection: keep-alive"); //client.println("Connection: close"); client.println(); unsigned long start = millis(); String reply = ""; int sign = 0; while (true) { while (client.available()) { char c = client.read(); reply.concat(c); if (c == ' ') { if (sign == 0) { if (reply.indexOf("200") != -1) { sign = 1; } else { sign = -1; } } else if (sign == 1) { if (reply == " ") { sign = 2; delay(500); } } Serial.print(reply); reply = ""; } } if (sign == 2 || millis() - start > 5000) { break; } } Serial.print("cloud IP: "); Serial.println(reply); client.stop(); if (sign == 2) { return reply; } } return ""; }