android wifi接收数据库,Android客户端通过WiFi从TCP服务器(ESP8266-12E)接收数据

目标简介:

1)启动ESP8266 Wifi模块作为侦听特定端口的TCP服务器 - >到目前为止完成

2)从服务器发送int或String到Client

2)编写一个作为客户端的Android应用程序(Android Studios),连接到服务器并接收发送的数据。

问题:

1)我的客户端应用程序没有收到任何数据

2)我也无法从我的MainActivity类访问消息以显示它在TextView中,变量处于Try,Catch Block中。

Android Studio

客户代码:

package com.example.petarmaric.client;

import android.util.Log;

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.InputStreamReader;

import java.net.InetAddress;

import java.net.Socket;

/**

* Created by petarmaric on 04/02/2017.

*/

public class TCP_Client {

// Variables and Constants

public static final String SERVERIP = "192.168.1.2";

public static final int SERVERPORT = 3030;

DataInputStream in;

Integer serverMessage;

// Run the Client

public void run(){

try {

InetAddress serverAddress = InetAddress.getByName(SERVERIP);

Log.e("Connection", "connecting to server...");

//Create the Socket

Socket socket = new Socket(serverAddress, SERVERPORT);

Log.e("Socket", "Socket was created");

try {

//receive the message from the Server

//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

in = new DataInputStream(socket.getInputStream());

Log.e("InputStream","DataInputStream created");

serverMessage = in.read();

Log.e("Message from Server", "Maybe");

} catch (Exception e) {

Log.e("InputERROR", "Nachricht wurde nicht empfangen");

} finally {

socket.close();

Log.e("Socket","Socket was closed");

}

}catch (Exception e){

Log.e("ConnectionERROR","Nicht connected");

}

Log.e("End","End of Run Method");

}

}应用程序启动后的Logcat:

Markdown and HTML are turned off in code blocks:

This is not italic, and this is not a link

02-04 18:36:17.475 25858-25858/com.example.petarmaric.client W/System: ClassLoader referenced unknown path: /data/app/com.example.petarmaric.client-2/lib/x86_64

02-04 18:36:17.788 25858-25858/com.example.petarmaric.client W/System: ClassLoader referenced unknown path: /data/app/com.example.petarmaric.client-2/lib/x86_64

02-04 18:36:18.157 25858-25858/com.example.petarmaric.client W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable

02-04 18:36:18.213 25858-25895/com.example.petarmaric.client E/Connection: connecting to server...

02-04 18:36:18.217 25858-25896/com.example.petarmaric.client D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

[ 02-04 18:36:18.219 25858:25858 D/ ]

HostConnection::get() New Host Connection established 0x7fa68eb72bc0, tid 25858

[ 02-04 18:36:18.278 25858:25896 D/ ]

HostConnection::get() New Host Connection established 0x7fa68eb72e80, tid 25896

02-04 18:36:18.295 25858-25896/com.example.petarmaric.client I/OpenGLRenderer: Initialized EGL, version 1.4

02-04 18:36:18.367 25858-25895/com.example.petarmaric.client E/Socket: Socket was created

02-04 18:36:18.367 25858-25895/com.example.petarmaric.client E/InputStream: DataInputStream createdMainAcitvity代码:

Markdown and HTML are turned off in code blocks:

This is not italic, and this is not a link

package com.example.petarmaric.client;

import android.os.AsyncTask;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

public class MainActivity extends AppCompatActivity {

//Declaration of Client object

private TCP_Client mTcpClient;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//Connect to the Server

new ConnectTask().execute("");

}

//ConnectClass definition

public class ConnectTask extends AsyncTask

{

@Override

protected TCP_Client doInBackground(Object[] params) {

//We create the TCP client object and run the Task

mTcpClient = new TCP_Client();

mTcpClient.run();

Log.e("Main Thread", "Client run initiated");

return null;

}

}

}Arduino IDE

ESP8266 12-E(Wifi模块代码):

#include

#include

#include

// declare the Variables

char* ssid = "NETGEAR54";

char* password = "calmpiano742";

