我正在使用带有脉冲传感器的nodemcu“esp8266”,我想将模拟数据发送到xampp服务器上的数据库。
我在WiFi客户端使用了以下代码:

#include <ESP8266WiFi.h>
const char* ssid     = "tabark";
const char* password = "tabarekghassan";
int value;
const char* host = "http://localhost/mysql0.php?value"+ value;

void setup() {
    Serial.begin(115200);
    delay(10);

    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by           default,
    would try to act as both a client and an access-point and could cause
    network-issues with your other WiFi-devices on your WiFi-network. */
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);

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

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


void loop() {
    delay(5000);
    value=analogRead(A0);
    Serial.print(value);

    Serial.print("connecting to ");
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
    }

    // We now create a URI for the request
    String url = "/input/";


    url += "&value=";
    url += value;

    Serial.print("Requesting URL: ");
    Serial.println(url);

    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
           "Host: " + host + "\r\n" +
           "Connection: close\r\n\r\n");
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout > 5000) {
            Serial.println(">>> Client Timeout !");
            client.stop();
            return;
        }
    }

    // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
        String line = client.readStringUntil('\r');
        Serial.print(line);
    }

    Serial.println();
    Serial.println("closing connection");
}

我用这个做数据库:
<?php

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

$value = $_GET["value"];

$conn = mysql_connect($servername, $username, $password);
if ($conn) {
    echo "Connected successfully";
}
else {
    echo "connection failed";
}

$conndb = mysql_select_db('database', $conn);

echo  "<br>";

$sql_insert ="insert into pulsesensor(value) values ('$value')";


if($sql_insert){
    echo "insert successfull";
}
else {
    echo "insert failed";
}
echo  "<br>";

$result = mysql_query($sql_insert);
if($result){
    echo "insert successfull";
}
else {
    echo "insert failed" . mysql_error($result);
}
?>

当我将该值放入url时,该值将保存在数据库中,但它不起作用,我得到了以下结果:
http - WifiClient未连接到XAMPP服务器-LMLPHP
那该怎么办?

最佳答案

你好像在做一个不允许的反驳:

const char* host = "http://localhost/mysql0.php?value"+ value;

10-08 04:47