Skip to content

Commit

Permalink
WIP mms
Browse files Browse the repository at this point in the history
  • Loading branch information
tartinesKiller committed Oct 22, 2021
1 parent 003a434 commit 92a8a3c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
55 changes: 39 additions & 16 deletions GPRS_Shield_Arduino.cpp
Expand Up @@ -88,12 +88,11 @@ bool GPRS::initMms(const char *mmsUrl, const char *mmsGw, uint8_t port, const ch
termMms();
return false;
}
sim900_clean_buffer(gprsBuffer, 32);
sim900_send_cmd(F("AT+CMMSCURL=\""));
sim900_send_cmd(mmsUrl);
sim900_send_cmd("\"\r\n");
sim900_read_buffer(gprsBuffer, 32, DEFAULT_TIMEOUT);
if (strstr(gprsBuffer, "OK") == NULL) {

char *cmmsUrlCmd = (char*)calloc(50, sizeof(char));
sprintf(cmmsUrlCmd, "AT+CMMSCURL=\"%s\"\r\n", mmsUrl);
bool successCmmsUrl = sim900_check_with_cmd(cmmsUrlCmd, "OK", CMD);
if (!successCmmsUrl) {
DEBUG(F("Failed to configure MMS url"));
termMms();
return false;
Expand All @@ -105,6 +104,16 @@ bool GPRS::initMms(const char *mmsUrl, const char *mmsGw, uint8_t port, const ch
return false;
}

char *mmsProtoCmd = (char*)calloc(50, sizeof(char));
sprintf(mmsProtoCmd, "AT+CMMSPROTO=\"%s\",%d\r\n", mmsGw, port);
bool successMmsProto = sim900_check_with_cmd(mmsProtoCmd, "OK", CMD);
free(mmsProtoCmd);
if (!successMmsProto) {
DEBUG(F("Failed to set GW and port"));
termMms();
return false;
}

sim900_clean_buffer(gprsBuffer, 32);
char *cMmsSendCfgCmd = (char*)calloc(31, sizeof(char));
sprintf(cMmsSendCfgCmd, "AT+CMMSSENDCFG=%d,%d,0,0,2,4,2,0\r\n", (byte)validity, (byte)priority);
Expand Down Expand Up @@ -133,7 +142,9 @@ bool GPRS::initMms(const char *mmsUrl, const char *mmsGw, uint8_t port, const ch
return false;
}

if (!sim900_check_with_cmd(F("AT+SAPBR=1,1\r\n"), "OK", CMD)) {
delay(1000);

if (!sim900_check_with_cmd(F("AT+SAPBR=1,1\r\n"), "OK", CMD, DEFAULT_TIMEOUT, 30000)) {
DEBUG(F("Failed to activate bearer context"));
termMms();
return false;
Expand All @@ -144,13 +155,15 @@ bool GPRS::initMms(const char *mmsUrl, const char *mmsGw, uint8_t port, const ch
termMms();
return false;
}

return true;
}

void GPRS::termMms() {
sim900_send_cmd(F("AT+CMMSTERM\r\n"));
}

bool GPRS::sendMMS(const char *dstNumber, char (*nextByteFn)(), uint32_t bodySize) {
bool GPRS::sendMMS(const char *dstNumber, byte (*nextByteFn)(), uint32_t bodySize) {
if (!sim900_check_with_cmd(F("AT+CMMSEDIT=1\r\n"), "OK", CMD)) {
DEBUG(F("Failed to MMSEDIT"));
termMms();
Expand All @@ -159,7 +172,7 @@ bool GPRS::sendMMS(const char *dstNumber, char (*nextByteFn)(), uint32_t bodySiz

char *mmsDownCmd = (char*)calloc(40, sizeof(char));
sprintf(mmsDownCmd, "AT+CMMSDOWN=\"PIC\",%d,300000\r\n", bodySize);
bool successMmsDownCmd = sim900_check_with_cmd(mmsDownCmd, "OK", CMD);
bool successMmsDownCmd = sim900_check_with_cmd(mmsDownCmd, "CONNECT", CMD);
free(mmsDownCmd);
if (!successMmsDownCmd) {
DEBUG(F("Failed to initiate MMS down"));
Expand All @@ -168,7 +181,7 @@ bool GPRS::sendMMS(const char *dstNumber, char (*nextByteFn)(), uint32_t bodySiz
}

for (uint32_t byteSent = 0; byteSent < bodySize; byteSent++) {
char byte = nextByteFn();
byte byte = nextByteFn();
sim900_send_byte(byte);
}
if (!sim900_wait_for_resp("OK", CMD)) {
Expand All @@ -177,22 +190,28 @@ bool GPRS::sendMMS(const char *dstNumber, char (*nextByteFn)(), uint32_t bodySiz
return false;
}

sim900_send_cmd(F("AT+CMMSRECP=\""));
sim900_send_cmd(dstNumber);
sim900_send_cmd("\"\r\n");
if (!sim900_wait_for_resp("OK", CMD)) {
char *setRecpCmd = (char*)calloc(50, sizeof(char));
sprintf(setRecpCmd, "AT+CMMSRECP=\"%s\"\r\n", dstNumber);
bool successSetRecp = sim900_check_with_cmd(setRecpCmd, "OK", CMD);
free(setRecpCmd);

if (!successSetRecp) {
DEBUG(F("Failed to set recipient"));
termMms();
return false;
}

if (!sim900_check_with_cmd(F("AT+CMMSSEND"), "OK", CMD, 300000)) {
sim900_send_cmd(F("AT+CMMSVIEW\r\n"));
delay(500);
sim900_flush_serial();

if (!sim900_check_with_cmd(F("AT+CMMSSEND\r\n"), "OK", CMD, 300000, 900000)) {
DEBUG(F("Failed to send MMS"));
termMms();
return false;
}

sim900_send_cmd(F("AT+CMMSEDIT=0")); // clear current MMS
sim900_send_cmd(F("AT+CMMSEDIT=0\r\n")); // clear current MMS
termMms();
return true;
}
Expand All @@ -202,6 +221,10 @@ bool GPRS::init(void) {
return false;
}

if (!sim900_check_with_cmd(F("AT+CMEE=2\r\n"), "OK", CMD)) {
return false;
}

if (!sim900_check_with_cmd(F("AT+CFUN=1\r\n"), "OK\r\n", CMD)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion GPRS_Shield_Arduino.h
Expand Up @@ -136,7 +136,7 @@ class GPRS {

bool initMms(const char *mmsUrl, const char *mmsGw, uint8_t port, const char *apn, MmsValidity validity = MmsValidity::NOT_SET, MmsPriority priority = MmsPriority::NOT_SET);

bool sendMMS(const char *dstNumber, char (*nextByteFn)(), uint32_t bodySize);
bool sendMMS(const char *dstNumber, byte (*nextByteFn)(), uint32_t bodySize);

void termMms();

Expand Down
8 changes: 5 additions & 3 deletions sim900.cpp
Expand Up @@ -73,7 +73,7 @@ void sim900_read_buffer(char* buffer, int count, unsigned int timeout, unsigned
char c = serialSIM900->read();
prevChar = millis();
buffer[i++] = c;
DEBUG("-"); DEBUG(c);
DEBUG("<"); DEBUG(c);
if (i >= count) {
break;
}
Expand Down Expand Up @@ -104,6 +104,7 @@ char* sim900_read_string_until(char* buffer, uint16_t count, const char* pattern
while (1) {
if (serialSIM900->available()) {
char c = serialSIM900->read();
DEBUG("<");
DEBUG(c);
prevChar = millis();
buffer[i++] = c;
Expand All @@ -128,7 +129,6 @@ char* sim900_read_string_until(char* buffer, uint16_t count, const char* pattern
break;
}
}
DEBUG("\n\n");

return foundPtr;
}
Expand All @@ -142,12 +142,14 @@ void sim900_clean_buffer(char* buffer, int count) {

void sim900_send_byte(uint8_t data) {
serialSIM900->write(data);
DEBUG(">");
DEBUG((char)data);
}


void sim900_send_char(const char c) {
serialSIM900->write(c);
DEBUG(">");
DEBUG(c);
}

Expand Down Expand Up @@ -188,7 +190,7 @@ boolean sim900_wait_for_resp(const char* resp, DataType type, unsigned int timeo
while (1) {
if (sim900_check_readable()) {
char c = serialSIM900->read();
DEBUG("-");
DEBUG("<");
DEBUG(c);
prevChar = millis();
sum = (c == resp[sum]) ? sum + 1 : 0;
Expand Down
4 changes: 2 additions & 2 deletions sim900.h
Expand Up @@ -38,8 +38,8 @@
#define DEFAULT_TIMEOUT 5 //seconds
#define DEFAULT_INTERCHAR_TIMEOUT 3000 //miliseconds

#define DEBUG(x)
//#define DEBUG(x) Serial.print(x)
// #define DEBUG(x)
#define DEBUG(x) Serial.print(x)
enum DataType {
CMD = 0,
DATA = 1,
Expand Down

0 comments on commit 92a8a3c

Please sign in to comment.