android - ESP8266 与安卓通信
问题描述
这实际上是我第一次在这里提出问题,并且真的希望有人可以解决我的问题。
我一直在从事我的设计项目,其中一个功能是它可以从 ESP8266 NodeMCU 到 Android 进行通信,而无需 NodeMCU 连接到网络。所以这就像Android直接连接到Wifi模块,将NodeMCU设置为Access Point和WebServer。
我已经建立了一个简单的通信,但我希望它保持不变且同时进行,因为我正在使用此连接来显示从 NodeMCU 到 Android 的传感器读数。我的问题是,它不会不断地给出值,它只会给出第一个数据,然后什么也没有。
这是我在 Android 中的代码:
首先是客户端类
public class Client extends AsyncTask<Void, Void, String> {
String dstAddress;
int dstPort;
String response = "";
TextView textResponse;
Socket socket = null;
Client(String addr, int port, TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.textResponse = textResponse;
}
@Override
protected String doInBackground(Void... arg0) {
Log.d("Status_2", "Sample_2");
try {
if(socket == null)
{
socket = new Socket(dstAddress, dstPort);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
/*
* notice: inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
} /*finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//textResponse.setText(response);
return response;
}
@Override
protected void onPostExecute(String result)
{
Log.d("Status_1", "sample");
textResponse.setText(response);
super.onPostExecute(result);
}
}
这是来自主要活动
public class MainActivity extends Activity {
TextView response;
EditText editTextAddress, editTextPort;
Button buttonConnect, buttonClear, buttonStop;
int check = 0;
public static Client myClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAddress = findViewById(R.id.addressEditText);
editTextPort = findViewById(R.id.portEditText);
buttonConnect = findViewById(R.id.connectButton);
buttonClear = findViewById(R.id.clearButton);
buttonStop = findViewById(R.id.buttonStop);
response = findViewById(R.id.responseTextView);
myClient = new Client(editTextAddress.getText()
.toString(), Integer.parseInt(editTextPort
.getText().toString()), response);
buttonStop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
check = 0;
}
});
buttonConnect.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
check = 1;
myClient.execute();
}
});
buttonClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
response.setText("");
}
});
}
}
这是来自 Arduino 的代码:
int status = WL_IDLE_STATUS;
boolean alreadyConnected = false; // whether or not the client was connected
previously
WiFiServer server(8080);
void setup()
{
delay(1000);
Serial.begin(115200);
Serial.println();
// MotionSensor_Setup();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid,password);
delay(10000);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.println("\nStarting server...");
// start the server:
server.begin();
}
void loop()
{
// wait for a new client:
WiFiClient client = server.available();
// when the client sends the first byte, say hello:
if(client)
{
if (!alreadyConnected)
{
// clead out the input buffer:
client.flush();
Serial.println(analogRead(A0));
Serial.println();
client.println("Hello, client!");
client.println(analogRead(A0));
delay(500);
alreadyConnected = true;
}
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.write(thisChar);
}
}
最终,我在 void loop() 方法中尝试了此代码。
void loop()
{
// wait for a new client:
WiFiClient client = server.available();
// when the client sends the first byte, say hello:
while(client.connected())
{
client.println(analogRead(A0));
delay(1000);
Serial.println(analogRead(A0));
}
}
它进入了while条件并在串行监视器中显示数据,但未在Android中显示。
我已经尝试了几种技术,但仍然没有运气。
非常感谢您的回复,谢谢!
解决方案
你可以试试这个 ->非常简单的 TCP 客户端 我已经根据本手册做了我的 android 服务器,一切正常
推荐阅读
- python - 是否有创建 3D 对象/椭圆体的功能,其颜色透明度会根据中心的半径而变化(使用 python/matplotlib)?
- python-3.x - 如何将 MinMaxScaler feature_range 以相反的方式从正到负
- python - 为什么我们将 Mnist 训练图像重塑为 (60000,28,28,1) 而不是像这样直接使用 (60,28,28)?
- javascript - 未捕获的 RangeError:jquery 超出了最大调用堆栈大小
- r - R从Zip文件中的文件模式创建DataFrame
- r - 如何以递归方式获取 data.tree 对象的后代提示名称列表?
- logstash - ELK - Logstash 排除主机
- swift - 使用 .tag 快速更改 @IBoutlet 集合图像而不是图像
- javascript - 调用函数 onkeyup innerHTML
- mysql - SQL 如何找出谁没有条目?