about using MKR1000 send a POST request to NodeJS server

hi guys:
I am building a server that can work with Arduino MKR1000. I've not found any example that can help me to send POST request yet. Does anyone have example to help me with it?

Hi @kcml,

Please take a look at the ArduinoHttpClient library, there is a POST example there: ArduinoHttpClient/examples/SimplePost at master · arduino-libraries/ArduinoHttpClient · GitHub

The library can be installed via the IDE's Library manager and is also included with Arduino Create.

hey Sandeep:
Thank you so much, that was really really really helpful. Would you mind me asking you other question when I have other questions?

Hi @kcml,

I'm glad my answer was helpful! Sure, just create a new post for any questions you have in the future. Myself or someone else from the Arduino community will try our best to answer :slight_smile:

hi Sandeep:
I need your help when I start using Heroku to host my website and trying to get the post request from the MKR1000. I get the -2 as status code. What is the -2 means?

The list of error codes can be found here: https://github.com/arduino-libraries/ArduinoHttpClient/blob/master/src/HttpClient.h

-2 means:

// This call was made when the HttpClient class wasn't expecting it
// to be called.  Usually indicates your code is using the class
// incorrectly
static const int HTTP_ERROR_API =-2;

Maybe you can share your sketch?

sure. I also wonder if the problem is happen to Heroku, but not very sure.

#include <ArduinoHttpClient.h>
#include <WiFi101.h>
#include <PMS5003.h>
// MKR1000 Pins setup
const int SLEEP_PIN = 6;
// POST request
const String content_type = "application/x-www-form-urlencoded";
String postData ="";
//char serverAddress[] = "192.168.1.2";
char serverAddress[] = "smart-valley-project.herokuapp.com";
int port = 8080;

// WiFi Login Data
String ssid = "CasaDoRioAPS WiFi";
String password ="riverHouse";

// PMS5003 sensor
PMS5003 pms5003;
String PM1 = "";
String PM2_5 = "";
String PM10 = "";

// HTTP Connection
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
String response;
int statusCode = 0;

void setup() 
{
  Serial.begin(9600);
  Serial1.begin(9600);//Serial1 port for PMS5006
  pinMode(SLEEP_PIN,OUTPUT);
  digitalWrite(SLEEP_PIN,HIGH);
  while(status != WL_CONNECTED) {
    Serial.print("Attempting to connect to your wifi: " + ssid + "\n");
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, password);
  }
  // print the SSID of the your WiFi
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  
  // print your WiFi Module's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");Serial.println(ip);
}

void loop() {
  Serial.println("PMS5003 Data on Process");
  
  //digitalWrite(SLEEP_PIN,HIGH);
  //delay(20000);
  for(;;){
    pms5003.processDataOn(Serial1);
    PM1 = String(pms5003.getPM1(),DEC);
    PM2_5 = String(pms5003.getPM25(),DEC);
    PM10 = String(pms5003.getPM10(),DEC);
    if (PM1 !="-1" && PM2_5 !="-1" && PM10 != "-1"){
      break;
    }
  }
  
  
  postData ="PM1=" + PM1 + "&PM2.5=" + PM2_5 + "&PM10=" + PM10;
  
  Serial.println("making POST request");
  client.post("/",content_type,postData);

  statusCode = client.responseStatusCode();
  response = client.responseBody();
  Serial.print("Status Code: ");Serial.println(statusCode);
  Serial.print("Response: ");Serial.println(response);
  
  Serial.print("PM1:\t");Serial.println(PM1);
  Serial.print("PM2.5:\t");Serial.println(PM2_5);
  Serial.print("PM10:\t");Serial.println(PM10);
  //digitalWrite(SLEEP_PIN,LOW);
  //delay(10000);
}