Commit d62d8722 authored by Patrick's avatar Patrick

EOD: state machine in place, but no sending

parent 5dfa8e38
......@@ -5,10 +5,11 @@
// master only functions
void red_peg::begin(uint8_t ss_pin, bool report_serial)
void red_peg::begin(bool sd_write, uint8_t ss_pin, bool report_serial)
{
// set the slave select pin
_ss_pin = ss_pin;
_sd_write = sd_write;
pinMode(_ss_pin, OUTPUT);
digitalWrite(_ss_pin, HIGH);
pinMode(RP_SDI_PIN, INPUT);
......@@ -87,14 +88,19 @@ void red_peg::print_data(t_SensorData data_record)
Serial.write(',');
Serial.print(data_record.y);
Serial.write('-');
if (data_record.m < 10) { Serial.write('0'); }
Serial.print(data_record.m);
Serial.write('-');
if (data_record.d < 10) { Serial.write('0'); }
Serial.print(data_record.d);
Serial.write('T');
if (data_record.hh < 10) { Serial.write('0'); }
Serial.print(data_record.hh);
Serial.write(':');
if (data_record.mm < 10) { Serial.write('0'); }
Serial.print(data_record.mm);
Serial.write(':');
if (data_record.ss < 10) { Serial.write('0'); }
Serial.print(data_record.ss);
Serial.write('Z');
Serial.write(',');
......
......@@ -82,6 +82,7 @@ private:
volatile uint8_t _rx_buffer_head;
volatile uint8_t _rx_buffer_tail;
bool _sd_write = false;
uint8_t _ss_pin;
uint8_t _get_timeout = 4;
void printHex(byte* data, int data_len);
......@@ -90,7 +91,7 @@ public:
const size_t size_t_SensorData = sizeof(t_SensorData);
// set up the red-peg library requirements (call during setup())
void begin(uint8_t ss_pin = 8, bool report_serial = true);
void begin(bool sd_write = false, uint8_t ss_pin = 8, bool report_serial = true);
// use RP.ask(REQUEST) to retrieve the value from the requested sensor
t_SensorData ask(sensor_e request = EMPTY, uint8_t _y = END_BYTE, uint8_t _m = END_BYTE, uint8_t _d = END_BYTE, uint8_t _hh = END_BYTE, uint8_t _mm = END_BYTE, uint8_t _ss = END_BYTE, uint16_t _the_reading = END_BYTE); //, float _the_data = 0.0);
// use RP.get(REQUEST) to queue a request for a reading
......
......@@ -6,30 +6,165 @@ red_peg RP;
#include <teal_mallet_gsm.h>
teal_mallet_gsm TM;
// set > 0 to get debug serial output
#define DEBUG_MODE 1
#define PIN ""
// APN data (EE sim)
#define GPRS_APN "everywhere" // replace your GPRS APN
#define GPRS_LOGIN "eesecure" // replace with your GPRS login
#define GPRS_PASSWORD "secure" // replace with your GPRS password
#define RECORDING_PERIOD 1000UL*60UL // 60 seconds
uint32_t last_record = -RECORDING_PERIOD; // send data immediately
const t_SensorType theSensor = {"Temp:3 min:TMP", "C", "INSTANT"};
const t_SensorType theSensor = {"Temp:1 min:TMP", "C", "INSTANT"};
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 3
// array to hold the variables
#define READINGS_ARRAY_LENGTH 50
t_SensorData readings[READINGS_ARRAY_LENGTH];
uint8_t readings_head = READINGS_ARRAY_LENGTH-1;
uint8_t readings_tail = READINGS_ARRAY_LENGTH-1;
t_SensorData last_sending_time;
void setup() {
RP.begin();
RP.begin(true);
delay(100);
TM.begin("training.teal-mallet.net", "355e6cff-165b-5355-80a7-38be25275cfd", "a3b40af1-16b5-4727-bcdc-60b0816a1e7b", 80);
TM.begin("training.teal-mallet.net", "355e6cff-165b-5355-80a7-38be25275cfd");
Serial.begin(BAUD);
Serial.println(F("start tm_one does-1"));
Serial.println(F("start EaMU purple-mallard"));
last_reading_time = get_reading(RTC);
last_sending_time = get_reading(RTC);
if (last_reading_time.y == 2165 or last_reading_time.y == 2000) {
// the RTC has not been set or is missing, abort now
Serial.println(F("RTC time is unset or missing"));
pinMode(LED_BUILTIN, OUTPUT);
while(true) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
#if DEBUG_MODE > 0
RP.print_data(last_reading_time);
delay(100);
#endif
}
void loop() {
// lets just keep the main loop as clean as possible
// driving state machine
if (g_current_state == SLEEP_IDLE) {
sleep_idle();
} else if (g_current_state == TAKE_READING) {
take_reading();
} else if (g_current_state == SEND_READING) {
send_reading();
}
}
void sleep_idle() {
// first, go to sleep
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // select the watchdog timer mode
MCUSR &= ~(1 << WDRF); // reset status flag
WDTCSR |= (1 << WDCE) | (1 << WDE); // enable configuration changes
WDTCSR = (1<< WDP0) | (1 << WDP1) | (1 << WDP2); // set the prescalar = 7
WDTCSR |= (1 << WDIE); // enable interrupt mode
sleep_enable(); // enable the sleep mode ready for use
// sleep is only for 4 seconds-ish, so lets run round a few times
for (uint8_t i=0; i<= 5; i++) {
sleep_mode(); // trigger the sleep
/* ...time passes ... */
}
sleep_disable(); // prevent further sleeps
// is it time to take a reading?
// get the time from the RTC
t_SensorData current_time = get_reading(RTC);
#if DEBUG_MODE > 0
RP.print_data(current_time);
delay(100);
#endif
if ((current_time.mm % ORDINAL_INTERVAL == 0)
&& (current_time.mm - last_reading_time.mm) % 60 > 1) {
// if it's an ordinal time 0|15|30|45 min take a reading
g_current_state = TAKE_READING;
} else if (readings_head = READINGS_ARRAY_LENGTH-1
&& (current_time.hh - last_sending_time.hh % 24 > 1)) {
// the readings buffer is full, send them out
g_current_state = SEND_READING;
}
}
void take_reading() {
#if DEBUG_MODE > 0
Serial.print(F("take_reading() at: "));
delay(100);
#endif
t_SensorData current_reading = get_reading(ANA);
#if DEBUG_MODE > 0
RP.print_data(current_reading);
Serial.print(F("distance: "));
Serial.println(RP.distance(current_reading));
delay(100);
#endif
// update the current time
if (current_reading.sensor == ANA) {
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;
}
// and return to idle/sleep mode
g_current_state = SLEEP_IDLE;
}
// we need to take readings a few times
t_SensorData get_reading(sensor_e target) {
// get a reading
RP.sensorsOn();
delay(100);
t_SensorData sensorData = RP.get(target);
RP.sensorsOff();
return sensorData;
}
void send_reading() {
#if DEBUG_MODE > 0
Serial.print(F("send_reading() at: "));
delay(100);
#endif
t_SensorData send_time = get_reading(ANA);
#if DEBUG_MODE > 0
RP.print_data(send_time);
#endif
}
ISR( WDT_vect ) {
/* dummy */
}
/* --- Do Not Use --- */
#define RECORDING_PERIOD 1000UL*60UL // 60 seconds
uint32_t last_record = -RECORDING_PERIOD; // send data immediately
void tmp() {
if (millis() - last_record >= RECORDING_PERIOD) {
// get a reading
RP.sensorsOn();
......@@ -88,7 +223,3 @@ void loop() {
Serial.println("awake!");
}
}
ISR( WDT_vect ) {
/* dummy */
}
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