资讯详情

【Io开发笔记】机智云智能浇花器实战(1)-基础Demo实现

该系统主要由传感器执行部分、无线通信部分、智能云自助开发平台部分三部分组成。传感器执行部分由主控制单元、显示单元和存储单元组成。传感器检测部分收集空气环境参数信息,实时处理收集到的传感器数据,然后将数据发送到无线通信模块。

机智云AIoT利用微服务架构,自助开发平台将大量使用IoT通过傻瓜式开发工具,技术原理、行业知识、基本模型规则化、软件化、模块化MCU代码自动生成,APP开源框架、IoTSDK、开放API,尽量减少IoT设备开发的技术门槛和开发成本,提高开发、测试、部署效率,已服务超过320000 开发者。
GAgent-设备对接平台可在1天内完成标准串口通信程序,加快智能设备网络开发。GAgent是运行在WiFi/蓝牙/5G/4G/NB-IOT等待通信模块的应用程序,使通信模块主动连接平台服务器,实现与云的通信。将通信模块集成在开发人员产品的控制电路板上,只需与通信模块实现串口通信(代码自动生成),即可直接访问使能平台服务器,无需关键底部的网络电缆传输。

原理图

  1. #include "led.h"
  2. #include "systick.h"
  3. void LED_Init(void)
  4. {
  5. RCC_APB2PeriphClockCmd(LED1_CLK, ENABLE);
  6. RCC_APB2PeriphClockCmd(LED2_CLK, ENABLE);
  7. RCC_APB2PeriphClockCmd(LED3_CLK, ENABLE);
  8.         GPIO_InitTypeDef            LED_InitStruct = {0};
  9.         
  10.         LED_InitStruct.GPIO_Pin   = LED1_PIN;
  11.         LED_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;                 
  12.         LED_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;        
  13.         
  14.         GPIO_Init(LED1_PORT, &LED_InitStruct);
  15.         
  16.         LED_InitStruct.GPIO_Pin   = LED2_PIN;
  17.         GPIO_Init(LED2_PORT, &LED_InitStruct);
  18.         
  19.         LED_InitStruct.GPIO_Pin   = LED3_PIN;
  20.         GPIO_Init(LED3_PORT, &LED_InitStruct);
  21.         
  22.         LED1(0);  
  23.   LED2(0);
  24.         LED3(0);
  25. }
  26. void LED_Task(void)
  27. {
  28.         static uint32_t Timer = 0;
  29.         static uint8_t  Sta   = 0;
  30.         if(SoftTimer(Timer,500))
  31.         {
  32.                 Timer=GetSoftTimer();
  33.                 Sta?(Sta=0):(Sta=1);
  34.                 LED1(Sta);
  35.                 LED2(Sta);
  36.                 LED3(Sta);
  37.         }
  38. }
  39. void GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  40. {
  41.         uint32_t ODR;
  42.         ODR = GPIOx->ODR;
  43.         GPIOx->BSRR = ((ODR & GPIO_Pin) << 16U) | (~ODR & GPIO_Pin);
  44. }
复制代码
  1. #ifndef __LED_H_
  2. #define __LED_H_
  3. #include "STM32f10x.h"
  4. //PB9  --- LED1   --- 低有效
  5. //PB8  --- LED2   --- 低有效
  6. //PA3  --- LED3   --- 低有效
  7. //基于STM32标准库  芯片是 STM32F103C8T6
  8. #define LED1_CLK  RCC_APB2Periph_GPIOB
  9. #define LED1_PORT GPIOB
  10. #define LED1_PIN  GPIO_Pin_9
  11. #define LED2_CLK  RCC_APB2Periph_GPIOB
  12. #define LED2_PORT GPIOB
  13. #define LED2_PIN  GPIO_Pin_8
  14. #define LED3_CLK  RCC_APB2Periph_GPIOA
  15. #define LED3_PORT GPIOA
  16. #define LED3_PIN  GPIO_Pin_3
  17. //宏定义的一个开关
  18. #define LED1(X)  X?(GPIO_ResetBits(LED1_PORT,LED1_PIN)):(GPIO_SetBits(LED1_PORT,LED1_PIN))
  19. #define LED2(X)  X?(GPIO_ResetBits(LED2_PORT,LED2_PIN)):(GPIO_SetBits(LED2_PORT,LED2_PIN))
  20. #define LED3(X)  X?(GPIO_ResetBits(LED3_PORT,LED3_PIN)):(GPIO_SetBits(LED3_PORT,LED3_PIN))
  21. void LED_Init(void);
  22. void GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  23. void LED_Task(void);
  24. #endif
