vshymanskyy / TinyGSM

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

it doesn't allow me to send data, bad request 400 #741

Open pollo1308 opened 11 months ago

pollo1308 commented 11 months ago

I am using this code, conect with server but when use the resource in Post, get me bad request 400, can saomebody help me plis?

// Your GPRS credentials (leave empty, if not needed) const char apn[] = "internet.itelcel.com"; // APN (example: internet.vodafone.pt) use https://wiki.apnchanger.org const char gprsUser[] = "wepgprs"; // GPRS User const char gprsPass[] = "pollomaton99"; // GPRS Password

// SIM card PIN (leave empty, if not defined) const char simPIN[] = "";

// Server details // The server variable can be just a domain name or it can have a subdomain. It depends on the service you are using const char server[] = "http://pruebaesp32pantaleon.000webhostapp.com"; // domain name: example.com, maker.ifttt.com, etc const char resource[] = "/EspPost.php"; // resource path, for example: /post-data.php const int port = 80; // server port number

// Keep this API Key value to be compatible with the PHP code provided in the project page. // If you change the apiKeyValue value, the PHP file /post-data.php also needs to have the same key String apiKeyValue = "Pollito.99";

// TTGO T-Call pins

define MODEM_RST 5

define MODEM_PWKEY 4

define MODEM_POWER_ON 23

define MODEM_TX 27

define MODEM_RX 26

// Set serial for debug console (to Serial Monitor, default speed 115200)

define SerialMon Serial

// Set serial for AT commands (to SIM800 module)

define SerialAT Serial1

// Configure TinyGSM library

define TINY_GSM_MODEM_SIM800 // Modem is SIM800

define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb

// Define the serial console for debug prints, if needed //#define DUMP_AT_COMMANDS

include

ifdef DUMP_AT_COMMANDS

include

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

else

TinyGsm modem(SerialAT);

endif

TinyGsmClient client(modem);

define uS_TO_S_FACTOR 1000000UL / Conversion factor for micro seconds to seconds /

define TIME_TO_SLEEP 10 / Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour /

void setup() { // Set serial monitor debugging window baud rate to 115200 SerialMon.begin(115200);

// Set modem reset, enable, power pins pinMode(MODEM_PWKEY, OUTPUT); pinMode(MODEM_RST, OUTPUT); pinMode(MODEM_POWER_ON, OUTPUT); digitalWrite(MODEM_PWKEY, LOW); digitalWrite(MODEM_RST, HIGH); digitalWrite(MODEM_POWER_ON, HIGH);

// Set GSM module baud rate and UART pins SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX); delay(3000);

// Restart SIM800 module, it takes quite some time // To skip it, call init() instead of restart() SerialMon.println("Initializing modem..."); modem.restart(); // use modem.init() if you don't need the complete restart

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

// Configure the wake up source as timer wake up
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); }

void loop() { SerialMon.print("Connecting to APN: "); SerialMon.print(apn); if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { SerialMon.println(" fail"); } else { SerialMon.println(" OK");

SerialMon.print("Connecting to ");
SerialMon.print(server);
if (!client.connect(server,port)) {
  SerialMon.println(" fail");
}
else {
  SerialMon.println(" OK");

  // Making an HTTP POST request
  SerialMon.println("Performing HTTP POST request...");
  // Prepare your HTTP POST request data (Temperature in Celsius degrees)
  String httpRequestData = "api_key=" + apiKeyValue+"&Ambiental=55.5&CajaCabina=44.4&CajaPuerta=33.3&CajaMedio=22.2&Evaporeador=11.1";
  // Prepare your HTTP POST request data (Temperature in Fahrenheit degrees)
  //String httpRequestData = "api_key=" + apiKeyValue + "&value1=" + String(1.8 * bme.readTemperature() + 32)
  //                       + "&value2=" + String(bme.readHumidity()) + "&value3=" + String(bme.readPressure()/100.0F) + "";

  // You can comment the httpRequestData variable above
  // then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
  //String httpRequestData = "api_key=tPmAT5Ab3j7F9&value1=24.75&value2=49.54&value3=1005.14";

  client.print(String("POST ") + resource + " HTTP/1.0\r\n");
  client.print(String("Host: ") + server + "\r\n");
  client.println("Connection: close");
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.print("Content-Length: ");
  client.println(httpRequestData.length());
  client.println();
  client.println(httpRequestData);

  unsigned long timeout = millis();
  while (client.connected() && millis() - timeout < 10000L) {
    // Print available data (HTTP response from server)
    while (client.available()) {
      char c = client.read();
      SerialMon.print(c);
      timeout = millis();
    }
  }
  SerialMon.println();

  // Close client and disconnect
  client.stop();
  SerialMon.println(F("Server disconnected"));
  modem.gprsDisconnect();
  SerialMon.println(F("GPRS disconnected"));
}

} // Put ESP32 into deep sleep mode (with timer wake up) esp_deep_sleep_start(); }