Search code examples
arduinoarduino-unoarduino-c++sim800sim800l

Arduino UNO - SIM808/GPRS Module is not making POST request


Good night, I am opened a ngrok tunnel for my web project and I am trying to make a POST request from SIM808 module. The codes and output given below:

// Libraries / Defines
...
char httpLogin[] = "POST /api/device/login HTTP/1.1\r\nHost: subdomain.ngrok-free.app\r\nContent-Type: application/json\r\n\r\n{}";
char buffer[512];

The "{}" my request payload for testing.

...
void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  delay(1000);
  Serial.println("[MSG] : Starting...");
  while( !sim808.init() ) Serial.println("[ERR] : SIM Module couldn't initialized.");
  
  mySerial.println("AT&F"); // Factory reset for SIM.
  delay(1000);
  while(mySerial.available()) {
    char c = mySerial.read();
    Serial.print(c);
  }
  
  while( !sim808.join( F("internet") ) ) Serial.println("[ERR] : GPRS connection failed.");
  Serial.println("[STA] : GPRS connection successful.");
  Serial.print("[MSG] : IP Address is "); Serial.println(sim808.getIPAddress());
  sim808.detachGPS();
  Serial.println("[MSG] : Logging In...");
  
  while( !sim808.connect(TCP, "subdomain.ngrok-free.app", 80) ) {
    Serial.println("[ERR] : Connection couldn't established.");
    sim808.close();
    delay(500);
  }
  
  Serial.println("[STA] : Connection established.");
  Serial.print("[MSG] : Payload => \r\n"); Serial.println(httpLogin);
  int result = sim808.send(httpLogin, sizeof(httpLogin) - 1 );
  Serial.println( String(result) );
  delay(5000);
  
  int ret = sim808.recv(buffer, sizeof(buffer)-1);
  if (ret <= 0) {
    Serial.println("[MSG] : Fetch over...");
  } else {
    buffer[ret] = '\0';
    Serial.print("Recv: ");
    Serial.print(ret);
    Serial.print(" bytes: ");
    Serial.println(buffer);
  }

  sim808.close(); // Closes TCP/UDP connection.
  sim808.disconnect();
}

Let's talk about what problem is? The problem is, I get message of "[MSG] : Fetch over..." every try and when sim808.send() works, it returns 0. It returns sent byte of data, why it is 0 ? I can't solve this and I'm working for 3 days about it. I am giving an output below:

23:35:29.323 -> OK
23:35:30.975 -> [STA] : GPRS connection successful.
23:35:31.008 -> [MSG] : IP Address is 100.**.***.**
23:35:31.040 -> [MSG] : Logging In...
23:35:34.689 -> [STA] : Connection established.
23:35:34.722 -> [MSG] : Payload => 
23:35:34.722 -> POST /api/device/login HTTP/1.1
23:35:34.755 -> Host: subdomain.ngrok-free.app
23:35:34.822 -> Content-Type: application/json
23:35:34.855 -> 
23:35:34.855 -> {}
23:35:54.060 -> 0
23:36:04.104 -> [MSG] : Fetch over...
23:36:05.662 -> AT+CIPSHUT
23:36:05.662 -> SHUT OK

Lastly, the ngrok panel :

ngrok
(Ctrl+C to quit)
Try the ngrok Kubernetes Ingress Controller: https://ngrok.com/s/k8s-ingress

Session Status                online
Account                       ***** ****** (Plan: Free)
Version                       3.3.4
Region                        Europe (eu)
Latency                       -
Web Interface                 http://127.0.0.1:4040
Forwarding                    https://subdomain.ngrok-free.app -> http://127.0.0.1:8000

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00                                                                                                                                                              

Lastly, I've tried these two things:

Serial.println("[STA] : Connection established.");
int size = sizeof(httpLogin) - 1; // 210
int sizestr = String(httpLogin).length(); // 0
Serial.print("[MSG] : Size / Payload => " + String(size) + " / " + String(sizestr) + "\r\n"); Serial.println(httpLogin);

Output: [MSG] : Size / Payload => 0 / 210 ... After this test, I've tried to send two of them. int result = sim808.send( httpLogin, size /* 210 */ ); Output:

13:00:49.517 -> 0
13:00:54.551 -> [MSG] : Fetch over...

int result = sim808.send( httpLogin, sizestr /* 0 */); Output:

13:02:32.586 -> 0
13:02:35.908 -> [MSG] : Received 93 bytes => 
13:02:35.908 -> HTTP/1.1 400 Bad Request
13:02:35.954 -> Content-Length: 11
13:02:35.990 -> Connection: close
13:02:36.031 -> Content-Type: text/plain
13:02:36.031 -> 
13:02:36.031 -> 

Solution

  • I just deployed my web project to the live servers. After configured subdomain. of my domain.com the problem solved. It might the TCP connection of SIM808 could stuck at firewall of ngrok. Or ngrok domains could very long. I don't know.