A6 GSM Module stop transmitting after a while

a6
sim800
gsm
sim800l

#1

Hello,
I recently made a GPS tracker using Ublox Neo-6M gps module, A6 GSM Module and Atmega328P. The device is working fine but after few hours, sometimes after 2 days the device stops transmitting data, when i try calling the sim card it get through and i can hear it ring, but somehow the data is no transmitted. Upon disconnecting from power and reconnecting the device start sending data again and stops after a while, so i added a transistor to automatically power down the GSM module if the velocity drop to zero and switch on if the velocity climb above a particular value, however this do not work either. I have applied AES encryption to the data, i don’t know whether its causing the problem or not. I will post the code below, it would be really grateful if anyone could point out whats wrong with the code.

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <AES.h>
#include <Base64.h>
#include <String.h>

SoftwareSerial SIM900(7, 8);
int gsm = 4;
int gps = 5;
int count = 0;
float velocity = 0;
float flat = 0;
float flon = 0;
bool gsmOFF = false;

void setup()
{
pinMode(gps, OUTPUT);
pinMode(gsm, OUTPUT);

digitalWrite(gsm, HIGH);
digitalWrite(gps, HIGH);
}

void loop()
{
String ndata = gpsData();

if (velocity < 7)
{
if (count < 6){
count = count + 1;
}
if (count == 5)
{

  String data = aesEncryptedData(ndata);
  data = "GET http://******************************************+data+  "\r\n\x1A";
  sendData(data);

  digitalWrite(gsm, LOW);
  gsmOFF = true;

}

}
else {
if (gsmOFF) {
digitalWrite(gsm, HIGH);
delay(20000);
gsmOFF = false;
String data = aesEncryptedData(ndata);
data = “GET http://”******************************************" + data + “\r\n\x1A”;
sendData(data);
}
count = 0;
}
if (!gsmOFF) {
String data = aesEncryptedData(ndata);
data = “GET http://******************************************” + data + “\r\n\x1A”;
sendData(data);

}

}

String gpsData()
{
SoftwareSerial ss(3, 2);

TinyGPS gps;

unsigned long age;

String getStr;

ss.begin(9600);

for (unsigned long start = millis(); millis() - start < 1000;) {

if (ss.available()) {

  char c = ss.read();
  gps.encode(c);
}

}

gps.f_get_position(&flat, &flon, &age);
velocity = round(gps.f_speed_kmph());
char buf[16];
getStr = dtostrf(flat, 4, 6, buf);
getStr += “~”;
getStr += dtostrf(flon, 4, 6, buf);
getStr += “~”;
getStr += dtostrf(velocity, 3, 1, buf);
ss.end();
return getStr;
}

String aesEncryptedData(String dataString) {

AES aes;
byte key = (unsigned char)“01234567890101230123456789010123”;
int bits = 256;
byte iv [N_BLOCK] ;
unsigned long long int myIv = 123456789;

byte plain[dataString.length()];
int plainLength = sizeof(plain) - 1;
dataString.getBytes(plain, plainLength);
int padedLength = plainLength + N_BLOCK - plainLength % N_BLOCK;
byte plain_p[padedLength];
byte cipher [padedLength] ;
byte check [padedLength] ;
aes.set_IV(myIv);
aes.get_IV(iv);
aes.do_aes_encrypt(plain, plainLength, cipher, key, bits, iv);
aes.set_IV(myIv);
aes.get_IV(iv);

char ivdata[sizeof(iv)];
int encrypted_size = sizeof(cipher);
char encoded[encrypted_size];
base64_encode(encoded, (char*)cipher, encrypted_size);
base64_encode(ivdata, (char*)iv, sizeof(iv));

String d = (char*) encoded;
String d1 = (char*) ivdata;

String d2 = d1 + “~” + d;
return (urlencode(d2));

}

void sendData(String data) {

int delayTime  = 1000;
int delayTimeF = 250;

SIM900.begin(9600);

SIM900.println("AT"); /* Check Communication */
delay(delayTime);

SIM900.println("AT+CIPMODE=0"); /* Non-Transparent (normal) mode for TCP/IP application */
delay(delayTime);

SIM900.println("AT+CIPMUX=0");  /* Single TCP/IP connection mode */
delay(delayTime);

SIM900.println("AT+CGATT=1"); /* Attach to GPRS Service */
delay(delayTime);

SIM900.println("AT+CREG?"); /* Network registration status */
delay(delayTime);

SIM900.println("AT+CGATT?");  /* Attached to or detached from GPRS service */
delay(delayTime);

SIM900.println("AT+CSTT=\"internet\""); /* Start task and set APN */
delay(delayTime);

SIM900.println("AT+CIICR"); /* Bring up wireless connection with GPRS */
delay(delayTime);

SIM900.println("AT+CIFSR"); /* Get local IP address */
delay(delayTime);

SIM900.println("AT+CIPSTART=\"TCP\",\"****.************.in\",80");
delay(2000);

SIM900.println("AT+CIPSEND"); /* Send data through TCP connection */
delay(delayTime);

SIM900.println(data);  /* URL for data to be sent to */
delay(delayTime);

SIM900.println("AT+CIPSHUT"); /* Deactivate GPRS PDP content */
delay(delayTime);

SIM900.end();

}

String urlencode(String str)
{
String encodedString = “”;
char c;
char code0;
char code1;
char code2;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == ’ ') {
encodedString += ‘+’;
} else if (isalnum©) {
encodedString += c;
} else {
code1 = (c & 0xf) + ‘0’;
if ((c & 0xf) > 9) {
code1 = (c & 0xf) - 10 + ‘A’;
}
c = (c >> 4) & 0xf;
code0 = c + ‘0’;
if (c > 9) {
code0 = c - 10 + ‘A’;
}
code2 = ‘\0’;
encodedString += ‘%’;
encodedString += code0;
encodedString += code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}

Thank You.
Best Regards.
Richards Babu.


#2

Modules can become non responsive or enter a bad state. You have to have provision in your code to exit and reboot the module in case of no response or in case of no data sent for a while. I do this by adding timeouts which count for a while whether data is sent or not and if not sent for a while or if there is no response for AT commands for a while then i reboot the module. A hard reset of power using MOSFET is recommended.