1

I've MKR GSM 1400 with sim and antenna, I want to send data to the KAA cloud by HTTP POST request. By using cURL I've succeded to send data to the cloud using thise code

curl --location --request POST 'https://connect.cloud.kaaiot.com:443/kp1/<app-version-name>/dcx/<endpoint-token>/json' \
--data-raw '[
  {
    "temperature": 23,
    "humidity": 48
  }
]

Based on this I've written this code so far but I'm not sure what I'm missing for it to work :

#include <ArduinoHttpClient.h>
#include <MKRGSM.h>
#include <ArduinoJson.h>



const char PINNUMBER[]     = ""; 
const char GPRS_APN[]      = "";
const char GPRS_LOGIN[]    = "";
const char GPRS_PASSWORD[] = "";
const String TOKEN = "";   // Deleted for security purpose
const String APP_VERSION = "";  // Deleted for security purpose


const unsigned long fiveSeconds = 1 * 5 * 1000UL;
static unsigned long lastPublish = 0 - fiveSeconds;

int temp;



GSMClient client;
GPRS gprs;
GSM gsmAccess;

char server[] = "connect.cloud.kaaiot.com";
char path[] = "/";
int port = 443;


HttpClient httpClient = HttpClient(client, server, port);

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 GSM connection...");
  // connection state
  boolean connected = false;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected) {
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  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() {
 

  if (client.connect(server, port)) {

  unsigned long now = millis();
  if (now - lastPublish >= fiveSeconds) {
    lastPublish += fiveSeconds;
    temp = random(18, 23);
    Serial.print("temperature = ");
    Serial.println(temp);
    String queryString = String(" \"temperature\":  ) + String(temp) "); 
    String topic = "/kp1/" + APP_VERSION + "/dcx/" + TOKEN + "/json";
    httpClient.println("POST https://" + String(server) + ":" + String(port) + String(topic) + " HTTP/1.1");
    Serial.println("POST https://" + String(server) + ":" + String(port) + String(topic) + " HTTP/1.1");
    httpClient.println("Connection: close");
    httpClient.println(); // end HTTP header
   // send HTTP body
    httpClient.print("{ \"temperature\": ");
    httpClient.print(temp);
    httpClient.print("}");
    //httpClient.println(queryString);
  }

  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

  delay(3000); // Wait for 3 seconds to post again
}

On Serial monitor it shows that I've connected to the server.

  • 1
    There are several issues, 1) You can't access an https secure site with `GSMClient client;`, you need to use `GSMSSLClient`. See [GSMSSLClient](https://www.arduino.cc/en/Tutorial/MKRGSMExamplesSSLWebClient). 2) if you are using HTTP1.1, you need to add HTTP header `Host: example.com`, alternatively, change your 'HTTP1.1' to 'HTTP1.0'. – hcheung Sep 29 '21 at 12:53
  • If I try to use GSMSSL, I get connection failed. – Timor Bader Sep 30 '21 at 11:57

0 Answers0