找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1734|回复: 1
打印 上一主题 下一主题
收起左侧

基于单片机的远程自动农业灌溉--连江设计

[复制链接]
跳转到指定楼层
楼主
ID:530391 发表于 2019-9-10 07:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

基本原理
程序设置有自动手动模式,可用远程相关代码控制模式的切换,自动模式可检测土壤湿度然后灌溉。
代码
#include <reg52.h>
#include <intrins.h>
                      //包含头文件
#define uint unsigned int
#define uchar unsigned char
#define RELOAD_COUNT 0xFA         //宏定义波特率发生器的载入值
//define RELOAD_COUNT (256-(((11520000/16)/12)/9600)) 也可以或波特率9600 //256-晶振频率/波特率x16=BRT


sfr AUXR=0x8E;
sfr BRT=0x9C;
sfr AUXR1=0xA2;
uchar Receive;
uchar i;                             
uint n;
uchar Recive_table[15];        //用于接收wifi模块反馈到MCU上的数据
sbit sd0 = P0^0;  //湿度端口
sbit sd1 = P0^1;
sbit sd2 = P0^2;
sbit sd3 = P0^3;


//继电器端口
sbit jd1 = P2^0;
sbit jd2 = P0^5;
sbit jd3 = P0^6;
sbit jd4 = P0^7;


uchar k;  //  开水代码
uchar m;        //        关税代码


sbit LED0 = P1^0;


void ms_delay(uint t)
{
      uint i,j;
      for(i=t;i>0;i--)
       for(j=110;j>0;j--);
}










void us_delay(uchar t)
{
      while(t--);
}














void Uart_Init()//使用定时器1作为波特率发生器(STC89C52、STC89C51、AT89C51或者STC12C560S2等均可)
{
      SCON=0x50;     //设置为串行口以方式1工作,8位异步通讯,允许接收中断。
      //一帧信息为10位,1位起始位,8位数据位(低位在先),1位停止位。
      PCON=0x80;     //SMOD波特率选择位为1,SMOD=1.
      TMOD=0x21;     //设置定时器1为波特率发生器,工作在模式2,8位自动装载
      TH1=RELOAD_COUNT;//波特率9600 ,TH1=256-FOSC/16/12/波特率
      TL1=TH1;
      EA=1;                            //总中断打开
      ES=0;                             //关闭串口中断
      TR1=1;                     //启动定时器1
}
















void Send_Uart(uchar value)
{
      ES=0;         //关闭串口中断
      TI=0;         //清发送完毕中断请求标志位
      SBUF=value;     //发送
      while(TI==0);   //等待发送完毕
      TI=0;         //清发送完毕中断请求标志位
      ES=1;         //允许串口中断
}












void ESP8266_Set(uchar *puf) // 数组指针*puf指向字符串数组               
{


      while(*puf!='\0')    //遇到空格跳出循环
      {
           Send_Uart(*puf);  //向WIFI模块发送控制指令。
           us_delay(5);
           puf++;      
      }
      us_delay(5);
      Send_Uart('\r');//回车
      us_delay(5);
      Send_Uart('\n');   //换行
      ms_delay(50);
}  


//四位数据发送

void ESP8266_Sent(uchar *puf)      // 数组指针*puf指向字符串数组               
{
      ESP8266_Set("AT+CIPSEND=0,4");
           ms_delay(30);
      while(*puf!='\0')    //遇到空格跳出循环
      {
           Send_Uart(*puf);   //向WIFI模块发送控制指令。
           us_delay(5);
           puf++;      
      }
      us_delay(5);
      Send_Uart('\n');   //换行
      ms_delay(10);
}   


//六位数据发送


void ESP82661_Sent(uchar *puf)      // 数组指针*puf指向字符串数组               
{
      ESP8266_Set("AT+CIPSEND=0,10");
           ms_delay(100);
      while(*puf!='\0')    //遇到空格跳出循环
      {
           Send_Uart(*puf);   //向WIFI模块发送控制指令。
           us_delay(5);
           puf++;      
      }
      us_delay(5);
      Send_Uart('\n');   //换行
          Send_Uart('\n');   //换行
      ms_delay(100);
}   






//多位数据发送


void ESP826612_Sent(uchar *puf)      // 数组指针*puf指向字符串数组               
{
      ESP8266_Set("AT+CIPSEND=0,40");
           ms_delay(100);
      while(*puf!='\0')    //遇到空格跳出循环
      {
           Send_Uart(*puf);   //向WIFI模块发送控制指令。
           us_delay(5);
           puf++;      
      }
      us_delay(5);
      Send_Uart('\n');   //换行
          Send_Uart('\n');   //换行
      ms_delay(100);
}   










