6.03-综合实战篇-8266TCP服务器(espconn实现)(NONOS 2.2.0)与Android T...
实现的功能Android TCP客户端连接8266的TCP服务器,网络控制开发板继电器 1.控制继电器吸合
2.控制继电器断开
前言1.用户在看这一节之前请先学习https://www.cnblogs.com/yangfengwu/p/12543923.html Android TCP客户端https://www.cnblogs.com/yangfengwu/p/12543847.html 8266 TCP服务器 2.控制继电器引脚 协议规定Android TCP客户端发送给ESP8266TCP服务器控制继电器吸合指令:0xaa0x550x01 0x01ESP8266执行以后回复给C#TCP客户端:0x550xaa0x01 0x01 Android TCP客户端发送给ESP8266TCP服务器控制继电器断开指令:0xaa0x550x01 0x00ESP8266执行以后回复给C#TCP客户端:0x550xaa0x01 0x00
ESP8266程序编写1.处理程序在这节的基础上修改添加https://www.cnblogs.com/yangfengwu/p/12543847.html
2.配置GPIO5为普通引脚
/*设置GPIO5为普通引脚*/
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U , FUNC_GPIO5);
3.定义用于返回继电器状态的数组
/*用于返回继电器的状态*/
u8 RelayOn={0x55,0xaa,0x01,0x01};//继电器吸合
u8 RelayOff={0x55,0xaa,0x01,0x00};//继电器断开
4.编写TCP接收处理程序
if(length>= 4 && pusrdata == 0xaa && pusrdata == 0x55){
if(pusrdata == 0x01){
if(pusrdata == 0x01){
GPIO_OUTPUT_SET(5, 1);//设置GPIO5输出高电平
espconn_send(TCPSendDate,RelayOn,4);
}
else if(pusrdata == 0x00){
GPIO_OUTPUT_SET(5, 0);//设置GPIO5输出低电平
espconn_send(TCPSendDate,RelayOff,4);
}
}
}
5.先用 TCP调试助手测试(手机连接Wi-Fi模块的无线)
Android APP程序编写1.处理程序在这节的基础上修改添加https://www.cnblogs.com/yangfengwu/p/12543923.html 2.页面修改如下:页面用了一个ImageButton
ImageButton imageButtonControl;
imageButtonControl = findViewById(R.id.imageButtonControl);
imageButtonControl.setTag(false);//默认是关闭
3.点击按钮发送相应的数据,同时切换图片
imageButtonControl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final byte[] bytes = new byte;
bytes = (byte) 0xaa;
bytes = (byte) 0x55;
bytes = (byte) 0x01;
if ((boolean) (imageButtonControl.getTag()) == false) {
imageButtonControl.setImageResource(R.mipmap.switch_button_on);//吸合图片
imageButtonControl.setTag(true);
bytes = 0x01;
} else {
imageButtonControl.setImageResource(R.mipmap.switch_button_off);//断开图片
imageButtonControl.setTag(false);
bytes = 0x00;
}
new Thread(new Runnable() {
@Override
public void run() {
try{
outputStream.write(bytes,0,4);//发送数据
}catch (Exception e){}
}
}).start();
}
});
4.处理8266返回的数据
//提示byte范围是-127 -- 127 &0xFF以后便自动转为int型
if ( msg.arg1>=4 && Buffer == 0x55 && (Buffer&0xff) == 0xaa ) {
if (Buffer == 0x01) {
if (Buffer == 0x01)//继电器吸合
{
textViewState.setText("吸合");
imageButtonControl.setImageResource(R.mipmap.switch_button_on);//吸合图片
imageButtonControl.setTag(true);
}
else if (Buffer == 0x00)//继电器断开
{
textViewState.setText("断开");
imageButtonControl.setImageResource(R.mipmap.switch_button_off);//断开图片
imageButtonControl.setTag(false);
}
}
}
测试请使用手机APP连接Wi-Fi模块IP地址:192.168.4.1端口号:8080
1.控制继电器吸合
https://img2020.cnblogs.com/blog/819239/202003/819239-20200322172450224-1745308993.png
https://img2020.cnblogs.com/blog/819239/202003/819239-20200322172521176-2065886166.png
2.控制继电器断开https://img2020.cnblogs.com/blog/819239/202003/819239-20200322172554977-1083909816.png
https://img2020.cnblogs.com/blog/819239/202003/819239-20200322172606604-481823075.png
页:
[1]