[牛人杂谈] ESP8266 wifi模块库文件测试

[复制链接]
3104|11
 楼主| Bermanrep 发表于 2016-11-22 20:52 | 显示全部楼层 |阅读模式
上周终于写完ESP8266 wifi 模块的库了,测试OK!在此开出源码和实例教程,希望能帮到大家!

主要配件:



一张图介绍mini RBoard


步骤:

1. 下载库文件

烧写程序之前,我们需要用到ESP8266库,下载地址为:

http://pan.baidu.com/s/1sj2nUyT

温馨提示: ESP8266 wifi库文件是基于arduino IDE 1.0.5版本编译的,如遇编译不过,请尝试其他版本。

打开库中的uartWIFI.h文件,因为我们的mini RBoard使用的是跟UNO一样的芯片,所以要在H文件里去掉定义UNO的注释,注销掉MEGA。

#define UNO
//#define MEGA

然后把_DBG_RXPIN_和_DBG_TXPIN_引脚定义改为D9和D10.

#define _DBG_RXPIN_ 9                //A0
#define _DBG_TXPIN_ 10                //A1

然后保存uartWIFI.h文件。

烧写代码:

插上FOCA,板子的类型选择“Arduino UNO”,点击烧写程序。如下图:



 楼主| Bermanrep 发表于 2016-11-22 20:53 | 显示全部楼层
ESP8266 library

When you use with UNO board, uncomment the follow line in uartWIFI.h.
#define UNO

When you use with MEGA board, uncomment the follow line in uartWIFI.h.
#define MEGA

Connection:
When you use it with UNO board, the connection should be like these:
ESP8266_TX->D0
ESP8266_RX->D1
ESP8266_CH_PD->3.3V
ESP8266_VCC->3.3V
ESP8266_GND->GND

FTDI_RX->D3        //The baud rate of software serial can't be higher that 19200, so we use software serial as a debug port
FTDI_TX->D2

When you use it with MEGA board, the connection should be like these:
ESP8266_TX->RX1(D19)
ESP8266_RX->TX1(D18)
ESP8266_CH_PD->3.3V
ESP8266_VCC->3.3V
ESP8266_GND->GND

When you want to output the debug information, please use DebugSerial. For example,

DebugSerial.println("hello");


Note:        The size of message from ESP8266 is too big for arduino sometimes, so the library can't receive the whole buffer because
the size of the hardware serial buffer which is defined in HardwareSerial.h is too small.

Open the file from \arduino\hardware\arduino\avr\cores\arduino\HardwareSerial.h.
See the follow line in the HardwareSerial.h file.

#define SERIAL_BUFFER_SIZE 64

The default size of the buffer is 64. Change it into a bigger number, like 256 or more.


*/


#define SSID "Itead_1(Public)"        //type your own SSID name
#define PASSWORD "27955416"        //type your own WIFI password


#include "uartWIFI.h"
#include <SoftwareSerial.h>
WIFI wifi;

extern int chlID;        //client id(0-4)


void setup()
{
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
wifi.begin();
bool b = wifi.Initialize(STA, SSID, PASSWORD);
if(!b)
{
DebugSerial.println("Init error");
}
delay(8000); //make sure the module can have enough time to get an IP address
String ipstring = wifi.showIP();
DebugSerial.println(ipstring);        //show the ip address of module

delay(2000);
wifi.confMux(1);
delay(100);
if(wifi.confServer(1,8080))
DebugSerial.println("Server is set up");


}
 楼主| Bermanrep 发表于 2016-11-22 20:56 | 显示全部楼层
  1. void loop()
  2. {

  3. char buf[100];
  4. int iLen = wifi.ReceiveMessage(buf);
  5. if(iLen > 0)
  6. {
  7. //mini Rboard D4 pin is relay,mini Rboard URL:http://item.taobao.com/item.htm? ... keTJ&id=39334222369

  8. if (strcmp(buf, "CH0ON") == 0)
  9. {
  10. digitalWrite(4,HIGH);
  11. DebugSerial.println("CH0ON");
  12. }
  13. if (strcmp(buf, "CH0OFF") == 0)
  14. {
  15. digitalWrite(4,LOW);
  16. DebugSerial.println("CH0OFF");
  17. }
  18. /* the other 3 chanels below are for Rboard D5 D6 D7 , Rboard URL:http://item.taobao.com/item.htm? ... keTJ&id=39334222369

  19. if (strcmp(buf, "CH1ON") == 0)
  20. {
  21. digitalWrite(5,HIGH);
  22. DebugSerial.println("CH1ON");        
  23. }
  24. if (strcmp(buf, "CH1OFF") == 0)
  25. {
  26. digitalWrite(5,LOW);
  27. DebugSerial.println("CH1OFF");        
  28. }
  29. if (strcmp(buf, "CH2ON") == 0)
  30. {
  31. digitalWrite(6,HIGH);
  32. DebugSerial.println("CH2ON");
  33. }
  34. if (strcmp(buf, "CH2OFF") == 0)
  35. {
  36. digitalWrite(6,LOW);
  37. DebugSerial.println("CH2OFF");
  38. }
  39. if (strcmp(buf, "CH3ON") == 0)
  40. {
  41. digitalWrite(7,HIGH);
  42. DebugSerial.println("CH3ON");        
  43. }
  44. if (strcmp(buf, "CH3OFF") == 0)
  45. {
  46. digitalWrite(7,LOW);
  47. DebugSerial.println("CH3OFF");        
  48. }
  49. */

  50. }
  51. }


 楼主| Bermanrep 发表于 2016-11-22 20:59 | 显示全部楼层
打开该软件,点击右上角的settings,将Target IP设置为wifi的IP地址,本文为172.16.1.12,以每次分配的实际IP为准。UPD/TCP选择TCP,Target Port设置为8080,TCP connection behavior设置为“Initiate connection,and keep open”。





然后返回上一级,点击Button Settings。设置buttons的数量为8个(适用于4通道的Rboard),分别在button的名字和命令上修改成:“CH0ON”, “CH1ON”, “CH2ON”, “CH3ON”, “CH0OFF”, “CH1OFF”, “CH2OFF”, “CH3OFF”。这分别代表设置对应继电器通道的开关状态。此次测试用的mini Rboard只需要用到“CH0ON”和“CH0OFF”这一个通道。


返回主页面,我们可以看到按钮的名称已改变,点击对应的按钮后,会看到mini Rboard上对应的通道设置为按钮上的状态。


gejigeji521 发表于 2017-3-12 14:29 | 显示全部楼层
怎么用ESP8266的AT指令获取网络时间,比如UDP连接time.windows.com 的123端口后,怎么操作。。。。
gejigeji521 发表于 2017-3-12 14:30 | 显示全部楼层
我用AT连接到服务器了,不知道怎么操作才能得到那个网络时间。
稳稳の幸福 发表于 2017-3-17 20:29 | 显示全部楼层
需要自己安装支持包。
zhuotuzi 发表于 2017-3-18 18:26 | 显示全部楼层
下载了Arduino,没找到ESP8266
huangcunxiake 发表于 2017-3-18 22:44 | 显示全部楼层
非常感谢,知道这个软件了,下载个手机调试。
温柔黄 发表于 2019-7-20 11:28 | 显示全部楼层

库文件只要这个就可以对吗?
643757107 发表于 2019-7-20 13:59 | 显示全部楼层
学习一下,干货。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

22

主题

132

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部