本帖最后由 gxy5057232 于 2015-12-24 09:47 编辑
申请开发板时准备用做一个智能家居的控制中心,需要的时间太长,很快就要到发实战的截至日期了,就先做了个DEMO功能出来,不然板子就得被收回了~
主要功能:
通过手机或电脑在局域网内控制 469 Discovery 的5个LED,包括 LD1 LD2 LD3 LD4 LD7, LD7。
硬件组成:
STM32F469 Discovery + LBP100 WIF模块
LBP100 WIF模块由上海汉枫生产,可以通过 smart link 软件来配置其连接到路由器,具体的资料可以从其官网下载。外观如下:
通过漆包线连接排针插到 STM32F469 Discovery 的Arduino uno connector接口,电路如下:
模块控制引脚连接:
电源引脚:
实物图:
USB转UART接口用来监测WIFI模块向MCU返回的数据。
使用 ST Link 的虚拟串口作为调试信息打印接口。
软件主要流程:
1初始化LED
2配置WIFI模块
3搭建TCP SERVER
4解析、执行指令
主函数:
int main(void)
{
debugInit();
debug_log("*****************21ic*****************\r\n");
debug_log("* STM32F469IN_Discovery Board ʵսÈÕ¼Ç 1 *\r\n");
debug_log("* ---------- ÖÇÄÜLED Demo ---------- *\r\n");
debug_log("************************************************\r\n");
debug_log("all led init\r\n");
all_led_Init();
debug_log("wifi init\r\n");
wifiInit();
while(1)
{
ledControlDetect();
delay(500);
}
}
wifi配置过程:void wifiInit(void)
{
LPB100_IO_init(); //³õʼ»¯IO
LPB100_RELOAD_PIN_SET(); //À¸ß nReload ·ÀÖ¹WIFIÄ£¿é»Ö¸´³ö³§ÉèÖÃ
LPB100_reset(); //¸´Î»WIFIÄ£¿é
//µÈ´ý WIFI ready
while(1 == LPB100_READY_PIN_STA()) //LPB100 Æô¶¯Íê³Éºó Ready PIN Êä³ö0
{
debug_log("wait wifi ready...\r\n");
delay(5000);
}
debug_log("wifi is ready\r\n");
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) // userKey °´Ï WIFI»Ö¸´³ö³§ÉèÖÃ
{
debug_log("clear LPB100\r\n");
LPB100_RELOAD_PIN_RESET();
delay(30000);
LPB100_RELOAD_PIN_SET();
delay(10000);
//µÈ´ý WIFI ready
while(1 == LPB100_READY_PIN_STA()) //LPB100 Æô¶¯Íê³Éºó Ready PIN Êä³ö0
{
debug_log("wait wifi ready...\r\n");
delay(5000);
}
debug_log("wifi is ready\r\n");
}
wifi_link_detect(); //wifi Á¬½Ó¼ì²â
wifi_tcpServer_init(); //¾ÖÓòÍø TCP SERVER ÉèÖÃ
intoDirectTranfser(); //wifi Ä£¿é»Øµ½Í¸´«Ä£Ê½
debug_log("wifi init ok\r\nmain loop\r\n");
}
简单的写了WIFI模块的连接检测
void wifi_link_detect(void)
{
uint8_t sta = 0;
uint16_t height_count = 0;
uint16_t low_count = 0;
uint32_t total_count = 0;
debug_log("link detect\r\n");
while(1)
{
switch(sta)
{
case 0:
{
if(LPB100_LINK_PIN_STA() == 0) // link pin Ϊ0
{
low_count++;
height_count = 0;
if(low_count > 15) //link Á¬Ðø15´ÎΪ 0 ÈÏΪÁ¬½Óµ½Â·ÓÉÆ÷
{
debug_log("wifi link ok\r\n");
return;
}
}
else
{
low_count = 0;
height_count++;
if(height_count == 30) //Á¬Ðø30´ÎΪ1 ÈÏΪÁ¬½Ó²»µ½Â·ÓÉÆ÷ ½øÈë smart link
{
LPB100_RELOAD_PIN_RESET();
delay(1000);
LPB100_RELOAD_PIN_SET();
low_count = 0;
height_count = 0;
sta = 1;
debug_log("\r\nsmart link\r\n\r\n");
}
}
}break;
case 1:
{
if(LPB100_LINK_PIN_STA() == 0) // link pin Ϊ 0
{
low_count++;
height_count = 0;
if(low_count > 15) //link Á¬Ðø15´ÎΪ0 ÈÏΪÁ¬½Óµ½Â·ÓÉÆ÷
{
debug_log("\r\nsmart link ok\r\n\r\n");
return;
}
}
else
{
low_count = 0;
height_count++;
}
}break;
}
total_count++;
delay(500);
if((total_count%4) == 0)
debug_log("total:%d low:%d height:%d\r\n",total_count,low_count,height_count);
}
}
TCP SERVER设置代码:
//ÉèÖÃWIFI×öTCP SERVER
void wifi_tcpServer_init(void)
{
uint8_t ipMode;
uint32_t ip[4];
uint32_t mask[4];
uint32_t gateway[4];
//½øÈëATÖ¸Áîģʽ
intoATmode();
//¹Ø±Õ»ØÏÔ
delay(500);
wifi_printf("AT+E=off\n");
if(wifi_read_wann(&ipMode,ip,mask,gateway))
{
debug_log("%c\r\n",ipMode);
if(ipMode == 'D')
{
debug_log("IP_MODE:DCHP\r\n");
}
else
{
debug_log("IP_MODE:static\r\n");
}
debug_log("ip:%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
debug_log("mask:%d.%d.%d.%d\r\n",mask[0],mask[1],mask[2],mask[3]);
debug_log("gateway:%d.%d.%d.%d\r\n",gateway[0],gateway[1],gateway[2],gateway[3]);
//ÉèÖÃ
delay(500);
wifi_printf("AT+NETP=TCP,SERVER,8899,%d.%d.%d.%d\n",ip[0],ip[1],ip[2],ip[3]);
}
else
{
debug_log("get wann err\r\n");
}
}
协议解析代码:
//"ledNum":"1","sta":"0"
void ledControlDetect(void)
{
uint32_t val;
uint32_t ret;
uartScanf(&ret,500,"ledNum\":\"%d",&val);
if(!ret)
{
return;
}
switch(val)
{
case 1:
{
uartScanf(&ret,500,"sta\":\"%d",&val);
if(ret)
{
debug_log("LED1 ");
if(val == 0)
{
LD1_CLOSE();
debug_log("close\r\n");
}
else if(val == 1)
{
LD1_OPEN();
debug_log("open\r\n");
}
wifi_printf("LED1 OK\r\n");
}
}break;
case 2:
{
uartScanf(&ret,500,"sta\":\"%d",&val);
if(ret)
{
debug_log("LED2 ");
if(val == 0)
{
LD2_CLOSE();
debug_log("close\r\n");
}
else if(val == 1)
{
LD2_OPEN();
debug_log("open\r\n");
}
wifi_printf("LED2 OK\r\n");
}
}break;
case 3:
{
uartScanf(&ret,500,"sta\":\"%d",&val);
if(ret)
{
debug_log("LED3 ");
if(val == 0)
{
LD3_CLOSE();
debug_log("close\r\n");
}
else if(val == 1)
{
LD3_OPEN();
debug_log("open\r\n");
};
wifi_printf("LED3 OK\r\n");
}
}break;
case 4:
{
uartScanf(&ret,500,"sta\":\"%d",&val);
if(ret)
{
debug_log("LED4 ");
if(val == 0)
{
LD4_CLOSE();
debug_log("close\r\n");
}
else if(val == 1)
{
LD4_OPEN();
debug_log("open\r\n");
}
wifi_printf("LED4 OK\r\n");
}
}break;
case 7:
{
uartScanf(&ret,500,"sta\":\"%d",&val);
if(ret)
{
debug_log("LED7 ");
if(val == 0)
{
LD7_CLOSE();
debug_log("close\r\n");
}
else if(val == 1)
{
LD7_OPEN();
debug_log("open\r\n");
}
wifi_printf("LED7 OK\r\n");
}
}break;
default:wifi_printf("err cmd\n");break;
}
}
为了和HTTP协议兼容 指令格式采用了json格式: "ledNum":"1","sta":"0" 通过 scanf()进行解析。
软件运行LOG:
smart link 完成 TCP SERVER 建立完成
smart link软件:
手机端发指令:
收到恢复后 对应的LED亮起:
|