<aside> <img src="/icons/computer-chip_gray.svg" alt="/icons/computer-chip_gray.svg" width="40px" />
Using MCU as server
I tested an Arduino UNO R4 with this example code and documentation.
For the below code, the port 80 on MCU is listening to HTTP request, and it will turn the LED on/off when receiving GET / H or GET /L.
WiFiServer server(80); //this determine the port open for listening
//{...} Codes in between excluded
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H HTTP/1.1")) {
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L HTTP/1.1")) {
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
Below is the bash to send the corresponding HTTP request to get the MCU’s LED on and off.
% curl <http://10.23.11.250:80/H>
% curl <http://10.23.11.250:80/L>
For experimenting, I also changed the code into the below version:
WiFiServer server(100); //this determine the port open for listening
//{...} Codes in between excluded
// Check to see if the client request was "POST /Cat" or "GET /Cute":
if (currentLine.endsWith("POST /Cat HTTP/1.1")) {
digitalWrite(LED_BUILTIN, HIGH); // POST /Cat turns the LED on
}
if (currentLine.endsWith("GET /Cute HTTP/1.1")) {
digitalWrite(LED_BUILTIN, LOW); // GET /Cute turns the LED off
}
Now that the MCU is listening on port 100, it will turn the LED on/off when receiving POST / Cat or GET /Cute. So I twig the bash into below:
% curl -X POST -d "anything will do" <http://10.23.11.250:100/Cat>
% curl <http://10.23.11.250:100/Cute>
</aside>
<aside> <img src="/icons/computer-chip_gray.svg" alt="/icons/computer-chip_gray.svg" width="40px" />
Using MCU as client (basically using curl in MCU)
I tested an Arduino UNO R4 with this example code and documentation.
I encountered some issues in the beginning, later on realizing the issue is a combination of the port number, and the path, meaning the path I put in content.get()
When requesting on port 8080, the console shows:
Status code: -3 , meaning connection timed out, the server didn’t response. This issue is resolved after changing the port to 80.
I checked the ufw status, and port 8080/80 are both open. I found out that even if the port is open, if there’s no application (ex: nginx) listening to that port, it’s going to time out since the server wouldn’t respond. I checked the nginx with $ sudo ss -ltnp |grep nginx, and nginx didn't listen to port 8080.
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=657004,fd=5),("nginx",pid=657003,fd=5))
LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=657004,fd=6),("nginx",pid=657003,fd=6))
When requesting on a path that doesn’t exist, ex: client.get(”/notexist”), the console shows:
Status code: 404 Response: <html> <head><title>404 Not Found</title></head> <body> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.26.3 (Ubuntu)</center> </body> </html>
Meaning that the server did response but the there’s no content in that URL.
</aside>