将nodemcu与000webhost数据库连接

将nodemcu与000webhost数据库连接,c,http,arduino,iot,nodemcu,C,Http,Arduino,Iot,Nodemcu,我想将nodemcu与000webhost数据库连接。为此,我在我的网站文件管理器上构建了php文件。当我通过web浏览器发出http请求时,它可以正常工作。这意味着它可以正常执行其功能。但是,每当我试图通过nodemcu发出http请求时,它都不起作用 我的nodemcu代码: #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include &l

我想将nodemcu与000webhost数据库连接。为此,我在我的网站文件管理器上构建了php文件。当我通过web浏览器发出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
}
#包括
#包括
#包括
#包括
#定义WIFI_SSID“imayank2”
#定义WIFI_密码“123456789”
无效设置(){
序列号。开始(74880);
wifiConnect();
}
void循环()
{ 
HTTPClient http;//声明HTTPClient类的对象
http.begin(“mywebsite address\dataenter.php”);//指定请求目的地
int httpCode=http.GET();//发送请求
如果(httpCode>0){//请检查返回的代码
字符串负载=http.getString();//获取请求-响应负载
Serial.println(负载);//打印响应负载
}
http.end();//关闭连接
延迟(5000);
}
void wifiConnect()
{
WiFi.begin(WiFi\u SSID,WiFi\u密码);//连接到网络
串行打印(“连接到”);
串行打印(WIFI_SSID);
Serial.println(“…”);
int-teller=0;
while(WiFi.status()!=WL_已连接)
{//等待Wi-Fi连接
延迟(1000);
串行打印(++出纳员);
序列号。打印(“”);
}
Serial.println('\n');
Serial.println(“已建立连接!”);
串行打印(“IP地址:\t”);
Serial.println(WiFi.localIP());//将ESP8266的IP地址发送到计算机
}
输出:
没有任何东西只与我的wifi连接,没有有效负载

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

您需要添加所需的头
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();
}
#包括
#包括
//设置Wifi信息
const char*ssid=“ssid”//你的SSID
const char*password=“password”//你的密码
无线客户端;
无效设置(){
//以9600波特通过串行线进行通信初始化
Serial.begin(9600);
//连接到wifi
WiFi.begin(ssid,密码);
串行打印(“连接”);
而(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
Serial.println(“”);
串行打印(“连接到IP地址为:”)的WiFi网络;
Serial.println(WiFi.localIP());
}
void循环(){
//连接到服务器
if(client.connect(“ADDRESS.000webhostapp.com”,80)){
Serial.println(“已连接”);
//发出HTTP请求:
Serial.print(“GET/index.php?tempV=20.1”);
client.print(“GET/index.php?tempV=20.1”);//您的URL
client.print(“”;//HTTP/1.1前的空格
client.print(“HTTP/1.1”);
client.println();
client.println(“主机:ADDRESS.000webhostapp.com”);
client.println(“连接:关闭”);
client.println();
}否则{
//如果您没有连接到服务器:
Serial.println(“连接失败”);
}
//终端客户端连接
client.stop();
}

如果您分享您的输出是什么,预期输出是什么,这会有所帮助。