ESP8266

开发环境地址
乐鑫芯片论坛网站
安信集成商可论坛

系统启动基本调节
1)电源稳定纯净,如果esp工作不正常(可能商因为ttl转换模块工具),
16管脚必须穿了104陶片电容
2)电容0.1uf,耐压大于3.3v
3)启动时候gpio15必须那里,gpi0 gpio2为高
CH_PD这个引脚必须拉高


SDK目录
->Config
->project 项目目录
->at-firmware at例子源码
->esp_iot_rtos_sdk 时时操作系统源码
->iot-firmware iot源码
->nodemcu-firmware nodemcu源码
ESP8266CON.exe 编译程序
->cygwin 编译器目录
ESP8266Flasher.exe 下载程序
ESP8266IDE.exe 编辑程序
->eclipse 编辑器目录

编译官方SDK
1.打开esp8266con.exe
dir 查看project目录
2.在命令行中进入编译的工程目录
cd at-firmware
3.使用make clean清除当前编译文件
make clean
4.使用make进行编译
make
生成文件在at-firmware/bin目录下
下载0x00000.bin 0x40000.bin到wifi芯片里面
SDK概述
sdk为用户提供了一套数据接受,发送数据的接口,用户不必关系底层网络的具体实现
只需要专注于物联网上层应用开发
ESP8266物联网平台所以网络功能均在库中实现,对用户不透明,用户初始化功能可以
在user_mian.c中实现
void user_init(void)是上层的入口函数给用户提供一个初始化接口,用户可以在该函数内添
加硬件初始化,网络参数设置,定时器初始化等
SDK_v1.1以后版本,请在user_main.c添加user_rf_pre_init(void)

创建自己的工程,输出hello world
1.复制相关必要的工程文件到工程目录中
2.在eclipse中选择导入自己的工程目录
3.编译器选择cygwin gcc
4.在user_mian.c文件中创建两个必要的函数user_init()user_rf_pre_init()
5.在user_init()函数中配置串口波特率为115200.并输出hello word

/********************************************wifi ap station start******************************************/
/*
* user_main.c
*
* Created on: 2018年5月13日
* Author: Administrator
*/
#include "driver/uart.h"
#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "mem.h"
//wifi连接的定时器
ETSTimer connect_timer;
//udp不停的发送广播的定时器
ETSTimer send_udp_timer;
//检查新设备连接到ap的定时器
ETSTimer check_timer;
//udp conn
struct espconn user_udp_espconn;
//tcp client conn
struct espconn user_tcp_client_espconn;
//tcp service conn
struct espconn user_tcp_service_espconn;

void ICACHE_FLASH_ATTR Wifi_conned(void *arg) {
static uint8 count = 0;
uint8 status;
//关闭定时器
os_timer_disarm(&connect_timer);
count++;
//获取当前wifi连接状态
status = wifi_station_get_connect_status();
if (status == STATION_GOT_IP) {
os_printf("Wifi connect success!");
return;
} else {
if (count >= 7) {
os_printf("Wifi connect fail!");
return;
}
}
//启动定时器
os_timer_arm(&connect_timer, 2000, NULL);
}

void ICACHE_FLASH_ATTR user_upd_send(void *arg){
struct espconn *user_udp_espconn=(struct espconn *)arg;
static int send_count=0;
send_count++;
char macaddr[6];
char drive_buffer[40]={0};
wifi_get_macaddr(STATION_IF,macaddr);
os_sprintf(drive_buffer,"send count %d, macadd is "MACSTR"!!\r\n",send_count,MAC2STR(macaddr));
//发送udp广播
espconn_sent(user_udp_espconn, drive_buffer, os_strlen(drive_buffer));
}

void ICACHE_FLASH_ATTR user_udp_recv_callback(void *arg, char *pdata, unsigned short len){
struct espconn *user_udp_espconn=(struct espconn *)arg;
os_printf("udp接收成功");
}

void ICACHE_FLASH_ATTR user_udp_sent_callback(void *arg){
struct espconn *user_udp_espconn=(struct espconn *)arg;
os_printf("udp发送成功");
os_timer_disarm(&send_udp_timer);
//设置回调函数以及参数
os_timer_setfn(&send_udp_timer, user_upd_send, user_udp_espconn);
os_timer_arm(&send_udp_timer, 2000, NULL);
}


void ICACHE_FLASH_ATTR user_tcp_recv_callback(void *arg, char *pdata, unsigned short len){
struct espconn *user_tcp_espconn=(struct espconn *)arg;
//延时300毫秒后断开
os_delay_us(300);
espconn_disconnect((struct espconn *)arg);
}

