POST request to node server with Arduino Nano 33 IOT

I'm having trouble getting the arduino to talk with a node server.

Here is my code, I'm trying to make a simple post request. Right now I'm using WiFiClient, but I also tried with WiFiSSLClient and had the same issue.

To sum up the error I'm getting a -2 error response.

Here is the arduino code...

#include "secret.hpp"
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
// for simplifying JSON formatting:
#include <Arduino_JSON.h>
//please enter your sensitive data in the Secret tab
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;             // the Wi-Fi radio's status
int ledState = LOW;                       //ledState used to set the LED
unsigned long previousMillisInfo = 0;     //will store last time Wi-Fi information was updated
unsigned long previousMillisLED = 0;      // will store the last time LED was updated
const int intervalInfo = 5000;            // interval at which to update the board information


///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// WiFi Settings ///////

char serverAddress[] = "arduino-click-listener.glitch.me/";  // server address
int port = 8080;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

JSONVar data;
const char contentType[] = "application/json";

void setup() {
  Serial.begin(9600);
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  Serial.println("making POST request");

  data["msg"] = "hello from adrduino";
  /* client.post(route, contentType, ); */

  client.post("/clicks", contentType, JSON.stringify(data));

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait five seconds");
  delay(5000);
}

Here is the node server, I'm running it in a gltich instance.

//api server
const express = require('express');
const app = express();
const port = process.env.PORT;

//serials number for the arduino board
let knownClients = ["serial"];

app.use(express.json()); // Middleware to parse JSON bodies

// Simple in-memory data storage
let clicks = [{"test":"hello?"}];

// Routes

// Get all users
app.get('/clicks', (req, res) => {
    res.json(clicks);
});


// add a new click event!

app.post('/clicks', (req, res) => {
  //check if the requenst is from a known client
  
  console.log("got a click");
  
  console.log(req.body);
  
  let data = {
    time: Date.now(),//get js time
    
    msg: req.body.msg
  };
  
    clicks.push(data);
    console.log(data);
    res.status(201).json(data);
});

// Start the server
app.listen(port, () => {
    console.log(`Server is running on port: ${port}`);
});

I can make both GET and POST requests to the server using Postman. So I'm assuming it is something with the arduino or the sketch.

Let me know if you have any ideas. I've gotten really frustrated by this and don't know what else to try.

You need to post the entire error log, use codetags. -2 means nothing.

HTTP status codes are in the range [100, 600). A negative number or zero usually means a problem using the library; the call probably didn't even make it to the server.

The constructor takes either aServerName, a string; or aServerAddress, an IPAddress. The serverAddress defined here is neither. As a server name, it should not end with a slash /. Try removing it. (Note that the post path correctly starts with a slash.)

ok the / was the issue. Removed it and sketch started working.

Thanks for you help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.