如何解决排球请求到本地ESP8266
我正在尝试创建一些将保持本地连接的设备。但是我想创建一个应用程序,当我连接到家庭WiFi时可以控制此设备。
要控制我正在使用Node MCU的设备,以便可以使用ESP8266模块连接到WiFi,请从现在开始将其称为ESP。目前,我只是想让卡上的集成LED闪烁以对其进行测试,当我通过Chrome App进行一些HTTP请求时,它可以工作。我正在使用ESP8266WebServer库接收和响应HTTP请求。
我的想法是,使用按钮向我的ESP的本地IP(192.168.1.57)发送HTTP请求的按钮来制作应用程序很容易,所以我下载了Android Studio,并制作了一个按钮来触发请求。为此,我正在使用Volley库,该库对于我正在尝试的内容似乎很完美,并且该应用程序在请求https://www.google.com时可以正常工作。
我的问题是它可以发出请求,但无法发送到我的ESP服务器(http://192.168.1.57)。
我看到有人建议将ESP IP设置在盒子的端口上,这样我就可以通过Internet到达我的ESP,但我想留在本地。
以下是代码:
- 节点MCU V3(ESP 8266)
#include <Arduino.h>
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
/* VARIABLES */
/* Server */
char name[] = "FlowerPot";
char password[] = "xxxxxxxx";
int port = 80;
ESP8266WebServer server(port);
int request = 0;
/* Output */
int led1 = LED_BUILTIN; // Led de la carte,Pin D4,est allumé pour 0 et éteinte pour 1
int servo_pin = 2; // Variable pin PWM servo,ici Pin D4
Servo servo1; // Variable servo
/* FUNCTIONS DECLaraTION */
void handleRoot();
void handleLED();
void handleNotFound();
/* CODE */
void setup() {
/* Server setup */
Serial.begin(9600);
WiFiManager wifiManager;
//se connecte au WIFI
if (!wifiManager.autoConnect(name,password)) {
Serial.println("Failed to connect and hit timeout");
//reset and try again,or maybe put it to deep sleep
ESP.reset();
delay(1000);
}
Serial.println("Connected!");
//demarre le serveur
server.on("/",handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
server.on("/LED",handleLED); // Call the 'handleLED' function quand on appelle l'URL avec "/LED"
//server.on("/LED",HTTP_POST,handleLED); // Call the 'handleLED' function when a POST request is made to URI "/LED"
server.onNotFound(handleNotFound); // When a client requests an unkNown URI (i.e. something other than "/"),call function "handleNotFound"
server.begin(); // Actually start the server
Serial.println("HTTP server started");
/* Blink setup */
pinMode(led1,OUTPUT);
/* Servo setup *//*
servo1.attach(servo_pin); */
}
void loop() {
/* Server */
server.handleClient(); // Listen for HTTP requests from clients
/* Blink */
if(request){
digitalWrite(led1,0);
delay(1000);
digitalWrite(led1,1);
delay(4000);
}
/* Servo controle *//*
servo1.write(10);
delay(1000);
servo1.write(90);
delay(1000);
servo1.write(170);
delay(1000); */
}
void handleRoot() {
//server.send(200,"text/html","<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>");
server.send(200,"Root");
digitalWrite(led1,!digitalRead(led1));
}
void handleLED() { // If a POST request is made to URI /LED
digitalWrite(led1,!digitalRead(led1)); // Change the state of the LED
/* server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect */
server.send(200,"LED");
}
void handleNotFound(){
server.send(404,"text/plain","404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}
- Android App主要活动
package com.example.flowerpotapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolBox.StringRequest;
import com.android.volley.toolBox.Volley;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Button myButton = (Button)findViewById(R.id.button);
final TextView myTextView = (TextView)findViewById(R.id.errors);
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://192.168.1.57";
//String url ="https://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET,url,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// display the first 500 characters of the response string.
myTextView.setText("Response is: "+ response.substring(0,500));
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
myTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
if(myButton.getText() == "OFF"){
myButton.setText("ON");
} else {
myButton.setText("OFF");
}
}
}
有些精确度,我用手机在Android 10上测试了OnePlus 7,并通过手机将其与ESP连接在同一WiFi上。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。