This scenario, I created an API the endpoint is
method : PUT
http://localhost:5202/api/BinLevel/PutBinById?binId=1&binStatus=3 this is working via Postman.
Now trying to trigger this in Arduino but i have encountering an error
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* serverAddress = "192.168.1.14";
const int serverPort = 139;
const String apiUrl = "http://localhost:5202/api/BinLevel/PutBinById?binId=1&binStatus=";
WiFiClient wifiClient;
HttpClient client = HttpClient(wifiClient, serverAddress, serverPort);
#define TRIGGER_PIN 7
#define ECHO_PIN 6
#define BIN_HEIGHT_CM 121.92
void setup() {
Serial.begin(9600);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
connectToWiFi(ssid, password);
}
void loop() {
long duration, distance;
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
if (distance < 0 || distance > BIN_HEIGHT_CM) {
distance = BIN_HEIGHT_CM;
}
float fillLevelPercentage = ((BIN_HEIGHT_CM - distance) / BIN_HEIGHT_CM) * 100;
int binStatus = categorizeBinStatus(fillLevelPercentage);
Serial.print("Fill Level Percentage: ");
Serial.println(fillLevelPercentage);
Serial.print("Bin Status: ");
Serial.println(binStatus);
sendBinStatusToAPI(binStatus);
delay(5000);
}
void connectToWiFi(const char* ssid, const char* password) {
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("nConnected to WiFi");
}
void sendBinStatusToAPI(int binStatus) {
String fullUrl = apiUrl + String(binStatus);
Serial.print("API URL: ");
Serial.println(fullUrl);
client.connectionKeepAlive();
client.setTimeout(10000); // Set timeout to 10 seconds
if (client.connect(serverAddress, serverPort)) {
Serial.println("Connected Success Client.");
}
else
{
Serial.println("Connection failed Client");
return;
}
client.beginRequest();
client.put(fullUrl); // Use PUT method for simplicity, adjust as per your server requirements
int statusCode = client.responseStatusCode();
Serial.print("HTTP Status Code: ");
Serial.println(statusCode);
if (statusCode == 200) {
Serial.println("Bin status updated successfully");
} else {
Serial.print("Error on HTTP request: ");
Serial.println(statusCode);
String response = client.responseBody();
Serial.print("Response: ");
Serial.println(response);
}
client.stop();
}
int categorizeBinStatus(float fillLevelPercentage) {
if (fillLevelPercentage >= 75 && fillLevelPercentage <= 100) {
return 4;
} else if (fillLevelPercentage >= 50 && fillLevelPercentage < 75) {
return 3;
} else if (fillLevelPercentage >= 25 && fillLevelPercentage < 50) {
return 2;
} else {
return 1;
}
}
Here’s the Serial Monitor from Arduino
Additonal Information
Board: Arduino WIFI r2
local machine IP and port: 192.168.1.14 139
I already setup port 139 and 5052 in Inbound rules
Already Off the firewall.
I try also to use the Ip and port on the endpoint still didn’t work
Still encountering an error
HTTP Status Code: -2
Error on HTTP request: -2
The 139 port came from netstat -a
then i try to telnet 192.168.1.14 139 it connects
6