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
-
Open Control Panel: Click the Start button, type "control panel," and select it.
-
Navigate to Programs and Features: In the Control Panel, click "Programs" and then "Programs and Features".
-
Turn Windows features on or off: Click the link "Turn Windows features on or off".
-
Locate and Select IIS: In the Windows Features window, find "Internet Information Services" and check the box next to it.
-
Confirm and Install: Click "OK" to start the installation. Windows will download and install the necessary components.
-
Restart (if needed): Depending on the specific components selected, you may need to restart your computer for the changes to take effect.
-
Run IISMGR Which allows you to make adjustments.