乐鑫技术交流
直播中

三心四意

9年用户 888经验值
私信 关注
[问答]

如何通过SD卡将TXT文件发送到网页ESP8266?

我目前正在构建一个能够将数据发送到网页的模块。我正在使用 MKR ZERO Arduino。基本上,到目前为止,我能够发送一个简单的HTML GET请求,该请求由php文件处理。然后,它将所有内容发送到 mySQL 数据库。GET请求从Arduino发送到网页。然后,php 文件执行其余的工作。首先,这是我的arduino代码的样子:

无效 sendSQL(){
  Serial1.println("AT+CIPSTART=1,"TCP","" + server + "",80");
  delay(200);
  if( Serial1.find("OK")){
    Serial.println("TCP connection ready");
   
    delay(200);

    String postRequest = "GET " + uri + "?location=" + "'" + location + "'" +
      "&sun_influence=" + "'" + sunInfluence + "'" + "&module_name=" + "'" + moduleName + "'" + "
      HTTP/1.1rnHost: " + server + "rnrn";
  
    Serial1.println("AT+CIPSEND=1," + String(postRequest.length()));  
    Serial.println("AT+CIPSEND=1," + String(postRequest.length()));  
    delay(200);
    if(Serial1.available()){
      if(Serial1.find(">")) {
        Serial.println("Sending..");
        Serial.print(postRequest);
        Serial1.print(postRequest);
        delay(200);
        if(Serial1.find("SEND OK")){
          Serial.println("Packet sent");
          Serial1.println("AT+CIPCLOSE=1");
          delay(200);           
        }
        else
          Serial.println("Packet cannot be sent");
      }
      else
        Serial.println("ERROR");
    }
    else
      Serial.println("Nothing received");
  }
  else
    Serial.println("TCP connection failed");   
}



php 文件看起来像这样:


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gestion_lux";

创建连接
$conn = 新的 mysqli($servername, $username,$password, $dbname);

检查连接
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$val1 = $_GET['location'];
$val2 = $_GET['sun_influence'];
$val3 = $_GET['module_name'];

$sql = "INSERT INTO room(location,sun_influence,module_name) VALUES ($val1,$val2,$val3);";

if ($conn->query($sql) === TRUE) {
    echo "Room parameters Saved Successfully!";
} else {
    echo "Error:" . $sql . "
" . $conn->error;
}

$conn->close();

?>



所有这些部分都运行良好!不幸的是,在我的情况下,我将无法使用此解决方案。事实上,这意味着在测试的每一端(大约 1 分钟)发送数据,这是一种能源浪费。在我的情况下,我的模块由 3.3V 电池供电。我的想法是在我的 MKR ZERO 的 SD 卡中收集尽可能多的数据,例如,每 30 分钟一次发送所有内容。这样,我就会浪费更少的能源。

问题是我在HTTP / Php处理方面不是那么好,这就是为什么我寻求帮助。我的想法是在我的SD卡中创建一个文本文件(我已经知道如何做到这一点),并在30分钟后将其发送到网页。是否可以通过 HTML 请求来做到这一点?那么,我应该如何处理php文件中的数据呢?php 应该能够将每个测试每行发送到数据基线行。也许我的解决方案并不聪明,所以我愿意听取任何建议!

回帖(1)

丁冬芹

2024-7-19 17:25:17
要通过SD卡将TXT文件发送到网页ESP8266,您可以按照以下步骤操作:

1. 确保您的Arduino MKR ZERO已连接到SD卡模块。

2. 在Arduino IDE中安装SD库。您可以在Arduino IDE的库管理器中搜索并安装它。

3. 初始化SD卡和ESP8266模块。在您的Arduino代码中添加以下代码:

```cpp
#include
#include
#include

const char* ssid = "您的WiFi名称";
const char* password = "您的WiFi密码";
const char* server = "您的服务器地址";
const int port = 80;

File file;
```

4. 在`setup()`函数中添加以下代码以连接WiFi和SD卡:

```cpp
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  if (!SD.begin(4)) { // 确保使用正确的引脚连接SD卡
    Serial.println("SD card initialization failed!");
    return;
  }
}
```

5. 读取TXT文件并将其内容发送到网页。在`loop()`函数中添加以下代码:

```cpp
void loop() {
  if (SD.open("data.txt", FILE_READ)) { // 确保使用正确的文件名
    while (file.available()) {
      char c = file.read();
      Serial1.write(c);
    }
    file.close();
  }

  sendSQL();
  delay(60000); // 每60秒发送一次
}
```

6. 修改您的`sendSQL()`函数以发送POST请求:

```cpp
void sendSQL() {
  HTTPClient http;
  http.begin("http://" + String(server) + "/path/to/your/php_file.php"); // 替换为您的PHP文件路径
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");

  int httpCode = http.POST("data=" + String(Serial1.readString()));

  if (httpCode > 0) {
    Serial.printf("HTTP Response code: %dn", httpCode);
  } else {
    Serial.printf("HTTP Request failed, error: %sn", http.errorToString(httpCode).c_str());
  }

  http.end();
}
```

7. 确保您的PHP文件可以处理POST请求并将其数据保存到MySQL数据库中。

通过这些步骤,您应该能够通过SD卡将TXT文件发送到网页ESP8266。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分