정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

아두이노 와이파이 쉴드에서의 sd카드 읽고 쓰기 문제입니다.
김덕수 | 2016-08-30

와이파이 웹서버를 구축하였는데요. 그 소스에 sd 카드에 읽고 쓰기를 하려하였으나 단순 예제 합치기임에도 불구하고

업로드시 와이파이 쉴드를 인식하지 못하는 문제가 생겼습니다. 일단 소스를 보시는게 나을것 같아 소스를 올립니다.

어떠한 문제가 있는지 잘모르겠습니다. 왜일까요 ㅠㅠ..

​ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
#include <SPI.h>
#include <WiFi.h>
#include <SD.h>
File myFile;
char ssid[] = "dlink-B180";      // your network SSID (name)
char pass[] = "ivmdu26002";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue:
    while (true);
  }
  Serial.print("Initializing SD card...");//
  if (!SD.begin(4)) {//
    Serial.println("initialization failed!");//
    return;//
  }//
  Serial.println("initialization done.");//
  String fv = WiFi.firmwareVersion();
  if (fv != "1.1.0") {
    Serial.println(F("Please upgrade the firmware"));
  }
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}
void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println(F("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);
        savedata(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(F("HTTP/1.1 200 OK"));
          client.println(F("Content-Type: text/html"));
          client.println(F("Connection: close"));  // the connection will be closed after completion of the response
          client.println(F("Refresh: 5"));  // refresh the page automatically every 5 sec
          client.println();
          client.println(F("<!DOCTYPE HTML>"));
          client.println(F("<html>"));
          client.print(F("Useing :"));
          client.println(c);
          client.println(F("<br />"));
          client.println(F("<br />"));
          client.println(F("</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(F("client disonnected"));
  }
}
void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print(F("IP Address: "));
  Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print(F("signal strength (RSSI):"));
  Serial.print(rssi);
  Serial.println(F(" dBm"));
}
void savedata(char c){
  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile){
    Serial.print("Writing to test.txt...");
    myFile.println(c);
    myFile.close();
    Serial.println("done");
  }
  else {
    Serial.println("error opening test.txt");
  }
  myFile = SD.open("test.txt");
  if (myFile){
    Serial.println("test.txt:");
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  }
  else {
    Serial.println("error checking test.txt");
  }
}
 
이전글   |    아두이노 센서값을 이용한 컴퓨터 제어 가능여부에 대한 질문입니다.. ... 2016-08-28
다음글   |    인생에 "한번 더"란 없다 2016-08-30