Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Enable Network Time Sync on FONA 3G #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 87 additions & 2 deletions Adafruit_FONA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,39 @@ bool Adafruit_FONA::enableNetworkTimeSync(bool onoff) {
return true;
}

/**
* @brief Enable network time sync and toggle network state to update time.
*
* @param onoff true: enable false: disable
* @return true: success, false: failure
*/
bool Adafruit_FONA_3G::enableNetworkTimeSync(bool onoff) {
if (onoff) {
if (!sendCheckReply(F("AT+CTZU=1"), ok_reply))
return false;
} else {
if (!sendCheckReply(F("AT+CTZU=0"), ok_reply))
return false;
}
// Disable and re-enable FONA's transmit/receive RF circuits
// This is because time updates occur during network registration
// See 4.1 AT+CFUN (https://simcom.ee/documents/SIM5320/SIMCOM_SIM5320_ATC_EN_V2.05.pdf)

// Get existing network network functionality level
uint16_t prior_level;
sendParseReply(F("AT+CFUN?"), F("+CFUN: "), &prior_level);
// Set network functionality level to 4 ('disable both transmit and receive RF circuits')
if (!sendCheckReply(F("AT+CFUN=4"), ok_reply, 4000))
return false;
// Restore existing network functionality level
if (!sendCheckReply(F("AT+CFUN="), prior_level, ok_reply))
return false;

flushInput(); // eat any 'Unsolicted Result Code'

return true;
}

/**
* @brief Enable NTP time sync
*
Expand Down Expand Up @@ -1109,7 +1142,7 @@ bool Adafruit_FONA_3G::enableGPS(bool onoff) {
return true;
}
/**
* @brief Get teh GPS status
* @brief Get the GPS status
*
* @return int8_t The GPS status:
* * 0: GPS off
Expand Down Expand Up @@ -2891,7 +2924,7 @@ bool Adafruit_FONA::sendParseReply(FONAFlashStringPtr tosend,
/**
* @brief Send data and parse the reply
*
* @param tosend Pointer to he data buffer to send
* @param tosend Pointer to the data buffer to send
* @param toreply The expected reply string
* @param f Pointer to a float buffer to hold value of the parsed field
* @param divider The divider character
Expand All @@ -2911,6 +2944,29 @@ bool Adafruit_FONA_3G::sendParseReply(FONAFlashStringPtr tosend,
return true;
}

/**
* @brief Send data and parse the reply
*
* @param tosend Pointer to the data buffer to send
* @param toreply The expected reply string
* @param f Pointer to a uint16_t buffer to hold value of the parsed field
* @param divider The divider character
* @param index The index of the parsed field to retrieve
* @return true: success, false: failure
*/
bool Adafruit_FONA_3G::sendParseReply(FONAFlashStringPtr tosend,
FONAFlashStringPtr toreply, uint16_t *v,
char divider, uint8_t index) {
getReply(tosend);

if (!parseReply(toreply, v, divider, index))
return false;

readline(); // eat 'OK'

return true;
}

/**
* @brief Parse a reply
*
Expand Down Expand Up @@ -2939,3 +2995,32 @@ bool Adafruit_FONA_3G::parseReply(FONAFlashStringPtr toreply, float *f,

return true;
}

/**
* @brief Parse a reply
*
* @param toreply Pointer to a buffer with the expected reply string
* @param f Pointer to a uint16_t buffer to hold the value of the parsed field
* @param divider The divider character
* @param index The index of the parsed field to retrieve
* @return true: success, false: failure
*/
bool Adafruit_FONA_3G::parseReply(FONAFlashStringPtr toreply, uint16_t *v,
char divider, uint8_t index) {
char *p = prog_char_strstr(replybuffer, (prog_char *)toreply);
if (p == 0)
return false;
p += prog_char_strlen((prog_char *)toreply);
// DEBUG_PRINTLN(p);
for (uint8_t i = 0; i < index; i++) {
// increment dividers
p = strchr(p, divider);
if (!p)
return false;
p++;
// DEBUG_PRINTLN(p);
}
*v = atoi(p);

return true;
}
5 changes: 5 additions & 0 deletions Adafruit_FONA.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,20 @@ class Adafruit_FONA_3G : public Adafruit_FONA {
bool playToolkitTone(uint8_t t, uint16_t len);
bool hangUp(void);
bool pickUp(void);
bool enableNetworkTimeSync(bool onoff);
bool enableGPRS(bool onoff);
bool enableGPS(bool onoff);

protected:
bool parseReply(FONAFlashStringPtr toreply, float *f, char divider,
uint8_t index);
bool parseReply(FONAFlashStringPtr toreply, uint16_t *v, char divider,
uint8_t index);

bool sendParseReply(FONAFlashStringPtr tosend, FONAFlashStringPtr toreply,
float *f, char divider = ',', uint8_t index = 0);
bool sendParseReply(FONAFlashStringPtr tosend, FONAFlashStringPtr toreply,
uint16_t *v, char divider = ',', uint8_t index = 0);
};

#endif
2 changes: 1 addition & 1 deletion examples/FONAtest/FONAtest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void printMenu(void) {
Serial.println(F("[u] Send USSD"));

// Time
Serial.println(F("[y] Enable network time sync (FONA 800 & 808)"));
Serial.println(F("[y] Enable network time sync"));
Serial.println(F("[Y] Enable NTP time sync (GPRS FONA 800 & 808)"));
Serial.println(F("[t] Get network time"));

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit FONA Library
version=1.3.6
version=1.3.7
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino library for the Adafruit FONA
Expand Down