vshymanskyy / TinyGSM

A small Arduino library for GSM modules, that just works
GNU Lesser General Public License v3.0
1.91k stars 709 forks source link

Issues connecting to web address after connecting to previous address with SIM7000G #610

Closed Techmokid closed 2 years ago

Techmokid commented 2 years ago

[*] I have read the Troubleshooting section of the ReadMe

What type of issues is this?

[ ] Request to support a new module [ ] Bug or problem compiling the library [ ] Bug or issue with library functionality (ie, sending data over TCP/IP) [* ] Question or request for help

What are you working with?

Modem: SIM7000G Main processor board: Arduino Mega 2560 TinyGSM version: Latest

Scenario, steps to reproduce

I am attempting to connect to a server using the TinyGSM library, then disconnect and connect to a different server afterwards. This is a manual attempt to read frrom a website and, if there is a redirect, punch in the IP of that new server and connect to that instead.

Expected result

The module to:

Actual result

`

define PWRKEY 6

define RST 7

//#define DTR 8 //#define RI 9

define TX 10

define RX 11

define ALRT 12

// Select your modem: // #define TINY_GSM_MODEM_SIM800 // #define TINY_GSM_MODEM_SIM808 // #define TINY_GSM_MODEM_SIM868 // #define TINY_GSM_MODEM_SIM900

define TINY_GSM_MODEM_SIM7000

// #define TINY_GSM_MODEM_SIM7000SSL // #define TINY_GSM_MODEM_SIM7080 // #define TINY_GSM_MODEM_SIM5360 // #define TINY_GSM_MODEM_SIM7600 // #define TINY_GSM_MODEM_UBLOX // #define TINY_GSM_MODEM_SARAR4 // #define TINY_GSM_MODEM_M95 // #define TINY_GSM_MODEM_BG96 // #define TINY_GSM_MODEM_A6 // #define TINY_GSM_MODEM_A7 // #define TINY_GSM_MODEM_M590 // #define TINY_GSM_MODEM_MC60 // #define TINY_GSM_MODEM_MC60E // #define TINY_GSM_MODEM_ESP8266 // #define TINY_GSM_MODEM_XBEE // #define TINY_GSM_MODEM_SEQUANS_MONARCH

include

SoftwareSerial SerialAT(TX, RX); // RX, TX

// This is the amount of bytes to use for holding the message returned by your website

if !defined(TINY_GSM_RX_BUFFER)

define TINY_GSM_RX_BUFFER 1024

endif

define TINY_GSM_USE_GPRS true

define TINY_GSM_USE_WIFI false

define GSM_PIN ""

// Your GPRS credentials, if any const char apn[] = "telstra.wap"; const char gprsUser[] = ""; const char gprsPass[] = "";

// Your WiFi connection credentials, if applicable const char wifiSSID[] = "Never_Gonna_Give_You_Up"; const char wifiPass[] = "beezChurger";

// Server details

// #define DUMP_AT_COMMANDS // #define TINY_GSM_DEBUG Serial

// Range to attempt to autobaud // NOTE: DO NOT AUTOBAUD in production code. Once you've established // communication, set a fixed baud rate using modem.setBaud(#).

define GSM_AUTOBAUD_MIN 9600

define GSM_AUTOBAUD_MAX 115200

// Uncomment this if you want to use SSL // #define USE_SSL

// --------------------------------------------------------------------------------------------------------------------------------------

include

// Just in case someone defined the wrong thing..

if TINY_GSM_USE_GPRS && not defined TINY_GSM_MODEM_HAS_GPRS

undef TINY_GSM_USE_GPRS

undef TINY_GSM_USE_WIFI

define TINY_GSM_USE_GPRS false

define TINY_GSM_USE_WIFI true

endif

if TINY_GSM_USE_WIFI && not defined TINY_GSM_MODEM_HAS_WIFI

undef TINY_GSM_USE_GPRS

undef TINY_GSM_USE_WIFI

define TINY_GSM_USE_GPRS true

define TINY_GSM_USE_WIFI false

endif

ifdef DUMP_AT_COMMANDS

include

StreamDebugger debugger(SerialAT, Serial); TinyGsm modem(debugger);

else

TinyGsm modem(SerialAT);

endif

TinyGsmClient client(modem);

String modemName = ""; String modemInfo = "";

void setup() { Serial.begin(115200); Serial.println(); Serial.println("----------------------------------------------------");

setupModem();

char server[] = "techmo.unity.chickenkiller.com"; int port = 80;

getServerResponse(server, port); Serial.println("Done"); Serial.println(); Serial.println(); Serial.println(); Serial.println(getServerResponse(server, port)); }

void loop() {}

void setupModem() { // Set GSM module baud rate //TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX); SerialAT.begin(9600); pinMode(PWRKEY, OUTPUT); digitalWrite(PWRKEY, HIGH); delay(260); digitalWrite(PWRKEY, LOW); delay(10000);

// Restart takes quite some time // To skip it, call init() instead of restart() Serial.println("Initializing modem..."); if (!modem.restart()) { //if (!modem.init(180000L)) { DBG("Failed to restart modem, delaying 10s and retrying"); // restart autobaud in case GSM just rebooted // TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX); return; }

modemName = modem.getModemName(); modemInfo = modem.getModemInfo();

if TINY_GSM_USE_GPRS

// Unlock your SIM card with a PIN if needed if ((GSM_PIN != "") && modem.getSimStatus() != 3) { modem.simUnlock(GSM_PIN); }

endif

}

void startServerComms() {

if TINY_GSM_USE_WIFI

// Wifi connection parameters must be set before waiting for the network if (!modem.networkConnect(wifiSSID, wifiPass)) { Serial.println("Could not set SSID/Password"); return; }

endif

if TINY_GSM_USE_GPRS && defined TINY_GSM_MODEM_XBEE

// The XBee must run the gprsConnect function BEFORE waiting for network! modem.gprsConnect(apn, gprsUser, gprsPass);

endif

if (!modem.waitForNetwork()) { Serial.println("Could not connect to network"); return; }

if (modem.isNetworkConnected()) { Serial.println("Network connected"); }

if TINY_GSM_USE_GPRS

// GPRS connection parameters are usually set after network registration if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { Serial.println("Failed to connect to " + String(apn)); return; }

if (modem.isGprsConnected()) { Serial.println("GPRS connected"); }

endif

}

bool isServerConnected = false; String getServerResponse(String server, int port, char resource[]) { char tempServer[128]; server.toCharArray(tempServer,server.length()); return getServerResponse(tempServer,port,"/"); } String getServerResponse(String server, int port) { return getServerResponse(server,port,"/"); } String getServerResponse(char server[], int port) { return getServerResponse(server, port, "/"); } String getServerResponse(char server[], int port, char resource[]) { Serial.println("Connecting to: " + String(server) + ":" + String(port)); if (!isServerConnected) {startServerComms(); isServerConnected=true;}

if (!client.connect(server, port)) { Serial.println("Failed to connect to " + String(server)); delay(10000); return; }

// Make a HTTP GET request: Serial.println("Performing HTTP GET request..."); client.print(String("GET ") + resource + " HTTP/1.1\r\n"); client.print(String("Host: ") + server + "\r\n"); client.print(String("Accept: /") + "\r\n"); client.print("Connection: close\r\n\r\n"); client.println();

Serial.println("Awaiting response..."); String result = ""; uint32_t timeout = millis(); while (client.connected() && millis() - timeout < 10000L) { // Print available data while (client.available()) { char c = client.read(); result += c; timeout = millis(); } }

client.flush(); client.stop();

//Serial.println("Response from server: " + result); delay(1000);

int temp_start = result.indexOf("Location: http://"); if (temp_start != -1) { String redirectData = result.substring(temp_start,result.length()); if (redirectData.indexOf("\n") != -1) { redirectData = redirectData.substring(0,redirectData.indexOf("\n")); }

redirectData = redirectData.substring(
  redirectData.indexOf("http://") + String("http://").length(),
  redirectData.length()-1
);

//Serial.println("---------------------------------");

/ /Serial.println("Orig: " + redirectData); / /Serial.println("Orig - 1: " + redirectData.substring(0,redirectData.length()-1)); / /Serial.println("Orig - 2: " + redirectData.substring(0,redirectData.length()-2)); / /Serial.println(); / /Serial.println("Orig: " + redirectData); / /Serial.println("Orig - 1: " + redirectData.substring(1,redirectData.length())); / /Serial.println("Orig - 2: " + redirectData.substring(2,redirectData.length())); / /Serial.println("---------------------------------");

String temp_server = redirectData.substring(0,redirectData.indexOf(":"));
String temp_port = redirectData.substring(redirectData.indexOf(":")+1,redirectData.length());

Serial.println("Detected redirect to: " + temp_server + ":" + temp_port);
result = "";
redirectData = "";
return getServerResponse(temp_server, temp_port.toInt(), resource);

}

isServerConnected = false; return result; }`

