Hey everyone, I have a project for a weather station which sends data by GPRS to my database. My problem is: In some places the signal from one celphone company is better than others. I was wondering if I could adapt my code to try to connect to more than one APN. Can this be done? This can really help me to improve the GPRS network?
This is the part of the code that I am using to send the data via gprs
/*
* Versão do envio do 3G usando biblioteca e não o comandos AT diretamente
*
*/
extern float temperatura;
extern float umidadeSolo;
extern float temperaturaSolo;
extern float umidade;
extern float temperatura;
extern float pressao;
extern float velocidadeVento;
extern float precipitacao;
extern float altitude;
extern uint8_t direcaoVento;
/* Dados APN */
const char apn[] = "claro";
const char gprsUser[] = "claro.com.br";
const char gprsPass[] = "claro";
/* Datalhes do servidor */
const char server[] = "MYSERVER";
const char resource[] = "MYPHP";
void iniciaModem3GV1() {
/* Inicia porta do modem GPRS */
Serial.println();
Serial.println( F( "Iniciando modem GPRS") );
gprsSerial.begin(baudeRate3G);
delay(3000);
while (!gprsSerial);
modem.restart();
String modemInfo = modem.getModemInfo();
Serial.print( F( "Informações do modem: ") );
Serial.println(modemInfo);
Serial.print( F( "Aguardando rede ") );
uint32_t tFinal = millis() + 60000;
while ((!modem.waitForNetwork(5000))&&(millis()<tFinal)) {
Serial.print(F("."));
}
if (modem.isNetworkConnected()) {
Serial.println( F( "OK") );
} else{
Serial.println( F( "Falha, sem rede, reiniciando") );
#ifdef ARDUINO_ARCH_AVR
wdt_enable(WDTO_8S);
#endif
while (1);
}
Serial.print(F("GPRS, Conectando a "));
Serial.print(apn);
tFinal = millis() + 60000;
while ((!modem.gprsConnect(apn, gprsUser, gprsPass))&&(millis()<tFinal)) {
Serial.print(F( "."));
}
if (modem.isGprsConnected()) {
Serial.println(" OK");
} else {
Serial.println(" falhou");
#ifdef ARDUINO_ARCH_AVR
wdt_enable(WDTO_8S);
#endif
while (1);
}
}
bool enviaDados3GV1() {
#ifdef ARDUINO_ARCH_AVR
wdt_reset();
#elif defined(ARDUINO_ARCH_ESP8266)
yield();
#endif
/* Reinicia quando o GPRS estiver desconectado */
if (!modem.isGprsConnected()){
Serial.println( F( "GPR desconectadoo") );
return false;
}
bool result = true;
Serial.println( F( "Enviando dados por 3G") );
Serial.print( F( "Conectando ao servidor: ") );
Serial.print(server);
unsigned long timeout = millis() + 30000UL;
while ((!client.connect(server, 80, 5))&&(millis()<timeout)){
Serial.print( F( "."));
#ifdef ARDUINO_ARCH_AVR
wdt_reset();
#elif defined(ARDUINO_ARCH_ESP8266)
yield();
#endif
delay(5);
}
result = client.connected();
if (result){
Serial.println(F( " OK") );
/* HTTP GET request */
Serial.println( F( "HTTP GET request...") );
client.print( "GET ");
client.print(resource);
client.print("data1=");client.print(temperatura);
client.print("&data2=0");
client.print("&data3=");client.print(umidade);
client.print("&data4=");client.print(pressao);
client.print("&data5=");client.print(altitude);
client.print("&data6=");client.print(velocidadeVento);
client.print("&data7=");client.print(direcaoVento);
client.print("&data8=");client.print(temperaturaSolo);
client.print("&data9=");client.print(umidadeSolo);
client.print("&data10=");client.print(precipitacao);
client.println( " HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
Serial.println(F( "Dados enviados, aguardando retorno") );
/* Lê retorno do servidor */
unsigned long timeout = millis() + 10000UL;
while ((client.connected()) && (client.available()<1) && (millis() < timeout )) {
#ifdef ARDUINO_ARCH_AVR
wdt_reset();
#endif
}
if (client.available()<1){
Serial.println( F( "Sem resposta do servidor ") );
result = false;
} else {
while (client.available()>0) {
while (client.available()>0) {
char c = client.read();
Serial.print(c);
#ifdef ARDUINO_ARCH_AVR
wdt_reset();
#endif
}
delay(2);
}
}
Serial.println();
}
/* Desconecta */
client.stop();
return (result);
}