2017-02-09 8 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 canyou помочь мне, пожалуйста! @leetibbett –

+0

Поместите его IP-адрес в свой код вместо localhost. Вам также может потребоваться открыть брандмауэр на компьютере xampp, чтобы разрешить веб-трафик. – leetibbett

ответ

0

Похоже, что вы делаете недозволенные opperation:

const char* host = "http://localhost/mysql0.php?value"+ value; 
Смежные вопросы