Compare commits

...

1 Commits

Author SHA1 Message Date
eKa
e25e9fdcdb Old backup 2020-01-24 22:32:44 +05:00
4 changed files with 363 additions and 1005 deletions

View File

@ -35,7 +35,8 @@
GPRS* GPRS::inst;
GPRS::GPRS(Stream& serial, uint8_t pkPin) {
GPRS::GPRS(Stream& serial, uint8_t pkPin)
{
_pkPin = pkPin;
inst = this;
@ -44,13 +45,16 @@ GPRS::GPRS(Stream& serial, uint8_t pkPin) {
sim900_init(stream);
}
bool GPRS::init(void) {
bool GPRS::init(void)
{
if (!sim900_check_with_cmd("AT\r\n","OK\r\n",CMD))
return false;
if (!sim900_check_with_cmd("AT+CFUN=1\r\n","OK\r\n",CMD))
return false;
if (!checkSIMStatus())
return false;
@ -101,7 +105,8 @@ void GPRS::reset() {
powerOn();
}
bool GPRS::checkSIMStatus(void) {
bool GPRS::checkSIMStatus(void)
{
char gprsBuffer[32];
byte count = 0;
sim900_clean_buffer(gprsBuffer,32);
@ -120,7 +125,8 @@ bool GPRS::checkSIMStatus(void) {
return true;
}
bool GPRS::sendSMS(const char *number, const char *data) {
bool GPRS::sendSMS(char *number, char *data)
{
// Set message mode to ASCII
if(!sim900_check_with_cmd("AT+CMGF=1\r\n", "OK\r\n", CMD)) {
return false;
@ -138,11 +144,11 @@ bool GPRS::sendSMS(const char *number, const char *data) {
sim900_send_cmd(data);
delay(500);
sim900_send_End_Mark();
return sim900_wait_for_resp("OK\r\n", CMD, 2);
return true;
}
char GPRS::isSMSunread() {
char GPRS::isSMSunread()
{
char gprsBuffer[48]; //48 is enough to see +CMGL:
char *s;
@ -170,7 +176,7 @@ char GPRS::isSMSunread() {
*/
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);
if(NULL != ( s = strstr(gprsBuffer,"OK"))) {
@ -229,148 +235,22 @@ bool GPRS::readSMS(int messageIndex, char *message, byte length) {
return false;
}
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) {
bool GPRS::getSMS(char *message, char *phone, char *datetime) {
/* Response is like:
+CMT: "+79772941911","","15/12/15,01:51:24+12"
SMS text here
*/
//if(!sim900_check_with_cmd("","+CMT: ", CMD, 3)) return false;
if(!sim900_wait_for_resp("+CMT: ", CMD, 1)) return false;
if(!sim900_check_with_cmd("","+CMT: ",CMD)) return false;
byte i = 0;
//char gprsBuffer[80 + 160];
char gprsBuffer[80 + 160];
char *p1, *p2;
sim900_clean_buffer(gprsBuffer, size);//sizeof(gprsBuffer));
sim900_read_buffer(gprsBuffer, size - 1);//sizeof(gprsBuffer));
sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
sim900_read_buffer(gprsBuffer,sizeof(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(gprsBuffer);
@ -443,9 +323,9 @@ void GPRS::answer(void)
sim900_send_cmd("ATA\r\n");
}
bool GPRS::ifcallNow(byte timeout)
bool GPRS::ifcallNow(void)
{
return sim900_check_with_cmd("","RING\r\n",CMD, timeout);
return sim900_check_with_cmd("","RING\r\n",CMD);
}
bool GPRS::ifcallEnd(void)
@ -468,9 +348,9 @@ bool GPRS::disableCLIPring(void)
return sim900_check_with_cmd("AT+CLIP=0\r\n","OK\r\n",CMD);
}
bool GPRS::isCallActive(char *number, char *gprsBuffer)
bool GPRS::isCallActive(char *number)
{
//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;
int i = 0;
@ -490,12 +370,11 @@ bool GPRS::isCallActive(char *number, char *gprsBuffer)
OK
*/
sim900_clean_buffer(gprsBuffer, 46);
sim900_read_buffer(gprsBuffer, 45);
sim900_clean_buffer(gprsBuffer,29);
sim900_read_buffer(gprsBuffer,27);
//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
//Serial.println("Wait OK");
//sim900_wait_for_resp("OK\r\n", CMD);
sim900_wait_for_resp("OK\r\n", CMD);
//Serial.print("Buffer isCallActive 1: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"+CPAS:"))) {
s = s + 7;
@ -513,8 +392,8 @@ bool GPRS::isCallActive(char *number, char *gprsBuffer)
OK
*/
sim900_clean_buffer(gprsBuffer, 46);
sim900_read_buffer(gprsBuffer, 45);
sim900_clean_buffer(gprsBuffer,46);
sim900_read_buffer(gprsBuffer,45);
//Serial.print("Buffer isCallActive 2: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"+CLCC:"))) {
//There is at least one CALL ACTIVE, get number
@ -565,9 +444,9 @@ bool GPRS::getDateTime(char *buffer) {
return sim900_wait_for_resp("OK\r\n", CMD);
}
return false;
}
}
byte GPRS::getSignalStrength() {
byte GPRS::getSignalStrength() {
//AT+CSQ: 00,00 --> 13 + CRLF = 15
// --> CRLF = 2
//OK --> 2 + CRLF = 4
@ -588,7 +467,8 @@ byte GPRS::getSignalStrength() {
//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(char* apn, char* userName, char* passWord, int timeout) {
bool GPRS::join(char* apn, char* userName, char* passWord, int timeout)
{
byte i;
char *p, *s;
char ipAddr[32];
@ -659,11 +539,13 @@ bool GPRS::join(char* apn, char* userName, char* passWord, int timeout) {
return false;
}
void GPRS::disconnect() {
void GPRS::disconnect()
{
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 num[4];
char resp[96];
@ -676,8 +558,8 @@ bool GPRS::connect(Protocol ptl,const char * host, int port, int timeout) {
itoa(port, num, 10);
sim900_send_cmd(num);
sim900_send_cmd("\r\n");
// sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n",host, port);
} else if(ptl == UDP) {
// sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n",host, port);
} else if(ptl == UDP) {
sim900_send_cmd("AT+CIPSTART=\"UDP\",\"");
sim900_send_cmd(host);
sim900_send_cmd("\",");
@ -685,14 +567,14 @@ bool GPRS::connect(Protocol ptl,const char * host, int port, int timeout) {
sim900_send_cmd(num);
sim900_send_cmd("\r\n");
// sprintf(cmd, "AT+CIPSTART=\"UDP\",\"%s\",%d\r\n",host, port);
} else {
// sprintf(cmd, "AT+CIPSTART=\"UDP\",\"%s\",%d\r\n",host, port);
} else {
return false;
}
}
delay(2000);
delay(2000);
//sim900_send_cmd(cmd);
sim900_read_buffer(resp,96,timeout);
sim900_read_buffer(resp,96,timeout);
//Serial.print("Connect resp: "); Serial.println(resp);
if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
@ -702,7 +584,8 @@ bool GPRS::connect(Protocol ptl,const char * host, int port, int timeout) {
}
//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 resp[96];
@ -727,7 +610,8 @@ bool GPRS::connect(Protocol ptl,const __FlashStringHelper *host, const __FlashSt
return false;
}
bool GPRS::is_connected(void) {
bool GPRS::is_connected(void)
{
char resp[96];
sim900_send_cmd("AT+CIPSTATUS\r\n");
sim900_read_buffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
@ -741,7 +625,8 @@ bool GPRS::is_connected(void) {
}
}
bool GPRS::close() {
bool GPRS::close()
{
// if not connected, return
if (!is_connected()) {
return true;
@ -749,19 +634,23 @@ bool GPRS::close() {
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();
}
int GPRS::wait_readable(int wait_time) {
int GPRS::wait_readable(int 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;
}
int GPRS::send(const char * str, int len) {
int GPRS::send(const char * str, int len)
{
//char cmd[32];
char num[4];
if(len > 0){
@ -823,7 +712,8 @@ int GPRS::send(const char * str, int len) {
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;
char* p = (char*)str;
for(int i = 0; i < 4; i++) {
@ -839,17 +729,20 @@ uint32_t GPRS::str_to_ip(const char* str) {
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);
return ip_string;
}
unsigned long GPRS::getIPnumber() {
unsigned long GPRS::getIPnumber()
{
return _ip;
}
bool GPRS::controlGPIO(uint8_t index, uint8_t level) {
char num[4];
bool GPRS::controlGPIO(uint8_t index, uint8_t level)
{
char num[1];
sim900_send_cmd("AT+SGPIO=0,");
itoa(index, num, 10);

View File

@ -32,18 +32,6 @@
#ifndef __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"
/** GPRS class.
@ -56,8 +44,9 @@ enum Protocol {
UDP = 2,
};
class GPRS {
public:
class GPRS
{
public:
/** Create GPRS instance
*/
@ -100,7 +89,7 @@ class GPRS {
* false on success
* true on error
*/
bool sendSMS(const char* number, const char* data);
bool sendSMS(char* number, char* data);
/** Check if there is any UNREAD SMS: this function DOESN'T change the UNREAD status of the SMS
* @returns
@ -122,8 +111,6 @@ class GPRS {
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
* @param message buffer used to get SMS message
* @param phone buffer used to get SMS's sender phone number
@ -133,7 +120,7 @@ class GPRS {
* false on error
*/
bool getSMS(char *message, char *phone, char *datetime, char *gprsBuffer, byte size);
bool getSMS(char *message, char *phone, char *datetime);
/** delete SMS message on SIM card
* @param index the index number which SMS message will be delete
@ -170,7 +157,7 @@ class GPRS {
* true on success
* false on error
*/
bool ifcallNow(byte timeout = 1);
bool ifcallNow(void);
bool ifcallEnd(void);
@ -183,7 +170,7 @@ class GPRS {
* true on success
* false on error
*/
bool isCallActive(char *number, char *gprsBuffer);
bool isCallActive(char *number);
/** get DateTime from SIM900 (see AT command: AT+CLTS=1) as string
* @param
@ -291,7 +278,7 @@ class GPRS {
*/
bool controlGPIO(uint8_t num, uint8_t level);
private:
private:
bool checkSIMStatus(void);
uint8_t _pkPin = 9;

View File

@ -60,15 +60,15 @@ int sim900_wait_readable (int wait_time)
return dataLen;
}
void sim900_flush_serial() {
while(sim900_check_readable()) {
void sim900_flush_serial()
{
while(sim900_check_readable()){
char c = serialSIM900->read();
}
}
void sim900_read_buffer(char *buffer, int count, unsigned int timeout, unsigned int chartimeout)
{
//Serial.println("Read buff");
int i = 0;
unsigned long timerStart, prevChar;
timerStart = millis();
@ -76,12 +76,11 @@ void sim900_read_buffer(char *buffer, int count, unsigned int timeout, unsigned
while(1) {
while (sim900_check_readable()) {
char c = serialSIM900->read();
//Serial.print(c);
prevChar = millis();
buffer[i++] = c;
if(i >= count) break;
if(i >= count)break;
}
if(i >= count) break;
if(i >= count)break;
if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
break;
}
@ -94,7 +93,7 @@ void sim900_read_buffer(char *buffer, int count, unsigned int timeout, unsigned
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';
}
}
@ -150,43 +149,30 @@ boolean sim900_wait_for_resp(const char* resp, DataType type, unsigned int timeo
int len = strlen(resp);
int sum = 0;
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();
prevChar = 0;
//Serial.println("Received: ");
while(1) {
if(sim900_check_readable()) {
char c = serialSIM900->read();
//Serial.print(c);
prevChar = millis();
sum = (c==resp[sum]) ? sum+1 : 0;
if(sum == len - 1) {
//Serial.print("BREAK");
break;
}
if(sum == len)break;
}
if ((unsigned long) (millis() - timerStart) > timeout * 1000UL) {
//Serial.println("Timeout");
return false;
}
//If interchar Timeout => return FALSE. So we can return sooner from this function.
if (((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)) {
//Serial.println("Interchar timeout");
return false;
}
}
//Serial.println("End wait cycle");
//If is a CMD, we will finish to read buffer.
if(type == CMD) sim900_flush_serial();
return true;
}
boolean sim900_check_with_cmd(const char* cmd, const char *resp, DataType type, unsigned int timeout, unsigned int chartimeout)
{
sim900_send_cmd(cmd);

File diff suppressed because it is too large Load Diff