节点MCU获取未知数据

时间:2019-04-15 10:03:46

标签: php sql arduino esp8266 nodemcu

我正在尝试找到一种方法,可以将get请求发送到本地服务器。

听到我的代码

#include <ESP8266WiFi.h>

#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
const char* ssid     = "Jio-Fi 002";
const char* password = "password";

const char* host = "192.168.225.127";
int ledPin = 12;
int failPin = 14;


void setup() {

  pinMode(ledPin , OUTPUT);
  pinMode(failPin , OUTPUT);
  delay(10);
  digitalWrite(failPin , LOW);
  digitalWrite(ledPin , HIGH);
  // We start by connecting to a WiFi network



  delay(1000);

  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
  delay(1000);
  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot

  WiFi.begin(ssid, password);     //Connect to your WiFi router



  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    digitalWrite(failPin , HIGH);
    digitalWrite(ledPin , LOW);  
  }
  digitalWrite(ledPin , HIGH);
  digitalWrite(failPin , LOW);
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) { //works!
    digitalWrite(failPin , HIGH);
    digitalWrite(ledPin , LOW);

    return;

  }
   digitalWrite(ledPin , HIGH);
  digitalWrite(failPin , LOW);

  // We now create a URI for the request

  String url = "/Anet/Projects/Index.php";
  url += "?product_id=";
  url += "001";
  url += "&data=";
  url += "Some one is on the door !!";



  // 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");


    ESP.deepSleep(0); 
}
void loop () {

  }

我在这里找不到任何错误。

当服务器脱机时,失败指示灯会发光,但是当我启动服务器并尝试通过nodemcu发送数据时,失败指示灯会发光。

我尝试使用浏览器进行操作时,我的php代码没有错误,并且可以正常工作。

我对这一切完全陌生。

很抱歉出现语法错误。

非常感谢!

1 个答案:

答案 0 :(得分:0)

您应该在循环内调用一些代码或函数。 打开NodeMCU时,该设置仅运行一次。