botletics / SIM7000-LTE-Shield

Botletics SIM7000 LTE CAT-M1/NB-IoT Shield for Arduino
https://www.botletics.com/products/sim7000-shield
GNU General Public License v3.0
478 stars 216 forks source link

Sim7000 http post doesn't work if http get is not used before http post #108

Open bihmedical opened 5 years ago

bihmedical commented 5 years ago

I have attached the code here. I am using ESP32 with Botletics sim7000 sheild and got this sketch from ESP32_LTE_Demo example. Http get works seemlessly buy when I use only http post method it seems to work intermittently. And when i do get before post then the code seems to be working fine. For my use purposes I have to make post method to work. Any help or suggestion would be useful. Thank you

` // Post data to website via 2G or LTE CAT-M/NB-IoT float temperature = analogRead(A0) * 1.23; // Change this to suit your needs

    uint16_t battLevel = 3600;

    char URL[150];
    char body[100];
    char tempBuff[16];
    char battLevelBuff[16];

    // Format the floating point numbers as needed
    dtostrf(temperature, 1, 2, tempBuff); // float_val, min_width, digits_after_decimal, char_buffer
    dtostrf(battLevel, 1, 0, battLevelBuff);

    // GET request
    sprintf(URL, "dweet.io/dweet/for/%s?temp=%s&batt=%s", imei, tempBuff, battLevelBuff); // No need to specify http:// or https://
    //        sprintf(URL, "http://dweet.io/dweet/for/%s?temp=%s&batt=%s", imei, tempBuff, battLevelBuff); // But this works too

   // if (!fona.postData("GET", URL))
   //  Serial.println(F("Failed to complete HTTP GET..."));

      sprintf(URL, "http://dweet.io/dweet/for/%s", imei);
      sprintf(body, "{\"temp\":%s,\"batt\":%s}", tempBuff, battLevelBuff);

    if (!fona.postData("GET", URL))
     Serial.println(F("Failed to complete HTTP GET..."));

    if (!fona.postData("POST", URL, body)) 
      Serial.println(F("Failed to complete HTTP POST..."));

        sprintf(URL, "http://dweet.io/dweet/for/%s", imei);
      sprintf(body, "{\"data1\":%s,\"data2\":%s}", tempBuff, battLevelBuff);

    if (!fona.postData("POST", URL, body)) 
      Serial.println(F("Failed to complete HTTP POST..."));`
botletics commented 5 years ago

Please only use the lines relevant to the POST request and comment out the GET request code. It also looks like you are trying to post twice, one right after the other.

bihmedical commented 5 years ago

I need to send multiple post requests as the data being sent is limited by 350 bytes. Is there any better way to post the data multiple times then?

botletics commented 5 years ago

Sorry for the late reply. There is one thing you could do to increase the size:

bihmedical commented 5 years ago

Thank you, I will give it a try and see if it works for our application.