复制代码
按键驱动的代码编写
原理图

  1. #include "key.h"
  2. #include "systick.h"
  3. void KEY_Init(void)
  4. {
  5.         RCC_APB2PeriphClockCmd(KEY0_CLK,ENABLE);
  6.         RCC_APB2PeriphClockCmd(KEY1_CLK,ENABLE);
  7.         RCC_APB2PeriphClockCmd(KEY2_CLK,ENABLE);
  8.         
  9.         GPIO_InitTypeDef            KEY_InitStruct;
  10.         KEY_InitStruct.GPIO_Pin   = KEY0_PIN|KEY1_PIN|KEY2_PIN;
  11.         KEY_InitStruct.GPIO_Mode  = GPIO_Mode_IPU;      
  12.         KEY_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  13.         
  14.         GPIO_Init(KEY0_PORT ,&KEY_InitStruct);
  15.         GPIO_Init(KEY1_PORT ,&KEY_InitStruct);
  16.         GPIO_Init(KEY2_PORT ,&KEY_InitStruct);
  17.         
  18. }
  19. static uint8_t Key0Value=0;
  20. static uint8_t Key1Value=0;
  21. static uint8_t Key2Value=0;
  22. void KeyScan(void)
  23. {
  24.         static uint16_t Key0Timer=0;
  25.         static uint16_t Key1Timer=0;
  26.         static uint16_t Key2Timer=0;
  27.         if(KEY0==0)
  28.         {
  29.                 if(Key0Timer<10)
  30.                 {
  31.                         Key0Timer++;
  32.                         if(Key0Timer>=10)
  33.                                 Key0Value=1;
  34.                 }        
  35.         }
  36.         else
  37.         {
  38.                 Key0Timer = 0;
  39.         }
  40.         if(KEY1==0)
  41.         {
  42.                 if(Key1Timer<10)
  43.                 {
  44.                         Key1Timer++;
  45.                         if(Key1Timer>=10)
  46.                                 Key1Value=1;
  47.                 }        
  48.         }
  49.         else
  50.         {
  51.                 Key1Timer = 0;
  52.         }
  53.         if(KEY2==0)
  54.         {
  55.                 if(Key2Timer<10)
  56.                 {
  57.                         Key2Timer++;
  58.                         if(Key2Timer>=10)
  59.                                 Key2Value=1;
  60.                 }        
  61.         }
  62.         else
  63.         {
  64.                 Key2Timer = 0;
  65.         }
  66. }
  67. uint8_t GetKey0(void)
  68. {
  69.         uint8_t Key=Key0Value;
  70.         Key0Value=0;
  71.         return Key;
  72. }
  73. uint8_t GetKey1(void)
  74. {
  75.         uint8_t Key=Key1Value;
  76.         Key1Value=0;
  77.         return Key;
  78. }
  79. uint8_t GetKey2(void)
  80. {
  81.         uint8_t Key=Key2Value;
  82.         Key2Value=0;
  83.         return Key;
  84. }
复制代码
  1. #ifndef __KEY_H_
  2. #define __KEY_H_
  3. #include "stm32f10x.h"
  4. #define KEY0_CLK         RCC_APB2Periph_GPIOA                        
  5. #define KEY0_PORT                     GPIOA                        
  6. #define KEY0_PIN                     GPIO_Pin_0
  7. #define KEY1_CLK         RCC_APB2Periph_GPIOA                        
  8. #define KEY1_PORT                     GPIOA                        
  9. #define KEY1_PIN                     GPIO_Pin_1
  10. #define KEY2_CLK         RCC_APB2Periph_GPIOA                        
  11. #define KEY2_PORT                     GPIOA                        
  12. #define KEY2_PIN                     GPIO_Pin_2
  13. #define KEY0             GPIO_ReadInputDataBit(KEY0_PORT,KEY0_PIN)           
  14. #define KEY1             GPIO_ReadInputDataBit(KEY1_PORT,KEY1_PIN)     
  15. #define KEY2             GPIO_ReadInputDataBit(KEY2_PORT,KEY2_PIN)     
  16. void KEY_Init(void);
  17. void KeyScan(void);
  18. uint8_t GetKey0(void);
  19. uint8_t GetKey1(void);
  20. uint8_t GetKey2(void);
  21. #endif
  22. <b>
  23. </b>
