// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address on serial monitor
Serial.print("Use this URL to connect: ");
Serial.print("http://"); //URL IP to be typed in mobile/desktop browser
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);
}
// Read the first line of the request
String request = client.readStringUntil('r');
Serial.println(request);
client.flush();
// Match the request
float percentage = 0.0;
int value = LOW;
if (request.indexOf("/Up=ON") != -1) {
//Analog pin reading output voltage by water moisture rain sensor
float reading = analogRead(Raw);
percentage = (reading/1024) * 100; //Converting the raw value in percentage
value = HIGH;
}
// 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("");
client.println("");
client.println("
}
在代码中,可见的标题和按钮是由于在 nodemcu arduino ide 代码中出现的 html 代码。只需制作以上威廉希尔官方网站
并下载网络通信部分的代码即可。下载代码后,从 arduino ide 以115200bps 波特率打开串行监视器。你会看到 nodemcu 正在连接到你的路由器,启动它的服务器,在服务器初始化之后,它会显示在屏幕上。这个地址或 IP 是显示土壤湿度状态的页面。IP 看起来就像这样。人们必须在他/她的移动电话,台式机,笔记本电脑或笔记本电脑的浏览器中输入这个 IP,以查看花园土壤湿度的状况。最终的结果是像下面这样的东西