首页 > 解决方案 > 将 nodemcu 与 000webhost 数据库连接

问题描述

我想将 nodemcu 与 000webhost 数据库连接。因为我已经在我的网站文件管理器上构建了 php 文件。当我通过网络浏览器发出 http 请求时,它可以正常工作。意味着它可以正常执行其功能。但是每当我尝试通过 nodemcu 发出 http 请求时,它都不起作用!

我的nodemcu代码:

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#define WIFI_SSID "imayank2"
#define WIFI_PASSWORD "123456789"


void setup() {

Serial.begin(74880);
  wifiConnect();

}

void loop()
{ 


 HTTPClient http;  //Declare an object of class HTTPClient

http.begin("mywebsite address\dataenter.php");  //Specify request destination
int httpCode = http.GET();                                                                  //Send the request

if (httpCode > 0) { //Check the returning code

String payload = http.getString();   //Get the request response payload
Serial.println(payload);                     //Print the response payload

}

http.end();   //Close connection



  delay(5000);

}


void wifiConnect()
{
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);
  Serial.println(" ...");
  int teller = 0;
  while (WiFi.status() != WL_CONNECTED)
  {                                       // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++teller);
    Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer
}

输出: nothing only connected with my wifi , not payload

标签: chttparduinoiotnodemcu

解决方案


Web 服务器正在使用虚拟主机。除非您告诉它您向哪个虚拟主机发出请求,否则服务器将不知道将请求发送到哪里并生成错误。

您需要添加所需的 header Host,它就是这样告诉它的。

client.println("Host: ADDRESS.000webhostapp.com");

但是我不知道如何使用您的库来做到这一点,所以这是我的解决方案:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

//Setting Wifi information
const char* ssid     = "SSID";       //your SSID 
const char* password = "PASSWORD";   //your Password

WiFiClient client;

void setup() {
  // initialization of communication via serial line at 9600 baud
  Serial.begin(9600);
  
  //connect to wifi
  WiFi.begin(ssid, password);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED){   
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());

}

void loop() {
  //connect to server
   if (client.connect("ADDRESS.000webhostapp.com", 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    Serial.print("GET /index.php?tempV=20.1");
    client.print("GET /index.php?tempV=20.1");     //YOUR URL
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: ADDRESS.000webhostapp.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  // end client connection
  client.stop();
}

推荐阅读