Hi
Im building a GPS Tracking device and I need some help on posting the data to my webserver api.
Currently using the MKR GSM 1400
I'm able to connect successfully to the carrier network and can receive calls, send texts etc.
Now I'm trying to send an http post request to a server and cannot get it to work.
Here is my code.
#include <ArduinoHttpClient.h>
#include <MKRGSM.h>
const char PINNUMBER[] = "9999";
// APN data
const char GPRS_APN[] = "mworld.be";
const char GPRS_LOGIN[] = "";
const char GPRS_PASSWORD[] = "";
// initialize the library instance
GSMSSLClient client;
GPRS gprs;
GSM gsmAccess;
char server[] = "myserver.eu";
int port = 443;
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Starting Arduino web client.");
// connection state
boolean connected = false;
// After starting the modem with GSM.begin()
// attach to the GPRS network with the APN, login and password
while (!connected) {
Serial.println("Begin gsm Access");
//Serial.println(gsmAccess.begin()); //Uncomment for testing
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
Serial.println("GSM Access Success");
}
else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("connecting...");
if (client.connect(server, port)) {
Serial.println("connected");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
post();
delay(15000);
}
//I tried both formats, but none of them work
String PostData= "{\"Email\" : \"mymail\" , \"Password\" : \"mypass\"}";
//String PostData = "Email=mymail&Password=mypass";
Serial.println(PostData);
if (client.connect(server , port)) {
Serial.println("connected");
client.println("POST /Api/Login HTTP/1.1");
client.println("Host:https://myserver.eu);
client.println("User-Agent: Arduino/1.0");
//client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);
} else {
Serial.println("connection failed");
}
Any idea what I'm doing wrong?