NodeMCU从webswerver获取数据并更新值

时间:2018-07-16 07:16:03

标签: nodemcu arduino-esp8266

我在arduino IDE中使用Nodenmcu。我已使用DHT11 IC读取温度和湿度。现在我想在名为set_temp的网页中添加参数。将Set_temp值设置为value时,按下更改图标后,该值应会更新。这是我的代码。我的代码一直有效,直到从Web输入文本为止,但它不会更新set_temp值

#include <ESP8266WiFi.h>
#include "DHT.h"

static float Set_Temp;
DHT dht;
int value = LOW;

const char* ssid = "esp8266";
const char* password = "Test123456";

int ledPin = 13; // GPIO13
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  dht.setup(D3);   /* D1 is used for data communication */
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }


// New code has been added 

 delay(dht.getMinimumSamplingPeriod());  /* Delay of amount equal to sampling period */

  float humidity = dht.getHumidity(); /* Get humidity value */
  float temperature = dht.getTemperature(); /* Get temperature value */

 // Serial.print(dht.getStatusString());  /* Print status of communication */

if(temperature>=Set_Temp)
{

  digitalWrite(ledPin, HIGH);
    value = HIGH;

}else
{
  digitalWrite(ledPin, LOW);
    value = LOW;
}




  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  // Match the request


 /* if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }*/

// Set ledPin according to the request
//digitalWrite(ledPin, value);

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");

  client.print("Relay_Turn_On_Status: ");

  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
 // client.println("<br><br>");
 // client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
//  client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  
//  client.println("</html>");


client.print("</html>");
client.print("<head>");
client.print("<title>My Page</title>");
client.print("</head>");
client.print("<body>");
  client.print("<br><br>");
   client.print("Set_Temp: ");
    client.println("<input type=text name=textbox size=5 value=Enter_Temp_Here");
    client.println("<br><input type=submit value=Change ><br>");
    client.println("</div>");
     client.println("</body>");
     client.println("</html>");

Set_Temp:"<br><input type=submit value=Change ><br>";
//Set_Temp:"<input type=text name=textbox size=5 value=Enter_Temp_Here>";
Serial.println(Set_Temp);



client.println("<br><br>");
  client.println("DHT11_HumidityReading: ");
  client.println(humidity,1);
   client.println("<br><br>");
  client.println("DHT11_Temprature Reading: ");
  client.println(temperature,1);
   client.println("<br><br>");

  client.println("Set_Temp: ");
  client.println(Set_Temp);
   client.println("<br><br>");


  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");

}

我对这部分代码的主要怀疑是,一旦我阅读它,就不会更新。

 Set_Temp:"<br><input type=submit value=Change ><br>";
    //Set_Temp:"<input type=text name=textbox size=5 value=Enter_Temp_Here>";
    Serial.println(Set_Temp);

如果我这样使用。它会显示-1

Set_Temp=readString.indexOf(2);

我遵循了从HTTP https://www.w3schools.com/htmL/[Serial此处][web page]进行获取和发布的方法

如何从文本框中更改我的Set_Temp值。

1 个答案:

答案 0 :(得分:0)

看一下这篇文章,我跟随它使用Arduino IDE打开和关闭NodeMCU中的灯。网络中的客户端是使用PHP编写的。

https://blog.nyl.io/esp8266-led-arduino/

<?php
$light = $_GET['light'];
if($light == "on") {
  $file = fopen("light.json", "w") or die("can't open file");
  fwrite($file, '{"light": "on"}');
  fclose($file);
} 
else if ($light == "off") {
  $file = fopen("light.json", "w") or die("can't open file");
  fwrite($file, '{"light": "off"}');
  fclose($file);
}
?>

希望对您有帮助。

致谢