极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3956|回复: 0

請幫忙解一下,在WEB+SD寫資料進SD卡

[复制链接]
发表于 2013-8-15 12:34:21 | 显示全部楼层 |阅读模式
本帖最后由 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卡中,為什麼會這樣??


原程式碼,拜託幫忙謝謝。

  1. #include <SdFat.h>
  2. #include <SdFatUtil.h>
  3. #include <Ethernet.h>
  4. #include <SPI.h>

  5. /************ ETHERNET STUFF ************/
  6. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  7. byte ip[] = { 192, 168, 2, 107 };
  8. EthernetServer server(80);
  9. int xx =0;
  10. String InByte;  //UART暫存
  11. String number;  //
  12. int temperature; //溫度
  13. int humidity;    //濕度
  14. int ten = 0, num = 0;
  15. int hten = 0, hnum = 0;
  16. int mten = 0, mnum = 0;
  17. int bten = 0, bnum = 0;
  18. //File myFile;
  19. //SdFat sd;
  20. /************ SDCARD STUFF ************/
  21. Sd2Card card;
  22. SdVolume volume;
  23. SdFile root;
  24. SdFile file;
  25. // store error strings in flash to save RAM
  26. #define error(s) error_P(PSTR(s))

  27. void error_P(const char* str) {
  28.   PgmPrint("error: ");
  29.   SerialPrintln_P(str);
  30.   if (card.errorCode()) {
  31.     PgmPrint("SD error: ");
  32.     Serial.print(card.errorCode(), HEX);
  33.     Serial.print(',');
  34.     Serial.println(card.errorData(), HEX);
  35.   }
  36.   while(1);
  37. }

  38. void setup() {
  39.   Serial.begin(38400);
  40.   Serial3.begin(38400);

  41.   PgmPrint("Free RAM: ");
  42.   Serial.println(FreeRam());  

  43.   // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  44.   // breadboards.  use SPI_FULL_SPEED for better performance.
  45.   pinMode(53, OUTPUT);                       // set the SS pin as an output (necessary!)
  46.   digitalWrite(53, HIGH);                    // but turn off the W5100 chip!

  47.   if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");

  48.   // initialize a FAT volume
  49.   if (!volume.init(&card)) error("vol.init failed!");

  50.   PgmPrint("Volume is FAT");
  51.   Serial.println(volume.fatType(),DEC);
  52.   Serial.println();

  53.   if (!root.openRoot(&volume)) error("openRoot failed");

  54.   // list file in root with date and size
  55.   PgmPrintln("Files found in root:");
  56.   root.ls(LS_DATE | LS_SIZE);
  57.   Serial.println();

  58.   // Recursive list of all directories
  59.   PgmPrintln("Files found in all dirs:");
  60.   root.ls(LS_R);

  61.   Serial.println();
  62.   PgmPrintln("Done");
  63.   // Debugging complete, we start the server!
  64.   Ethernet.begin(mac, ip);
  65.   server.begin();
  66.    
  67. }

  68. void ListFiles(EthernetClient client, uint8_t flags) {
  69.   // This code is just copied from SdFile.cpp in the SDFat library
  70.   // and tweaked to print to the client output in html!
  71.   dir_t p;

  72.   root.rewind();
  73.   client.println("<ul>");
  74.   while (root.readDir(&p) > 0) {
  75.     // done if past last used entry
  76.     if (p.name[0] == DIR_NAME_FREE) break;

  77.     // skip deleted entry and entries for . and  ..
  78.     if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;

  79.     // only list subdirectories and files
  80.     if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;

  81.     // print any indent spaces
  82.     client.print("<li><a href="");
  83.     for (uint8_t i = 0; i < 11; i++) {
  84.       if (p.name[i] == ' ') continue;
  85.       if (i == 8) {
  86.         client.print('.');
  87.       }
  88.       client.print((char)p.name[i]);
  89.     }
  90.     client.print("">");

  91.     // print file name with possible blank fill
  92.     for (uint8_t i = 0; i < 11; i++) {
  93.       if (p.name[i] == ' ') continue;
  94.       if (i == 8) {
  95.         client.print('.');
  96.       }
  97.       client.print((char)p.name[i]);
  98.     }

  99.     client.print("</a>");

  100.     if (DIR_IS_SUBDIR(&p)) {
  101.       client.print('/');
  102.     }

  103.     // print modify date/time if requested
  104.     if (flags & LS_DATE) {
  105.        root.printFatDate(p.lastWriteDate);
  106.        client.print(' ');
  107.        root.printFatTime(p.lastWriteTime);
  108.     }
  109.     // print size if requested
  110.     if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) {
  111.       client.print(' ');
  112.       client.print(p.fileSize);
  113.     }
  114.     client.println("</li>");
  115.   }
  116.   client.println("</ul>");

  117. }

  118. // How big our line buffer should be. 100 is plenty!
  119. #define BUFSIZ 100

  120. void loop()
  121. {
  122.   while(Serial3.available() > 0) {
  123.     // 讀取進來的 byte
  124.     InByte+= char (Serial3.read());
  125.     //delay(2);
  126.     xx += 1;
  127.     if(xx == 5){
  128.     //if(InByte[0] = 1)
  129.     //{
  130.       //溫度
  131.       //temperature = -40 + (InByte[7]*16*16+InByte[8])*0.01;
  132.       temperature = -40 + (InByte[1]*16*16+InByte[2])*0.01;
  133.       //濕度
  134.       //humidity = -4 + (InByte[9]*16*16+InByte[10])*0.0405-(InByte[9]*16*16+InByte[10])*(InByte[9]*16*16+InByte[10])*0.0000028;
  135.       humidity = -4 + (InByte[3]*16*16+InByte[4])*0.0405-(InByte[3]*16*16+InByte[4])*(InByte[3]*16*16+InByte[4])*0.0000028;
  136.       ten = temperature / 10;
  137.       num = temperature % 10;
  138.       hten = humidity / 10;
  139.       hnum = humidity % 10;
  140.     //Serial.println("done.");
  141.     file.open("123.txt", O_RDWR | O_CREAT | O_AT_END);  //宣告TXT檔名
  142.   if (!file.isOpen()) {
  143.     Serial.print("Writing to 123.txt...");  //等待文件創建
  144.     delay(2);
  145.     file.print(ten);
  146.     file.print(num);
  147.     file.print(",");
  148.     file.print(hten);
  149.     file.println(hnum);
  150.     file.close();                        //資料寫入結束
  151.     Serial.println("done.");
  152.   } else {
  153.     Serial.println("error opening test.txt");
  154.   }
  155.    
  156.     //}
  157.    
  158.     InByte = String("");
  159.     xx = 0;
  160.     }
  161.   }
  162.   char clientline[BUFSIZ];
  163.   int index = 0;

  164.   EthernetClient client = server.available();
  165.   
  166.   if (client) {
  167.     // an http request ends with a blank line
  168.     boolean current_line_is_blank = true;

  169.     // reset the input buffer
  170.     index = 0;

  171.     while (client.connected()) {
  172.       if (client.available()) {
  173.         char c = client.read();
  174.         // If it isn't a new line, add the character to the buffer
  175.         if (c != '\n' && c != '\r') {
  176.           clientline[index] = c;
  177.           index++;
  178.           // are we too big for the buffer? start tossing out data
  179.           if (index >= BUFSIZ)
  180.             index = BUFSIZ -1;
  181.           // continue to read more data!
  182.           continue;
  183.         }
  184.         // got a \n or \r new line, which means the string is done
  185.         clientline[index] = 0;
  186.         // Print it out for debugging
  187.         Serial.println(clientline);
  188.         // Look for substring such as a request to get the root file
  189.         if (strstr(clientline, "GET / ") != 0) {
  190.           // send a standard http response header
  191.           client.println("HTTP/1.1 200 OK");
  192.           client.println("Content-Type: text/html");
  193.           client.println();
  194.           //*************************************************
  195.           // now output HTML data starting with standart header   
  196.           //set background to yellow
  197.           client.print("<body style=background-color:white>");
  198.           //send first heading
  199.           client.println("<font color='red' size='20'><h1 align='center'>Web Server</font></h1>");    //標題
  200.           client.println("<hr />");  //水平線
  201.           client.println("<font color='black' size='5'>*Used to display the temperature & humidity</font>");
  202.           //client.println("<br />"); //換行
  203.           client.println("<hr />"); //水平線
  204.           //顯示溫度*********************************************************************************
  205.           client.println("<table border=2>"); // 表格 粗體
  206.           client.println("<tr><td>");          // 表格開始  第一行
  207.           client.println("<font color='blue' size='7'>temperature:");
  208.           client.println("</td><td>");//格線
  209.           number = webnumber (ten);
  210.           client.println("<img src ='" + number + "'>");
  211.           number = webnumber (num);
  212.           client.println("<img src ='" + number + "'>");
  213.           client.println("</td><td>");
  214.           //顯示濕度*********************************************************************************
  215.           //client.println("<tr><td>");
  216.           client.println("<font color='blue' size='7'>humidity:");
  217.           client.println("</td><td>");
  218.           //client.println(humidity);client.println("</font>");
  219.           //client.println("<br />");
  220.           number = webnumber (hten);
  221.           client.println("<img src ='" + number + "'>");
  222.           number = webnumber (hnum);
  223.           client.println("<img src ='" + number + "'>");
  224.           client.println("</td></tr>");
  225.           client.println("<tr><td>");
  226.           //光罩**********************************************************************************
  227.           client.println("<font color='blue' size='7'>Mask degrees:");  
  228.           client.println("</td><td>");
  229.           number = webnumber (mten);
  230.           client.println("<img src ='" + number + "'>");
  231.           number = webnumber (mnum);
  232.           client.println("<img src ='" + number + "'>");
  233.           client.println("</td><td>");
  234.           //client.println("<tr><td>");
  235.           //電力*********************************************************************************
  236.           client.println("<font color='blue' size='7'>Battery:");   
  237.           client.println("</td><td>");
  238.           number = webnumber (bten);
  239.           client.println("<img src ='" + number + "'>");
  240.           number = webnumber (bnum);
  241.           client.println("<img src ='" + number + "'>");
  242.           client.println("</td></tr>");
  243.           client.println("</table>");
  244.           //*********************

  245.           // print all the files, use a helper to keep it clean
  246.           client.println("<h2>Files:</h2>");
  247.           ListFiles(client, LS_SIZE);
  248.         } else if (strstr(clientline, "GET /") != 0) {
  249.           // this time no space after the /, so a sub-file!
  250.           char *filename;

  251.           filename = clientline + 5; // look after the "GET /" (5 chars)
  252.           // a little trick, look for the " HTTP/1.1" string and
  253.           // turn the first character of the substring into a 0 to clear it out.
  254.           (strstr(clientline, " HTTP"))[0] = 0;

  255.           // print the file we want
  256.           Serial.println(filename);

  257.           if (! file.open(&root, filename, O_READ)) {
  258.             client.println("HTTP/1.1 404 Not Found");
  259.             client.println("Content-Type: text/html");
  260.             client.println();
  261.             client.println("<h2>File Not Found!</h2>");
  262.             break;
  263.           }

  264.           Serial.println("Opened!");

  265.           client.println("HTTP/1.1 200 OK");
  266.           client.println("Content-Type: text/plain");
  267.           client.println();

  268.           int16_t c;
  269.           while ((c = file.read()) > 0) {
  270.               // uncomment the serial to debug (slow!)
  271.               //Serial.print((char)c);
  272.               client.print((char)c);
  273.           }
  274.           file.close();
  275.         } else {
  276.           // everything else is a 404
  277.           client.println("HTTP/1.1 404 Not Found");
  278.           client.println("Content-Type: text/html");
  279.           client.println();
  280.           client.println("<h2>File Not Found!</h2>");
  281.         }
  282.         break;
  283.       }
  284.     }
  285.     // give the web browser time to receive the data
  286.     delay(1);
  287.     client.stop();
  288.   }
  289. }
  290. String webnumber(int xx)
  291. {
  292.    String N;
  293.    switch (xx)
  294.    {
  295.      case 0:
  296.      N = "http://13p.imghost.us/vl/0.png";
  297.      return (N);
  298.      break;
  299.      case 1:
  300.      N = "http://13q.imghost.us/lb/1.png";
  301.      return (N);
  302.      break;
  303.      case 2:
  304.      N = "http://13q.imghost.us/oB/2.png";
  305.      return (N);
  306.      break;
  307.      case 3:
  308.      N = "http://13q.imghost.us/Iv.png";
  309.      return (N);
  310.      break;
  311.      case 4:
  312.      N = "http://13q.imghost.us/I9.png";
  313.      return (N);
  314.      break;
  315.      case 5:
  316.      N = "http://13q.imghost.us/bU.png";
  317.      return (N);
  318.      break;
  319.      case 6:
  320.      N = "http://13q.imghost.us/eVx.png";
  321.      return (N);
  322.      break;
  323.      case 7:
  324.      N = "http://13q.imghost.us/8i.png";
  325.      return (N);
  326.      break;
  327.      case 8:
  328.      N = "http://13q.imghost.us/Ra.png";
  329.      return (N);
  330.      break;
  331.      case 9:
  332.      N = "http://13q.imghost.us/sL.png";
  333.      return (N);
  334.      break;
  335.    }
  336. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-6-4 00:28 , Processed in 0.038155 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表