How to connect the Arduino to the API

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#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;
}
}
</code>
<code>#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; } } </code>
#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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật