当前位置:网站首页>Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)

Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)

2022-07-06 04:17:00 DS brother Bruce Lee

1. Function is introduced

The name of the design topic : be based on ZigBee Automatic lighting system design

Use it all together 3 individual CC2530 ZigBee Development board , The codes are ABC.

A The development board acts as a host : A light sensor is connected to it ,ESP8266WIFI modular . adopt ESP8266 And mobile phones APP Communication between ,ESP8266 Create hotspots , Configure to TCP Server mode ; mobile phone APP Connected to the ESP8266 After the hot spot , Connect again ESP8266 Created servers , Complete communication . mobile phone APP You can control... Through buttons on the B,C Development board LED The light switch .

mobile phone APP The automatic lighting mode can be set on the :

  1. Automatic lighting mode : according to A The light sensing module on the development board , Collect light intensity , control B、C On the development board LED Light switch .
  2. Manual mode : mobile phone APP You can choose to control it separately B、C On the development board LED Light switch .
  3. Support setting the light sensing threshold : On the cell phone APP Can be set on A Development board light sensor threshold ( That is, the threshold value for controlling the light on and off ).
  4. Support setting brightness , It can also automatically control the brightness according to the ambient light

B The development board acts as a slave node : B On board... Is used on the development board LED The lamp , Simulate the lighting of the bedroom at home , Achieve control effect demonstration .

C The development board acts as a slave node : C On board... Is used on the development board LED The lamp , Simulate the lighting of the living room at home , Achieve control effect demonstration .

#yyds Dry inventory # be based on CC2530(ZigBee) Designed automatic lighting system _ Development board

#yyds Dry inventory # be based on CC2530(ZigBee) Designed automatic lighting system _ Development board _02

#yyds Dry inventory # be based on CC2530(ZigBee) Designed automatic lighting system _ Development board _03

Complete project code download address : https://download.csdn.net/download/xiaolong1126626497/75317115

2. Hardware introduction

2.1 ESP8266 wifi

Adopted ESP8266 Series wireless modules are cost-effective WIFI SOC module , This series of modules support standard IEEE802.11b/g/n agreement , Built in complete TCP/IP Protocol stack . Users can use this series of modules to add networking functions to existing devices , You can also build an independent network controller .

CC2530 The development board itself has a ESP8266 Dedicated interface , Just plug it in .

ES8266 WIFI Module connected to A serial port 1 above (P0.4/P0.5 mouth ) , The baud rate is 115200 .

CC2530 Serial port 0 As a routine debugging serial port (P0_2 and P0_3 port ), You can take the collected AD Print data to serial port .
You can also print other debugging information by yourself .

#yyds Dry inventory # be based on CC2530(ZigBee) Designed automatic lighting system _ Development board _04

#yyds Dry inventory # be based on CC2530(ZigBee) Designed automatic lighting system _ Development board _05

#yyds Dry inventory # be based on CC2530(ZigBee) Designed automatic lighting system _CC2530_06

2.2 Photosensitive sensors

#yyds Dry inventory # be based on CC2530(ZigBee) Designed automatic lighting system _CC2530_07

#yyds Dry inventory # be based on CC2530(ZigBee) Designed automatic lighting system _ Development board _08

3. Project code

3.1 Serial port code

#include "uart.h"
/*  The functionality : A serial port 0 initialization  */
void Init_Uart0(void)
{
  PERCFG&=~(1<<0);  // A serial port 0 The pins of the are mapped to positions 1, namely P0_2 and P0_3
  P0SEL|=0x3<<2;   // take P0_2 and P0_3 Set the port as a peripheral function 
  U0BAUD = 216;     //32MHz System clock generation 115200BPS Baud rate 
  U0GCR&=~(0x1F<<0);// Clear baud rate index 
  U0GCR|=11<<0;      //32MHz System clock generation 115200BPS Baud rate 
  U0UCR |= 0x80;    // No flow control ,8 Bit data , Clear buffer 
  U0CSR |= 0x3<<6;  // choice UART Pattern , Enable the receiver 
}


/*  The functionality :UART0 Send string function  */
void UR0SendString(char *str)
{
 while(*str!='\0')
  {
    U0DBUF = *str;    // About to send 1 Byte data write U0DBUF
    while(UTX0IF == 0);// Wait for the data to be sent 
    UTX0IF = 0;       // Clear the send complete flag , Prepare for the next send 
    str++;
  }
}

/*  The functionality :  imitation printf Style formatting and printing function  */
char USART0_PRINT_BUFF[200]; // Format data cache data 
void USART0_Printf(const char *format,...)
{
  char *str=NULL;
  /*1.  Format conversion */
  va_list ap; // va_list---->char *
  va_start(ap,format); // Initialization parameter list 
  vsprintf(USART0_PRINT_BUFF,
        format,
        ap); // Format printing 
  va_end(ap); // End parameter acquisition 
  /*2.  Serial printing */
  str=USART0_PRINT_BUFF;// Pointer assignment 
  while(*str!='\0')
  {
    U0DBUF=*str; // Send a byte of data 
    str++; // The pointer increases by itself , Point to the next data 
    while(UTX0IF == 0);// Wait for the data to be sent 
    UTX0IF = 0;       // Clear the send complete flag , Prepare for the next send 
  }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.

3.2 Key code

#include "key.h"

/*  The functionality : Key IO Mouth initialization   Hardware connection :KEY1-->P0_1 KEY2-->P2_0 */
void KEY_Init(void)
{
    P0SEL&=~(0x1<<1); // To configure P0_1 In general GPIO Mouth mode 
    P0DIR&=~(0x1<<1); // To configure P0_1 Is the input mode 
    P0INP|= 0x1<<1;   // Pull up  
    
    P2SEL&=~(0x1<<0); // To configure P2_0 In general GPIO Mouth mode 
    P2DIR&=~(0x1<<0); // To configure P2_0 Is the input mode 
    P2INP|= 0x1<<0;   // Pull up  
}

/*  The functionality : Key scan   return   return   value : The value of the key pressed  */
unsigned char Key_Scan(void)
{
    static unsigned char stat=1;
    if((KEY1==0||KEY2==0)&&stat)
    {
       stat=0;
       delay10ms();
       if(KEY1==0)return 1;
       if(KEY2==0)return 2;
    }
    else
    {
        if(KEY1&&KEY2)stat=1;
    }
    return 0;
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

3.3 The time delay function

#include "delay.h"

/* Time delay 200 millisecond */
void delay200ms(void)   // error  -0.125us
{
    unsigned char a,b,c;
    for(c=95;c>0;c--)
        for(b=181;b>0;b--)
            for(a=14;a>0;a--);
}


void delay10ms(void)   // error  0us
{
    unsigned char a,b,c;
    for(c=193;c>0;c--)
        for(b=118;b>0;b--)
            for(a=2;a>0;a--);
}

/****************************************** *  Function description :32M Millisecond delay function under system clock  ******************************************/  
void DelayMs(unsigned int ms)  
{  
  unsigned int i,j;  
  for(i = 0; i < ms; i++)  
  {  
    for(j = 0;j < 1774; j++);  
  }  
}  

void Onboard_wait( int timeout )
{
  while (timeout--)
  {
    asm("NOP");
    asm("NOP");
    asm("NOP");
  }
}

// The time delay function 
void DelayUs(int us) //1 us Time delay 
{
    Onboard_wait(us);   
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.

3.4 A Development board ( The coordinator ) Main function code

/* The main function */
void main(void)
{
    u8 time_cnt=0;
    unsigned char key;
    u8 switch_val=150;  // Switch threshold 
    
    u8 mode=1; //0  Represents manual mode  1 Indicates automatic mode 
    
    // Delay waiting for the system to stabilize 
    DelayMs(1000);
    DelayMs(1000);
    
    init_clk();   // Configure clock 
    LED_Init();   // initialization LED Light control IO mouth 
    KEY_Init();   // Key initialization 
    Init_Uart0(); // Initialize serial port 0
    RF_Init();    //RF initialization 
    Init_Uart1(); // Initialize serial port 1-- Connect WIFI
    SetWifi();  // Reset WIFI
    SetESP8266_AP_TCP_Server(); // initialization WIFI
    adc_Init();
    
    clearBuffU1(); // Empty BUFF
    
    // Prompt for normal operation 
    USART0_Printf(" What is currently running is A Development board program , Everything is all right .\r\n");
          
    while(1)          
    {  
       // Key detection 
       key=Key_Scan();
       if(key)
       {
          LED2 = !LED2;
       }  
       
/*  Data sent by the server  value:100 // threshold  led1_on // Bedroom LED open  led1_off // Bedroom LED Turn off  led2_on // A living room LED open  led2_off // A living room LED Turn off  yes_auto_mode // Automatic mode  no_auto_mode // Manual mode  */ 
       // Determine whether you have received WIFI The data of 
       if(lenU1>=13)
       {
           RecdataU1[lenU1]='\0';
           
           // Serial port print data 
           USART0_Printf("WIFI Data received :%s\n",(u8*)RecdataU1); 
           
           // Bedroom LED open 
           if(strstr((char*)RecdataU1,"led1_on"))
           {
                USART0_Printf(" Bedroom LED open .\n"); 
                u8 buff[100]="B:1---";
                // send data 
                tx_data(buff,strlen((char*)buff));
                // Operation status light 
                LED1 = !LED1; 
           }
           // Bedroom LED Turn off 
           else  if(strstr((char*)RecdataU1,"led1_off"))
           {
                USART0_Printf(" Bedroom LED Turn off .\n"); 
                 u8 buff[100]="B:0---"; 
                // send data 
                tx_data(buff,strlen((char*)buff));
                // Operation status light 
                LED1 = !LED1; 
           }
            // A living room LED open 
           else  if(strstr((char*)RecdataU1,"led2_on"))
           {
                 USART0_Printf(" A living room LED open .\n"); 
                 u8 buff[100]="C:1---"; 
                // send data 
                tx_data(buff,strlen((char*)buff));
                // Operation status light 
                LED1 = !LED1; 
           }
            // Automatic mode 
           else  if(strstr((char*)RecdataU1,"yes_auto_mode"))
           {
                 USART0_Printf(" Set to automatic mode .\n"); 
                 mode=1;
           }
            // Manual mode 
           else  if(strstr((char*)RecdataU1,"no_auto_mode"))
           {
                  USART0_Printf(" Set to manual mode .\n"); 
                 mode=0;
           }
           // threshold 
           else  if(strstr((char*)RecdataU1,"value:"))
           {
                // Operation status light 
                LED1 = !LED1; 
                
                char *p=strstr((char*)RecdataU1,"value:");
                if(p)
                {
                    int a=atoi(p+6);
                    if(a>0)
                    {
                      switch_val=a;  // Get a new threshold 
                    }
                }
                
                USART0_Printf(" Set a new threshold :%d.\n",switch_val); 
           }
           
           clearBuffU1(); // Clean cache 
       }
       
     
        time_cnt++;
        DelayMs(10);
           
       //1 Seconds to arrive 
       if(time_cnt>100)
       {
           time_cnt=0;
           
          // Read ADC value -- That is, the value of the light sensor 
          GasData = ReadGasData();
          
          // Send the value of the light sensor to the upper computer 
          USART0_Printf(" Illuminance :%d, Pattern :%d\n",GasData,mode);
          
          // If it's automatic mode 
          if(mode)
          {
               // Judge whether to open or close the bedroom according to the threshold value , In the living room LED The lamp 
               if(GasData  >  switch_val )
               {
                    // turn on the light  
                    u8 buff1[10]="B:1---"; 
                    // send data 
                    tx_data(buff1,strlen((char*)buff1));
                      // turn on the light  
                    u8 buff2[10]="C:1---"; 
                    // send data 
                    tx_data(buff2,strlen((char*)buff2));
               }
               else
               {
                      // Turn off the lights  
                    u8 buff1[10]="B:0---"; 
                    // send data 
                    tx_data(buff1,strlen((char*)buff1));
                      // Turn off the lights  
                    u8 buff2[10]="C:0---"; 
                    // send data 
                    tx_data(buff2,strlen((char*)buff2));    
               }
          }
       }
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
原网站

版权声明
本文为[DS brother Bruce Lee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132234361822.html