i have a arduino r4 with wifi, i have connected my local mongo db along with my local server to my Arduino, im using wifis3 library and AurdinohttpClient version 0.6.0. The code works great..... but the issue im facing is that after getting reponse payload for 8 times then it goes to error code -1 error.
here's my code
#include <WiFiS3.h> #include <ArduinoHttpClient.h>
`const char* ssid = "SSID";
const char* password = "password";
const char* serverAddress = "serverip"; // IP address of your server
const int serverPort = 3000; // Port number of your server
const int timeout = 10000; // Timeout duration in milliseconds (e.g., 10 seconds)
const unsigned long requestInterval = 2000; // Interval between requests in milliseconds (e.g., 2 seconds)
void setup() {
Serial.begin(9600);
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// Wait for WiFi connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print WiFi connection status
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Check if WiFi is connected
if (WiFi.status() == WL_CONNECTED) {
// Print message indicating successful WiFi connection
Serial.println("WiFi connected, sending HTTP request...");
WiFiClient client;
// Set up the HttpClient object with custom timeout
HttpClient http(client, serverAddress, serverPort);
http.setTimeout(timeout); // Set timeout duration
// Make HTTP GET request
http.beginRequest();
int httpResponseCode = http.get("/collection"); // Assuming you have a route for fetching a single document
http.endRequest();
// Check HTTP response code
if (httpResponseCode == HTTP_SUCCESS) {
String payload = http.responseBody();
Serial.println("Response payload: ");
Serial.println(payload);
// Parse the "status" field from the payload
if (payload.indexOf(""status":"On"") != -1) {
// Turn on the relay board
Serial.println("Turning relay board on...");
// Code to turn on relay board
} else if (payload.indexOf(""status":"Off"") != -1) {
// Turn off the relay board
Serial.println("Turning relay board Off...");
// Code to turn off relay board
} else {
Serial.println("Unknown status received.");
}
if (payload.indexOf(""water_level":"200"") != -1) {
// Turn on the relay board
Serial.println("Changing water level to sensor 1");
// Code to turn on relay board
} else if (payload.indexOf(""water_level":"400"") != -1) {
// Turn off the relay board
Serial.println("Changing water level to sensor 2");
// Code to turn off relay board
} else {
Serial.println("Unknown status received.");
}
} else {
Serial.print("Error occurred during HTTP GET request. Error code: ");
Serial.println(httpResponseCode);
}`
} else {
// Print message indicating WiFi is not connected
Serial.println("WiFi not connected. Unable to perform HTTP request.");
}
delay(requestInterval); // Wait for the specified interval before sending the next request
}
that was my aurdino code
here's the node code
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const port = 3000;
// Increase default server configurations
app.use(express.json()); // Enable JSON parsing
app.use(express.urlencoded({ extended: true })); // Enable URL-encoded form data parsing
// Increase the maximum number of listeners for better scalability
app.setMaxListeners(0);
// Connect to MongoDB with additional options for better compatibility
mongoose.connect('connectionstring', {
useNewUrlParser: true,
useUnifiedTopology: true,
// Removed poolSize option
})
.then(() => console.log('Connected to MongoDB'))
.catch((err) => console.error('MongoDB connection error:', err));
// Define Mongoose schema and model
const dataSchema = new mongoose.Schema({
name: String,
age: Number,
}, { collection: 'Aurdino' });
const Data = mongoose.model('Data', dataSchema);
// Route to handle GET requests
app.get('/collection', async (req, res) => {
try {
const data = await Data.find().lean(); // Use lean() for better performance
res.json(data);
} catch (error) {
console.error('Error fetching data:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Start the server
const server = app.listen(port, () => {
console.log(Server is listening at http://localhost:${port});
});
// Increase server timeout for long-running requests server.timeout = 0;
[/quote]