Arduino Can';t从esp8266接收数据
我试图使用esp8266和aruino uno从HTML页面接收密码,在代码中,串行监视器读取信息,如果正确,则闪烁led,但我无法正确接收此信息 这里是esp8266的代码,其中是HTML页面、密码请求和服务器Arduino Can';t从esp8266接收数据,arduino,esp8266,arduino-esp8266,esp8266wifi,Arduino,Esp8266,Arduino Esp8266,Esp8266wifi,我试图使用esp8266和aruino uno从HTML页面接收密码,在代码中,串行监视器读取信息,如果正确,则闪烁led,但我无法正确接收此信息 这里是esp8266的代码,其中是HTML页面、密码请求和服务器 #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> // WiFi network const char* ssid = "Rede Wi-Fi de Leonard"; c
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// WiFi network
const char* ssid = "Rede Wi-Fi de Leonard";
const char* password = "a01051970c";
ESP8266WebServer server ( 80 );
char htmlResponse[3000];
void handleRoot() {
snprintf ( htmlResponse, 3000,
"<!DOCTYPE html>\
<html lang=\"en\">\
<head>\
<style>\
body {background-color: rgb(250,250, 250);}\
h3 {color: red;text-align:center;}\
p {color: black; text-align:center;}\
div {color: black; text-align:center;}\
ID {text-align:center;}\
input {text-align:center;}\
</style>\
<meta charset=\"utf-8\">\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
</head>\
<body>\
<h3>\<canter>MACK HOTEL</canter>\</h3>\
<p>\<canter>Entre com sua Senha</canter>\</p>\
<div>SENHA: <input type='text' name='pass_word' id='pass_word' align='center' size=10 autofocus></div> \
<div>\
<br><button id=\"save_button\">ENTRAR</button>\
</div>\
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>\
<script>\
var pass;\
$('#save_button').click(function(e){\
e.preventDefault();\
pass = $('#pass_word').val();\
$.get('/save?pass=' + pass, function(data){\
console.log(data);\
});\
});\
</script>\
</body>\
</html>");
server.send ( 200, "text/html", htmlResponse );
}
void handleSave() {
if (server.arg("pass")!= ""){
Serial.println(server.arg("pass"));
}
}
void setup() {
// Start serial
Serial.begin(115200);
delay(10);
// Connecting to a 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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.on ( "/", handleRoot );
server.on ("/save", handleSave);
server.begin();
Serial.println ( "HTTP server started" );
}
void loop() {
server.handleClient();
}
谢谢大家的支持 在比较之前,请尝试stringComplete.trim()
。它将删除\r\n
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(115200);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
pinMode(13,OUTPUT);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
if (inputString=="123456")
{
digitalWrite(13,HIGH);
delay(300);
digitalWrite(13,LOW);
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}