-->
Page 1 of 1

Striped Data or Retrieved Page -Saved to SD - Simple Example

PostPosted: Sun Dec 13, 2015 8:10 am
by GengusKahn
The Following sketch can be used to transfer data from ESP to ESP or retrieve a remote page and strip HTML etc.

This is a simple adaptation of the existing Arduino code for drop in use on ESP.......

Code: Select all//
//  This has been adapted for the ESP8266 to allow the retrieval of any specific data
//  from any specific resource, just supply the URI.
//  Supply Port IP URL and search String data to strip headers or even ALL HTML
//
//  In this example...(I am hosting and collecting from isolated units to provide Data to Web)
//  this strips the All the HTML from a Google scripted chart and provides the RAW Chart Data
//
//  Provide a routine specifically inside your own sketch and edit the search String.....
//  This can also function as SPIFFS/SD to SPIFFS/SD for the ESP-AP connected devices.....
//
//  This is the simple way, you will need to fine tune the "Stripping Searches" to your needs.............
//
// Gets a file from the Internet and saves it to the SD card
// Removes the incoming HTTP header, saves the file only
// This example gets the XML cricket score from synd.cricbuzz.com **** From Original Sketch !!!
//
// References - Arduino Example sketches from IDE by David A. Mellis et al.:
// -- File --> Examples --> Ethernet --> WebClient
// http://www.arduino.cc/en/Tutorial/WebClient
// -- File --> Examples --> SD --> ReadWrite
// http://www.arduino.cc/en/Tutorial/ReadWrite
//
// Author: W.A. Smith    Date: 10 June 2015 <--- Original Arduino Sketch
// http://startingelectronics.org/software/arduino/save-web-file-to-SD-card/
//

/*

The data here has been stripped by the sketch from the Hosted Google Chart page.......
Headers are Optional, add 1 more loop to the search for "["

'Time / GMT', 'InTmp', 'InHum', 'ExTmp', 'ExHum', 'SySVcc', 'DSTemp0', 'DSTemp1', 'DSTemp2', 'DSTemp3', 'DSTemp4'
'13 Dec - 11:49:25',16.80,38.40,15.50,40.10,3.43,15.56,-0.06,15.63,13.38,16.56
'13 Dec - 11:50:25',16.80,38.30,15.50,40.00,3.43,15.56,0.00,15.63,13.44,16.56
'13 Dec - 11:51:25',16.80,38.50,15.50,40.10,3.43,15.50,0.00,15.63,13.44,16.56
'13 Dec - 11:52:25',16.80,38.40,15.50,40.20,3.43,15.50,0.06,15.69,13.44,16.56
'13 Dec - 11:53:25',16.80,38.50,15.50,40.10,3.42,15.50,0.06,15.63,13.44,16.56
'13 Dec - 11:54:25',16.80,38.50,15.50,40.10,3.43,15.50,0.06,15.69,13.38,16.56
.
.
.
.
*/

#include <SPI.h>
#include <WiFiClient.h>
#include <SD.h>
#include <ESP8266WiFi.h>

IPAddress server(192, 168, 1, 5); // This is serving the data from 5 DS18B20's and 2 DHT22's
WiFiClient client;
const char* ssid = "Your-SSID";
const char* password = "Your-Password";
boolean currentLineIsBlank = true;
File theFile;

void setup() {

 
  Serial.begin(115200);
  Serial.println("");
  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  Serial.println(ssid);

  // Wait for connection
  uint8_t i = 0;
  while (WiFi.status() != WL_CONNECTED && i++ < 20) {//wait 10 seconds
    delay(500);
  }
  if(i == 21){
    Serial.print("Could not connect to");
    Serial.println(ssid);
    while(1) delay(500);
  }
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print(F("Initializing SD card..."));

  if (!SD.begin(15)) {
    Serial.println(F(" initialization failed!"));
    return;
  }
  if (SD.exists("chartD.txt")){SD.remove("chartD.txt");}// change file name to write to a fresh file here
  Serial.println(F(" initialization done."));
  Serial.println("connecting...");
 
// The Start of the Routine to clollect the data...........

  // if you get a connection, report back via serial:
  if (client.connect(server, 87)) {                           // Connect to IP and PORT !!!!
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /graphic HTTP/1.1");    // change resource to get here, raw - csv - file - PAGE....
    client.println();
  }
  else {
    // didn't get a connection to the server:
    Serial.println("connection failed");
  }
  // open the file for writing
  Serial.println("Creating file.");
  theFile = SD.open("chartD.txt", FILE_WRITE);  // change file name to write to here
  if (!theFile) {
    Serial.println("Could not create file");
    while (1);
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    if (c == '<') {  // <HTML> First Tag.....
// Use a marker for self constructed formats, I am using this as it works for stripping header header and my files
          c = client.read();  // get file byte to push past tag......not really needed but...Belt & Braces....
    for (int i=0;i<3;i++){                             // DE-Select the Headers by adding 1......
         String dlinew = client.readStringUntil('[');  // Strip Google code for chart data extraction
         }
// end of HTTP header & Google code, now save requested file       
      while (client.available()) {
        // stay in this loop until the file has been received
         String dlinew = client.readStringUntil('[');
         String linew = client.readStringUntil(']');  // get data
          theFile.println(linew);   // save data to file   
    }
   }
  }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    theFile.close();
    Serial.println("Finished writing to file");
   
// do nothing... Use this as Function in your sketch, Call void getfile() {copy the loop + section from Setup}
    delay(-1);
  }
}