Search code examples
pythonhttparduino-uno

http post data with gsim 800l and arduino


ijust try to http request to webserver with gsim and aurdino i already did it with python and it works fine

requests.post(url="https://9336-154-239-164-63.ngrok-free.app/id=tiva1&lat=30.282092&lon=31.748090&timestamp=2&hdop=3&altitude=44&speed=5"); to an Arduino code using GSIM 800l with same functionality

now I wanna do the same http request with GSIM I tried this code but it didn't work at all . so hope that some one can help me

`#include <SoftwareSerial.h>

// SoftwareSerial object to communicate with SIM800L
SoftwareSerial sim800l(10, 11); // RX, TX

void setup() {
  Serial.begin(9600);
  sim800l.begin(9600);

  delay(2000);
 
  sendATCommand("AT"); // Check if the module is responding
  sendATCommand("AT+CPIN?"); // Check SIM card status
  sendATCommand("AT+CGATT?"); // Check if attached to GPRS
  sendATCommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\""); // Set the connection type
  sendATCommand("AT+SAPBR=1,1"); // Enable GPRS
  sendATCommand("AT+HTTPINIT");
   // Initialize HTTP service
}

void loop() {
  
  // Make the HTTP POST request
  String url = "https://9336-154-239-164-63.ngrok-free.app/id=tiva1&lat=30.282092&lon=31.748090&timestamp=2&hdop=3&altitude=44&speed=5";
  sendATCommand("AT+HTTPPARA=\"URL\",\"" + url + "\"");
  sendATCommand("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\"");
  sendATCommand("AT+HTTPDATA=0,10000"); // Set the data length
  sendATCommand("AT+HTTPACTION=1"); // Start the POST request

  delay(5000); // Wait for the response

  // Read the response
  sendATCommand("AT+HTTPREAD=0,1000");

  // Close the HTTP service
  sendATCommand("AT+HTTPTERM");

  delay(60000); // Wait for 1 minute before making the next request
}


void sendATCommand(String command) {
  sim800l.println(command);
  delay(500);
  while (sim800l.available()) {
    char c = sim800l.read();
    Serial.write(c);
    delay(30); // Delay between characters to allow time for printing
  }
}` 

Solution

  • From memory I think you don't need to use quotes in the command strings you are sending (been a while since I've used a sim800 and can't find the damn code now!)

    for example

    sendATCommand("AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\"");

    would simply be

    sendATCommand("AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded");

    or

    sendATCommand("AT+HTTPPARA=\"URL\",\"" + url + "\"");

    would be

    sendATCommand("AT+HTTPPARA=URL," + url);

    [EDIT]

    Answer to your comment

    I think error 603 means a DNS problem, basically the sim800 can't resolve the url you are trying to reach.

    There could be different reasons, one can be that the url is a problem (https may not work on all devices) although if it's DNS that's most likely that you are not properly connecting to the mobile provider data channel (GPRS, 3G...)

    I get a similar issue when I travel and use a MiFi with a foreign sim, the modem is showing that I am connected to the mobile network but data/internet doesn't work and that's almost always an issue with the APN credentials for that mobile provider.

    You need to find out what is the AT command to set the APN and the APN credentials for the mobile provider, it's usually just the mobile provider name and empty strings for pin and password Ok, I've googled it, it looks like it's

    AT+SAPBR=3,1,"APN","some provider name" or AT+CSTT="some provider name","",""