請幫忙解一下,在WEB+SD寫資料進SD卡
本帖最后由 s9930081 于 2013-11-13 11:11 编辑用WEB來顯示我所接收到感測器數據,並存到SD卡中,再由網頁連結來顯示SD卡中的數據
我在LOOP中加入:
file.open("123.txt", O_RDWR | O_CREAT | O_AT_END);//宣告TXT檔名
if (!file.isOpen()) {
Serial.print("Writing to 123.txt...");//等待文件創建
delay(2);
file.print(ten);
file.print(num);
file.print(",");
file.print(hten);
file.println(hnum);
file.close(); //資料寫入結束
Serial.println("done.");
他有執行過去但沒有正確的存進SD卡中,為什麼會這樣??
原程式碼,拜託幫忙謝謝。
#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, 2, 107 };
EthernetServer server(80);
int xx =0;
String InByte;//UART暫存
String number;//
int temperature; //溫度
int humidity; //濕度
int ten = 0, num = 0;
int hten = 0, hnum = 0;
int mten = 0, mnum = 0;
int bten = 0, bnum = 0;
//File myFile;
//SdFat sd;
/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
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);
}
void setup() {
Serial.begin(38400);
Serial3.begin(38400);
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(53, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(53, HIGH); // but turn off the W5100 chip!
if (!card.init(SPI_HALF_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 == DIR_NAME_FREE) break;
// skip deleted entry and entries for . and..
if (p.name == DIR_NAME_DELETED || p.name == '.') 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 == ' ') continue;
if (i == 8) {
client.print('.');
}
client.print((char)p.name);
}
client.print("\">");
// print file name with possible blank fill
for (uint8_t i = 0; i < 11; i++) {
if (p.name == ' ') continue;
if (i == 8) {
client.print('.');
}
client.print((char)p.name);
}
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("</li>");
}
client.println("</ul>");
}
// How big our line buffer should be. 100 is plenty!
#define BUFSIZ 100
void loop()
{
while(Serial3.available() > 0) {
// 讀取進來的 byte
InByte+= char (Serial3.read());
//delay(2);
xx += 1;
if(xx == 5){
//if(InByte = 1)
//{
//溫度
//temperature = -40 + (InByte*16*16+InByte)*0.01;
temperature = -40 + (InByte*16*16+InByte)*0.01;
//濕度
//humidity = -4 + (InByte*16*16+InByte)*0.0405-(InByte*16*16+InByte)*(InByte*16*16+InByte)*0.0000028;
humidity = -4 + (InByte*16*16+InByte)*0.0405-(InByte*16*16+InByte)*(InByte*16*16+InByte)*0.0000028;
ten = temperature / 10;
num = temperature % 10;
hten = humidity / 10;
hnum = humidity % 10;
//Serial.println("done.");
file.open("123.txt", O_RDWR | O_CREAT | O_AT_END);//宣告TXT檔名
if (!file.isOpen()) {
Serial.print("Writing to 123.txt...");//等待文件創建
delay(2);
file.print(ten);
file.print(num);
file.print(",");
file.print(hten);
file.println(hnum);
file.close(); //資料寫入結束
Serial.println("done.");
} else {
Serial.println("error opening test.txt");
}
//}
InByte = String("");
xx = 0;
}
}
char clientline;
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 = 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 = 0;
// Print it out for debugging
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();
//*************************************************
// now output HTML data starting with standart header
//set background to yellow
client.print("<body style=background-color:white>");
//send first heading
client.println("<font color='red' size='20'><h1 align='center'>Web Server</font></h1>"); //標題
client.println("<hr />");//水平線
client.println("<font color='black' size='5'>*Used to display the temperature & humidity</font>");
//client.println("<br />"); //換行
client.println("<hr />"); //水平線
//顯示溫度*********************************************************************************
client.println("<table border=2>"); // 表格 粗體
client.println("<tr><td>"); // 表格開始 第一行
client.println("<font color='blue' size='7'>temperature:");
client.println("</td><td>");//格線
number = webnumber (ten);
client.println("<img src ='" + number + "'>");
number = webnumber (num);
client.println("<img src ='" + number + "'>");
client.println("</td><td>");
//顯示濕度*********************************************************************************
//client.println("<tr><td>");
client.println("<font color='blue' size='7'>humidity:");
client.println("</td><td>");
//client.println(humidity);client.println("</font>");
//client.println("<br />");
number = webnumber (hten);
client.println("<img src ='" + number + "'>");
number = webnumber (hnum);
client.println("<img src ='" + number + "'>");
client.println("</td></tr>");
client.println("<tr><td>");
//光罩**********************************************************************************
client.println("<font color='blue' size='7'>Mask degrees:");
client.println("</td><td>");
number = webnumber (mten);
client.println("<img src ='" + number + "'>");
number = webnumber (mnum);
client.println("<img src ='" + number + "'>");
client.println("</td><td>");
//client.println("<tr><td>");
//電力*********************************************************************************
client.println("<font color='blue' size='7'>Battery:");
client.println("</td><td>");
number = webnumber (bten);
client.println("<img src ='" + number + "'>");
number = webnumber (bnum);
client.println("<img src ='" + number + "'>");
client.println("</td></tr>");
client.println("</table>");
//*********************
// 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;
// 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();
}
}
String webnumber(int xx)
{
String N;
switch (xx)
{
case 0:
N = "http://13p.imghost.us/vl/0.png";
return (N);
break;
case 1:
N = "http://13q.imghost.us/lb/1.png";
return (N);
break;
case 2:
N = "http://13q.imghost.us/oB/2.png";
return (N);
break;
case 3:
N = "http://13q.imghost.us/Iv.png";
return (N);
break;
case 4:
N = "http://13q.imghost.us/I9.png";
return (N);
break;
case 5:
N = "http://13q.imghost.us/bU.png";
return (N);
break;
case 6:
N = "http://13q.imghost.us/eVx.png";
return (N);
break;
case 7:
N = "http://13q.imghost.us/8i.png";
return (N);
break;
case 8:
N = "http://13q.imghost.us/Ra.png";
return (N);
break;
case 9:
N = "http://13q.imghost.us/sL.png";
return (N);
break;
}
}
页:
[1]