/*
* This sketch uses the microSD card slot on the Arduino Ethernet shield
* to serve up files over a very minimal browsing interface
*
* Some code is from Bill Greiman's SdFatLib examples, some is from the
* Arduino Ethernet WebServer example and the rest is from Limor Fried
* (Adafruit) so its probably under GPL
*
* Tutorial is at http://www.ladyada.net/learn/arduino/ethfiles.html
* Pull requests should go to http://github.com/adafruit/SDWebBrowse
*/
#include <SdFat.h>
#include <SdFatUtil.h>
#include <Ethernet.h>
#include <SPI.h>
/************ ETHERNET STUFF ************/
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, 1, 8 };
EthernetServer server(80);
/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
//SdFat sd;
// TMP102
#include <Wire.h>
int TMP102Address = 0x48;
// RTC
#include "RTClib.h"
RTC_DS1307 RTC;
// haven't figured out how to use the RTC as intervalometer
long previousMillis;
long interval = 15000;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
//// //// error print? //// ////
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
//// //// SETUP //// ////
void setup() {
Serial.begin(9600);
Serial.println();
//Serial.println();
PgmPrint("20121020 0000");
Serial.println();
Wire.begin();
RTC.begin();
PgmPrint("Free RAM: ");
Serial.println(FreeRam());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
// pin 4?
if (!card.init(SPI_FULL_SPEED, 4)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) error("openRoot failed");
// list file in root with date and size
PgmPrintln("Files found in root:");
root.ls(LS_DATE | LS_SIZE);
Serial.println();
// Recursive list of all directories
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);
Serial.println();
PgmPrintln("Done");
// Debugging complete, we start the server!
Ethernet.begin(mac, ip);
server.begin();
}
//// //// //// ////
void ListFiles(EthernetClient client, uint8_t flags) {
// This code is just copied from SdFile.cpp in the SDFat library
// and tweaked to print to the client output in html!
dir_t p;
root.rewind();
//client.println("<ul>");
while (root.readDir(p) > 0) {
// done if past last used entry
if (p.name[0] == DIR_NAME_FREE) break;
// skip deleted entry and entries for . and ..
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;
// only list subdirectories and files
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
// print any indent spaces
//client.print("<li><a href=\"");
for (uint8_t i = 0; i < 11; i++) {
if (p.name[i] == ' ') continue;
if (i == 8) {
client.print('.');
}
client.print((char)p.name[i]);
}
//client.print("\">");
// print file name with possible blank fill
/*
for (uint8_t i = 0; i < 11; i++) {
if (p.name[i] == ' ') continue;
if (i == 8) {
client.print('.');
}
client.print((char)p.name[i]);
}
*/
//client.print("</a>");
if (DIR_IS_SUBDIR(&p)) {
client.print('/');
}
// print modify date/time if requested
if (flags & LS_DATE) {
root.printFatDate(p.lastWriteDate);
client.print(' ');
root.printFatTime(p.lastWriteTime);
}
// print size if requested
if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
client.print(' ');
client.print(p.fileSize);
client.println("<br>");
}
// client.println("</li>");
}
//client.println("</ul>");
}
//// //// TMP102 //// ////
float readTMP102_C() {
Wire.requestFrom(TMP102Address, 2);
byte MSB = Wire.read();
byte LSB = Wire.read();
// don't understand this yet
int TemperatureSum = ((MSB << 8) | LSB) >> 4;
float TMP102_C = TemperatureSum*0.0625;
return TMP102_C;
}
// How big our line buffer should be. 100 is plenty!
// don t fully understand this. 100 bytes? i ve tried making this 200
#define BUFSIZ 100
//// //// LOOP //// ////
void loop()
{
//
unsigned long currentMillis = millis();
// IF
if (currentMillis - previousMillis > interval) { //replace with RTC
previousMillis = currentMillis;
// TMP102
float TMP102_C = readTMP102_C();
//////////////////////////////////////////////HIH4030
float HIH4030 = analogRead(0);
HIH4030 = (HIH4030/1023)/(1.0546 - 0.00216*TMP102_C);
//////////////////////////////////////MOISTURE SENSOR
/*
digitalWrite(moistureSensorVcc, HIGH);
delay(500);
moistureValue = analogRead(moistureSensor);
digitalWrite(moistureSensorVcc, LOW);
moistureValue = map(moistureValue, 0, 1023, 0, 100);
*/
//debug delete files
//char deleteFile[] = "DATA118.CSV";
//if(!file.open(root, deleteFile, O_WRITE)) ("Opening delete file failed");
//if (!file.remove()) error("Deletion of 106 failed");
//Serial.print(deleteFile);
//PgmPrintln(" deleted.");
////
char name[] = "DATA119.CSV";
//PgmPrint("Appending to: ");
//Serial.println(name);
if (!file.open(root, name, O_CREAT | O_APPEND | O_WRITE)) error("open failed");
DateTime now = RTC.now();
file.print(now.year(), DEC);
file.print(now.month(), DEC);
file.print(now.day(), DEC);
file.print("_");
if (int(now.hour()) < 10) {
file.print("0");
file.print(now.hour(), DEC);
}
else {
file.print(now.hour(), DEC);
}
file.print(":");
if (int(now.minute()) < 10) {
file.print("0");
file.print(now.minute(), DEC);
}
else {
file.print(now.minute(), DEC);
}
file.print(":");
if (int(now.second()) < 10) {
file.print("0");
file.print(now.second(), DEC);
}
else {
file.print(now.second(), DEC);
}
file.print(", ");
file.print(now.unixtime());
//Serial.print(now.unixtime());
file.print(", ");
//Serial.print(", ");
file.print(TMP102_C);
//Serial.println(TMP102_C);
file.print(", ");
file.print(HIH4030);
file.println("");
if (file.writeError) error("write failed");
if (!file.close()) error("close failed");
//Serial.println("Done");
Serial.println();
}
////
char clientline[BUFSIZ];
int index = 0;
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
// reset the input buffer
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// If it isn't a new line, add the character to the buffer
if (c != '\n' && c != '\r') {
clientline[index] = c;
index++;
// are we too big for the buffer? start tossing out data
if (index >= BUFSIZ)
index = BUFSIZ -1;
// continue to read more data!
continue;
}
// got a \n or \r new line, which means the string is done
clientline[index] = 0;
// Print it out for debugging
Serial.print("clientline: ");
Serial.println(clientline);
// Look for substring such as a request to get the root file
if (strstr(clientline, "GET / ") != 0) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// print all the files, use a helper to keep it clean
//client.println("<h2>Files:</h2>");
ListFiles(client, LS_SIZE);
}
else if (strstr(clientline, "GET /") != 0) {
// this time no space after the /, so a sub-file!
char *filename;
filename = clientline + 5; // look after the "GET /" (5 chars)
// a little trick, look for the " HTTP/1.1" string and
// turn the first character of the substring into a 0 to clear it out.
(strstr(clientline, " HTTP"))[0] = 0;
// print the file we want
Serial.println(filename);
if (! file.open(&root, filename, O_READ)) {
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File Not Found!</h2>");
break;
}
Serial.println("Opened!");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/plain");
client.println();
int16_t c;
while ((c = file.read()) > 0) {
// uncomment the serial to debug (slow!)
//Serial.print((char)c);
client.print((char)c);
}
file.close();
}
else {
// everything else is a 404
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File Not Found!</h2>");
}
break;
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}