esp8266和arduino连接,怎么实现他们的通信,有完整的程序吗?-Arduino中文社区 - Powered by Discuz! Archiver

tianlinwudi 发表于 2016-5-30 09:11

esp8266和arduino连接,怎么实现他们的通信,有完整的程序吗?

以AP模式执行就行了,如果需要用到库函数,希望把库也分享一下,谢谢!

敢问路在何方 发表于 2016-5-30 09:20

本人也在学习,希望大侠支招。

太清 发表于 2016-5-30 09:41

先用USB转TTL模块和串口调试助手在电脑端,将ESP8266各种模式、参数设置好(用AT指令集就行),然后将ESP8266模块,按照硬件原理接到Arduino上面,留出两个IO口做软串口,最后Arduino程序按照软串口的写法写就行了,就是负责数据的收发,你当然也可以自定义报文格式。PS:亲测,软串口波特率选为115200和ESP8266一致,也行,收发数据也挺稳定。希望的回答能帮到你

liutao88 发表于 2016-5-30 11:36

有现成的库可用,如下例子:
网上可以下载:https://github.com/itead/ITEADLIB_Arduino_WeeESP8266

/*
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)"
#define PASSWORD   "27955416"


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

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


void setup()
{

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(5000);
wifi.confMux(1);
delay(100);
if(wifi.confServer(1,8080))
        DebugSerial.println("Server is set up");
       

}
void loop()
{

char buf;
int iLen = wifi.ReceiveMessage(buf);
if(iLen > 0)
{
    //if receive a "HELLO", send a "HELLO BACK" back to the client
    if (strcmp(buf, "HELLO") == 0)
    {
      DebugSerial.print("Get a message from id ");
      DebugSerial.print(chlID);
      DebugSerial.println(":");
      DebugSerial.println(buf);
      
      DebugSerial.print("Send a message back to id ");
      DebugSerial.print(chlID);
      DebugSerial.println(":");
      DebugSerial.println("HELLO BACK");      
      wifi.Send(chlID,"HELLO BACK");
    }

}
}

smilence_ 发表于 2016-5-30 21:04

liutao88 发表于 2016-5-30 11:36
有现成的库可用,如下例子:
网上可以下载:https://github.com/itead/ITEADLIB_Arduino_WeeESP8266



问什么会有编译错误。。

liutao88 发表于 2016-5-30 21:20

smilence_ 发表于 2016-5-30 21:04
问什么会有编译错误。。

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
要选择对应的板子。,建议你使用MEGA2560,因为他支持4个串口

以下是一个教程:http://www.360doc.com/content/15/0119/00/21471366_441925876.shtml


敢问路在何方 发表于 2016-6-18 11:09

本帖最后由 敢问路在何方 于 2016-6-18 11:23 编辑

smilence_ 发表于 2016-5-30 21:04
问什么会有编译错误。。
liutao88给出的库是新的,而程序却是老的。因为,新库的头文件是“ESP8266.H",不是“uartWIFI.h”。要用新库的示例文件。还有如果用的是Uno的软串口, 要修改“ESP8266.H"里一句:
//#define ESP8266_USE_SOFTWARE_SERIAL
将前面两个斜杠删除:
#define ESP8266_USE_SOFTWARE_SERIAL
还有,示例程序是针对Mega2560的:
#include "ESP8266.h"
ESP8266 wifi(Serial1);
要将上面两句改成:
#include "ESP8266.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2); /* RX:D3, TX:D2 */管脚可以自定义
ESP8266 wifi(mySerial);
还有,示例文件里两处波特率都要统一修改为115200,祝你顺利!

初学者及爱好者 发表于 2016-7-9 17:24

楼主现在我正在做esp8266加arduino来控制舵机你能分享一下经验吗??
页: [1]
查看完整版本: esp8266和arduino连接,怎么实现他们的通信,有完整的程序吗?