Hallo,
ich weis ich nerve vermutlich schon aber es ist halt jedes mal was neues.
Diesmal aber eine einfache bitte an jemanden der einen ESP32 und ein SD shield hat.
Hier ein Bsp code der mir zwar das file auf der SD Karte erzeugt aber nichts hineinschreibt?
Oder habe ich etwas falsch gemacht im code?
/*
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 <SPI.h>
#include <SD.h>
const int chipSelect = 5;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
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:
while (1);
}
Serial.println("card initialized.");
}
void loop() {
// make a string for assembling the data to log:
String dataString = "Hallo";
// 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.println(dataString);
dataFile.flush();
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
Wie gesagtmir wird das file auf der SD karte erstellt aber "Hallo" nicht hineingeschrieben, bleibt einfach leer.
Mein code wo ich dann wirklich was reinschreibe macht fast das selbe jedoch schreibt er genau 1 mal die daten auf das file und dann nciht wieder.
Dachte zuerst es liegt am interupt der im code ist aber wie gesagt passiert am minimalbsp. garnichts
Hier mein code der eigendlich laufen soll:
#include <SPI.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <RTClib.h>
RTC_DS3231 rtc;
DeviceAddress DS18B20_Address;
const int oneWireBus = 16;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = { 0x28, 0x0, 0x3E, 0x4F, 0x26, 0x4C, 0xF, 0x90 };
DeviceAddress sensor2 = { 0x28, 0x84, 0xC2, 0x7, 0xD6, 0x1, 0x3C, 0x1E };
const int chipSelect = 5;
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
char Date[14];
char Time[13];
String dataStringTempsensor1 = "";
String dataStringTempsensor2 = "";
String dataStringTemp = "";
String dataStringNois = "";
int RTC_temp;
int sleep_time;
int new_time;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
#define LED 2
#define CLOCK_INTERRUPT_PIN 33
#define VOLT_NOIS_PIN 35
void CreateLogfileName() {
DateTime now = rtc.now();
snprintf(Date, sizeof(Date), "%02d%02d%02d.log", now.year(), now.month(), now.day()); // ein 8.3 Name wie z.B. "06032345.log"
snprintf(Time, sizeof(Time), "%02d%02d%02d.log", now.hour(), now.minute(), now.second()); // ein 8.3 Name wie z.B. "06032345.log"
}
void onAlarm() {
Serial.println("Alarm occured!");
}
void setup() {
Serial.begin(9600);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0); //1 = High, 0 = Low //Pin 33
//esp_sleep_enable_ext1_wakeup(0x400000000, ESP_EXT1_WAKEUP_ALL_LOW); //Pin 34
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
rtc.disable32K();
pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);
rtc.clearAlarm(1);
rtc.clearAlarm(2);
rtc.writeSqwPinMode(DS3231_OFF);
sensors.begin();
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("card initialized.");
/*
rtc.disableAlarm(2);
if (!rtc.setAlarm1(rtc.now() + TimeSpan(30), DS3231_A1_Second // this mode triggers the alarm when the seconds match. See Doxygen for other options
)) {
Serial.println("Error, alarm wasn't set!");
} else {
Serial.println("Alarm will happen in 30 seconds!");
}
*/
rtc.disableAlarm(1);
rtc.setAlarm2(rtc.now(), DS3231_A2_PerMinute); // this mode triggers the alarm when the minutes match. See Doxygen for other options
Serial.println("Alarm 2 will happen in 2 minutes (when full minutes match)!");
}
void loop() {
RTC_temp = rtc.getTemperature();
sensors.requestTemperatures(); // Send the command to get temperatures
dataStringTempsensor1 = String(sensors.getTempC(sensor1));
dataStringTempsensor2 = String(sensors.getTempC(sensor2));
dataStringTemp = dataStringTempsensor1 + "," + dataStringTempsensor2;
unsigned long startMillis = millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 4096;
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(VOLT_NOIS_PIN);
if (sample < 4096) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
//Serial.print("PeakToPeak: ");
//Serial.println(peakToPeak);
double volts = (peakToPeak * 3.3) / 4096; // convert to volts
dataStringNois = String(volts, 2);
CreateLogfileName();
String date_str = String(Date);
String FileName = "/" + date_str;
File dataFile = SD.open(FileName, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(Time);
dataFile.print("Sensor temperature: ");
dataFile.println(dataStringTemp);
dataFile.print("Volts noise: ");
dataFile.println(volts);
dataFile.print("RTC temperature: ");
dataFile.println(RTC_temp);
Serial.println("Dato to file ok");
dataFile.flush();
dataFile.close();
}
else {
Serial.println("error opening xxxx.txt");
}
if (rtc.alarmFired(2)) {
rtc.clearAlarm(2);
Serial.println("Alarm 2 cleared");
}
/*
if (rtc.alarmFired(1)) {
rtc.clearAlarm(1);
Serial.println("Alarm 1 cleared");
}
*/
DateTime now = rtc.now();
new_time = now.second();
sleep_time = new_time + 30;
Serial.print("Now time loop: ");
Serial.println(now.second(), DEC);
if (new_time == 30) {
Serial.println("Going to sleep now");
esp_deep_sleep_start();
}
//delay(1000);
}
Und so sieht das ergebnis aus:
231626.logSensor temperature: 28.19,28.56
Volts noise: 0.02
RTC temperature: -20
Info: das println wurde erst danach verÀndert also der nicht vorhandene Zeilenumbruch ist ok
Jedoch wundert mich die Temperatur welche ich vom RTC DS3231 bekomme schon, da es eigendlich im bereich der beiden anderen temperaturen liegen sollte
Wie immer danke an jeden
Lg