1 Commits

Author SHA1 Message Date
eKa
4f4bba55f1 EEPROM version 2020-01-24 22:44:50 +05:00
5 changed files with 1305 additions and 404 deletions

View File

@@ -35,8 +35,7 @@
GPRS* GPRS::inst; GPRS* GPRS::inst;
GPRS::GPRS(Stream& serial, uint8_t pkPin) GPRS::GPRS(Stream& serial, uint8_t pkPin) {
{
_pkPin = pkPin; _pkPin = pkPin;
inst = this; inst = this;
@@ -45,16 +44,13 @@ GPRS::GPRS(Stream& serial, uint8_t pkPin)
sim900_init(stream); sim900_init(stream);
} }
bool GPRS::init(void) bool GPRS::init(void) {
{
if (!sim900_check_with_cmd("AT\r\n","OK\r\n",CMD)) if (!sim900_check_with_cmd("AT\r\n","OK\r\n",CMD))
return false; return false;
if (!sim900_check_with_cmd("AT+CFUN=1\r\n","OK\r\n",CMD)) if (!sim900_check_with_cmd("AT+CFUN=1\r\n","OK\r\n",CMD))
return false; return false;
if (!checkSIMStatus()) if (!checkSIMStatus())
return false; return false;
@@ -78,7 +74,7 @@ bool GPRS::init(void)
} }
bool GPRS::checkPowerUp(void) { bool GPRS::checkPowerUp(void) {
return sim900_check_with_cmd("AT\r\n","OK\r\n", CMD); return sim900_check_with_cmd("AT\r\n","OK\r\n", CMD);
} }
// power Up GPRS Shield // power Up GPRS Shield
@@ -105,8 +101,7 @@ void GPRS::reset() {
powerOn(); powerOn();
} }
bool GPRS::checkSIMStatus(void) bool GPRS::checkSIMStatus(void) {
{
char gprsBuffer[32]; char gprsBuffer[32];
byte count = 0; byte count = 0;
sim900_clean_buffer(gprsBuffer,32); sim900_clean_buffer(gprsBuffer,32);
@@ -125,8 +120,7 @@ bool GPRS::checkSIMStatus(void)
return true; return true;
} }
bool GPRS::sendSMS(char *number, char *data) bool GPRS::sendSMS(const char *number, const char *data) {
{
// Set message mode to ASCII // Set message mode to ASCII
if(!sim900_check_with_cmd("AT+CMGF=1\r\n", "OK\r\n", CMD)) { if(!sim900_check_with_cmd("AT+CMGF=1\r\n", "OK\r\n", CMD)) {
return false; return false;
@@ -144,11 +138,11 @@ bool GPRS::sendSMS(char *number, char *data)
sim900_send_cmd(data); sim900_send_cmd(data);
delay(500); delay(500);
sim900_send_End_Mark(); sim900_send_End_Mark();
return true;
return sim900_wait_for_resp("OK\r\n", CMD, 2);
} }
char GPRS::isSMSunread() char GPRS::isSMSunread() {
{
char gprsBuffer[48]; //48 is enough to see +CMGL: char gprsBuffer[48]; //48 is enough to see +CMGL:
char *s; char *s;
@@ -176,7 +170,7 @@ char GPRS::isSMSunread()
*/ */
sim900_clean_buffer(gprsBuffer,31); sim900_clean_buffer(gprsBuffer,31);
sim900_read_buffer(gprsBuffer,30,DEFAULT_TIMEOUT); sim900_read_buffer(gprsBuffer, 30, DEFAULT_TIMEOUT);
//Serial.print("Buffer isSMSunread: ");Serial.println(gprsBuffer); //Serial.print("Buffer isSMSunread: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"OK"))) { if(NULL != ( s = strstr(gprsBuffer,"OK"))) {
@@ -235,23 +229,149 @@ bool GPRS::readSMS(int messageIndex, char *message, byte length) {
return false; return false;
} }
bool GPRS::getSMS(char *message, char *phone, char *datetime) { byte GPRS::getEvent(char *message, char *phone, char *datetime, char *gprsBuffer, byte size, bool check) {
int len;
byte i = 0;
char *s, *p1, *p2;
// Read buffer
sim900_clean_buffer(gprsBuffer, size);
sim900_read_buffer(gprsBuffer, size - 1);
len = strlen(gprsBuffer);
// Get SMS
s = strstr(gprsBuffer,"+CMT: ");
if (s != NULL) {
// Extract phone number string
p1 = strstr(gprsBuffer, "\"+");
p2 = p1 + 1; //First phone number character
p1 = strstr((char *)(p2), "\"");
if (p1 != NULL) {
i = 0;
while (p2 < p1) {
phone[i++] = *(p2++);
}
phone[i] = '\0';
}
// Extract date time string
p1 = strstr((char *)(p2), ",");
p2 = p1 + 1;
p1 = strstr((char *)(p2), ",");
p2 = p1 + 2; //First date time character
p1 = strstr((char *)(p2), "\"");
if (p1 != NULL) {
i = 0;
while (p2 < p1) {
datetime[i++] = *(p2++);
}
datetime[i] = '\0';
}
// Extract message
p2 = strstr(gprsBuffer, "\r\n");
p2 = p2 + 2;
p1 = strstr(p2, "\r\n");
if (p1 != NULL) {
i = 0;
p1 = p1 + 2; //First message character
while((*p1 != '\r') && (i < len - 1)) {
message[i++] = *(p1++);
}
message[i] = '\0';
}
return SMS;
}
// Get Call
s = strstr(gprsBuffer,"+CLIP: ");
if (s != NULL) {
// Extract phone number string
p1 = strstr(gprsBuffer, "\"+");
p2 = p1 + 1; //First phone number character
p1 = strstr((char *)(p2), "\"");
if (p1 != NULL) {
i = 0;
while (p2 < p1) {
phone[i++] = *(p2++);
}
phone[i] = '\0';
}
return CALL;
}
// Get outgoing call
s = strstr(gprsBuffer,"+COLP: ");
if (s != NULL) {
// Extract phone number string
p1 = strstr(gprsBuffer, "\"+");
p2 = p1 + 1; //First phone number character
p1 = strstr((char *)(p2), "\"");
if (p1 != NULL) {
i = 0;
while (p2 < p1) {
phone[i++] = *(p2++);
}
phone[i] = '\0';
}
return ANSWER;
}
// Get BUSY
s = strstr(gprsBuffer,"BUSY");
if (s != NULL) {
return BUSY;
}
// Get NO CARRIER
s = strstr(gprsBuffer,"NO CARRIER");
if (s != NULL) {
return NO_CARRIER;
}
// Get NO ANSWER
s = strstr(gprsBuffer,"NO ANSWER");
if (s != NULL) {
return NO_ANSWER;
}
// Check modem
if (check) {
if (!sim900_check_with_cmd("AT\r\n","OK\r\n", CMD)) return ERR;
}
return NONE;
}
bool GPRS::getSMS(char *message, char *phone, char *datetime, char *gprsBuffer, byte size) {
/* Response is like: /* Response is like:
+CMT: "+79772941911","","15/12/15,01:51:24+12" +CMT: "+79772941911","","15/12/15,01:51:24+12"
SMS text here SMS text here
*/ */
if(!sim900_check_with_cmd("","+CMT: ",CMD)) return false; //if(!sim900_check_with_cmd("","+CMT: ", CMD, 3)) return false;
if(!sim900_wait_for_resp("+CMT: ", CMD, 1)) return false;
byte i = 0; byte i = 0;
char gprsBuffer[80 + 160]; //char gprsBuffer[80 + 160];
char *p1, *p2; char *p1, *p2;
sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer)); sim900_clean_buffer(gprsBuffer, size);//sizeof(gprsBuffer));
sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer)); sim900_read_buffer(gprsBuffer, size - 1);//sizeof(gprsBuffer));
int len = strlen(gprsBuffer); int len = strlen(gprsBuffer);
//int len = sizeof(message);
//byte len = 160;
// Serial.print("Buff len: ");
// Serial.println(len);
// Serial.print("Buff: ");
// Serial.println(gprsBuffer);
// Serial.println("----- buffer -----"); // Serial.println("----- buffer -----");
// Serial.println(gprsBuffer); // Serial.println(gprsBuffer);
// Serial.println("^^^^^ buffer ^^^^^"); // Serial.println("^^^^^ buffer ^^^^^");
@@ -323,9 +443,9 @@ void GPRS::answer(void)
sim900_send_cmd("ATA\r\n"); sim900_send_cmd("ATA\r\n");
} }
bool GPRS::ifcallNow(void) bool GPRS::ifcallNow(byte timeout)
{ {
return sim900_check_with_cmd("","RING\r\n",CMD); return sim900_check_with_cmd("","RING\r\n",CMD, timeout);
} }
bool GPRS::ifcallEnd(void) bool GPRS::ifcallEnd(void)
@@ -348,9 +468,9 @@ bool GPRS::disableCLIPring(void)
return sim900_check_with_cmd("AT+CLIP=0\r\n","OK\r\n",CMD); return sim900_check_with_cmd("AT+CLIP=0\r\n","OK\r\n",CMD);
} }
bool GPRS::isCallActive(char *number) bool GPRS::isCallActive(char *number, char *gprsBuffer)
{ {
char gprsBuffer[46]; //46 is enough to see +CPAS: and CLCC: //char gprsBuffer[46]; //46 is enough to see +CPAS: and CLCC:
char *p, *s; char *p, *s;
int i = 0; int i = 0;
@@ -370,11 +490,12 @@ bool GPRS::isCallActive(char *number)
OK OK
*/ */
sim900_clean_buffer(gprsBuffer,29); sim900_clean_buffer(gprsBuffer, 46);
sim900_read_buffer(gprsBuffer,27); sim900_read_buffer(gprsBuffer, 45);
//HACERR cuando haga lo de esperar a OK no me haría falta esto //HACERR cuando haga lo de esperar a OK no me haría falta esto
//We are going to flush serial data until OK is recieved //We are going to flush serial data until OK is recieved
sim900_wait_for_resp("OK\r\n", CMD); //Serial.println("Wait OK");
//sim900_wait_for_resp("OK\r\n", CMD);
//Serial.print("Buffer isCallActive 1: ");Serial.println(gprsBuffer); //Serial.print("Buffer isCallActive 1: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"+CPAS:"))) { if(NULL != ( s = strstr(gprsBuffer,"+CPAS:"))) {
s = s + 7; s = s + 7;
@@ -392,8 +513,8 @@ bool GPRS::isCallActive(char *number)
OK OK
*/ */
sim900_clean_buffer(gprsBuffer,46); sim900_clean_buffer(gprsBuffer, 46);
sim900_read_buffer(gprsBuffer,45); sim900_read_buffer(gprsBuffer, 45);
//Serial.print("Buffer isCallActive 2: ");Serial.println(gprsBuffer); //Serial.print("Buffer isCallActive 2: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"+CLCC:"))) { if(NULL != ( s = strstr(gprsBuffer,"+CLCC:"))) {
//There is at least one CALL ACTIVE, get number //There is at least one CALL ACTIVE, get number
@@ -431,22 +552,22 @@ bool GPRS::getDateTime(char *buffer) {
sim900_read_buffer(gprsBuffer,43,DEFAULT_TIMEOUT); sim900_read_buffer(gprsBuffer,43,DEFAULT_TIMEOUT);
if(NULL != ( s = strstr(gprsBuffer,"+CCLK:"))) { if(NULL != ( s = strstr(gprsBuffer,"+CCLK:"))) {
s = strstr((char *)(s),"\""); s = strstr((char *)(s),"\"");
s = s + 1; //We are in the first phone number character s = s + 1; //We are in the first phone number character
p = strstr((char *)(s),"\""); //p is last character """ p = strstr((char *)(s),"\""); //p is last character """
if (NULL != s) { if (NULL != s) {
i = 0; i = 0;
while (s < p) { while (s < p) {
buffer[i++] = *(s++); buffer[i++] = *(s++);
}
buffer[i] = '\0';
} }
//We are going to flush serial data until OK is recieved buffer[i] = '\0';
return sim900_wait_for_resp("OK\r\n", CMD);
} }
return false; //We are going to flush serial data until OK is recieved
return sim900_wait_for_resp("OK\r\n", CMD);
} }
return false;
}
byte GPRS::getSignalStrength() { byte GPRS::getSignalStrength() {
//AT+CSQ: 00,00 --> 13 + CRLF = 15 //AT+CSQ: 00,00 --> 13 + CRLF = 15
// --> CRLF = 2 // --> CRLF = 2
//OK --> 2 + CRLF = 4 //OK --> 2 + CRLF = 4
@@ -467,11 +588,10 @@ bool GPRS::getDateTime(char *buffer) {
//Here is where we ask for APN configuration, with F() so we can save MEMORY //Here is where we ask for APN configuration, with F() so we can save MEMORY
//bool GPRS::join(const __FlashStringHelper *apn, const __FlashStringHelper *userName, const __FlashStringHelper *passWord) //bool GPRS::join(const __FlashStringHelper *apn, const __FlashStringHelper *userName, const __FlashStringHelper *passWord)
bool GPRS::join(char* apn, char* userName, char* passWord, int timeout) bool GPRS::join(char* apn, char* userName, char* passWord, int timeout) {
{ byte i;
byte i; char *p, *s;
char *p, *s; char ipAddr[32];
char ipAddr[32];
/* if(!sim900_check_with_cmd("AT+CIPSHUT\r\n","SHUT OK\r\n", CMD)) { /* if(!sim900_check_with_cmd("AT+CIPSHUT\r\n","SHUT OK\r\n", CMD)) {
Serial.write("Error = 1\r\n"); Serial.write("Error = 1\r\n");
return false; return false;
@@ -539,42 +659,40 @@ bool GPRS::getDateTime(char *buffer) {
return false; return false;
} }
void GPRS::disconnect() void GPRS::disconnect() {
{
sim900_send_cmd("AT+CIPSHUT\r\n"); sim900_send_cmd("AT+CIPSHUT\r\n");
} }
bool GPRS::connect(Protocol ptl,const char * host, int port, int timeout) bool GPRS::connect(Protocol ptl,const char * host, int port, int timeout) {
{
//char cmd[64]; //char cmd[64];
char num[4]; char num[4];
char resp[96]; char resp[96];
//sim900_clean_buffer(cmd,64); //sim900_clean_buffer(cmd,64);
if(ptl == TCP) { if(ptl == TCP) {
sim900_send_cmd("AT+CIPSTART=\"TCP\",\""); sim900_send_cmd("AT+CIPSTART=\"TCP\",\"");
sim900_send_cmd(host); sim900_send_cmd(host);
sim900_send_cmd("\","); sim900_send_cmd("\",");
itoa(port, num, 10); itoa(port, num, 10);
sim900_send_cmd(num); sim900_send_cmd(num);
sim900_send_cmd("\r\n"); sim900_send_cmd("\r\n");
// sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n",host, port); // sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n",host, port);
} else if(ptl == UDP) { } else if(ptl == UDP) {
sim900_send_cmd("AT+CIPSTART=\"UDP\",\""); sim900_send_cmd("AT+CIPSTART=\"UDP\",\"");
sim900_send_cmd(host); sim900_send_cmd(host);
sim900_send_cmd("\","); sim900_send_cmd("\",");
itoa(port, num, 10); itoa(port, num, 10);
sim900_send_cmd(num); sim900_send_cmd(num);
sim900_send_cmd("\r\n"); sim900_send_cmd("\r\n");
// sprintf(cmd, "AT+CIPSTART=\"UDP\",\"%s\",%d\r\n",host, port);
} else {
return false;
}
// sprintf(cmd, "AT+CIPSTART=\"UDP\",\"%s\",%d\r\n",host, port); delay(2000);
} else {
return false;
}
delay(2000);
//sim900_send_cmd(cmd); //sim900_send_cmd(cmd);
sim900_read_buffer(resp,96,timeout); sim900_read_buffer(resp,96,timeout);
//Serial.print("Connect resp: "); Serial.println(resp); //Serial.print("Connect resp: "); Serial.println(resp);
if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
@@ -584,8 +702,7 @@ sim900_read_buffer(resp,96,timeout);
} }
//Overload with F() macro to SAVE memory //Overload with F() macro to SAVE memory
bool GPRS::connect(Protocol ptl,const __FlashStringHelper *host, const __FlashStringHelper *port, int timeout) bool GPRS::connect(Protocol ptl,const __FlashStringHelper *host, const __FlashStringHelper *port, int timeout) {
{
//char cmd[64]; //char cmd[64];
char resp[96]; char resp[96];
@@ -610,8 +727,7 @@ bool GPRS::connect(Protocol ptl,const __FlashStringHelper *host, const __FlashSt
return false; return false;
} }
bool GPRS::is_connected(void) bool GPRS::is_connected(void) {
{
char resp[96]; char resp[96];
sim900_send_cmd("AT+CIPSTATUS\r\n"); sim900_send_cmd("AT+CIPSTATUS\r\n");
sim900_read_buffer(resp,sizeof(resp),DEFAULT_TIMEOUT); sim900_read_buffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
@@ -625,8 +741,7 @@ bool GPRS::is_connected(void)
} }
} }
bool GPRS::close() bool GPRS::close() {
{
// if not connected, return // if not connected, return
if (!is_connected()) { if (!is_connected()) {
return true; return true;
@@ -634,23 +749,19 @@ bool GPRS::close()
return sim900_check_with_cmd("AT+CIPCLOSE\r\n", "CLOSE OK\r\n", CMD); return sim900_check_with_cmd("AT+CIPCLOSE\r\n", "CLOSE OK\r\n", CMD);
} }
int GPRS::readable(void) int GPRS::readable(void) {
{
return sim900_check_readable(); return sim900_check_readable();
} }
int GPRS::wait_readable(int wait_time) int GPRS::wait_readable(int wait_time) {
{
return sim900_wait_readable(wait_time); return sim900_wait_readable(wait_time);
} }
int GPRS::wait_writeable(int req_size) int GPRS::wait_writeable(int req_size) {
{
return req_size+1; return req_size+1;
} }
int GPRS::send(const char * str, int len) int GPRS::send(const char * str, int len) {
{
//char cmd[32]; //char cmd[32];
char num[4]; char num[4];
if(len > 0){ if(len > 0){
@@ -712,8 +823,7 @@ int GPRS::send(const char * str, int len)
return strlen(buf); return strlen(buf);
} }
uint32_t GPRS::str_to_ip(const char* str) uint32_t GPRS::str_to_ip(const char* str) {
{
uint32_t ip = 0; uint32_t ip = 0;
char* p = (char*)str; char* p = (char*)str;
for(int i = 0; i < 4; i++) { for(int i = 0; i < 4; i++) {
@@ -729,20 +839,17 @@ uint32_t GPRS::str_to_ip(const char* str)
return ip; return ip;
} }
char* GPRS::getIPAddress() char* GPRS::getIPAddress() {
{
//I have already a buffer with ip_string: snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (_ip>>24)&0xff,(_ip>>16)&0xff,(_ip>>8)&0xff,_ip&0xff); //I have already a buffer with ip_string: snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (_ip>>24)&0xff,(_ip>>16)&0xff,(_ip>>8)&0xff,_ip&0xff);
return ip_string; return ip_string;
} }
unsigned long GPRS::getIPnumber() unsigned long GPRS::getIPnumber() {
{
return _ip; return _ip;
} }
bool GPRS::controlGPIO(uint8_t index, uint8_t level) bool GPRS::controlGPIO(uint8_t index, uint8_t level) {
{ char num[4];
char num[1];
sim900_send_cmd("AT+SGPIO=0,"); sim900_send_cmd("AT+SGPIO=0,");
itoa(index, num, 10); itoa(index, num, 10);

View File

@@ -32,6 +32,18 @@
#ifndef __GPRS_SHIELD_ARDUINO_H__ #ifndef __GPRS_SHIELD_ARDUINO_H__
#define __GPRS_SHIELD_ARDUINO_H__ #define __GPRS_SHIELD_ARDUINO_H__
#define NONE 0
#define SMS 1
#define CALL 2
#define BUSY 3
#define NO_CARRIER 4
#define NO_ANSWER 5
#define ANSWER 6
#define ERR 7
#define CHECK true
#define NO_CHECK false
#include "sim900.h" #include "sim900.h"
/** GPRS class. /** GPRS class.
@@ -39,14 +51,13 @@
*/ */
enum Protocol { enum Protocol {
CLOSED = 0, CLOSED = 0,
TCP = 1, TCP = 1,
UDP = 2, UDP = 2,
}; };
class GPRS class GPRS {
{ public:
public:
/** Create GPRS instance /** Create GPRS instance
*/ */
@@ -89,7 +100,7 @@ public:
* false on success * false on success
* true on error * true on error
*/ */
bool sendSMS(char* number, char* data); bool sendSMS(const char* number, const char* data);
/** Check if there is any UNREAD SMS: this function DOESN'T change the UNREAD status of the SMS /** Check if there is any UNREAD SMS: this function DOESN'T change the UNREAD status of the SMS
* @returns * @returns
@@ -110,6 +121,8 @@ public:
*/ */
bool readSMS(int messageIndex, char *message, byte length); bool readSMS(int messageIndex, char *message, byte length);
byte getEvent(char *message, char *phone, char *datetime, char *gprsBuffer, byte size, bool check = true);
/** get SMS on the fly, phone and date /** get SMS on the fly, phone and date
* @param message buffer used to get SMS message * @param message buffer used to get SMS message
@@ -120,7 +133,7 @@ public:
* false on error * false on error
*/ */
bool getSMS(char *message, char *phone, char *datetime); bool getSMS(char *message, char *phone, char *datetime, char *gprsBuffer, byte size);
/** delete SMS message on SIM card /** delete SMS message on SIM card
* @param index the index number which SMS message will be delete * @param index the index number which SMS message will be delete
@@ -157,7 +170,7 @@ public:
* true on success * true on success
* false on error * false on error
*/ */
bool ifcallNow(void); bool ifcallNow(byte timeout = 1);
bool ifcallEnd(void); bool ifcallEnd(void);
@@ -170,7 +183,7 @@ public:
* true on success * true on success
* false on error * false on error
*/ */
bool isCallActive(char *number); bool isCallActive(char *number, char *gprsBuffer);
/** get DateTime from SIM900 (see AT command: AT+CLTS=1) as string /** get DateTime from SIM900 (see AT command: AT+CLTS=1) as string
* @param * @param
@@ -278,7 +291,7 @@ public:
*/ */
bool controlGPIO(uint8_t num, uint8_t level); bool controlGPIO(uint8_t num, uint8_t level);
private: private:
bool checkSIMStatus(void); bool checkSIMStatus(void);
uint8_t _pkPin = 9; uint8_t _pkPin = 9;

View File

@@ -60,42 +60,43 @@ int sim900_wait_readable (int wait_time)
return dataLen; return dataLen;
} }
void sim900_flush_serial() void sim900_flush_serial() {
{ while(sim900_check_readable()) {
while(sim900_check_readable()){ char c = serialSIM900->read();
char c = serialSIM900->read(); }
}
} }
void sim900_read_buffer(char *buffer, int count, unsigned int timeout, unsigned int chartimeout) void sim900_read_buffer(char *buffer, int count, unsigned int timeout, unsigned int chartimeout)
{ {
int i = 0; //Serial.println("Read buff");
unsigned long timerStart, prevChar; int i = 0;
timerStart = millis(); unsigned long timerStart, prevChar;
prevChar = 0; timerStart = millis();
while(1) { prevChar = 0;
while (sim900_check_readable()) { while(1) {
char c = serialSIM900->read(); while (sim900_check_readable()) {
prevChar = millis(); char c = serialSIM900->read();
buffer[i++] = c; //Serial.print(c);
if(i >= count)break; prevChar = millis();
} buffer[i++] = c;
if(i >= count)break; if(i >= count) break;
if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
break;
}
//If interchar Timeout => return FALSE. So we can return sooner from this function. Not DO it if we dont recieve at least one char (prevChar <> 0)
if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) {
break;
}
} }
if(i >= count) break;
if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
break;
}
//If interchar Timeout => return FALSE. So we can return sooner from this function. Not DO it if we dont recieve at least one char (prevChar <> 0)
if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) {
break;
}
}
} }
void sim900_clean_buffer(char *buffer, int count) void sim900_clean_buffer(char *buffer, int count)
{ {
for(int i=0; i < count; i++) { for(int i = 0; i < count; i++) {
buffer[i] = '\0'; buffer[i] = '\0';
} }
} }
//HACERR quitar esta funcion ? //HACERR quitar esta funcion ?
@@ -149,30 +150,43 @@ boolean sim900_wait_for_resp(const char* resp, DataType type, unsigned int timeo
int len = strlen(resp); int len = strlen(resp);
int sum = 0; int sum = 0;
unsigned long timerStart, prevChar; //prevChar is the time when the previous Char has been read. unsigned long timerStart, prevChar; //prevChar is the time when the previous Char has been read.
// Serial.println("Wait for resp");
// Serial.println(resp);
// Serial.print("with length: ");
// Serial.println(len);
timerStart = millis(); timerStart = millis();
prevChar = 0; prevChar = 0;
//Serial.println("Received: ");
while(1) { while(1) {
if(sim900_check_readable()) { if(sim900_check_readable()) {
char c = serialSIM900->read(); char c = serialSIM900->read();
//Serial.print(c);
prevChar = millis(); prevChar = millis();
sum = (c==resp[sum]) ? sum+1 : 0; sum = (c==resp[sum]) ? sum+1 : 0;
if(sum == len)break; if(sum == len - 1) {
//Serial.print("BREAK");
break;
}
} }
if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) { if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
//Serial.println("Timeout");
return false; return false;
} }
//If interchar Timeout => return FALSE. So we can return sooner from this function. //If interchar Timeout => return FALSE. So we can return sooner from this function.
if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) { if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) {
//Serial.println("Interchar timeout");
return false; return false;
} }
} }
//Serial.println("End wait cycle");
//If is a CMD, we will finish to read buffer. //If is a CMD, we will finish to read buffer.
if(type == CMD) sim900_flush_serial(); if(type == CMD) sim900_flush_serial();
return true; return true;
} }
boolean sim900_check_with_cmd(const char* cmd, const char *resp, DataType type, unsigned int timeout, unsigned int chartimeout) boolean sim900_check_with_cmd(const char* cmd, const char *resp, DataType type, unsigned int timeout, unsigned int chartimeout)
{ {
sim900_send_cmd(cmd); sim900_send_cmd(cmd);

File diff suppressed because it is too large Load Diff

View File

@@ -5,18 +5,23 @@
// ---------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------
#include <EEPROM.h> #include <EEPROM.h>
#include <avr/pgmspace.h>
#include "GPRS_Shield_Arduino.h" #include "GPRS_Shield_Arduino.h"
#include <TroykaDHT.h> #include <TroykaDHT11.h>
//#define INIT
#define MAIN
/* /*
* EEPROM mapping * EEPROM mapping
* 1024 Kb * 1024 B
* 10 x 16 = 160 - address book | first addr 0 * 10 x 16 = 160 - address book | first addr 0
* 16 x 1 = 16 - parameters | first addr 160 * 16 x 1 = 16 - parameters | first addr 160
* 24 x 16 = 384 - commands | first addr 176
* 4 x 32 = 128 - short strings | first addr 560
* 4 x 64 = 256 - long strings | first addr 688
* ------------- * -------------
* 176 bytes * 944 bytes
* 848 bytes free * 80 bytes free
*/ */
#define AB_ADDR 0 #define AB_ADDR 0
@@ -27,40 +32,51 @@
#define SMS_ON_INDEX 0 #define SMS_ON_INDEX 0
#define AUTH_ON_INDEX 1 #define AUTH_ON_INDEX 1
#define FWD_ON_INDEX 2 #define FWD_ON_INDEX 2
#define EEPROM_INIT_INDEX 15
#define INIT_FLAG 0xAC #define CMD_ADDR 176
#define CMD_MAX_INDEX 23
const char cmd_status[] PROGMEM = {"status"}; #define S_STRING_ADDR 560
const char cmd_help[] PROGMEM = {"help"}; #define S_STRING_MAX_INDEX 3
const char cmd_sms_on[] PROGMEM = {"sms on"};
const char cmd_sms_off[] PROGMEM = {"sms off"};
const char cmd_on[] PROGMEM = {"on"};
const char cmd_off[] PROGMEM = {"off"};
const char cmd_reset[] PROGMEM = {"reset"};
const char cmd_a[] PROGMEM = {"a"};
const char cmd_b[] PROGMEM = {"b"};
const char cmd_forward[] PROGMEM = {"forward"};
const char cmd_modem_reset[] PROGMEM = {"modem reset"};
const char cmd_modem_console[] PROGMEM = {"modem console"};
const char cmd_edit_ab[] PROGMEM = {"edit ab"};
const char cmd_show_ab[] PROGMEM = {"show ab"};
const char cmd_show[] PROGMEM = {"show"};
const char cmd_clear[] PROGMEM = {"clear"};
const char cmd_exit[] PROGMEM = {"exit"};
const char cmd_auth_on[] PROGMEM = {"auth on"};
const char cmd_auth_off[] PROGMEM = {"auth off"};
const char cmd_fwd_on[] PROGMEM = {"forward on"};
const char cmd_fwd_off[] PROGMEM = {"forward off"};
const char str_init_eeprom[] PROGMEM = {"EEPROM init complete"}; #define L_STRING_ADDR 688
const char str_eab[] PROGMEM = {"Edit address book mode"}; #define L_STRING_MAX_INDEX 3
const char str_modem_cons_on[] PROGMEM = {"Modem console mode on"};
const char str_modem_cons_off[] PROGMEM = {"Modem console mode off"}; #define CMD_INDEX_STATUS 0
#define CMD_INDEX_HELP 1
#define CMD_INDEX_SMS_ON 2
#define CMD_INDEX_SMS_OFF 3
#define CMD_INDEX_ON 4
#define CMD_INDEX_OFF 5
#define CMD_INDEX_RESET 6
#define CMD_INDEX_A 7
#define CMD_INDEX_B 8
#define CMD_INDEX_FORWARD 9
#define CMD_INDEX_10 10
#define CMD_INDEX_MODEM_RESET 11
#define CMD_INDEX_MODEM_CONSOLE 12
#define CMD_INDEX_EDIT_AB 13
#define CMD_INDEX_SHOW_AB 14
#define CMD_INDEX_SHOW 15
#define CMD_INDEX_CLEAR 16
#define CMD_INDEX_EXIT 17
#define CMD_INDEX_AUTH_ON 18
#define CMD_INDEX_AUTH_OFF 19
#define CMD_INDEX_FWD_ON 20
#define CMD_INDEX_FWD_OFF 21
#define CMD_INDEX_22 22
#define CMD_INDEX_23 23
#define S_STRING_INDEX_1 0
#define S_STRING_INDEX_EAB_MODE 1
#define S_STRING_INDEX_MODEM_CONS_ON 2
#define S_STRING_INDEX_MODEM_CONS_OFF 3
#define L_STRING_INDEX_HELP_1 0
#define L_STRING_INDEX_HELP_AB 1
#define L_STRING_INDEX_FORMAT_AB 2
#define L_STRING_INDEX_HELP_2 3
const char str_help[] PROGMEM = {"Usage: help/status/on/off/reset/a/b/modem reset|console/edit ab/show ab/sms on|off/auth on|off/fwd on|off"};
const char str_help_ab[] PROGMEM = {"Type \"show\" to view, \"clear\" to erase, \"exit\" to return"};
const char str_format_ab[] PROGMEM = {"Format: \"i:+xxxxxxxxxxx\" to update or \"i:delete\" to delete"};
// Всякое // Всякое
#define TIMEOUT 60000 #define TIMEOUT 60000
@@ -72,10 +88,10 @@ const char str_format_ab[] PROGMEM = {"Format: \"i:+xxxxxxxxxxx\" to update or
// Сенсоры // Сенсоры
#define A_PIN A0 #define A_PIN A0
#define T_PIN A1 #define T_PIN A1
#define VOUT_PIN A2 #define BAT_T_PIN A2
#define VBAT_PIN A3 #define VBAT_PIN A3
#define V5_PIN A4 #define VUSB_PIN A4
#define V12_PIN A5 #define VIN_PIN A5
#define LED_PIN 13 #define LED_PIN 13
#define BTN_PIN 12 #define BTN_PIN 12
@@ -92,8 +108,9 @@ const char str_format_ab[] PROGMEM = {"Format: \"i:+xxxxxxxxxxx\" to update or
#define SMS_SIZE 160 #define SMS_SIZE 160
#define PHONE_SIZE 16 #define PHONE_SIZE 16
#define DATE_SIZE 24 #define DATE_SIZE 24
#define BUFFER_SIZE 64 #define SHORT_BUFFER_SIZE 32
#define LONG_BUFFER_SIZE 240 #define LONG_BUFFER_SIZE 64
#define VLONG_BUFFER_SIZE 240
// Флаги статуса // Флаги статуса
#define TEMP_WARN 0x01 #define TEMP_WARN 0x01
@@ -137,15 +154,22 @@ void printHelp();
void printParams(); void printParams();
bool authOK(char *phone); bool authOK(char *phone);
void sprint(const char *description, const byte data, const bool ln = true); void sprint(const char * description, const byte data, const bool ln = true);
void sprint(const char *description, const char *data, const bool ln = true); void sprint(const char * description, const char * data, const bool ln = true);
void sprint(const char *description, const int data, const bool ln = true); void sprint(const char * description, const int data, const bool ln = true);
void sprint(const char *description, const unsigned int data, const bool ln = true); void sprint(const char * description, const unsigned int data, const bool ln = true);
void sprint(const char *description, const long int data, const bool ln = true); void sprint(const char * description, const long int data, const bool ln = true);
void sprint(const char *description, const long unsigned int data, const bool ln = true); void sprint(const char * description, const long unsigned int data, const bool ln = true);
void sprint(const char *description, const float data, const bool ln = true); void sprint(const char * description, const float data, const bool ln = true);
void sprint(const char *description, const bool ln = true); void sprint(const char * description, const bool ln = true);
void sprint_P(const char *description, const bool ln = true);
// EEPROM utils
bool setCommand(byte index, const char *command);
bool getCommand(byte index, char *command);
bool setShortString(byte index, const char *s_buffer);
bool getShortString(byte index, char *s_buffer);
bool setLongString(byte index, const char *l_buffer);
bool getLongString(byte index, char *l_buffer);
bool smsOn(); bool smsOn();
void smsOn(bool temp); void smsOn(bool temp);
@@ -165,4 +189,3 @@ float readvcc();
void getVs(); void getVs();
void printVs(); void printVs();
void getTH(); void getTH();
void initEEPROM();