ESP8266编程实例,连接Wi-Fi,访问网页。
串口初始化
(以station模式) 接收数据
os_timer_t *mytimer; //定义一个回调函数 void Mytime_Fuction(void) { struct ip_info info; uint32 add; //判断是否连接成功路由器,并且获得了IP if(wifi_station_get_connect_status() == STATION_GOT_IP) { //将ESP8266的IP放入info结构体中 wifi_get_ip_info(0x00,&info); //比如192.168.1.2 //info.ip.addr = (2<<24)|(1<<16)|(168<<8)|192 add = info.ip.addr; uart0_sendStr("\r\nConnect WiFi Success!\r\n"); //以TCP方式,连接服务器,端口80 //具体实现方法在user_internet.c中 User_TCP_Client_Init("192.168.199.249", 80); } else { os_timer_arm(&mytimer, 1000, 0); //启动定时器, 周期 1S } } //串口初始化程序 void USART_init(void) { //初始化两个串口为波特率19200 uart_init(BIT_RATE_19200,BIT_RATE_19200); //发送字符串数据 uart0_sendStr("\r\n"); uart0_sendStr("\r\n=========================="); uart0_sendStr("\r\n$$ 开机 $$"); uart0_sendStr("\r\n=========================="); } //主程序入口 void user_init(void) { os_printf("SDK version:%s\n", system_get_sdk_version()); //串口初始化 USART_init(); //设置为station模式 wifi_set_opmode(0x01); //配置路由器登陆信息 char ssid[32] = "SSID"; char password[64] = "PASSWORD"; struct station_config stationConf; //设置为station模式 stationConf.bssid_set = 0; os_memcpy(&stationConf.ssid, ssid, 32); os_memcpy(&stationConf.password, password, 64); //开始连接 wifi_station_set_config(&stationConf); /* *下面是软件定时程序 *需要注意的是 *ESP8266需要定时喂狗 *所以在主程序中不要使用while(1)循环 *一定需要要定时喂狗 */ //配置之前需要先将"时间结构体"重新初始化 os_timer_disarm(&mytimer); //配置回调函数及其参数,会自动调用Mytime_Fuction()函数 os_timer_setfn(&mytimer, (ETSTimerFunc *) Mytime_Fuction,NULL); //启动定时器, 周期1S,定时一次 os_timer_arm(&mytimer, 1000, 0); }
extern struct espconn user_espconn; //TCP客户端初始化,主函数调用 void ICACHE_FLASH_ATTR User_TCP_Client_Init(char * ipAddress, int32_t port) { char ipTemp[128]; uint32_t ip = 0; user_contype.pCon = (struct espconn *) os_zalloc(sizeof(struct espconn)); user_contype.pCon->state = ESPCONN_NONE; user_contype.linkId = 0; ip = ipaddr_addr(ipAddress); user_contype.pCon->type = ESPCONN_TCP; user_contype.pCon->proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp)); user_contype.pCon->proto.tcp->local_port = espconn_port(); user_contype.pCon->proto.tcp->remote_port = port; os_memcpy(user_contype.pCon->proto.tcp->remote_ip, &ip, 4); user_contype.pCon->reverse = &user_contype; //连接成功回调函数 espconn_regist_connectcb(user_contype.pCon, InterNet_Connect_Cb); //重连回调函数 espconn_regist_reconcb(user_contype.pCon, InterNet_Reconnect_Cb); //断开连接回调函数 espconn_regist_disconcb(user_contype.pCon, InterNet_Disconnect_Cb); //接受数据回调函数 espconn_regist_recvcb(user_contype.pCon, InterNet_Receive); //发送数据成功回调函数 espconn_regist_sentcb(user_contype.pCon, InterNet_Send_Cb); espconn_connect(user_contype.pCon); } //连接服务器成功回调函数 //通过TCP连接成功服务器后调用 static void ICACHE_FLASH_ATTRInterNet_Connect_Cb(void *arg) { uart0_sendStr("\r\nConnect Cb\r\n"); /*发送HTTP包 *GET /app/get.php?IP=2 HTTP/1.1 *Host: 192.168.199.249 *User-Agent: ESP8266 * */ //HTTP包需要以一个空回车结束 InterNet_TCP_SendData("GET /app/get.php?IP=2 HTTP/1.1\r\nHost: 192.168.199.249\r\nUser-Agent: ESP8266\r\n\r\n",0); } //TCP方式发送数据 void ICACHE_FLASH_ATTRInterNet_TCP_SendData(uint8 *buf, uint16 len) { uint8 *str = buf; //如果长度为0,自动判断发送数据长度 if(len == 0) { len = 0; while (*str) { str++; len++; } } espconn_sent(user_contype.pCon, buf, len); } //数据发送成功回调函数 static void ICACHE_FLASH_ATTR InterNet_Send_Cb(void *arg) { uart0_sendStr("\r\nInternet Send Success \r\n"); } //接受回调函数 static void ICACHE_FLASH_ATTR InterNet_Receive(void *arg, char *pdata, unsigned short len) { uart0_sendStr("\r\nReceive Data:\r\n"); //接受到的数据从串口发送 uart0_sendStr(pdata); return; } |