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.