我想用arduino yun輸出一臺機器生成的部件數量到谷歌電子表格。通過我的設置,我可以將數據暫時輸出到電子表格。但是,一段時間後,雲停止工作。我用紅色LED指示代碼正在運行,我不能再看到端口列表中的arduino。重置32u4芯片會使LED重新亮起,表示代碼正在運行,但該板仍然不出現在端口菜單中。Arduino yun幾小時後停止工作
我已經在我們的車間進行過測試,它已經運行了7-12小時,沒有任何問題。只有當我們把它帶到生產車間時,我們纔會開始遇到這些問題。有誰知道這個問題可能是什麼?這裏是代碼的最相關的部分:
#include <elapsedMillis.h>
#include <Process.h>
#include <Bridge.h>
#include "TimeLib.h"
// On Arduino: 0 - 1023 maps to 0 - 5 voltsf
#define VOLTAGE_MAX 5.0
#define VOLTAGE_MAXCOUNTS 1023.0
unsigned int buttonCount = 0;
float voltage = 0;
elapsedMillis timeSinceLastCycle = 0;
elapsedMillis transmitData = 0;
int pressFlag = 0;
Process date;
int hours, minutes, seconds;
int lastSecond = -1;
Process sendData;
String printDate() {
// String currTime = String(hours) + ":" + String(minutes) + ":" + String(seconds);
if (lastSecond != seconds) { // if a second has passed
// print the time:
if (hours <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.print(hours);
Console.print(":");
if (minutes <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.print(minutes);
Console.print(":");
if (seconds <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.println(seconds);
// restart the date process:
if (!date.running()) {
date.begin("date");
date.addParameter("+%T");
date.run();
}
}
//if there's a result from the date process, parse it:
while (date.available() > 0) {
// get the result of the date process (should be hh:mm:ss):
String timeString = date.readString();
// find the colons:
int firstColon = timeString.indexOf(":");
int secondColon = timeString.lastIndexOf(":");
// get the substrings for hour, minute second:
String hourString = timeString.substring(0, firstColon);
String minString = timeString.substring(firstColon + 1, secondColon);
String secString = timeString.substring(secondColon + 1);
// convert to ints,saving the previous second:
hours = hourString.toInt();
minutes = minString.toInt();
lastSecond = seconds; // save to do a time comparison
seconds = secString.toInt();
String currTime = hourString + ":" + minString + ":" + String(seconds);
return currTime;
}
}
void checkVoltage() {
int sensorValue = analogRead(A0);
voltage = sensorValue * (VOLTAGE_MAX/VOLTAGE_MAXCOUNTS);
Console.println(voltage);
delay(50);
if (voltage >= 4.9 && pressFlag == 0) {
Console.println("Delaying");
sensorValue = analogRead(A0);
voltage = sensorValue * (VOLTAGE_MAX/VOLTAGE_MAXCOUNTS);
if (pressFlag == 0 && voltage >= 4.9) {
unsigned long int intCycleTime = timeSinceLastCycle;
timeSinceLastCycle = 0;
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
printDate();
String pressTime = printDate();
Console.print("PressTime is ");
Console.println(pressTime);
buttonCount++;
Console.println(buttonCount);
pressFlag = 1;
String part1 = "curl -X POST -H \"Content-Type: application/json\" -d '{\"value1\":\"";
String timeString = pressTime;
Console.print(timeString + " seconds");
String part2 = "\",\"value2\":\"";
String numParts = String(buttonCount);
String part3 = "\",\"value3\":\"";
String strCycleTime = String(intCycleTime/1000); // + " seconds";
String part4 = "\"}' https://maker.ifttt.com/trigger/arduino2Request/with/key/gL8YmxeaUChOMJvmwpdXp -k";
//curl -X POST -H "Content-Type: application/json" -d '{"value1":"1","value2":"2","value3":"3"}' https://maker.ifttt.com/trigger/arduino2Request/with/key/gL8YmxeaUChOMJvmwpdXp
String curlString = part1 + timeString + part2 + numParts + part3 + strCycleTime + part4;
// The curl string sends data to oue excel spreadhsheet using the IFTTT web service
sendData.runShellCommandAsynchronously(curlString);
elapsedMillis breakTimer = 0;
/*while(sendData.running()){
if(breakTimer > 5*1000){
break;
}
} */
Console.print("Data Available: "); // A value of 32 indicates a successful transmission of data, 0 also works if run asynchronously.
Console.println(sendData.available());
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
}
}
else if (voltage < 2.5) {
pressFlag = 0;
}
}
void setup() {
Bridge.begin();
Console.begin();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
// run an initial date process. Should return:
// hh:mm:ss :
if (!date.running()) {
date.begin("date");
date.addParameter("+%T");
date.run();
}
}
void loop() {
checkVoltage();
if ((timeSinceLastCycle > 300000) && (transmitData > 300000)) { // If 5 minutes have elapsed without a part being produced, output that the arduino is transmitting even if not part is available
sendData.runShellCommand("curl -X POST -H \"Content-Type: application/json\" -d '{\"value1\":\"1\",\"value2\":\"Arduino Transmitting\"}' https://maker.ifttt.com/trigger/transmitData/with/key/gKRo-zSur5rj6rD5rviCaV2RHI5g56Dy0Vc0S_XJ-oO -k");
transmitData = 0;
}
}
更新:我增加了一系列的打印語句到checkVoltage功能。上面的代碼已經更新,以反映這一點。我發現它嘗試異步使用sendData.runShellCommand時會掛起。輸出類似於
1.58
1.54
5.00
Delaying
PressTime is n" -d
3
可能的網絡連接切割出試圖runShellCommand導致此問題的時候?
的事情之一,我可以看到出問題隨着時間的推移'buttonCount'溢出。無論如何,代碼是不完整的。 –
問題是,在溢出成爲問題之前,它會失敗一段時間。另外,我並不關心buttonCount的確切值,因爲我的數據處理腳本只檢查是否有數字並自己重新輸入條目。這可以防止計算機在短時間內掉電並且該值重置爲0時出現錯誤計數。我已更新代碼以包含完整的代碼。 – MacMixer13
由於之前我有過打印語句,因此存在一些明顯的差距,特別是在printDate函數中。我帶他們出去希望縮小代碼將解決我的問題,但沒有運氣 – MacMixer13