[英]sending local wifi ssid and password to esp32 from android without configuration
[英]Trying to send data over WiFi to an ESP32 from an Android app - Data seemingly not sending
我对 ESP32(arduino IDE)上的 C++ 比我对 Android 应用程序的 Java 更满意,所以我认为问题在于 Java 代码实际上没有发送任何数据。
这是预期的行为:
我今天制作了 APK 并将其安装在 android 手机上,启动了 ESP32 并检查了串行监视器以确保 IP 与我在 Java 代码中指定的相同,端口 (80) 也相同。
我从我的 Android 设备和 go 连接到 ESP32 回到应用程序,按下按钮,没有任何反应。 ESP32 的 Arduino IDE 中的串行监视器也没有显示任何内容(它应该打印接收到的数据)。
所以我将粘贴 Java 代码,我认为这可能是错误所在。 如您所见,我正在尝试将“Hello ESP32”作为字符串发送。
package com.benledmatrix.esp32controloverwifi;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up a button to send data when clicked
Button sendButton = findViewById(R.id.send_button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendData();
}
});
}
private void sendData() {
// Set up a thread to send the data in the background
new Thread(new Runnable() {
@Override
public void run() {
try {
// Connect to the ESP32's IP address and port
Socket socket = new Socket("192.168.4.1", 80);
PrintWriter out = new PrintWriter(socket.getOutputStream());
// Send the data
out.println("Hello, ESP32!");
out.flush();
// Close the connection
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
这是 ESP32 上的 Arduino C++ 代码
#include <WiFi.h>
const char* ssid = "ESP32 HotSpot";
const char* password = "password";
WiFiServer server(80); // Create a server on port 80
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
server.begin(); // Start the server
pinMode(2, OUTPUT); // Set onboard LED as an output
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a client connects
String data = client.readStringUntil('\n'); // Read the data from the client
Serial.println(data); // Print the data to the serial monitor
if (data == "Hello, ESP32!") { // If the data is "Hello, ESP32!"
// Flash the onboard LED twice
for (int i = 0; i < 2; i++) {
digitalWrite(2, HIGH);
delay(200);
digitalWrite(2, LOW);
delay(200);
}
} else {
// Flash the onboard LED once
digitalWrite(2, HIGH);
delay(200);
digitalWrite(2, LOW);
delay(200);
}
client.stop(); // Disconnect the client
}
}
以防万一我是一个完整的 div 这里是按钮的 XML 代码:D 你永远不知道
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#008080"
android:text="Send Data" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
对于这样的转储感到抱歉,但由于我对 Android 没有那么多经验,所以我认为最好将所有内容都放在那里。 它编译得很好,没有发现错误,但只是没有执行所需的操作。
如果有人有想法,我会尝试。 非常感谢本
经过大量研究,我有一个功能代码,当按下 Android 应用程序上的“发送数据”按钮时,它会闪烁 ESP32 的板载 LED 两次,表示它已收到字符串。 很高兴!
最后的原因是我没有任何使用 inte.net 的权限。 我使用了 USB 调试并输入了一些 log.d,它告诉我我没有使用 inte.net 的权限,所以输入该权限和一些 toast 消息来检查权限修复它。
C++ 和 XML 代码保持不变,但在 Android XML 清单文件中我添加了:
<uses-permission android:name="android.permission.INTERNET" />
而最终的Java代码如下:
package com.benledmatrix.esp32controloverwifi;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check for Internet permission for SDK 23 or higher
if (ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.INTERNET)) {
//rationale to display why the permission is needed
Toast.makeText(this, "The app needs access to the Internet to send data to the ESP32", Toast.LENGTH_SHORT).show();
}
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET}, PERMISSION_REQUEST_CODE);
}
// Set up a button to send data when clicked
Button sendButton = findViewById(R.id.send_button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendData();
}
});
}
//Check for result of permissions and feedback accordingly
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, you can perform the network operation here
Log.d("Permission", "Granted");
} else {
// Permission denied, show a message to the user
Log.d("Permission", "Denied");
Toast.makeText(this, "Permission denied, you cannot perform network operations", Toast.LENGTH_SHORT).show();
}
}
}
private void sendData() {
// Set up a thread to send the data in the background
new Thread(new Runnable() {
@Override
public void run() {
try {
// Connect to the ESP32's IP address and port
Log.d("Sending Data", "Button Pressed, connecting to ESP32.....");
Socket socket = new Socket("192.168.4.1", 80);
PrintWriter out = new PrintWriter(socket.getOutputStream());
// Send the data
out.println("Hello, ESP32!");
out.flush();
Log.d("Sending Data", "Data Sent!");
// Close the connection
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("Sending Data", "Error Sending Data: "+e.getMessage());
}
}
}).start();
}
}
当然,任何关于我可能不需要的事情的进一步建议或我可以做出的改进都会很棒。
非常感谢本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.