init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
Revision 12:8345612bf867, committed 2018-12-16
- Comitter:
- pathfindr
- Date:
- Sun Dec 16 01:06:14 2018 +0000
- Parent:
- 11:60eb0ff945f2
- Child:
- 13:29f67f256709
- Commit message:
- 4
Changed in this revision
--- a/README.md Sat Dec 15 20:09:19 2018 +0000 +++ b/README.md Sun Dec 16 01:06:14 2018 +0000 @@ -1,3 +1,17 @@ +MUST DO!!!!!! + +1) Add watchdog kicks to modem functions + + + + + + + + + + + Temperature logging Location logging
--- a/main.cpp Sat Dec 15 20:09:19 2018 +0000
+++ b/main.cpp Sun Dec 16 01:06:14 2018 +0000
@@ -48,8 +48,11 @@
static char RET_motionData[100] __attribute__((section("noinit"),zero_init));
//EVENT HANDLING
+static time_t RET_eventInterval_location_log __attribute__((section("noinit"),zero_init));
static time_t RET_eventTime_location_log __attribute__((section("noinit"),zero_init));
static time_t RET_eventTime_location_bc __attribute__((section("noinit"),zero_init));
+
+static time_t RET_eventInterval_environmental_log __attribute__((section("noinit"),zero_init));
static time_t RET_eventTime_environmental_log __attribute__((section("noinit"),zero_init));
static time_t RET_eventTime_environmental_bc __attribute__((section("noinit"),zero_init));
static time_t RET_eventTime_activity_bc __attribute__((section("noinit"),zero_init));
@@ -67,7 +70,7 @@
//PERIPHERALS
//------------------------------------------------------------------------------
//BLE myble;
-WatchdogTimer watchdog(65.0); //Do not set to less than 4500ms or can cause issues with softdevice
+extern WatchdogTimer watchdog(240.0); //Do not set to less than 4500ms or can cause issues with softdevice
//SERIAL DEBUG?
#if CONSOLE_DEBUG
Serial uart(PN_UART_TX, PN_UART_RX, 115200);
@@ -145,12 +148,15 @@
RET_mode_prev = RET_mode;
RET_RTCunixtime = 0;
RET_RTCmicros = 0;
+
+ RET_eventInterval_environmental_log = 0;
RET_eventTime_location_log = 0;
RET_eventTime_location_bc = 0;
RET_eventTime_environmental_log = 0;
RET_eventTime_environmental_bc = 0;
RET_eventTime_activity_bc = 0;
RET_eventTime_wakeFromDormant = 0;
+
RET_motionPendingOffState = 0;
RET_motionPendingOnState = 0;
RET_motionTriggered = 0;
@@ -254,42 +260,48 @@
//EVENTS
//Location
- if(time(NULL) > RET_eventTime_location_log && RET_eventTime_location_log > 0) {
+ if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0) {
//getdata_environmental();
//event_location_log();
}
- if(time(NULL) > RET_eventTime_location_bc && RET_eventTime_location_bc > 0) {
+ if(RET_RTCunixtime > RET_eventTime_location_bc && RET_eventTime_location_bc > 0) {
//getdata_battery();
//event_location_broadcast();
}
//Environmental
- if(time(NULL) > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) {
- //event_environmental_log();
+ if(RET_RTCunixtime > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) {
+ Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
+ SI7060 temperature(PN_I2C_SDA,PN_I2C_SCL);
+ float temperature_c = temperature.getTemperature();
+ DEBUG("Temperature log: %u,%f \n", RET_RTCunixtime,temperature_c);
+ filesystem.addLogEntry_temperature(RET_RTCunixtime,temperature_c);
}
- if(time(NULL) > RET_eventTime_environmental_bc && RET_eventTime_environmental_bc > 0) {
+ if(RET_RTCunixtime > RET_eventTime_environmental_bc && RET_eventTime_environmental_bc > 0) {
//event_environmental_bc();
+ //Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
}
//Activity
- if(time(NULL) > RET_eventTime_activity_bc && RET_eventTime_activity_bc > 0) {
+ if(RET_RTCunixtime > RET_eventTime_activity_bc && RET_eventTime_activity_bc > 0) {
//event_activity_bc();
}
break;
case MODE_DORMANT :
- if (time(NULL) > RET_eventTime_wakeFromDormant) {
+ if (RET_RTCunixtime > RET_eventTime_wakeFromDormant) {
RET_mode = MODE_NORMAL;
}
- break;
+ break;
case MODE_BUTTONPRESS1 :
- LED1blink(2,300);
+ LED1blink(1,300);
RET_mode = RET_mode_prev;
break;
case MODE_BUTTONPRESS2 :
- LED1blink(4,300);
+ LED1blink(3,300);
RET_mode = RET_mode_prev;
break;
case MODE_BUTTONHOLD :
LED1blink(10,300);
- RET_mode = RET_mode_prev;
+ RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs
+ RET_mode = MODE_DORMANT;
break;
default :
RET_mode = MODE_SETUP;
@@ -333,13 +345,30 @@
if (RET_coldBoot != 1) gotoSleep(30000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP
watchdog.kick();
- Filesystem filesystem(PN_SPI_MOSI,PN_SPI_MISO,PN_SPI_CLK,PN_SPI_CS1);
+ //TEST SPACE
+ Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
+ if (modem.on()) {
+ if (modem.registerOnNetwork(3,60000)) {
+ if (modem.USSDsend("blahblah", 2)) {
+ while(1) {
+ char* response = modem.USSDreceive(105000);
+ DEBUG("response: %s",response);
+ }
+ }
+ } else {
+ //modem failed to register on network
+ }
+ } else {
+ //Modem failed to turn on
+ }
- //GET TEMPERATURE
- SI7060 temperature(PN_I2C_SDA,PN_I2C_SCL);
- float temperature_c = temperature.getTemperature();
- DEBUG("temp: %f \n", temperature_c);
- filesystem.addLogEntry_temperature(RET_RTCunixtime, 10.5);
+
+
+ while (1) {
+ Thread::wait(60000);
+ watchdog.kick();
+ }
+
//MAIN LOGIC
DEBUG("mode: %i time: %i, %i, %i, %i \n", RET_mode, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
--- a/mbed_app.json Sat Dec 15 20:09:19 2018 +0000
+++ b/mbed_app.json Sun Dec 16 01:06:14 2018 +0000
@@ -2,6 +2,16 @@
"macros": [
"NDEBUG=1"
],
+ "config": {
+ "uart_0_fifo_size": {
+ "help": "UART0 FIFO buffer. FIFO buffer is filled from DMA buffer.",
+ "value": 1024
+ },
+ "uart_dma_size": {
+ "help": "UART DMA buffer. 2 buffers per instance. DMA buffer is filled by UARTE",
+ "value": 32
+ }
+ },
"target_overrides": {
"*": {
"platform.stdio-flush-at-exit": false
@@ -9,6 +19,8 @@
"NRF52_DK": {
"target.OUTPUT_EXT": "bin",
"target.uart_hwfc": 0,
+ "nordic.uart_0_fifo_size": 1024,
+ "nordic.uart_dma_size": 32,
"target.device_has_add": ["LOWPOWERTIMER"],
"target.macros_add": ["MBED_TICKLESS"],
"target.components_add": ["SD"],
--- a/modem.cpp Sat Dec 15 20:09:19 2018 +0000
+++ b/modem.cpp Sun Dec 16 01:06:14 2018 +0000
@@ -1,30 +1,12 @@
#include "modem.h"
+static char ATinBuffer[180];
+
Modem::Modem(PinName tx, PinName rx, PinName cts, PinName rts, PinName pwrkey, PinName vreg_en): _uart(tx,rx,115200), _pwrkey(pwrkey), _vreg_en(vreg_en)
{
requireSoftReset = true; //TODO: this can be removed when uart sleep issue resolved
}
-void Modem::on(void)
-{
- //power on Modem
- _vreg_en = 1;
- ThisThread::sleep_for(500);
- _pwrkey = 0;
- ThisThread::sleep_for(1500);
- _pwrkey = 1;
- LED1on(1000);
-}
-
-void Modem::off(bool soft)
-{
- if (soft) {
- ATsendCMD("AT+QPOWD=0");
- ATwaitForWord("OK\r",5000);
- }
- _vreg_en = 0;
-}
-
void Modem::flushSerial(void)
{
char char1 = 0;
@@ -41,7 +23,7 @@
_uart.puts("\r");
}
-bool Modem::ATwaitForWord(char* word, long timeout)
+bool Modem::ATwaitForWord(char* word, uint32_t timeout)
{
int targetIndex = 0;
bool havefullmatch = false;
@@ -49,8 +31,8 @@
memset (captured,0,sizeof(captured));
Timer t;
t.start();
- long startmillis = t.read_ms();
- long runtime = 0;
+ uint32_t startmillis = t.read_ms();
+ uint32_t runtime = 0;
while(!havefullmatch && runtime < timeout) {
runtime = (t.read_ms() - startmillis);
if(_uart.readable()) {
@@ -66,7 +48,7 @@
havefullmatch = true;
}
}
- }
+ }
}
t.stop();
t.reset();
@@ -78,4 +60,124 @@
}
}
+bool Modem::on(void)
+{
+ _vreg_en = 1;
+ Thread::wait(500);
+ _pwrkey = 0;
+ Thread::wait(1500);
+ _pwrkey = 1;
+ if (ATwaitForWord("RDY",10000)) {
+ //TURN OFF ECHO
+ ATsendCMD("ATE 0");
+ ATwaitForWord("OK",5000);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void Modem::off(bool soft)
+{
+ if (soft) {
+ ATsendCMD("AT+QPOWD=0");
+ ATwaitForWord("OK\r",5000);
+ }
+ _vreg_en = 0;
+}
+
+bool Modem::registerOnNetwork(int maxAttempts, uint32_t timeout)
+{
+ bool registered = false;
+ int attempt = 0;
+ Timer t;
+ t.start();
+ //TRY X NUMBER OF TIMES
+ while (attempt <= maxAttempts) {
+ attempt ++;
+ t.reset();
+ uint32_t startmillis = t.read_ms();
+ uint32_t runtime = 0;
+ while(!registered && runtime < timeout) {
+ runtime = (t.read_ms() - startmillis);
+ Thread::wait(1000);
+ ATsendCMD("AT+CREG?");
+ if (ATwaitForWord("+CREG: 0,5",5000)) {
+ registered = true;
+ };
+ }
+ if (!registered) {
+ off(true);
+ on();
+ }
+ }
+ t.stop();
+ if (registered) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+bool Modem::USSDsend(char* message, int maxAttempts)
+{
+ bool sent = false;
+ int attempt = 0;
+ //TRY X NUMBER OF TIMES
+ while (!sent && attempt <= maxAttempts) {
+ attempt ++;
+ char bytestosend[160];
+ sprintf(bytestosend, "AT+CUSD=1,\"#469*%s#\"", message);
+ ATsendCMD(bytestosend);
+ if (ATwaitForWord("+CUSD: 0",10000)) {
+ flushSerial();
+ sent = true;
+ };
+ }
+ if (sent) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+char* Modem::USSDreceive(uint32_t timeout)
+{
+ bool received = false;
+ int bufferIndex = 0;
+ Timer t;
+ t.start();
+ //TRY UNTIL TIMEOUT
+ uint32_t startmillis = t.read_ms();
+ uint32_t runtime = 0;
+ memset(ATinBuffer, 0, sizeof(ATinBuffer));
+ while(!received && runtime < timeout) {
+ runtime = (t.read_ms() - startmillis);
+ //ATsendCMD("AT+CUSD?");
+ if (ATwaitForWord("+CUSD: 0,\"%",2000)) {
+ led1 = 0;
+ while(!received && runtime < timeout) {
+ runtime = (t.read_ms() - startmillis);
+ if(_uart.readable()) {
+ char c = _uart.getc();
+ if (c == '%') {
+ led1 = 1;
+ flushSerial();
+ received = true;
+ } else {
+ ATinBuffer[bufferIndex] = c;
+ }
+ bufferIndex ++; //this must got at end otherwise first digit is \0
+ }
+ }
+ }
+ }
+ if (received) {
+ return ATinBuffer;
+ } else {
+ return "error";
+ }
+}
+
+
Modem::~Modem(){};
\ No newline at end of file
--- a/modem.h Sat Dec 15 20:09:19 2018 +0000
+++ b/modem.h Sun Dec 16 01:06:14 2018 +0000
@@ -15,13 +15,19 @@
~Modem();
//control
- void on(void);
+ bool on(void);
void off(bool soft);
+
+ //funcs
+ bool registerOnNetwork(int maxAttempts, uint32_t timeout);
+ bool USSDsend(char* message, int maxAttempts);
+ char* USSDreceive(uint32_t timeout);
+ bool USSDmessage(char* message, int maxAttempts, bool needResponse);
- //commands
+ //AT
void flushSerial(void);
void ATsendCMD(char* cmd);
- bool ATwaitForWord(char* word, long timeout);
+ bool ATwaitForWord(char* word, uint32_t timeout);
private: