0

I am trying to send data to a web app using the TCP connection of a SIM808 that is connected to my NodeMcu. Everything appears to work fine, connection is perfect but at the send part, the post request does not execute fully. below is my code and output

please help check the http_cmd line. In my output, after the content is shown in the output I am suppose to see something like Recv: ### bytes but instead I get ☐fetch over

#include <ESP8266WiFi.h>
#include <DFRobot_sim808.h>
#include <sim808.h>
#include <SoftwareSerial.h>


DFRobot_SIM808 sim808(&Serial);

char http_cmd[] =  "POST /tracker/ HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: 34\r\nHost: haul1.herokuapp.com\r\n\r\n{ \"trackerId\": \"2222\",\"height\": \"42\" }";
char buffer[512];


void setup(){
  //mySerial.begin(9600);
  Serial.begin(9600);

  //******** Initialize sim808 module *************
  while(!sim808.init()) {
      delay(1000);
      Serial.print("Sim808 init error\r\n");
  }
  delay(3000);  

  //*********** Attempt DHCP *******************
  while(!sim808.join()) {
      Serial.println("Sim808 join network error");
      delay(2000);
  }

  //************ Successful DHCP ****************
  Serial.print("IP Address is ");
  Serial.println(sim808.getIPAddress());

  //*********** Establish a TCP connection ************
  if(!sim808.connect(TCP,"haul.herokuapp.com", 80)) {
      Serial.println("Connect error");
  }else{
      Serial.println("Connect haul.herokuapp.com success");
  }

  //*********** Send a GET request *****************
  Serial.println("waiting to fetch...");
  sim808.send(http_cmd, sizeof(http_cmd)-1);
  while (true) {
      int ret = sim808.recv(buffer, sizeof(buffer)-1);
      if (ret <= 0){
          Serial.println("fetch over...");
          break; 
      }
      buffer[ret] = '\0';
      Serial.print("Recv: ");
      Serial.print(ret);
      Serial.print(" bytes: ");
      Serial.println(buffer);
      break;
  }

  //************* Close TCP or UDP connections **********
  sim808.close();

  //*** Disconnect wireless connection, Close Moving Scene *******
  sim808.disconnect();
}

void loop(){

}

OUTPUT: OUTPUT from Serial Monitor

Sim808 init error
AT
Sim808 init error
AT
AT+CFUN=1
AT+CPIN?
AT+CSTT="","",""
AT+CIICR
AT+CIFSR
IP Address is 10.10.124.205
AT+CIPSTART="TCP", “haul.herokuapp.com”, 80
Connect haul.herokuapp.com success
waiting to fetch...
AT+CIPSEND=145
POST /tracker/ HTTP/1.1
Content—Type: application/json
Content—Length: 34
Host: haul.herokuapp.com

{"trackerId": "2222","height": "42"}☐fetch over...
AT+CIPSTATUS
AT+CIPSHUT

Coffmann
  • 3
  • 2
  • 1
    Why do you have a `Content-length: 34` ? The length of this specific body is 38! – Steffen Ullrich Mar 29 '20 at 13:48
  • @SteffenUllrich how did you get the value?, can you enlighten me. I am changing it now and testing to see what i get – Coffmann Mar 29 '20 at 14:22
  • *"how did you get the value?"* - count the bytes in the body? Or is the problem that you don't actually understand what the `Content-length` header is for and what a HTTP body is? – Steffen Ullrich Mar 29 '20 at 15:10
  • @SteffenUllrich can you please paste the byte you actually counted. this is what i counted to get 34 >>> `"trackerId": "2222","height": "42"`. so can you share how you got 38. because the code is working now. Big Thanks – Coffmann Mar 30 '20 at 02:39
  • The body you've counted is not the body you've send. You've failed to count the `"{ "` and `" }"` at beginning and end and thus missed 4 bytes. – Steffen Ullrich Mar 30 '20 at 05:15
  • Check this once: https://stackoverflow.com/a/69601941/8119511 – Ank_247shbm Oct 17 '21 at 09:05

0 Answers0