Connecting to specific file on website using Wifi on GIGA

I am using a Giga and I need to access files on a Windows machine (using the web server build into Windows) that are then in a specific location.

I created a simple HTML document within the Windows IIS directory which simply outputs the file (from it's actual location) and from another computer I can access this with the computer's IP address and the page name eg 192.168.0.100/json_backpack.htm OR via "elitesavedgame/json_backoack.htm" (with 192.168.0.100 in the hosts file as elitesavedgame".

Either way shows the content of the file which I could then store and display relevant information on the Giga's screen

However....

I cannot get anything from the Windows page server except the 404 page.

I have changed the home page to just plain text saying "Home Page" and that is what appears on outlook when you type 192.168.0.100 from Windows.

If I change the web server to provide the json file instead of the 404 file it does so and I get the document I want BUT I need access to about 10 documents in real time.

I would show the listing but it it literally the latest version of WifiWebClient with my details in the secret answers file and the following lines changed:

IPAddress server(192,168,0,199);  // IP address for example.com (no DNS)
//char server[] = "example.com";       // host name for example.com (using DNS)

IP as 192.168.0.100 >> display of the 404 page
char server set to example.com >> displays the example.com page as text.
IP as 192.168.0.101 >> display of the 404 page

So if I try to get a web page actually on the internet it works but does not work from local machines. DNS should not be an issue as I am supplying the actual IP address.

Any ideas where to look?

Been a while since I used IIS, but it should have server request logs. Compare the ones that work and the ones that don't. Hopefully the differences are visible in the UI.

In addition to the URL-path, a request also has headers. A 404 Not Found should only happen if that path differs. There are others like 405 Method Not Allowed or 406 Not Acceptable.

You can also try requests with simpler clients, like curl or wget; or even performing HTTP manually as often done on Arduino, with telnet or nc

Thanks. I will take a look. I genuinely only want the straight contents of about 8-10 text (json) files so I’ll look.

If IIS is being picky for whatever reason, you could also use an alternate server. It could even host files from the same default webroot directory.

Many programming languages nowadays offer an easy way to serve files, even some "one-liners". It might even be easier to debug. Something to try, especially if you happen to have one installed on that Windows box.

Ken,

After much messing about I found that the code works perfectly well on example.com or via it's IP address of 93.184.216.34

If I use the IP address of the computer I am getting the error:

404.15 - Query-String too long.

This URL is denied because its Query-String is longer than specified by configuration.

So it does appear that IIP is expecting the GET command in a different format. I have sent the commands exactly as they are shown in the sketch via outlook and they work every time so there appears to be some issue with the livbrary, some incompatability.

/*
  Web client

 This sketch connects to a website (http://example.com) using the WiFi module.

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * Arduino Portenta H7

 created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe
 */

#include <WiFi.h>

#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;        // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// 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,0,150);  // IP address for example.com (no DNS)
//char server[] = "example.com";       // host name for example.com (using DNS) 93.184.216.34



WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200); while (!Serial) { }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(3000);
  }
  Serial.println("Connected to wifi"); 
  printWifiStatus();
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:

  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    Serial.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    Serial.println("==GET /index.html HTTP/1.1");
    client.println("GET /index.html HTTP/1.1");
    
    Serial.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    Serial.print("==Host: "); 
    client.print("Host: "); 
    Serial.println(server); 
    client.println(server); 

    Serial.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    //Serial.println("==GET 192.168.0.150/Backpack.json"); 
    //client.println("==GET 192.168.0.150/Backpack.json"); 
    Serial.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    //Serial.println("==GET /Backpack.json"); 
    //client.println("==GET /Backpack.json"); 
    
     
    Serial.println("==Connection: close");
    client.println("Connection: close");
    client.println();
    Serial.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    Serial.println("<<<<<<<<<<<<<<<<<<BUFFER>>>>>>>>>>>>>>>>>>");
    
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

This outputs the following if you use example.com:

        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

disconnecting from server.

OR

this if you use my server:

h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>404 - File or directory not found.</h2>
  <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
 </fieldset></div>
</div>
</body>
</html>

disconnecting from server.

IF I type the same search criteria into outlook as per the above error I get a working return.

OK further update....

I have install Apache64 and it basically does the same.

The issue is that the wifiwebclient library WORKS using IP address OR webpage name but ONLY for the route/index file.

If you try and GET 192.168.0.100/myfile.json or webpage/myfile.json then this works in outlook but not from the sketch.

using the IP address works for the route file so I can get the default page by inserting the IP address. If I try and add to the end of that it doesn't work and it also doesn't work with the IP address in the speech marks. Clearly when you do this in outlook it processes the address which the library isn't doing.

So basically, at the moment, I can only get access to a SINGLE file when I need 8 OR alternatively I can change the error file to a second file and get two docs but that is not a particularly good work around and still gets only a quarter of the files I need.

Hi @cowasaki . I'm using HFS ~ HTTP File Server on my PC, with WiFi.download(); function for downloading to the giga's onboard flash and WiFiClient class to upload files to the HFS file server - it works flawlessly.

Thanks Steve. The 8 files I need access to can change at any time and I need access to them in a split second. Is this a web based setup and what is the latency?

It's a file server using http rather than ftp. It has a virtual file system that holds links to the underlying files.

Thanks. I will take a look. It could be just what I need.

Well I think I've got it.....

Will post result later as I am modifying the example so that you can specify a page, display it then repeat/specify a different one. As it currently stands the process is spread out between the setup and main loop

