2017-02-09 127 views
1

我正在使用帶有脈衝傳感器的NodeMCU「ESP8266」,並且希望將模擬數據發送到XAMPP服務器上的數據庫。WifiClient未連接到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 ,但它不工作,我得到了這樣的結果:

enter image description here

那該怎麼辦?

+3

無法連接從另一臺計算機到本地主機! – leetibbett

+0

因此,如何使用xampp服務器遠程連接,您可以幫助我! @leetibbett –

+0

把它的IP地址代替localhost。您也可能需要在xampp機器上打開防火牆以允許網絡流量。 – leetibbett

回答

0

好像你正在一個不允許的opperation:

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