• wifi 模块Yeelink联网


    #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!='')
        {
            Send_Uart(*s);
            s++;            
        }
        delay_ms(300);
    }
    void Send_String(uchar *s)
    {
        while(*s!='')
        {
            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
    ");
        Send_String("AT+CIPSEND=250
    ");
           Send_String("POST /v1.0/device/351905/sensor/396141/datapoints HTTP/1.1
    ");
        Send_String("Host: api.yeelink.net
    ");
           Send_String("Accept: */*
    ") ;
        Send_String("U-ApiKey: 71c27d52c090e00b65998a2d4dba73e8
    ");
           Send_String("Content-Length: 14
    ");
           Send_String("Content-Type: application/x-www-form-urlencoded
    ");
        Send_String("Connection: close
    ");
        Send_String("
    ");
    //    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("  }
    ");
        Send_String("
    ");
        Send_String("
    ");
        ceshi++;
        if(ceshi==100)
            ceshi=0;        
    }
    void wifi_accept()
    {
        Send_String("AT+CIPSTART="TCP","api.yeelink.net",80
    ");
        Send_String("AT+CIPSEND=250
    ");
        Send_String("GET /v1.0/device/351905/sensor/396173/datapoints HTTP/1.1
    ");
        Send_String("Host: api.yeelink.net
    ");
        Send_String("Accept: */*
    ");
        Send_String("U-ApiKey: 71c27d52c090e00b65998a2d4dba73e8
    ");
        Send_String("Content-Length: 0
    ");
        Send_String("Connection: close
    ");
        Send_String("
    ");
        Send_String("print get done.
    ");
        Send_String("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
    ");
    }
    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"
    ");
        ESP8266_Set("AT+CWJAP="qwe","123456789"
    ");
        ESP8266_Set("AT+CWJAP="qwe","123456789"
    ");
    
        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=='
    ')
            {
                if(Wifi_Buffer[5]=='1')
                {
                    LED=1;
                }
                else if(Wifi_Buffer[5]=='0')
                {
                    LED=0;
                }             
                memset(Wifi_Buffer,0,Wifi_Buffer_Length);//清空数组的数据    
            }
        }
        ES=1;
    
    }

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

  • 相关阅读:
    SharePoint 2010 新体验3 文档集
    Firebird 修改表名
    C++Builder XE7 up1 简单测试
    Firbird 将可 null 的列更新为 not null
    用delphiXE7 dbExpress Framework提供的功能获取数据表信息
    Linux的基本命令总结
    IOS 隐藏时间条
    最大流 ZQUOJ 10181 && POJ 1273
    最大二分匹配 匈牙利算法模板&&POJ 1469 COURSES
    新加坡第四天下午
  • 原文地址:https://www.cnblogs.com/bixiaopengblog/p/6056204.html
Copyright © 2020-2023  润新知