我想通过套接字WiFi连接将Nodemcu ESP8266的数据(POT电压)发送到android设备。我确定我的代码可以在Nodemcu上工作。因为我用puTTY检查过它。我想发送一个值并在我的android应用中显示它。我的应用可以通过特定的IP和端口连接到Nodemcu,但在“文本视图”中不显示值。 请帮助我。
主要活动:
package com.examplewifisocket.omid.wifisocket;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView response;
EditText editTextAddress, editTextPort;
Button buttonConnect, buttonClear;
@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);
response = findViewById(R.id.responseTextView);
buttonConnect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Client myClient = new Client(editTextAddress.getText().toString(), Integer.parseInt(editTextPort.getText().toString()), response);
myClient.execute();
}
});
buttonClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
response.setText("");
}
});
}
}
Client.java:
package com.examplewifisocket.omid.wifisocket;
import android.os.AsyncTask;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends AsyncTask<Void, Void, String> {
String dstAddress;
int dstPort;
String response = "";
TextView textResponse;
Client(String addr, int port, TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.textResponse = textResponse;
}
@Override
protected String doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
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();
}
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textResponse.setText(response);
super.onPostExecute(result);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:autoLink="web"
android:text=":)"
android:textStyle="bold" />
<EditText
android:id="@+id/addressEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Server ip address" />
<EditText
android:id="@+id/portEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Server port number" />
<Button
android:id="@+id/connectButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Connect..." />
<Button
android:id="@+id/clearButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear" />
<TextView
android:id="@+id/responseTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="esp data"
android:textSize="16sp"
android:textColor="#000000"
android:inputType="textMultiLine"/>
</LinearLayout>
arduino代码:
#include <ESP8266WiFi.h>
const char* ssid = "*****";
const char* password = "******";
const int analogInPin = A0;
int sensorValue = 0;
int outputValue = 0;
WiFiServer wifiServer(80);
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting..");
}
Serial.print("Connected to WiFi. IP:");
Serial.println(WiFi.localIP());
wifiServer.begin();
pinMode(D0, INPUT);
pinMode(D1, INPUT);
}
void loop() {
WiFiClient client = wifiServer.available();
if (client) {
while (client.connected()) {
sensorValue = analogRead(analogInPin);
char charBuf[10];
itoa(sensorValue,charBuf,10);
delay(1000);
client.write(charBuf);
client.print("_");
Serial.print("value = ");
Serial.println(charBuf);
delay(10);
}
client.stop();
Serial.println("Client disconnected");
}
}