?>

Hard Copy World

HCW

Q&A

Home > Forum >

Q&A

arduino wifi shield을 이용해서 PHP로 값전송 (PHP정상작동)

페이지 정보

작성자 도라에몽 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 작성일16-05-22 23:51 조회4,190회 댓글1건

본문

아두이노에서 센서값을 읽어 와서 get method를 통해 보내는데 원래는 됬던 소스인데 갑자기 안되네요.

뭐가 문제죠 아무리봐도 모르겠습니다. php는 정상 작동 하는데 db로 값이 안넘오 오네요.

postDate()함수 부분에서 get method 방식으로 값을 보내 주고 있습니다.

---------------------------------------------

#include <SPI.h>
#include <WiFi.h>
#include <string.h>

char ssid[] = "------";      //  your network SSID (name)
char pass[] = "-------";   // your network password
char server[] = "---------";
char url[50] = "";

int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int speed2 = 5;
int dir2 = 4;
int delay_time = 1000;

int humidity_value = analogRead(A0);
int water_left = analogRead(A5);

char comd_temp[20]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false;

int status = WL_IDLE_STATUS;
WiFiClient client;
//dcmotersart
boolean toggle = true;

void setup() {
  // start serial port
  Serial.begin(9600);      // initialize serial communication
  pinMode(5, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (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(delay_time);
  }
  printWifiStatus();                       // you're connected now, so print out the status  
}
//setup END

void loop() {
  
  humidity_value = analogRead(A5);
  water_left = analogRead(A0);
  //  int num2 = analogRead(A2);

  //post date to android
  postData();
  client.flush();
  
  //get command android
  String command = getData();
  client.flush();

// command 
  if (command == "AutoSwitchOn") {
    delay(1000);
    if (humidity_value <= 300) {
      if (command == "SwitchOn") {
        Motor1(255, true);
        Serial.println("Motor Spinning ~~!!!");
        delay(86400000);
      } else {
        Motor1(0, false);
        Serial.println("Motor Stopped ~~!!!");
      }
    } else {
      if (command == "SwitchOff") {
        Motor1(255, true);
        Serial.println("Motor Spinning ~~!!!");
      } else {
        Motor1(0, false);
        Serial.println("Motor Stopped ~~!!!");
      }
    }

    if (humidity_value >= 650) {
      if (command == "SwitchOff") {
        Motor1(255, true);
        Serial.println("Motor Spinning ~~!!!");
      } else {
        Motor1(0, false);
        Serial.println("Motor Stopped ~~!!!");
      }
    } else {
      if (command == "SwitchOn") {
        Motor1(0, false);
        Serial.println("Motor Stopped ~~!!!");
      } else {
        Motor1(255, true);
        Serial.println("Motor Spinning ~~!!!");
      }
    }

     if(water_left<=300&&humidity_value>=500){
  //  if (water_left >= 300 && humidity_value >= 500) {
      Motor1(0, false);
      Serial.println("Motor Stopped ~~!!!");
    }
  }

  if (command == "ManualSwitchOn" ) {
    Motor1(255, true);
    Serial.println("Motor Spinning ~~!!!");
  }

  if (command == "ManualSwitchOff") {
    Motor1(0, false);
    Serial.println("Motor Stopped ~~!!!");
  }

  //    if (command == "LedOn") {
  //      Motor1(255,true);
  //    }

  //    if (command == "LedOff") {
  //      Motor1(0,false);
  //    }
   delay(delay_time);
} //loop END


void Motor1(int pwm, boolean reverse)
{
  analogWrite(speed2, pwm); //set pwm control, 0 for stop, and 255 for maximum speed
  if (reverse)
  {
    digitalWrite(dir2, HIGH);
  }
  else
  {
    digitalWrite(dir2, LOW);
  }
}
//dcmocterend

void printWifiStatus() {

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();

  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  Serial.println();
}//printWifiStatus END

void postData() {

  // If there's a successful connection, send the HTTP POST request
  if (client.connect(server, 8080)) {
    Serial.println("Send Data to DB Connecting...");
    Serial.print("URL : ");
    Serial.println(server);

//    String Data ;
//    Data = "humi=" ;
//    Data = Data + humidity_value ;
//    Data = Data + "&water=" ;
//    Data = Data + humidity_value;
    

    if (client.connected()) {
// post method 
//       client.println("POST /ardu_db.php HTTP/1.1");
//       client.print("HOST:" );
//       client.println(server);
//       client.println("Content-Type: application/x-www-form-urlencoded");
//       client.print("Content-Length: ");
//       client.println(Data.length());
//       client.println();
//       client.print(Data);
//
//       Serial.println("POST /ardu_db.php HTTP/1.1");
//       Serial.print("HOST:" );
//       Serial.println(server);
//       Serial.println("Content-Type: application/x-www-form-urlencoded");
//       Serial.print("Content-Length: ");
//       Serial.println(Data.length());
//       Serial.println();
//       Serial.println(Data);
//       Serial.println();    
    
          client.print("GET /ardu_db.php?");     
          client.print("humi=");     
          client.print(humidity_value);      
          client.print("&");  
          client.print("water=");      
          client.print(water_left);     
          client.println(" HTTP/1.1");      
          client.print("HOST: ");      
          client.println(server);   
          client.println("Connection: close"); 
          client.println();            
    
          Serial.print("GET /ardu_db.php?");
          Serial.print("humi=");
          Serial.print(humidity_value);
          Serial.print("&");
          Serial.print("water=");
          Serial.print(water_left);
          Serial.println(" HTTP/1.1");
          Serial.print("HOST: ");
          Serial.println(server);
          Serial.println("Connection: close");
          Serial.println();   
          
          client.stop();    
 //     delay(delay_time);
    }
  }
  else {
    // If you couldn't make a connection:
    Serial.println("Send Data to DB Connection failed");
    Serial.println("Disconnecting.");
     Serial.println();
    client.stop();  
  //  delay(delay_time);
  }
}

String getData() {  

  if (client.connect(server, 8080)) {
    Serial.println("Recive Data to DB Connecting...");
    Serial.print("URL: ");
    Serial.println(server);

    if (client.connected()) {
      client.println("GET /db_ardu.php HTTP/1.1");      
      client.print("HOST: ");     
      client.println(server);      
      client.println("Connection: close");      
      client.println();
      
      Serial.println("GET /db_ardu.php HTTP/1.1");
      Serial.print("HOST:");
      Serial.println(server);
      Serial.println("Connection: close");
      Serial.println();
           
      stringPos = 0;
      memset( &comd_temp, 0, 20 ); //clear inString memory
      
      while (true) {         
        char c = client.read();                                       
        if (c == '[' ) { //'[' is our begining character
          startRead = true; //Ready to start reading the part
        } else if (startRead) {
          if (c != ']') { //']' is our ending character
            comd_temp[stringPos] = c;            
            stringPos ++;
          } else {
            //got what we need here! We can disconnect now
            startRead = false;
            break;
          }
        }
      }                       
    }
     Serial.print("Recived Command : ");
     Serial.println(comd_temp);
     Serial.println();
  }
  else{
     // If you couldn't make a connection:
    Serial.println("Get Data to DB Connection failed");
    Serial.println("Disconnecting.");
    Serial.println();  
  }                  
  client.stop();   
  return comd_temp;     
}

 

 

 

댓글목록

최고관리자님의 댓글

최고관리자 쪽지보내기 메일보내기 홈페이지 자기소개 아이디로 검색 전체게시물 작성일

PC에서 브라우저로 데이터 전송 테스트 해보세요. 만약 문제 없고, 원래 되던 소스가 안되는 거라면 공유기 접속 등에 문제가 있을 수 있습니다. 그리고 URL 경로에 변수를 넣어 보내는 경우만 응답이 없는것 같은데... PHP 소스도 로그 찍어서 다시 확인해 보셔야 할듯