Commit 7e60ef2a authored by Patrick's avatar Patrick

EOD: records data in YYYMM/DD.log file (DOT)

parent b428e7c2
......@@ -64,8 +64,6 @@ t_SensorData last_sending_time;
#error "Can't hold all the readings, reduce NUM_TO_SEND below NUM_MMNT"
#endif
uint16_t g_filenumber = 0;
void setup() {
RP.begin(true);
TM.begin(((char*)"training.teal-mallet.net"), ((char*)"a6c33855-49a0-5858-8ffe-3da7c2b0412d"));
......@@ -215,8 +213,20 @@ void take_measurement() {
measurements[readings_head].level = (float)temp;
// write to the open SD card file
char buff[9];
sprintf(buff, "%04d.txt", g_filenumber);
char buff[7];
// change directory to root
SD.chdir("/");
// create the current folder path
sprintf(buff, "%04d%02d", current_reading.y, current_reading.m);
if (!SD.exists(buff)) {
// make new directory if absent
SD.mkdir(buff);
}
// change to YYYYMM/
SD.chdir(buff);
// create the day file name
sprintf(buff, "%02d.log", current_reading.d);
// open DD.log file
myFile = SD.open(buff, FILE_WRITE);
myFile.print(current_reading.y);
myFile.write('-');
......
# 1 "/tmp/tmpmHIWJW"
#include <Arduino.h>
# 1 "/home/patrick/Projects/ea/purple-mallard/platformio/gsm-ultrasound/src/gsm-ultrasound.ino"
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <red_peg.h>
red_peg RP;
#include <teal_mallet_gsm.h>
teal_mallet_gsm TM;
#include <SPI.h>
#include "SdFat.h"
SdFat SD;
File myFile;
#define SD_CS_PIN SD_SS
#define MAX_DEPTH 15000
#define DEBUG_MODE 1
#define SLEEP_MODE_ON 1
const char level_param[] PROGMEM = "Level";
const char level_quali[] PROGMEM = "1 min";
const char level_dtype[] PROGMEM = "STAGE";
const char level_units[] PROGMEM = "mm";
const char level_perid[] PROGMEM = "INSTANT";
PGM_P const param[] PROGMEM = {level_param};
PGM_P const quali[] PROGMEM = {level_quali};
PGM_P const dtype[] PROGMEM = {level_dtype};
PGM_P const units[] PROGMEM = {level_units};
PGM_P const perid[] PROGMEM = {level_perid};
enum state_machine_e {
SLEEP_IDLE,
TAKE_READING,
SEND_READING
};
state_machine_e g_current_state = SLEEP_IDLE;
t_SensorData last_reading_time;
#define ORDINAL_INTERVAL 1
#define NUM_MMNT 11
uint8_t num_mmnt = NUM_MMNT;
t_measurement measurements[NUM_MMNT] = {};
uint8_t readings_head = 0;
uint8_t readings_tail = 0;
t_SensorData last_sending_time;
#define NUM_TO_SEND 10
#if NUM_TO_SEND >= NUM_MMNT
#error "Can't hold all the readings, reduce NUM_TO_SEND below NUM_MMNT"
#endif
uint16_t g_filenumber = 0;
void setup();
void loop();
void sleep_idle();
void take_measurement();
t_SensorData get_reading(sensor_e target);
void send_reading();
#line 69 "/home/patrick/Projects/ea/purple-mallard/platformio/gsm-ultrasound/src/gsm-ultrasound.ino"
void setup() {
RP.begin(true);
TM.begin(((char*)"training.teal-mallet.net"), ((char*)"a6c33855-49a0-5858-8ffe-3da7c2b0412d"));
delay(100);
Serial.begin(BAUD);
#if DEBUG_MODE > 1
Serial.println(F("start EaMU purple-mallard"));
#endif
delay(500);
last_sending_time = get_reading(RTC);
if (last_reading_time.y == 2165 or last_reading_time.y == 2000) {
#if DEBUG_MODE > 1
Serial.println(F("Bad RTC"));
#endif
pinMode(LED_BUILTIN, OUTPUT);
while(true) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
#if DEBUG_MODE > 1
RP.print_data(last_reading_time);
delay(100);
#endif
if (!SD.begin(SD_CS_PIN)) {
#if DEBUG_MODE > 1
Serial.println(F("No SD"));
#endif
return;
}
#if DEBUG_MODE > 1
Serial.println(F("SD OK"));
#endif
}
void loop() {
if (g_current_state == SLEEP_IDLE) {
sleep_idle();
} else if (g_current_state == TAKE_READING) {
take_measurement();
} else if (g_current_state == SEND_READING) {
send_reading();
}
}
void sleep_idle() {
#if SLEEP_MODE_ON > 0
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
MCUSR &= ~(1 << WDRF);
WDTCSR |= (1 << WDCE) | (1 << WDE);
WDTCSR = (1<< WDP0) | (1 << WDP1) | (1 << WDP2);
WDTCSR |= (1 << WDIE);
sleep_enable();
#if DEBUG_MODE > 0
#if DEBUG_MODE > 1
Serial.print(F("sleep..."));
#else
Serial.print(F("s."));
#endif
delay(50);
#endif
sleep_mode();
sleep_disable();
#if DEBUG_MODE > 0
#if DEBUG_MODE > 1
Serial.println(F("wake!"));
#else
Serial.println(F("!"));
#endif
#endif
#else
delay(4000);
#endif
t_SensorData current_time = get_reading(RTC);
#if DEBUG_MODE > 1
RP.print_data(current_time);
delay(100);
#endif
if ((current_time.mm % ORDINAL_INTERVAL == 0)
&& (current_time.mm != last_reading_time.mm)) {
g_current_state = TAKE_READING;
} else if (((readings_head + NUM_MMNT) - readings_tail) % NUM_MMNT >= NUM_TO_SEND) {
g_current_state = SEND_READING;
}
}
void take_measurement() {
long temp = -1;
t_SensorData current_reading = get_reading(ANA);
if (current_reading.sensor == ANA) {
temp = MAX_DEPTH - RP.distance(current_reading, MB7366);
}
#if DEBUG_MODE > 1
Serial.print(F("take mmnt at: "));
RP.print_data(current_reading);
Serial.print(F("STAGE: "));
#endif
#if DEBUG_MODE > 0
Serial.println(temp);
delay(100);
#endif
if (temp >= 0) {
readings_head = (readings_head + 1) % NUM_MMNT;
#if DEBUG_MODE > 0
Serial.print(((readings_head + NUM_MMNT) - readings_tail) % NUM_MMNT);
#endif
#if DEBUG_MODE > 1
Serial.print(F(" mmnts stored"));
#endif
#if DEBUG_MODE > 0
Serial.println();
#endif
measurements[readings_head].reading = 0;
measurements[readings_head].y = current_reading.y;
measurements[readings_head].m = current_reading.m;
measurements[readings_head].d = current_reading.d;
measurements[readings_head].hh = current_reading.hh;
measurements[readings_head].mm = current_reading.mm;
measurements[readings_head].ss = current_reading.ss;
measurements[readings_head].level = (float)temp;
char buff[9];
sprintf(buff, "%04d.txt", g_filenumber);
myFile = SD.open(buff, FILE_WRITE);
myFile.print(current_reading.y);
myFile.write('-');
if (current_reading.m < 10) { myFile.write('0'); }
myFile.print(current_reading.m);
myFile.write('-');
if (current_reading.d < 10) { myFile.write('0'); }
myFile.print(current_reading.d);
myFile.write('T');
if (current_reading.hh < 10) { myFile.write('0'); }
myFile.print(current_reading.hh);
myFile.write(':');
if (current_reading.mm < 10) { myFile.write('0'); }
myFile.print(current_reading.mm);
myFile.write(':');
myFile.print("00");
myFile.write('Z');
myFile.write(',');
myFile.print(temp);
myFile.println();
myFile.close();
last_reading_time.y = current_reading.y;
last_reading_time.m = current_reading.m;
last_reading_time.d = current_reading.d;
last_reading_time.hh = current_reading.hh;
last_reading_time.mm = current_reading.mm;
last_reading_time.ss = current_reading.ss;
#if DEBUG_MODE > 1
} else {
Serial.println(F("OOR"));
#endif
}
g_current_state = SLEEP_IDLE;
}
t_SensorData get_reading(sensor_e target) {
RP.sensorsOn();
if (target == ANA) {
delay(160);
delay(148);
}
delay(100);
t_SensorData sensorData = RP.get(target);
RP.sensorsOff();
return sensorData;
}
void send_reading() {
#if DEBUG_MODE > 1
Serial.print(F("sending "));
Serial.print(((readings_head + NUM_MMNT) - readings_tail) % NUM_MMNT);
Serial.print(F(" readings ("));
Serial.print(readings_tail);
Serial.write(':');
Serial.print(readings_head);
Serial.println(F(")"));
delay(100);
#endif
#if DEBUG_MODE > 1
Serial.println(F("connecting GPRS..."));
#endif
TM.connectGPRS();
#if DEBUG_MODE > 1
Serial.println(F("startConnection..."));
#endif
TM.startConnection();
#if DEBUG_MODE > 1
Serial.println(F("sendReadings..."));
#endif
TM.sendReadings(&param[0], &quali[0], &dtype[0], &units[0], &perid[0], measurements, num_mmnt, readings_head, readings_tail);
#if DEBUG_MODE > 1
Serial.println(F("endConnection..."));
#endif
TM.endConnection();
#if DEBUG_MODE > 1
Serial.println(F("disconnectGPRS..."));
#endif
TM.disconnectGPRS();
#if DEBUG_MODE > 1
Serial.println(F("GPRS powered down."));
#endif
readings_tail = readings_head;
g_current_state = SLEEP_IDLE;
}
ISR( WDT_vect ) {
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment