你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

查看: 4041|回复: 13

ARM®mbed OS入门开发 设置并测试ESP8266

[复制链接]

61

主题

1071

回帖

17

蝴蝶豆

论坛元老

最后登录
2020-12-9
发表于 2016-10-27 20:55:28 | 显示全部楼层 |阅读模式






设置并测试ESP8266 Wi Fi SOC. 设置SSID 及 PASSWORD串口打印状态信息



微信截图_20161027204954.png
微信截图_20161027205014.png

ESP8266
mbed LPC1768
330uf cap
VccVout-3.3V+
GndGnd-
Resetp26
RXp28-TX
TXp27-RX
CH_PDVout

须注意 本模块采用3.3v供电
#include "mbed.h"
Serial pc(USBTX, USBRX);
Serial esp(D10, D2); // tx D10, rx D2
DigitalOut reset(D1);
Timer t;
int  count,ended,timeout;
char buf[1024];
char snd[255];
char ssid[32] = "mySSID";     // enter WiFi router ssid inside the quotes
char pwd [32] = "myPASSWORD"; // enter WiFi router password inside the quotes
void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();


int main()
{
    reset=0; //hardware reset for 8266
    pc.baud(115200);  // set what you want here depending on your terminal program speed
    pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
    wait(0.5);
    reset=1;
    timeout=2;
    getreply();
    esp.baud(115200);   // change this to the new ESP8266 baudrate if it is changed at any time.
    //ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
    ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
    // continuosly get AP list and IP
    while(1)
     {
        pc.printf("\n---------- Listing Acces Points ----------\r\n");
        strcpy(snd, "AT+CWLAP\r\n");
        SendCMD();
        timeout=15;
        getreply();
        pc.printf(buf);
        wait(2);
        pc.printf("\n---------- Get IP and MAC ----------\r\n");
        strcpy(snd, "AT+CIFSR\r\n");
        SendCMD();
        timeout=10;
        getreply();
        pc.printf(buf);
        wait(2);
    }
}
// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
void ESPsetbaudrate()
{
    strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
    SendCMD();
}
//  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
void ESPconfig()
{
    wait(5);
    strcpy(snd,"AT\r\n");
    SendCMD();
    wait(1);
    strcpy(snd,"AT\r\n");
    SendCMD();
    wait(1);
    strcpy(snd,"AT\r\n");
    SendCMD();
    timeout=1;
    getreply();
    wait(1);
    pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
    pc.printf("---------- Reset & get Firmware ----------\r\n");
    strcpy(snd,"AT+RST\r\n");
    SendCMD();
    timeout=5;
    getreply();
    pc.printf(buf);
    wait(2);
    pc.printf("\n---------- Get Version ----------\r\n");
    strcpy(snd,"AT+GMR\r\n");
    SendCMD();
    timeout=4;
    getreply();
    pc.printf(buf);
    wait(3);
    // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
    pc.printf("\n---------- Setting Mode ----------\r\n");
    strcpy(snd, "AT+CWMODE=1\r\n");
    SendCMD();
    timeout=4;
    getreply();
    pc.printf(buf);
    wait(2);
    // set CIPMUX to 0=Single,1=Multi
    pc.printf("\n---------- Setting Connection Mode ----------\r\n");
    strcpy(snd, "AT+CIPMUX=1\r\n");
    SendCMD();
    timeout=4;
    getreply();
    pc.printf(buf);
    wait(2);
    pc.printf("\n---------- Listing Access Points ----------\r\n");
    strcpy(snd, "AT+CWLAP\r\n");
    SendCMD();
    timeout=15;
    getreply();
    pc.printf(buf);
    wait(2);
    pc.printf("\n---------- Connecting to AP ----------\r\n");
    pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
    strcpy(snd, "AT+CWJAP=\"");
    strcat(snd, ssid);
    strcat(snd, "\",\"");
    strcat(snd, pwd);
    strcat(snd, "\"\r\n");
    SendCMD();
    timeout=10;
    getreply();
    pc.printf(buf);
    wait(5);


    pc.printf("\n---------- Get IP's ----------\r\n");
    strcpy(snd, "AT+CIFSR\r\n");
    SendCMD();
    timeout=3;
    getreply();
    pc.printf(buf);
    wait(1);
    pc.printf("\n---------- Get Connection Status ----------\r\n");
    strcpy(snd, "AT+CIPSTATUS\r\n");
    SendCMD();
    timeout=5;
    getreply();
    pc.printf(buf);
    pc.printf("\n\n\n  If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
    pc.printf("  Run this if you want to reconfig the ESP8266 at any time.\r\n");
    pc.printf("  It saves the SSID and password settings internally\r\n");
    wait(10);
}
void SendCMD()
{
    esp.printf("%s", snd);
}
void getreply()
{
    memset(buf, '\0', sizeof(buf));
    t.start();
    ended=0;
    count=0;
    while(!ended) {
        if(esp.readable()) {
            buf[count] = esp.getc();
            count++;
        }
        if(t.read() > timeout) {
            ended = 1;
            t.stop();
            t.reset();
        }
    }
}
629805748602642110.jpg




<
回复

使用道具 举报

61

主题

1071

回帖

17

蝴蝶豆

论坛元老

最后登录
2020-12-9
 楼主| 发表于 2016-10-27 21:11:52 | 显示全部楼层
虚拟串口运行在115200波特率,到mbed的USB com端口(mbed com数字不同)。它将显示通过示例程序的USB虚拟串口发送 WiFi命令和状态消息
本程序将允许你检查设置是否正常,并且从WiFi路由器获取有效的IP地址。
在将代码下载到编译器窗口后,将需要查找和更改单独的WiFi设置的“SSID”和“密码”字段,然后重新编译和下载。
更改后才能连接到你的WiFi上并获取IP地址。在此代码中,SSID和密码数据在“main.cpp”中找到。
一旦你设置它,芯片会保存。某些WiFi安全设置可能需要设备的MAC地址,并且还输出此信息。
因此,要先运行此程序,并让它使用一个有效的IP地址。
如果你的初始Wi Fi设置有问题,它将显示帮助的状态消息。
转自:http://developer.mbed.org/users/ ... h-the-mbed-lpc1768/
回复 支持 反对

使用道具 举报

0

主题

4

回帖

0

蝴蝶豆

新手上路

最后登录
2017-12-21
发表于 2016-10-27 22:36:05 | 显示全部楼层
程序太长了,还没看懂
回复 支持 反对

使用道具 举报

37

主题

1084

回帖

0

蝴蝶豆

论坛元老

最后登录
2020-7-22
发表于 2016-10-27 23:39:13 | 显示全部楼层
谢谢分享学习一下
回复 支持 反对

使用道具 举报

4

主题

484

回帖

0

蝴蝶豆

金牌会员

最后登录
2020-8-12
发表于 2016-10-28 08:12:32 | 显示全部楼层
有情帮顶
回复 支持 反对

使用道具 举报

4

主题

574

回帖

3

蝴蝶豆

高级会员

最后登录
2020-12-9
发表于 2016-10-28 08:23:26 | 显示全部楼层
楼主辛苦了。天天分享好东西。点赞!!!!
回复 支持 反对

使用道具 举报

64

主题

744

回帖

23

蝴蝶豆

实习版主

最后登录
2020-12-8
发表于 2016-10-28 10:07:24 | 显示全部楼层

楼主辛苦
回复 支持 反对

使用道具 举报

20

主题

1110

回帖

125

蝴蝶豆

论坛元老

最后登录
2019-5-4
发表于 2016-10-28 11:46:59 | 显示全部楼层
回复 支持 反对

使用道具 举报

0

主题

159

回帖

0

蝴蝶豆

高级会员

最后登录
2017-3-9
发表于 2016-10-28 18:56:38 | 显示全部楼层
谢谢分享学习一下
回复 支持 反对

使用道具 举报

47

主题

1210

回帖

5

蝴蝶豆

论坛元老

最后登录
2020-8-13
发表于 2016-10-29 00:54:52 | 显示全部楼层
谢谢分享
回复 支持 反对

使用道具 举报

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版