Im trying to send a POST request from the Arduino MKR1400 GSM board to a server with a database.
I've tested the server using Postman (https://www.getpostman.com/) and it handles the POST request correctly, the json data gets uploaded to database fine.
The problem is that the same POST request from the Arduino MKR GSM 1400 does nothing. I've tried using client.print
and I get the same result.
I'm not sure what I'm doing wrong or how to debug this issue, any ideas?
Arduino code:
#include <MKRGSM.h>
#include "Credentials.h"
// Definiciones para el GPRS
const char PINNUMBER[] = SECRET_PINNUMBER;
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
GSMClient client;
GPRS gprs;
GSM gsmAccess;
GSMScanner scanner;
char ip[4] = {xxx,xxx,xxx,xxx};
char ipstring[14];
char path[] = "/atcapp.php";
int port = 80; // port 80 is the default for HTTP
char respName[30] = "Arduino MKR GSM 1400";
char toolName[30] = "Herramienta martillo";
char location[30] = "Mave Aeronautica S.L.";
char tlState[4] = "OUT";
unsigned long int user_id = 4294967295;
unsigned long int tool_id = 1234567899;
unsigned long int tmeStmp = 1532684104;
char notes[100] = "";
char postData[200];
char packetData[500];
void setup() {
Serial.begin(9600);
while(!Serial);
sprintf(ipstring,"%d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]);
post(respName,user_id,location,toolName,tool_id,tlState,tmeStmp,notes);
}
char post(char* resp,unsigned long int userid,char* location, char* toolName,unsigned long int toolid,char* toolState,unsigned long int timeStamp,char* notes)
{
Serial.println("Connecting to GPRS Network...");
boolean connected = false;
while (!connected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
} else {
Serial.println("GPRS connection failed");
delay(1000);
return -1;
}
}
if (client.connect(ip, port)) {
sprintf(packetData,"POST %s HTTP/1.1\nHost: %s\nUser-Agent: Arduino/1.0\nConnection: close\nContent-Length: %d\nContent-Type: application/json\nCache-Control: no-cache\n\n{\n\"responsable\":\"%s\",\n\"user_id\":%lu,\n\"loc\":\"%s\",\n\"tool\":\"%s\",\n\"tool_id\":%lu,\n\"tool_state\":\"%s\",\n\"timestamp\":%lu,\n\"note\":\"%s\"\n}\n",path,ipstring,sizeof(postData),resp,userid,location,toolName,toolid,toolState,timeStamp,notes);
client.beginWrite();
for(int i=0;i<sizeof(packetData);i++){
Serial.write(packetData[i]);
client.write(packetData[i]);
}
client.endWrite();
while(client.connected()){
Serial.println("Client still connected.");
while(client.available()){
Serial.println("Client still available.");
char c = client.read();
Serial.print(c);
}
}
Serial.println("disconnecting.");
client.stop();
Serial.println();
Serial.println("Done.");
return 1;
}else{
Serial.println("Cannot connect to server.");
return 0;
}
}
void loop() {}
Serial port output:
It's my first time ever asking a question here, any contructive criticism is more than valuable to me. Thank you in advance,