char thisChar;

//create wifi server listen on a given port

WiFiServer server(3030);

//create Wifi client

WiFiClient client;

void setup() {

//Connnect to WiFi Network

Serial.begin(115200);

WiFi.begin(ssid,password);

Serial.println("");

//Wait for connection

while (WiFi.status()!=WL_CONNECTED)

{

delay(500);

Serial.print(".");

}

//Print status to Serial Monitor

Serial.print("connected to: "); Serial.println(ssid);

Serial.print("IP Address: "); Serial.println(WiFi.localIP());

//Start the TCP server

server.begin();

}

void loop() {

client = server.available();

if(client){

if(client.connected()){

Serial.println("connected to client");

while(client.connected()){

server.write(1);

}

}

}

}串行监视器:

1384, room 16

tail 8

chksum ��

......connected to: NETGEAR54

IP Address: 192.168.1.2

connected to client

.....................................................................

Soft WDT reset

ctx: cont

sp: 3ffef2c0 end: 3ffef4f0 offset: 01b0

>>>stack>>>

3ffef470: 3ffe8644 3ffee49c 3ffee49c 40202d25

3ffef480: 3ffe8444 00000002 3ffe84ff 40203380

3ffef490: 00000001 3ffee388 3ffee49c 3ffee4c8

3ffef4a0: 3ffee388 3ffee49c 3ffee370 40201d40

3ffef4b0: 3ffe8480 00000000 000003e8 40201c89

3ffef4c0: 00000000 3fff0424 00000001 40202fc9

3ffef4d0: 3fffdad0 00000000 3ffee4c0 40202ff4

3ffef4e0: feefeffe feefeffe 3ffee4d0 40100718

<<

ets Jan 8 2013,rst cause:2, boot mode:(1,7)如果您需要更多信息,请提前感谢您的帮助,请在下面写下您的问题。

用户可以根据需求在驱动里选择相对应的通讯驱动程序,配置相应的通讯握手参数,自由定义需要控制和采集的变量名、通讯地址、数据类型和初始值。功能说明及图片展示: 1.安装完成首次进入将会显示三页功能简要展示页面。 2.点击“立即使用”按钮,进入登录界面(公测账户为admin,密码admin)。 3.登录成功进入APP主页,此时主页所有状态为初始未配置状态。 4.在主页状态下向右滑动,可打开功能模块选择列表。 5.点击“WIFI连接”进入连接现场设备网络页面,如果此时用户已通过手机WIFI页面连接现场设备,则直接跳过此步骤。 6.连接成功后返回功能模块选择列表,点击“参数设置”进入通讯相关参数配置页面(首先进入驱动配置页面),目前驱动中只有支持Modbus TCP的驱动,后续会持续更新,用户通过在驱动列表中长按操作将选中的驱动挑选至已选列表中,如果要取消,可以在已选列表中通过同样的长按操作完成。 7.配置要访问的设备通讯参数,与主页的操作逻辑一致,在驱动配置页面向右滑动打开配置功能列表,点击“通讯参数配置”进入通讯参数配置页,然后点击右上角的加号,此时软件会根据已选的驱动类型自动添加一条相对应的通讯参数,用户可以通过双击的方式打开修改列表,然后在列表的某一条参数处通过长按的方式进入最终的修改对话框,修改完成后,可以通过长按的方式选定当前需要的参数配置信息到已选区域。 8.通讯参数配置完成后,向右滑动进入配置功能列表,点击“IO参数配置”进入地址段的分配,点击右上角的加号,在弹出的对话框中根据实际需求分配响应的起始地址以及地址数量,分配完成后,可在屏幕右侧边缘向左滑动调出隐藏功能菜单,通过功能菜单可查看和修改配置地址段的功能对应的IO点信息。 9.所有配置完成后,可返回主页,此时主页显示当前配置的信息,其中“控制操作”功能可以通过按钮发送布尔值,操作逻辑为当前值为0时发送1,当前值为1时发送0,而“参数设置”功能通过对子项长按可以设置调出输入框设置相应的参数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值