复制代码
  1. #include "main.h"
  2. int main(void)
  3. {
  4.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  5.         USART1_Init(9600);        
  6.         //printf("打印串口初始化 OK !\r\n");        
  7.   SysTick_Init();   
  8.   //printf("系统嘀嗒初始化 OK !\r\n");        
  9.         LED_Init();  
  10.   //printf("状态指示初始化 OK !\r\n");               
  11.   KEY_Init();
  12.   //printf("按键配置初始化 OK !\r\n");        
  13.         while(1)
  14.         {        
  15.                 LED_Task();        
  16.                 if(GetKey0())
  17.                 {
  18.                         GPIO_TogglePin(LED1_PORT,LED1_PIN);
  19.                         //JiaoHua(1-currentDataPoint.valueRelay_1);
  20.                 }
  21.                 if(GetKey1())
  22.                 {
  23.                         GPIO_TogglePin(LED2_PORT,LED2_PIN);
  24.                         //gizwitsSetMode(WIFI_AIRLINK_MODE);
  25.                         //按键进入配网模式
  26.                 }
  27.                 if(GetKey2())
  28.                 {
  29.                         GPIO_TogglePin(LED3_PORT,LED3_PIN);
  30.                 }
  31.         }
  32. }
复制代码
开发板上用的是USB TO TTL 工具 串口1 打印 实际的项目板上没有设计该电路(失误1)

串口1 驱动代码
  1. #include "usart1.h"
  2. #include <stdio.h>
  3. void USART1_NVIC_Config(void)
  4. {
  5.   //接收中断使能
  6.         NVIC_InitTypeDef  NVIC_InitStruct;
  7.         /*NVIC控制器配置*/
  8.         NVIC_InitStruct.NVIC_IRQChannel    = USART1_IRQn;//具体中断源名字
  9.         NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//NVIC响应通道使能
  10.         NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级值
  11.         NVIC_InitStruct.NVIC_IRQChannelSubPriority        = 1;//响应优先级别值
  12.   NVIC_Init(&NVIC_InitStruct);
  13.         
  14. }
  15. /*打印调试串口*/
  16. void USART1_Init(uint32_t BaudRate)
  17. {
  18.         USART_DeInit(USART1);
  19.         //1.打开GPIO的时钟
  20.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  21.         
  22.         //2.配置相关结构体
  23.         GPIO_InitTypeDef           GPIO_InitStruct;
  24.         
  25.         //串口发送引脚的配置         PA9->复用推挽输出
  26.         GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_9;      
  27.         GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
  28.         GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  29.         GPIO_Init(GPIOA,&GPIO_InitStruct);
  30.         
  31.         //串口接收引脚的配置        PA10->浮空输入模式
  32.         GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_10;      
  33.         GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IN_FLOATING;//浮空输入模式
  34.         GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  35.         GPIO_Init(GPIOA,&GPIO_InitStruct);
  36.         
  37.         //1.打开串口的时钟
  38.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//注意APB2
  39.         
  40.         USART_InitTypeDef USART1_InitStruct;
  41.         //串口的参数配置     波特率可以更改   
  42.         //无硬件流控制       收发模式  
  43.         //1起始位 8数据位 无奇偶校验 1位停止位
  44.         USART1_InitStruct.USART_BaudRate            = BaudRate;
  45.         USART1_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  46.         USART1_InitStruct.USART_Mode                = USART_Mode_Rx|USART_Mode_Tx;
  47.         USART1_InitStruct.USART_Parity              = USART_Parity_No;
  48.         USART1_InitStruct.USART_StopBits            = USART_StopBits_1;
  49.         USART1_InitStruct.USART_WordLength          = USART_WordLength_8b;
  50.         //串口1初始化
  51.         USART_Init(USART1,&USART1_InitStruct);
  52.         
  53.         /*******************/
  54.         //开串口中断
  55.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//接收中断
  56.         //USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//空闲中断
  57.         //中断优先级配置
  58.         USART1_NVIC_Config();
  59.         USART_Cmd(USART1,ENABLE);
  60. }

    标签: sta继电器2af05继电器

    锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台