【平头哥RVB2601创意应用开发】实践8-TCP连接ESP8266节点控制LED亮灭
<p><br /></p>
<p cid="n2" mdtype="paragraph"> </p>
<p> </p>
<p cid="n3" mdtype="paragraph">本篇介绍RVB2601与ESP8266无线TCP通信,使得RVB2601无线控制ESP8266上的LED亮灭。</p>
<p cid="n4" mdtype="paragraph"></p>
<h1 cid="n5" mdtype="heading">1 RVB2601程序</h1>
<h2 cid="n6" mdtype="heading">1.1 连网后连接服务器</h2>
<p cid="n7" mdtype="paragraph">这里将RVB2601作为客户端,ESP8266板子作为服务器,当RVB2601连上WIFI后,再连接ESP8266板子。RVB2601与ESP8266处于同一局域网下,我这个ESP8266连上网后的IP为192.168.5.108,端口号可任选,我用的是666。</p>
<pre>
<code class="language-cpp">void connect_tcp_server(char *server_ip, uint16_t port)
{
char ssid;
int bssid;
int channel;
int rssi;
w800_ap_info( ssid, bssid , &channel, &rssi);
printf("ssid: %s\r\n", ssid);
printf("channel: %d\r\n", channel);
printf("rssi: %d\r\n", rssi);
w800_connect_remote(0, NET_TYPE_TCP_CLIENT, server_ip, port);
}
static void network_event(uint32_t event_id, const void *param, void *context)
{
switch(event_id) {
case EVENT_NETMGR_GOT_IP:
LOGD(TAG, "net got ip");
connect_tcp_server("192.168.5.108", 666); //网络连接成功之后连接服务器(测试另一个ESP8266上的LED控制)
break;
case EVENT_NETMGR_NET_DISCON:
LOGD(TAG, "net disconnect");
break;
}
/*do exception process */
app_exception_event(event_id);
}</code></pre>
<pre cid="n8" lang="c" mdtype="fences" spellcheck="false">
</pre>
<h2 cid="n9" mdtype="heading">1.2 注册自定义指令</h2>
<p cid="n10" mdtype="paragraph">为了方便测试LED控制指令,可以先利用串口指令来测试:</p>
<pre>
<code class="language-cpp">static void cmd_tcp_client_handler(char *wbuf, int wbuf_len, int argc, char **argv)
{
const uint8_t pdata[] = "Hello RVB2601!";
const uint8_t pledon[] = "led on";
const uint8_t pledoff[] = "led off";
int timeout=120;
LOGD(TAG, "[%s] argc:%d\n", __func__, argc);
for (int i=0; i<argc; i++)
{
LOGD(TAG, "[%s] argv:%s\n", __func__, argv);
}
if (argc == 3 && strcmp(argv,"led") == 0)
{
if (strcmp(argv,"on") == 0)
{
w800_send_data(pledon, sizeof(pledon), timeout);
}
else if (strcmp(argv,"off") == 0)
{
w800_send_data(pledoff, sizeof(pledoff), timeout);
}
else
{
LOGD(TAG, "[%s] para err\n", __func__);
}
}
else
{
w800_send_data(pdata, sizeof(pdata), timeout);
}
}
int cli_reg_cmd_tcp_client_test(void)
{
static const struct cli_command tcp_client_cmd_info = {
"tcp_client_test",
"tcp client test",
cmd_tcp_client_handler,
};
aos_cli_register_command(&tcp_client_cmd_info);
return 0;
}
void board_cli_init()
{
aos_cli_init();
extern void cli_reg_cmd_ps(void);
cli_reg_cmd_ps();
extern void cli_reg_cmd_free(void);
cli_reg_cmd_free();
cli_reg_cmd_kvtool();
cli_reg_cmd_ping();
cli_reg_cmd_ifconfig();
cli_reg_cmd_factory();
cli_reg_cmd_sysinfo();
cli_reg_cmd_tcp_client_test();//<--------加上这句
}</code></pre>
<h1 cid="n12" mdtype="heading">2 ESP8266程序</h1>
<p cid="n13" mdtype="paragraph">ESP8266使用Arduino IDE进行开发与程序编写,主程序逻辑如下,主要就是等待客户端连接,并接收客户端的指令,接收到后,对指令进行解析,执行对应的LED亮灭操作。</p>
<p cid="n14" mdtype="paragraph">这里是以字符串的形式进行数据传送,比如“led on”就是开灯,“led off”就是关灯,如果还想控制其它电器,如风扇,可以定义如“fan on"和“fan off”这类的指令。</p>
<pre>
<code class="language-cpp">void loop()
{
uint8_t i;
if (server.hasClient())
{ //判断是否有新的client请求进来
for (i = 0; i < MAX_SRV_CLIENTS; i++)
{
//释放旧无效或者断开的client
if (!serverClients || !serverClients.connected())
{
if (!serverClients)
{
serverClients.stop(); //停止指定客户端的连接
}
serverClients = server.available();//分配最新的client
Serial.print("1个新的客户端: ");
Serial.println(i);
break; //跳出一层for循环
}
}
//当达到最大连接数 无法释放无效的client,需要拒绝连接
if (i == MAX_SRV_CLIENTS)
{
WiFiClient client = server.available();
client.stop();
Serial.println("连接被拒绝 ");
}
}
//检测client发过来的数据
for (i = 0; i < MAX_SRV_CLIENTS; i++)
{
if (serverClients && serverClients.connected())
{
if (serverClients.available())
{
String recvStr = "";
while (serverClients.available())
{
recvStr += char(serverClients.read());
}
Serial.println("收到客户端的信息:");
Serial.println(recvStr);
std::vector<String> splitStr;
splitBySpace(recvStr, splitStr);
if (splitStr.size() == 2)
{
if (splitStr == "led")
{
if (splitStr == "on")
{
Serial.println("开灯");
digitalWrite(2, LOW); // 点亮LED
}
else if (splitStr == "off")
{
Serial.println("关灯");
digitalWrite(2, HIGH); // 熄灭LED
}
}
}
}
}
}
}</code></pre>
<p cid="n16" mdtype="paragraph"> </p>
<h1 cid="n17" mdtype="heading">3 测试效果</h1>
<p cid="n49" mdtype="paragraph">通过串口指令,可以先测试通信是否正常:</p>
<p cid="n49" mdtype="paragraph"></p>
<p cid="n50" mdtype="paragraph">然后可以通过按键以及屏幕来进行LED的亮灭控制:</p>
<p cid="n50" mdtype="paragraph"><iframe __idm_id__="565249" allowfullscreen="true" frameborder="0" height="450" src="//player.bilibili.com/player.html?bvid=1Mr4y1x7dC&page=1" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</p>
<h1 cid="n18" mdtype="heading">4 总结</h1>
<p cid="n19" mdtype="paragraph">本篇介绍了RVB2601,作为客户端,通过TCP协议,与作为服务端的ESP8266进行无线通信,控制ESP8266上LED的亮灭。</p>
<p cid="n20" mdtype="paragraph">最后,感谢之前各位网友关于RVB2601对于WIFI的使用介绍,本篇参考如下几篇,收获颇多:</p>
<p cid="n21" mdtype="paragraph"><a href="https://occ.t-head.cn/community/post/detail?spm=a2cl5.14300636.0.0.1cd8180fSVGmiI&id=4002392593328574464" spellcheck="false"><strong>【平头哥RVB2601开发板试用体验】WiFi无线通信</strong></a></p>
<p cid="n22" mdtype="paragraph"><a href="https://occ.t-head.cn/community/post/detail?spm=a2cl5.25411629.0.0.547a180fxaMF9K&id=3943105084740669440#%E4%BB%A5%E5%A4%AA%E7%BD%91%E9%80%9A%E8%AE%AF%E6%B5%8B%E8%AF%95" spellcheck="false"><strong>RVB2601评估板试用3: 以太网通讯测试</strong></a></p>
<p cid="n23" mdtype="paragraph"><a href="https://bbs.eeworld.com.cn/thread-1198791-1-1.html" spellcheck="false"><strong>【平头哥RVB2601创意应用开发】3、wifi网络通信</strong></a></p>
<p> </p>
<p>ESP8266连上网后的IP为192.168.5.108,端口号可任选,楼主用的是666<br />
这个端口任意选,应该也是3位就行</p>
页:
[1]