void ICACHE_FLASH_ATTR user_tcp_dis_callback(void *arg){
struct espconn *user_tcp_espconn=(struct espconn *)arg;
os_printf("tcp断开连接");
}

void ICACHE_FLASH_ATTR user_tcp_sent_callback(void *arg){
struct espconn *user_tcp_espconn=(struct espconn *)arg;
os_printf("tcp发送成功");
}


void ICACHE_FLASH_ATTR user_tcp_connect_cb(void *arg)
{
struct espconn *user_tcp_espconn=(struct espconn *)arg;
//注册接收回调
espconn_regist_recvcb(user_tcp_espconn, user_tcp_recv_callback);
//注册发送回调
espconn_regist_sentcb(user_tcp_espconn, user_tcp_sent_callback);
//注册断开回调
espconn_regist_disconcb(user_tcp_espconn, user_tcp_dis_callback);
//发送信息
espconn_sent(user_tcp_espconn,"这是esp8226",strlen("这是esp8226"));
}

void ICACHE_FLASH_ATTR user_tcp_recon_cb(void *arg, sint8 err)
{
struct espconn *user_tcp_espconn=(struct espconn *)arg;
os_printf("连接错误,错误代码为%d\r\n",err);
//连接失败后重新连接
espconn_connect(user_tcp_espconn);
}


void ICACHE_FLASH_ATTR user_service_listen(void *arg)
{
struct espconn *user_tcp_espconn=(struct espconn *)arg;
espconn_regist_recvcb(user_tcp_espconn, user_tcp_recv_callback);
espconn_regist_sentcb(user_tcp_espconn, user_tcp_sent_callback);
espconn_regist_disconcb(user_tcp_espconn, user_tcp_dis_callback);

}

void ICACHE_FLASH_ATTR user_service_listen_cb(void *arg, sint8 err)
{
struct espconn *user_tcp_espconn=(struct espconn *)arg;
os_printf("连接错误,错误代码为%d\r\n",err);
}

void ICACHE_FLASH_ATTR wifi_init(void)
{
struct station_config stationConf;
//设置wifi的ssid
os_memcpy(stationConf.ssid, "TP-LINK", 32);
//设置密码
os_memcpy(stationConf.password, "1122334455", 64);
//设置wifi但不保存到flash
wifi_station_set_config_current(&stationConf);
//wifi连接
wifi_station_connect();
//设置定时器回调
os_timer_setfn(&connect_timer, Wifi_conned, NULL);
//启动定时器
os_timer_arm(&connect_timer, 2000, NULL);
}

void ICACHE_FLASH_ATTR udp_init(void)
{
//设置为广播发送方式为ap+station
wifi_set_broadcast_if(STATIONAP_MODE);
user_udp_espconn.type = ESPCONN_UDP;
esp_udp *udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
user_udp_espconn.proto.udp = udp;
udp->local_port=2525;
udp->remote_port=2525;
const char remote_udp_ip[4]={255,255,255,255};
os_memcpy(udp->remote_ip,remote_udp_ip,sizeof(remote_udp_ip));
//设置接收回调
espconn_regist_recvcb(&user_udp_espconn, user_udp_recv_callback);
//设置发送回调
espconn_regist_sentcb(&user_udp_espconn, user_udp_sent_callback);
//发送广播
user_upd_send(&user_udp_espconn);
}

void ICACHE_FLASH_ATTR tcp_client_init(void)
{
struct ip_info info;
wifi_get_ip_info(STATION_IF,&info);
const char remote_ip[4]={192,168,0,1};
esp_tcp *tcp_client = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
user_tcp_client_espconn.type = ESPCONN_TCP;
user_tcp_client_espconn.proto.tcp=tcp_client;
tcp_client->local_port=espconn_port();
os_memcpy(tcp_client->local_ip,&info.ip,sizeof(remote_ip));
tcp_client->remote_port=espconn_port();
os_memcpy(tcp_client->remote_ip,remote_ip,sizeof(remote_ip));
//设置连接成功回调
espconn_regist_connectcb(&user_tcp_client_espconn,user_tcp_connect_cb);
//设置连接失败回调
espconn_regist_reconcb(&user_tcp_client_espconn,user_tcp_recon_cb);
//启动连接
espconn_connect(&user_tcp_client_espconn);
}

