首页 > 代码库 > wifi 模块

wifi 模块

#include"STC12C5A.H"
#include <string.h>
#include<intrins.h>
#define uint  unsigned int
#define uchar unsigned char
#define Wifi_Buffer_Length 10
sbit LED=P2^0;

int ceshi=1;
uchar temp;
uchar RX_Count=0;
uchar Wifi_Buffer[Wifi_Buffer_Length];
bit flag;
void delay_15s()        //@11.0592MHz
{
    unsigned char i, j, k;

    _nop_();
    _nop_();
    i = 119;
    j = 88;
    k = 45;
    do
    {
        do
        {
            while (--k);
        } while (--j);
    } while (--i);
}


//延时函数
void delay_ms(uint c)
{
    unsigned char i, j;

    _nop_();
    _nop_();
    _nop_();
    i = 11;
    j = 190;
    while(c--)
    {
        do
        {
            while (--j);
        } while (--i);    
    }
}
//波特率发生器
void UartInit(void)        //115200bps@11.0592MHz
{
    PCON &= 0x7F;        //波特率不倍速
    SCON = 0x50;        //8位数据,可变波特率
    AUXR |= 0x04;        //独立波特率发生器时钟为Fosc,即1T
    BRT = 0xFD;        //设定独立波特率发生器重装值
    AUXR |= 0x01;         //串口1选择独立波特率发生器为波特率发生器
    AUXR |= 0x10;        //启动独立波特率发生器
    ES=1;
    EA=1;//需要打开总中断
}
//串口发送函数
void Send_Uart(uchar value)
{
    ES=0;//发送数据之前关闭中断
    SBUF=value;
    while(!TI);//等待发送完毕
    TI=0;       //发送结束 软件置0
    ES=1;
}
//wifi模块设置函数
void ESP8266_Set(uchar *s)
{
    while(*s!=\0)
    {
        Send_Uart(*s);
        s++;            
    }
    delay_ms(300);
}
void Send_String(uchar *s)
{
    while(*s!=\0)
    {
        Send_Uart(*s);
        s++;            
    }
    delay_ms(30);
}
void wifi_send()
{
/*    ESP8266_Set("AT+CWMODE=1");
    ESP8266_Set("AT+RST");
     ESP8266_Set("AT+CWJAP=\"qwe\",\"123456789\"");
    ESP8266_Set("AT+CIPSTART=\"TCP\",\"10.25.97.164\",8001");
    ESP8266_Set("AT+CIPMODE=1");
    ESP8266_Set("AT+CIPSEND");     */
     
    Send_String("AT+CIPSTART=\"TCP\",\"api.yeelink.net\",80\r\n");
    Send_String("AT+CIPSEND=250\r\n");
       Send_String("POST /v1.0/device/351905/sensor/396141/datapoints HTTP/1.1\r\n");
    Send_String("Host: api.yeelink.net\r\n");
       Send_String("Accept: */*\r\n") ;
    Send_String("U-ApiKey: 71c27d52c090e00b65998a2d4dba73e8\r\n");
       Send_String("Content-Length: 14\r\n");
       Send_String("Content-Type: application/x-www-form-urlencoded\r\n");
    Send_String("Connection: close\r\n");
    Send_String("\r\n");
//    display("{\"value\":");
//    Send_Uart(temp/10);
    //Send_Uart(temp%10) ;
    Send_String("{\"value\":");
    Send_Uart(0x30+ceshi/10);
    Send_Uart(0x30+ceshi%10);
    Send_String("  }\r\n");
    Send_String("\r\n");
    Send_String("\r\n");
    ceshi++;
    if(ceshi==100)
        ceshi=0;        
}
void wifi_accept()
{
    Send_String("AT+CIPSTART=\"TCP\",\"api.yeelink.net\",80\r\n");
    Send_String("AT+CIPSEND=250\r\n");
    Send_String("GET /v1.0/device/351905/sensor/396173/datapoints HTTP/1.1\r\n");
    Send_String("Host: api.yeelink.net\r\n");
    Send_String("Accept: */*\r\n");
    Send_String("U-ApiKey: 71c27d52c090e00b65998a2d4dba73e8\r\n");
    Send_String("Content-Length: 0\r\n");
    Send_String("Connection: close\r\n");
    Send_String("\r\n");
    Send_String("print get done.\r\n");
    Send_String("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\r\n");
}
void main()
{
    P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0xff ;
    P2M1 = 0x00;
    P3M0 = 0xff;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;
    UartInit();
    ESP8266_Set("AT+CWJAP=\"qwe\",\"123456789\"\r\n");
    ESP8266_Set("AT+CWJAP=\"qwe\",\"123456789\"\r\n");
    ESP8266_Set("AT+CWJAP=\"qwe\",\"123456789\"\r\n");

    while(1)
    {    
        wifi_send();
        delay_15s();
        wifi_accept();
        delay_15s();
    }
}
void ser() interrupt 4 using 1
{
    ES=0;
    temp=SBUF;
    RI=0;
    if(temp==l)
    {
        RX_Count=0;
    }
    if(RX_Count<4)
    {
        Wifi_Buffer[RX_Count++]=temp;
    }
    else if(Wifi_Buffer[0]==l&&Wifi_Buffer[1]==u&&Wifi_Buffer[3]==")
    {
        Wifi_Buffer[RX_Count++]=temp;    
        if(temp==\n)
        {
            if(Wifi_Buffer[5]==1)
            {
                LED=1;
            }
            else if(Wifi_Buffer[5]==0)
            {
                LED=0;
            }             
            memset(Wifi_Buffer,0,Wifi_Buffer_Length);//清空数组的数据    
        }
    }
    ES=1;

}

今天实验成功了,有点小兴奋

wifi 模块