Go Down

Topic: reading a txt file from an sd card over the internet (Read 1 time) previous topic - next topic

PaulS

Quote
initialize one at a time, then do something like :  client.print(myFile.read()); ?

In that statement, you are NOT switching which device is active. You would need to do something like:

Code: [Select]
// Make SD active
char ltr = myFile.read();
// Make Ethernet active
client.print(ltr);

(where you put real code to activate the two devices, not comments like I did).

Quote
This points my attention to the getFile() call which prints the "temps.txt:" above and then its supposed to print the whole file followed by "done". The problem is I am only getting one character.

That is all that code is supposed to do. You open the file. read and print one character, and read and return the next character, The return statement ends the getFile() function, which causes reading to stop.

Doing two reads, like that, is not a good idea.

seanz2003

#6
Jul 07, 2012, 06:45 am Last Edit: Jul 07, 2012, 06:48 am by seanz2003 Reason: 1
thank you PaulS for your reply!

Quote
The return statement ends the getFile() function, which causes reading to stop.


So if i want to read the whole file, I cannot use return? Could you explain how  " char a = myFile.read(); " works? In a learning example, I used " Serial.write(myFile.read()); " to print out the entire content of a file, whereas here i am only getting one character. how is it these two statements yield different results? In my mind, it seems that  " char a = myFile.read(); " should read the entire file into char a...
..... Hmm .. I just read up on the char data type and i don't think it is appropriate at all if I want to read out 100+ temperature readings.  So i'll try a string then (or is it String?).

PaulS

Quote
So if i want to read the whole file, I cannot use return?

Sure you can. Just not there.

What is it the function should return? You have it defined to return 1 character, which really does not make sense.

If the intent is to return the contents of the file, that isn't really possible. You could pass another variable to the function, by reference, that is an array where the data would be stored, making the function return type void, and eliminating the need for a return statement.

Quote
In a learning example, I used " Serial.write(myFile.read()); " to print out the entire content of a file

That was done in a while loop, one character at a time.

Quote
So i'll try a string then (or is it String?).

string - a NULL terminated array of chars.
String - a class that will almost certainly fragment all of your memory, causing your Arduino to crash.

You decide which to use.

seanz2003

Is this better: char dataString [] = "myFile.read()"; ?

seanz2003

ah, Working code ! Thanks for your help PaulS! I learned something tonight.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <SD.h>
int DS18S20_Pin = 9;
OneWire ds(DS18S20_Pin);
const int sd_cs = 4;
const int ethernet_cs = 10;
long interval = 200000;
long previousMillis = 0;
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,25);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
File myFile;
char data [] = "";
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  pinMode(sd_cs, OUTPUT);
  pinMode(ethernet_cs, OUTPUT);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  digitalWrite(ethernet_cs, HIGH);
  digitalWrite(sd_cs, LOW);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = SD.open("temps.txt", FILE_WRITE);
  myFile.close();
}


void loop() {

  checkClient();
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {

    previousMillis = currentMillis;   
    recordConditions();

  }

}

void checkClient()  // listen for incoming clients
{
  digitalWrite(ethernet_cs, LOW);
  digitalWrite(sd_cs, HIGH);
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          digitalWrite(ethernet_cs, HIGH);
          digitalWrite(sd_cs, LOW);
          myFile = SD.open("temps.txt");
          if (myFile) {
            Serial.println("temps.txt:");

            // read from the file until there's nothing else in it:
            while (myFile.available()) {

              char data = myFile.read();

              digitalWrite(ethernet_cs, LOW);
              digitalWrite(sd_cs, HIGH);
              client.print(data);
              Serial.write(data); 

            }
            // close the file:
            digitalWrite(ethernet_cs, HIGH);
            digitalWrite(sd_cs, LOW);
            myFile.close();
          }
          else {
            // if the file didn't open, print an error:
            Serial.println("error opening test.txt");
          }
          digitalWrite(ethernet_cs, LOW);
          digitalWrite(sd_cs, HIGH);
          client.println(" done");
          Serial.println(" done");
          client.println("<br />");       
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}
void recordConditions()
{
  digitalWrite(ethernet_cs, HIGH);
  digitalWrite(sd_cs, LOW);
  myFile = SD.open("temps.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to temp.txt...");
    float temperature = getTemp();
    Serial.print(temperature);
    myFile.println(temperature);
    // close the file:
    myFile.close();
    Serial.println("done.");
  }
  else {
    // if the file didn't open, print an error:
    Serial.println("error opening temps.txt");
  }
}
float getTemp(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
    //no more sensors on chain, reset search
    ds.reset_search();
    return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
    Serial.print("CRC is not valid!\n");
    return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
    Serial.print("Device is not recognized");
    return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);   
  ds.write(0xBE); // Read Scratchpad


  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16 * 9 / 5 + 32;

  return TemperatureSum;

}

Go Up