void ICACHE_FLASH_ATTR tcp_service_init(void)
{
struct ip_info info;
wifi_get_ip_info(STATION_IF,&info);
esp_tcp *tcp_service = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
user_tcp_service_espconn.type = ESPCONN_TCP;
user_tcp_service_espconn.state = ESPCONN_NONE;
user_tcp_service_espconn.proto.tcp = tcp_service;
os_memcpy(tcp_service->local_ip,&info.ip,sizeof(struct ip_addr));
tcp_service->local_port=1213;
espconn_regist_connectcb(&user_tcp_service_espconn,user_service_listen);
espconn_regist_reconcb(&user_tcp_service_espconn,user_service_listen_cb);
//启动监听连接
espconn_accept(&user_tcp_service_espconn);
}

void ICACHE_FLASH_ATTR scan_done(void *arg, STATUS status) {
uint8 ssid[33];

if (status == OK) {
struct bss_info *bss_link = (struct bss_info *) arg;
bss_link = bss_link->next.stqe_next; //ignore first

while (bss_link != NULL ) {
os_memset(ssid, 0, 33);
if (os_strlen(bss_link->ssid) <= 32) {
os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid));
} else {
os_memcpy(ssid, bss_link->ssid, 32);
}
os_printf("+CWLAP:(%d,\"%s\",%d,\""MACSTR"\",%d)\r\n",
bss_link->authmode, ssid, bss_link->rssi,
MAC2STR(bss_link->bssid), bss_link->channel);

bss_link = bss_link->next.stqe_next;
}
wifi_init();
//初始化udp
udp_init();
//初始化tcp_client
tcp_client_init();
//初始化tcp_service
tcp_service_init();
} else {
// os_sprintf(temp,"err, scan status %d\r\n", status);
// uart0_sendStr(temp);
}

}

void to_scan(void) {
wifi_station_scan(NULL, scan_done);
}
void ICACHE_FLASH_ATTR check_clients(void)
{
os_timer_disarm(&check_timer);
//获取的所有设备信息
struct station_info * station = wifi_softap_get_station_info();
while(station){
os_printf("mack:"MACSTR" ip:"IPSTR"\n", MAC2STR(station->bssid), IP2STR(&station->ip));
station = STAILQ_NEXT(station, next);
}
wifi_softap_free_station_info(); // Free it by calling functions
}

void ICACHE_FLASH_ATTR wifi_event_callback(System_Event_t *evt)
{
os_printf("event %x\n", evt->event);
switch (evt->event) {
case EVENT_STAMODE_CONNECTED:
os_printf("connect to ssid %s, channel %d\n",
evt->event_info.connected.ssid,
evt->event_info.connected.channel);
break;
case EVENT_STAMODE_DISCONNECTED:
os_printf("disconnect from ssid %s, reason %d\n",
evt->event_info.disconnected.ssid,
evt->event_info.disconnected.reason);
break;
case EVENT_STAMODE_AUTHMODE_CHANGE:
os_printf("mode: %d -> %d\n", evt->event_info.auth_change.old_mode,
evt->event_info.auth_change.new_mode);
break;
case EVENT_STAMODE_GOT_IP:
os_printf("ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR,
IP2STR(&evt->event_info.got_ip.ip),
IP2STR(&evt->event_info.got_ip.mask),
IP2STR(&evt->event_info.got_ip.gw));
os_printf("\n");
break;
case EVENT_SOFTAPMODE_STACONNECTED://ap模式有设备连接wifi
os_printf("station: " MACSTR "join, AID = %d\n",
MAC2STR(evt->event_info.sta_connected.mac),
evt->event_info.sta_connected.aid);
os_timer_disarm(&check_timer);
os_timer_setfn(&check_timer, check_clients, NULL);
os_timer_arm(&check_timer, 1000, NULL);
break;
case EVENT_SOFTAPMODE_STADISCONNECTED:
os_printf("station: " MACSTR "leave, AID = %d\n",
MAC2STR(evt->event_info.sta_disconnected.mac),
evt->event_info.sta_disconnected.aid);
break;
default:
break;
}
}

void user_init() {
struct softap_config config;
uart_init(115200, 115200);
//设置模式为ap+station模式
wifi_set_opmode_current(STATIONAP_MODE);
//获取当前ap信息
wifi_softap_get_config(&config);
os_memcpy(config.ssid,"esp8266",os_strlen("esp8266"));
os_memcpy(config.password,"123456789",os_strlen("123456789"));
config.ssid_len=os_strlen("esp8266");
//设置ap信息
wifi_softap_set_config(&config);
//监听设备状态
wifi_set_event_handler_cb(wifi_event_callback);
//设置初始化完成后回调
system_init_done_cb(to_scan);
}
void user_rf_pre_init() {
}

阅读更多
文章标签: esp8266 物联网
个人分类: 物联网
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