I have a Sim900 using a Hologram SIM that connects just fine in France. When I’ve taken this Sim900 to Haiti, I can see that it connects to the cell tower (GSM), more specifically Digicel (Jamaica). That is this function boolean connectGsmNetwork()
returns true. But it fails to connect to the GPRS network in the script below. That is, this function boolean connectGsmNetwork()
returns false. Is this the expected behavior in Haiti, i.e. no 2G connection to GPRS as opposed to France where this works fine?
// Select your modem:
#define TINY_GSM_MODEM_SIM900
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
//include some libraries
//#include <ArduinoJson.h>
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Import paswword and other stuff
//#include "arduino_secrets.h"
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(7, 8); // RX, TX
// Increase RX buffer to capture the entire response
// Chips without internal buffering (A6/A7, ESP8266, M590)
// need enough space in the buffer for the entire response
// else data will be lost (and the http library will fail).
#if !defined(TINY_GSM_RX_BUFFER)
#define TINY_GSM_RX_BUFFER 650
#endif
// See all AT commands, if wanted
// #define DUMP_AT_COMMANDS
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "hologram";
const char gprsUser[] = "";
const char gprsPass[] = "";
const char server[] = "your.restapi.com"; //REST_SERVER;
const char resource[] = "/your/resource/";
const char path[] = "/api/endpoint/";// REST_ENDPOINT;
String imei_string = "";
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
// GSM connection
TinyGsm modem = TinyGsm(debugger);
#else
TinyGsm modem = TinyGsm(SerialAT);
#endif
// Socket connection
#ifdef USE_SSL
TinyGsmClientSecure socket = TinyGsmClientSecure(modem);
const int port = 443;
#else
TinyGsmClient socket = TinyGsmClient(modem);
const int port = 80;
#endif
boolean gprsOpen = false;
boolean sockOpen = false;
boolean postOK = false;
int attempt = 1; //compteur de tentative
void setup() {
// Set console baud rate
SerialMon.begin(9600);
delay(10);
SerialMon.println("Switching on...");
// Added to the script
powerSIM900();
// !!!!!!!!!!!
// Set your reset, enable, power pins here
// !!!!!!!!!!!
// Set GSM module baud rate
//TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
SerialAT.begin(9600);
delay(6000);
}
boolean isValidNumber(String str) {
for(byte i=0;i<str.length();i++) {
if(!isDigit(str.charAt(i))) return false;
}
return true;
}
boolean restartAndConnect() {
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// modem.init();
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo);
if (imei_string.length() == 0) {
imei_string = modem.getIMEI();
/* Test
SerialMon.print(F("imei_string: "));
SerialMon.println(imei_string);
imei_string = "1234?abc";
SerialMon.print(F("imei_string: "));
SerialMon.println(imei_string);
*/
// Sometimes modem.getIMEI() returns garbage, so checking it here
while(!isValidNumber(imei_string)) {
//SerialMon.print(F("imei_string: "));
//SerialMon.println(imei_string);
imei_string = modem.getIMEI();
//SerialMon.print(F("imei_string: "));
//SerialMon.println(imei_string);
delay(1000);
}
}
if (GSM_PIN && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }
if (!connectSocket()) {
return false; // failed to connect socket
}
return true;
}
void loop() {
if (restartAndConnect()) { httpPost(); }
networkDisconnect();
if (postOK==true) { attempt = 1; postOK = false; delay(3600000); } // attente 1 hr
else {
delay(5000);
SerialMon.println("failure of attempt " + attempt);
attempt = attempt + 1;
delay(25000); } //attente 30s si problème
// waitSecondsAndSayIt(1 * 60);
}
void waitSecondsAndSayIt(int s) {
Serial.println("Wait " + String(s) + " seconds");
delay(s * 1000);
}
boolean connectGsmNetwork() {
SerialMon.print("Waiting for GSM network...");
boolean connected = modem.waitForNetwork() && modem.isNetworkConnected();
if (connected) {
SerialMon.println("GSM network connected");
}
else {
SerialMon.println(" failed connecting GSM.");
}
return connected;
}
boolean connectGprs() {
if (!connectGsmNetwork()) {
return false;
}
SerialMon.println("Connecting GPRS...");
// GPRS connection parameters are usually set after network registration
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
boolean connected = modem.gprsConnect(apn, gprsUser, gprsPass) && modem.isGprsConnected();
if (connected) {
gprsOpen = true;
SerialMon.println(" - GPRS connected");
}
else {
SerialMon.println(" - failed connecting GPRS.");
}
return connected;
}
boolean connectSocket() {
if (!connectGprs()) {
return false;
}
SerialMon.print("Connecting socket...");
SerialMon.print("Connecting to ");
SerialMon.println(server);
boolean connected = socket.connect(server, port);
if (connected) {
sockOpen = true;
SerialMon.println("socket connected!");
}
else {
SerialMon.println("socket connection failed!");
}
return connected;
}
void networkDisconnect() {
if (sockOpen) {
sockOpen = false;
// Shutdown
socket.stop();
SerialMon.println(F("Server disconnected"));
}
if (gprsOpen) {
gprsOpen = false;
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
}
}
void httpGet() {
// previously
//if (!socket.connect(server, port)) {
// Make a HTTP GET request:
SerialMon.println("Performing HTTP GET request...");
// writing request
socket.print(String("GET ") + resource + " HTTP/1.1\r\n");
socket.print(String("Host: ") + server + "\r\n");
socket.print("Connection: close\r\n\r\n");
socket.println();
SerialMon.println();
// getting response
printSocketResponse();
}