void main()
{         jd1=0;
        sd0=1 ;
     LED0=0;          //gongzuozhishi
       ms_delay(5000)  ;
                   LED0=1;
                   ms_delay(1000)         ;
      LED0=0;//关闭LED灯
      Uart_Init();//使用独立的波特率发生器
//   ESP8266_Set("AT+CWMODE=2"); //设置路由器模式 1 station模式 2 AP
//点 路由器模式 3 station+AP混合模式
       // ms_delay(1000)         ;
//ESP8266_Set("AT+RST");     //重新启动wifi模块


       ms_delay(100)         ;


ESP8266_Set("AT+CIOBAUD=9600");     //重新启动wifi模块
                                         ms_delay(1000)         ;


   //       ESP8266_Set("AT+CWSAP=\"WIFI\",\"1234567890\",11,4");
//设置模块SSID:WIFI, PWD:密码 及安全类型加密模式(WPA2-PSK)
ESP8266_Set("AT+CIPMUX=1");//开启多连接模式,允许多个各客户端接入


         ms_delay(1000)         ;


ESP8266_Set("AT+CIPSERVER=1,8899");  //启动TCP/IP 端口为8080 实现基于网络//控制
                   ms_delay(1000)         ;
          ESP8266_Sent("连接");
                   ms_delay(1000)         ;
          ESP82661_Sent("即将开始  ");
                   ms_delay(2000)         ;
          ESP82661_Sent("初始化完成");




      ES=1;
           m=0;
      LED0=1;//关闭LED灯
          ms_delay(1000) ;
         
          ESP82661_Sent("进入循环扫");        
                                                                                         //允许串口中断
      while(1)
      {  
//sbit sd0 = P0^0;
//sbit sd1 = P0^1;
//sbit sd2 = P0^2;
//sbit sd3 = P0^3;








  if(m==0)
{
ESP82661_Sent("进入自动  ");


                     ms_delay(1000) ;




        if(sd0==1)
        {


           jd1=0;
           ESP8266_Sent("开水");


        }


         if(sd0==0)
        {


           jd1=1;
          ESP8266_Sent("关水");
           ms_delay(1000) ;
           //ESP8266_Sent("关水");


        }
}




  if(m==1)
{
          ESP82661_Sent("进入远程  ");


                     ms_delay(1000) ;


        if(k==11)
        {


           jd1=0;
           ESP8266_Sent("开水");


        }


         if(k==12)
        {


           jd1=1;
          ESP8266_Sent("关水");
           ms_delay(1000) ;
           //ESP8266_Sent("关水");


        }


}


  /*


        if(k==12)
        {


           jd1=1;
           ESP8266_Sent("  12");
          ms_delay(2000)         ;
           ESP8266_Sent("来水");


        }




         if(k==13)
        {
           sd0=0;
           jd1=0;
          ESP8266_Sent("1313");
         // ms_delay(2000)         ;
          // ms_delay(1000) ;
           //ESP8266_Sent("关水");
            sd0=1;
        }




*/


































//sd0=1;


}         
}









void uart() interrupt 4
{
  static uchar i=0;


    if(RI == 1)   


    {


        RI = 0;     //清除串口接收标志位


        Recive_table[ i]=SBUF;[ i]


        if(Recive_table[0]=='+')


        {


                i++;


        }


        else


        {


                i=0;


        }












        if(i>10)
        {
//                 if(Recive_table[7]=='1'||Recive_table[7]=='2')
                 {
            i=0;
                    }
                if(Recive_table[7]=='1')
                {
//                 ESP8266_Sent("7wei");
                        
                switch(Recive_table[9])
                {


                        case '1':


                                LED0=0;
                                ESP8266_Sent("循1");
        
                                break;


                        case '2':


                                LED0=1;
                                ESP8266_Sent("循2");
                                break;


                        case '3':
                                  ESP8266_Sent("循3");
                                LED0=0;


                                break;


                        case '4':


                                LED0=1;
                                  ESP8266_Sent("循4");
                                break;


                        case '5':


                                LED0=0;
                                    ESP8266_Sent("循5");
                                break;


                        case '6':


                                LED0=1;
                                    ESP8266_Sent("循6");
                                break;
               
                //        case '0':
         //
                //                LED0=1;
                //                    ESP8266_Sent("循0");
                //                break;


                        case '7':


                                LED0=0;
                                    ESP8266_Sent("循7");
                                break;




                        case '8':


                                LED0=1;
                                ESP8266_Sent("循8");
                                break;




                        case '9':


                                LED0=0;
                                    ESP8266_Sent("循9");
                                break;






                }        //switchjieshu
        }


           /*/
                         if(Recive_table[10]=='2')
    {
                                  ESP8266_Sent("10w2");


           }
                 */


         else if(Recive_table[7]=='2')
    {
                switch(Recive_table[10])


                {
                         case '0':




                                LED0=0;


                                k=10;


                                break;




                        case '1':


                                LED0=1;
                                   k=11;
//                         ESP8266_Sent("循11");
                                break;


                        case '2':


                                LED0=0;
                                   k=12;
                        //        ESP8266_Sent("循12");
                                break;


                        case '3':
                                 // ESP8266_Sent("循13");
                                 m=1;
                                 k=13;
                                LED0=1;


                                break;


                        case '4':


                                LED0=0;
                                m=0;
                         //          ESP8266_Sent("循14");
                                break;


                        case '5':


                                LED0=0;
                                  ESP8266_Sent("循15");
                                break;


                        case '6':


                                LED0=1;
                                  ESP8266_Sent("循16");
                                break;


                }        //switchjieshu
          }        ;
















        










                }        //switchjieshu














   


  }


}
















/*
void Uart_Interrupt() interrupt 4        
{
  static uchar i=0;
  
      if(RI==1)
      {
           RI=0;
      Receive=SBUF;        //MCU接收wifi模块反馈回来的数据        


      Recive_table[15]=Receive;              
           i++;         
           if((Recive_table[i-1]=='\n'))i=0;


                     //遇到换行 重新装值
                  
      }
      else TI=0;        
}
*/


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:505058 发表于 2021-4-7 19:00 | 只看该作者
请问有完整的工程文件和仿真吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表