我最近编写了这个代码来做一个带有 Arduino UNO 的Arduino传感器站。我需要从荨麻疹中恢复一些数据,以便以后有人可以分析它们。基本上,程序读取传感器,然后将读数存储在SD卡中。
我的代码存在问题。我不知道,也没有找到解决方法。代码正常执行,但它继续像循环函数一样执行设置。所以串口输出“设置完成”,然后arduino似乎重启等等。
我见过这可能是因为我的arduino没有足够的RAM。 IDE表示它占67%,程序库存占70%,我也尝试将arduino插入12V电源,但它也没有用。
感谢您的回答。
(有些变量可能是法语,因为我说法语,我可能会留下英语错误):)
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <OneWire.h>
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>
#include <Adafruit_MPL3115A2.h>
#include <DallasTemperature.h>
#include "DHT.h"
#include <OneWire.h>
#define DHTPIN 2
#define ONE_WIRE_BUS 3
#define DHTTYPE DHT22
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);
RTC_DS1307 rtc;
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
const int chipSelect = 10;
float temperatureWP;
float temperature2;
int luminosite;
float humidity;
float pascals;
float heatIndex;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
sensors.begin();
dht.begin();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
}
// following line sets the RTC to the date & time this sketch was compiled
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2018, 3, 11, 14, 19, 0));
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
File dataFile = SD.open("datalog12.txt", FILE_WRITE);
dataFile.println("Date et heure \t pression \t luminosite \t temperatureWP \t temperature2 \t humidity \t heat index");
dataFile.close();
Serial.println("Date et heure \t pression \t luminosite \t temperatureWP \t temperature2 \t humidity \t heat index");
Serial.println("SETUP DONE");
}
void loop() {
DateTime now = rtc.now();
// make a string for assembling the data to log:
String dataString = "";
sensors.requestTemperatures();
// read three sensors and append to the string:
temperatureWP = sensors.getTempCByIndex(0);
temperature2 = dht.readTemperature();
humidity = dht.readHumidity();
luminosite = analogRead(A1);
pascals = baro.getPressure();
heatIndex = dht.computeHeatIndex(temperature2, humidity, false);
dataString += String(pascals);
dataString += String("\t");
dataString += String(luminosite);
dataString += String("\t");
dataString += String(temperatureWP);
dataString += String("\t");
dataString += String(temperature2);
dataString += String("\t");
dataString += String(humidity);
dataString += String("\t");
dataString += String(heatIndex);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(now.day(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.year(), DEC);
dataFile.print(" ");
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.print(':');
dataFile.print(now.second(), DEC);
dataFile.print("\t");
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print("\t");
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay(10000);
}
答案 0 :(得分:0)
我的盐在这里。你的函数中有一些东西导致崩溃并使Arduino一次又一次地重启。所以我们需要找到这次崩溃发生的地方。
没有调试就很难猜到,但这是我的怀疑。您可以打印设置完成,因此设置正确完成。然后在循环中你在打印之前失败了。所以有两个地方:其中一个读取使其崩溃,或者多个字符串连接。我怀疑第二个,让我试着解释原因。
阅读Arduino代码库中String
的实现,接下来会发生什么。在每一行上,你做dataString += String(something);
这是用某事构造一个字符串,用dtostrf
调用一个新的缓冲区分配,然后调用+=
运算符,以concat
为dataString
作为参数调用String(something)
。这基本上是对char数组的底层指针和长度的连接,它重新分配缓冲区:read this。然后你基本上一次又一次地重新分配,不知道这是在扩展原始缓冲区还是制造新的缓冲区,可能会在你的RAM耗尽时以一种令人讨厌的方式分割堆...听起来它可能会崩溃:)
解决方案,我不知道你的编译器是否注意到了优化,但首先你可以摆脱虚假构造,同样为所有数值定义了运算符+=
。然后,您可以拥有自己的静态缓冲区,以便连接结果。像
char myBuffer[20];
dtostrf(pascals, 4, 2, *myBuffer[0]);
myBuffer[7] = '\t';
dtostrf(luminosite, 4, 2, *myBuffer[8]);
myBuffer[15] = '\t';
...
dataString += myBuffer;
顺便检查一下索引,这是C ++希望以某种开销为代价避免使用的非常类型不安全的指针 - 在这种情况下你可能没有资源支付的开销。
无论如何,这并不能解决每次进入循环时,dataString
仍在分配和解除分配的问题。这应该是一个确定性的分配,但是我没办法确保你会这样,然后你可能遇到堆碎片,不知道。