i am trying to parse a page from the route by using Arduino + Ethernet shield. before Arduino testing. I tested my code via python as it shows below. from this code i can get the data as i wish where we need authenticate user name and pass word to access to the router, as you see, here ( admin , admin) in the same time i have to used output variable to get only the data required. Python code is does it.
Python code:
My_url = "http://192.168.8.1/update.cgi"
r = requests.post (My_url, auth=('admin', 'admin'),data = 'output=netdev' )
print(r.status_code)
print(r.headers['content-type'])
print(r.encoding)
print(r.text)
The output is:
200
text/javascript
ISO-8859-1
netdev = {
'WIRED':{rx:0x84154,tx:0x5a0cba}
,'BRIDGE':{rx:0x7e680,tx:0x59d98a}
,'WIRELESS0':{rx:0x0,tx:0x0}
,'WIRELESS1':{rx:0x0,tx:0x0}
}
Now i am trying to do that by using Arduino + Ethernet shield and i would like print the output on the serial port as it is showing below. Here the router is server and the Arduino is client.
My problem is the POST request with the authentication, How I can do it ( POST with authentication + 'output=netdev' like in python code ). Please any idea .. Many thanks in advanced
Arduino code :
#include <Ethernet.h>
#include <SPI.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192,168,8,1); // numeric IP for Google (no DNS)
//char server[] = "jsonplaceholder.typicode.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 8, 100);
IPAddress myDns( 8, 8, 8, 8);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println("POST /update.cgi HTTP/1.0");
client.println("Host: 192.168.8.1");
client.println("Connection: close");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
//if (client.available()) {
while (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
Arduino output
Initialize Ethernet with DHCP:
DHCP assigned IP 192.168.8.107
connecting...
connected to 192.168.8.1
HTTP/1.0 401 Unauthorized
Server: httpd
Date: Sat, 01 Jan 2011 00:27:00 GMT
WWW-Authenticate: Basic realm="DR3800"
Content-Type: text/html
Connection: close
<HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>
<BODY BGCOLOR="#cc9999"><H4>401 Unauthorized</H4>
Authorization required.
</BODY></HTML>
disconnecting.
Thank you Dougie for your anser. I used it , please you can see it in my code below where i used this library it gives no error for GET request , but when i use it ti POST request it gives same authentication problem as you see . where the POST request is important to get the data from .cgi page . Maybe i use that in wrong way .. any advice ? thanks
void loop() {
Serial.println("making GET request with HTTP basic authentication");
String contentType = "update.cgi";
String postData = "output=netdev";
client.beginRequest();
client.get("/"+contentType);
client.sendBasicAuth("admin", "admin"); // send the username and password for authentication
client.endRequest();
// 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);
}
The output is ok
making GET request with HTTP basic authentication
Status code: 200
Response:
Wait five seconds
But for POST as follows :
void loop() {
Serial.println("making GET request with HTTP basic authentication");
String contentType = "update.cgi";
String postData = "output=netdev";
client.beginRequest();
client.post("/",contentType , postData);
client.sendBasicAuth("admin", "admin"); // send the username and password for authentication
client.endRequest();
// 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);
}
the output is :
making GET request with HTTP basic authentication
Status code: 401
Response: <HTML><HEAD><TITLE>401 Unauthorized</TITLE></HEAD>
<BODY BGCOLOR="#cc9999"><H4>401 Unauthorized</H4>
Authorization required.
</BODY></HTML>
Wait five seconds