簡體   English   中英

ESP8266 wifi模塊讀取PHP文件

[英]ESP8266 wifi module to read PHP file

尋求有關讓 arduino 和 ESP8266 wifi 模塊讀取網頁(不是 LAN;我正在使用網頁的域名和托管服務)上的 PHP 文件的建議,該文件回顯“1”或“0”。 如果它是“1”,我正在考慮打開 LED,如果是“0”,則將其關閉。

例如,打開 LED 的 PHP 文件如下所示: <?php echo 1; ?> <?php echo 1; ?>

我需要能夠讀取 php 文件才能打開 LED。 在這種情況下,最好的方法是什么? 向 ESP8266 wifi 模塊的 IP 地址發送 HTTP GET 請求是否更好,或者有沒有辦法對模塊進行編程以從 php 文件中讀取回顯數據? 是否有另一個 wifi 模塊可以使這更容易?

如果我沒有說清楚,或者您需要更多信息來告訴我,請告訴我。

提前致謝 !

我建議使用來自 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並且滿足了條件。

如果您需要更多信息或更明確的答復,請更新您的問題並提供更多詳細信息。 發布您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM