I've got a sensor (TMP102 I2C thermometer), Uno R3, Ethernet Shield, RTC.
I can log the sensor readings to the SD card in one sketch, and I can view the sensor readings over the ethernet in another sketch, but I can't figure out how to do both.
I've gotten as far as starting the server in the sensor reading and logging sketch, it will respond to ping, but it won't serve up my logs. I've been trying for weeks, about ready to give up.
// 20121012 1337
// TMP102
#include <Wire.h>
int TMP102Address = 0x48;
// SD
#include <SD.h>
// ss, cs
SdFile file;
// Ethernet
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 8 };
EthernetServer server(80);
// RTC
#include "RTClib.h"
RTC_DS1307 RTC;
void setup() {
Serial.begin(9600);
Serial.println("sketch: 20121012_1758 reboot");
Wire.begin();
RTC.begin();
pinMode(10, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(10, HIGH);
if (!SD.begin(4)) {
Serial.println("Card initialization failed...");
return;
}
Serial.println("Card initialized.");
Ethernet.begin(mac, ip);
digitalWrite(10, HIGH);
server.begin();
}
// not sure i need this
#define BUFSIZ 100
void loop() {
DateTime now = RTC.now();
float TMP102_C = readTMP102_C();
File sensorDataFile = SD.open("fuck10.csv", FILE_WRITE);
if (sensorDataFile) {
sensorDataFile.print(now.unixtime());
Serial.println(now.unixtime());
sensorDataFile.print(", ");
sensorDataFile.print(TMP102_C);
Serial.println(TMP102_C);
sensorDataFile.println("");
sensorDataFile.close();
} else {
Serial.println("Error opening data file.");
Serial.println("");
}
EthernetClient client = server.available();
char clientline[BUFSIZ];
int index = 0;
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = true;
//
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// i think the problem lies here
if (c != '\n' && c != '\r') {
clientline[index] = c;
index++;
if (index >= BUFSIZ){
index = BUFSIZ -1;
}
continue;
}
clientline[index] = 0;
Serial.println();
Serial.print("clientline: ");
Serial.println(clientline);
if (strstr(clientline, "GET /") !=0) {
char *filename;
filename = clientline + 5;
(strstr(clientline, " HTTP"))[0] = 0;
Serial.println("file opened!");
client.println("HTTP/1.1 200 OK");
client.println("Content-Tpe: text/plain");
client.println();
int16_t c;
while ((c = file.read()) > 0) {
client.print((char)c);
}
file.close();
} else {
Serial.println("404");
}
break;
}
}
delay(1);
client.stop();
Serial.println("Client disconnected.");
}
/*
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("Client disconnected");
}
*/
delay(2000);
}
// 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;
}