2015-04-28 54 views
-1

尋求建議,讓arduino和ESP8266 wifi模塊讀取網頁上的PHP文件(不是LAN;我使用域名和託管服務的網頁),回聲' 1「或」0「。如果是'1',我正在查看是否打開LED,如果是'0',則將其關閉。ESP8266 wifi模塊讀取PHP文件

例如PHP文件看起來像這反過來又導致對: <?php echo 1; ?>

我需要能夠讀出PHP文件打開LED上。在這種情況下,最好的方法是什麼?將HTTP GET請求發送到ESP8266 wifi模塊的IP地址會更好嗎?還是有辦法對模塊進行編程以從PHP文件讀取回顯數據?有沒有另一個WiFi模塊,會讓這更容易?

如果我還沒有讓自己清楚,或者您需要進一步的信息來通知我請讓我知道。

在此先感謝!

+0

PHP文件看起來像這樣<?php echo 1; ?> –

+0

歡迎來到SO。清楚的是,你有一個叫做PHP頁面的Arduino。根據PHP頁面的結果,您希望LED打開或關閉。你傳遞給PHP頁面的信息是什麼?什麼情況?另請發佈您的Arduino代碼和PHP代碼示例。 – Twisty

回答

0

我會建議使用來自Arduino的HTTP GET請求。根據您的堆棧代碼,如果未設置DNS,它可能無法解析域名。所以我會建議使用IP,除非您知道它可以將您的域名解析爲正確的IP。你可以看到更多的WebClient的例子:http://www.arduino.cc/en/Tutorial/WebClient

// if you get a connection, report back via serial: 
    if (client.connect(server, 80)) { 
    Serial.println("connected"); 
    // Make a HTTP request: 
    client.println("GET /arduino.php?led=1 HTTP/1.1"); 
    client.println("Host: www.yourwebsite.com"); 
    client.println("Connection: close"); 
    client.println(); 
    } 
    else { 
    // kf you didn't get a connection to the server: 
    Serial.println("connection failed"); 
    } 
在循環

然後,你看看正確的響應(假設LEDPIN已設置定義):然後

void loop() 
{ 
    // if there are incoming bytes available 
    // from the server, read them and print them: 
    if (client.available()) { 
    char c = client.read(); 
    if(c == 1){ 
     digitalWrite(LEDPIN, HIGH); 
    } else { 
     digitalWrite(LEDPIN, LOW); 
    } 
    Serial.print(c); 
    } 

    // if the server's disconnected, stop the client: 
    if (!client.connected()) { 
    Serial.println(); 
    Serial.println("disconnecting."); 
    client.stop(); 

    // do nothing forevermore: 
    while(true); 
    } 
} 

的PHP可以做是這樣的:

<?php 

if(isset($_GET['led']) && $_GET['led']){ 
    // LED is on, send 0 to turn it off 
    echo "0"; 
} else { 
    // Turn LED on 
    echo "1"; 
} 

?> 

所以頁面始終顯示爲0,除非你傳遞一個led傳遞和條件都得到滿足。

如果您需要更多信息或更清晰的回覆,請更新您的問題並提供更多詳細信息。發佈您的代碼。