OK so I've sorted it and I think that this might be useful to others as well

This sketch basically downloads files from a local (or not) server and provides you with the file's contents

This will work with whatever web server you want to use but either IIS (built into Windows) or Apache (available for Windows/Mac/Linux) will both do. I will include instructions for using IIS at the end of the post....

So this sketch includes lots of debugging/information stuff which you can remove BUT it shows what is going on so that you can bend the sketch to do what is needed

Basically use the example wifiwebclient then replace the main page with the following code.

server[] is equal to the Windows computer name of the computer you are using.

There is a char and a string for each page (in this example), so in this example I have

char docBackpack[] = "GET /Backpack.json HTTP/1.1";
String BackPack="";

This relates to a file called "Backpack.json" which I want to grab. The file is in the home directory of the web server so if using defaults with IIS it would be C:\inetpub\wwwroot\Backpack.json. The part you need to change is the /Backpack.json. Then we have String BackPack=""; this is the buffer which contains the entire file downloaded.

There are further functions which work on the buffer:

int GetLengthOfData(String buffer) which returns the length of the actual document
ExtractDataFromBuffer(String buffer) which removes the header from the document which was added by the server.

#include <WiFi.h>
#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;        // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
//IPAddress server(192,168,0,150);  // IP address for example.com (no DNS)
char server[] = "LAB-PC";       // host name for example.com (using DNS) 93.184.216.34

// Pages and page buffers
char docBackpack[] = "GET /Backpack.json HTTP/1.1";
String BackPack="";
int lenBackpack=0;
char docStatus[] = "GET /Status.json HTTP/1.1";
String Status="";
int lenStatus=0;

// REMOVE THESE.  JUST THERE TO SHOW TIMINGS
int T = 0;                              // remove
int T2 = 0;                             // remove
int T3 = 0;                             // remove
int T4 = 0;                             // remove
bool debugging = true;


WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200); while (!Serial) { }

  if (debugging) T=millis();                                // remove
  ConnectToWifi();
  if (debugging) T=millis()-T;                              // remove
  
  if (debugging) T3=millis();                               // remove
  GetPage(docBackpack);
  if (debugging) T3=millis()-T3;                            // remove
  
  //dumpPageToSerial();
  BackPack=CopyPageToBuffer();
  Serial.println(BackPack);                                 // remove

  if (debugging) T4=millis();                               // remove
  GetPage(docStatus);
  if (debugging) T4=millis()-T4;                            // remove
  
  Status=CopyPageToBuffer();
  Serial.println(Status);                                   // remove
  Serial.println(GetLengthOfData(Status));
  Serial.print(">>>");
  Serial.print(ExtractDataFromBuffer(Status));
  Serial.println("<<<");

  if (debugging) {                                               // remove
    Serial.print("\nConnect to WIFI = "); Serial.println(T);     // remove
    Serial.print("Connect to PAGE 1 = "); Serial.println(T3);    // remove
    Serial.print("Connect to PAGE 2 = "); Serial.println(T4);    // remove
    Serial.print("Download PAGE 2   = "); Serial.println(T2);    // remove
  }        
                                                        // remove
}

void loop() {
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
      Serial.println(); Serial.println("disconnecting from server.");   // remove
    client.stop();
    // do nothing forevermore:
    while (true);
  }
}

void ConnectToWifi() {
 // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: "); Serial.println(ssid);     // remove
    status = WiFi.begin(ssid, pass); delay(1000);
  }
  Serial.println("Connected to wifi");                                         // remove
  printWifiStatus();                                                           // remove
  Serial.println("\nStarting connection to server...");                        // remove
  // if you get a connection, report back via serial:
}

void GetPage(char webpage[]){
  if (client.connect(server, 80)) {
    Serial.println("connected to server");                                     // remove
    // Make a HTTP request:
    client.println(webpage);
    client.print("Host: "); 
    client.println(server); 
    client.println("Connection: stay connected");
    client.println();
    // wait whilst page downloads
    T2=millis();                             // remove
    while (!client.available()) {}
    T2=millis()-T2;                          // remove
  }
}

String CopyPageToBuffer(){
  // if there are incoming bytes available
  // from the server, read them into buffer String
  String buffer="";
  while (client.available()) {
   char c = client.read();
   buffer=buffer+c;
  }
  return buffer;
}

int GetLengthOfData(String buffer){
  String number=buffer.substring(16+buffer.indexOf("Content-Length:"),21+buffer.indexOf("Content-Length:"));
  int length=number.toInt();
  return length;
}

String ExtractDataFromBuffer(String buffer){
  String data=buffer.substring(buffer.length()-GetLengthOfData(buffer),buffer.length());
  return data;  
}

void printWifiStatus() {     // print the SSID of the network you're attached to:
  Serial.print("SSID: "); Serial.println(WiFi.SSID());
  // print your board's IP address:
  IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); 
}

To setup IIS which is the internet web server for Windows

  1. Open Control Panel: Click the Start button, type "control panel," and select it.

  2. Navigate to Programs and Features: In the Control Panel, click "Programs" and then "Programs and Features".

  3. Turn Windows features on or off: Click the link "Turn Windows features on or off".

  4. Locate and Select IIS: In the Windows Features window, find "Internet Information Services" and check the box next to it.

  5. Confirm and Install: Click "OK" to start the installation. Windows will download and install the necessary components.

  6. Restart (if needed): Depending on the specific components selected, you may need to restart your computer for the changes to take effect.

  7. Run IISMGR Which allows you to make adjustments.