It seems to operate just fine normally. But in the case where a redirect is called (Such as in the case of my private server), when the function loops around to connect to a different address, it seems to fail on the actual connecting phase: if (!client.connect(server, port)) {

The output looks like this: ` 11:13:02.848 -> 11:13:02.848 -> ---------------------------------------------------- 11:13:13.096 -> Initializing modem... 11:13:44.591 -> Connecting to: MY_IP_ADDRESS:PORT_NUMBER 11:13:44.695 -> Network connected 11:13:46.655 -> GPRS connected 11:13:47.404 -> Performing HTTP GET request... 11:13:47.816 -> Awaiting response... 11:13:49.543 -> Done 11:13:49.543 -> 11:13:49.543 -> 11:13:49.543 -> 11:13:49.543 -> Connecting to: MY_IP_ADDRESS:PORT_NUMBER 11:13:49.612 -> Network connected 11:13:51.935 -> GPRS connected 11:13:52.696 -> Performing HTTP GET request... 11:13:53.109 -> Awaiting response... 11:13:55.136 -> Detected redirect to: MY_REDIRECTED_IP_ADDRESS:PORT_NUMBER 11:13:55.136 -> Connecting to: MY_REDIRECTED_IP_ADDRESS:PORT_NUMBER 11:15:10.136 -> Failed to connect to MY_REDIRECTED_IP_ADDRESS

`

(I have edited out my public IP address for obvious reasons)

I'm just not sure what I'm missing, probably something I have forgotten to shut down? The code seems to be SO CLOSE to working, but the moment I try connecting to a different web address, it refuses to connect. It will connect once perfectly, then not again until I reboot the arduino or upload the same code to it again. Any help would be appreciated!

Techmokid commented 2 years ago

Upon doing a bunch of digging, I found what the problem was! The issue isn't actually the TinyGSM library. That was working fine. It was this section:

String getServerResponse(String server, int port, char resource[]) { Serial.println("DEBUGGING - INPUT TO FUNCTION: " + server); char tempServer[128]; server.toCharArray(tempServer,server.length()); Serial.println("DEBUGGING - OUTPUT FROM FUNCTION: " + server); return getServerResponse(tempServer,port,"/"); } String getServerResponse(String server, int port) { return getServerResponse(server,port,"/");} String getServerResponse(char server[], int port) { return getServerResponse(server, port, "/"); } String getServerResponse(char server[], int port, char resource[]) {

or more specifically: char tempServer[128]; server.toCharArray(tempServer,server.length());

Note to any code devs out there who have forgotten. When converting from a String to a char array, remember to add "server.length() +1". char arrays are null terminated, meaning you gotta add an extra character. I COMPLETELY forgot about this since I don't work with char arrays very often (Ik ik, very bad of me, Strings are terrible for memory, ik that. But they are juust so USEFUL!)

So I managed to solve the issue!

Thankyou anyways!