收藏官网首页
查看: 3458|回复: 2

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

610

主题

657

帖子

3万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
33415
跳转到指定楼层
楼主
 楼主| 发表于 2022-7-25 11:57:09 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
汉枫LPB120模块
本帖最后由 Kara 于 2022-7-25 14:32 编辑

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


一,系统总计设计
该系统主要由三个部分组成:传感执行部分、无线通信部分,机智云自助开发平台部分。传感执行部分由主控单元、显示单元、和存储单元共同组成。传感检测部分采集空气环境参数信息,并且实时处理采集到的传感器数据,然后将数据发送给无线通信模块。

无线通信部分就是esp8266模组,也是所有信息交互的驿站,所有采集的信息和控制信息都是通过无线通信模块跟云平台进行传递的。数据通过WIF引模块把数据发送到云平台,云平台的数据可以同步到手机或者其他移动设备。机智云平台上面是用户定义好的数据点可以实时的记录本地的传感器数据并对本地的设备进行控制。总体设计图如图1.




二,系统功能介绍
使用机智云自助开发平台,ESP8266模组联网,实现环境湿度检测,环境光照强度检测,OLED屏幕显示数据。继电器可以自动远程控制以及定时。环境参数远程查看,远程修改阀值参数。


三,智能硬件自助开发选择机智云平台


机智云AIoT自助开发平台,采用微服务架构,将大量IoT技术原理、行业知识、基础模型规则化、软件化、模块化,通过傻瓜式开发工具、MCU自动生成代码、APP开源框架、IoTSDK、开放API,最大限度降低IoT设备开发的技术门槛和开发成本,提高开发、测试、部署效率,已服务超过320000+开发者。


机智云自助开发平台-开发利器GAgent

GAgent-标准串口通信程序,加速智能设备联网开发过程,1天内可完成设备对接平台。GAgent是运行在WiFi/蓝牙/5G/4G/NB-IOT等通讯模组的应用程序,使通讯模块主动连接使能平台服务器,并实现与云端的通信。在开发者产品的控制电路板上集成通讯模块,只需要实现与通讯模组的串口通信(代码自动生成),即可直接接入使能平台服务器,而不需要处键底层的网绪传输。

四,项目文件夹框架



五,核心板原理图

六,LED灯的驱动代码编写
原理图


七,代码实现
  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.         
  9.         GPIO_InitTypeDef            LED_InitStruct = {0};
  10.         
  11.         LED_InitStruct.GPIO_Pin   = LED1_PIN;
  12.         LED_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;                 
  13.         LED_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;        
  14.         
  15.         GPIO_Init(LED1_PORT, &LED_InitStruct);
  16.         
  17.         LED_InitStruct.GPIO_Pin   = LED2_PIN;
  18.         GPIO_Init(LED2_PORT, &LED_InitStruct);
  19.         
  20.         LED_InitStruct.GPIO_Pin   = LED3_PIN;
  21.         GPIO_Init(LED3_PORT, &LED_InitStruct);
  22.         
  23.         LED1(0);  
  24.   LED2(0);
  25.         LED3(0);
  26. }

  27. void LED_Task(void)
  28. {
  29.         static uint32_t Timer = 0;
  30.         static uint8_t  Sta   = 0;
  31.         if(SoftTimer(Timer,500))
  32.         {
  33.                 Timer=GetSoftTimer();
  34.                 Sta?(Sta=0):(Sta=1);
  35.                 LED1(Sta);
  36.                 LED2(Sta);
  37.                 LED3(Sta);
  38.         }
  39. }

  40. void GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  41. {
  42.         uint32_t ODR;
  43.         ODR = GPIOx->ODR;
  44.         GPIOx->BSRR = ((ODR & GPIO_Pin) << 16U) | (~ODR & GPIO_Pin);
  45. }

复制代码

  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. }


  61. /*串口重定向函数 目的是让STM32支持printf("%d %x %c ")*/
  62. int fputc(int ch , FILE *stream)
  63. {
  64.         while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
  65.         USART_SendData(USART1,(uint16_t) ch); //数据通过串口发送
  66.         while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
  67.         return ch;        
  68. }

复制代码
  1. #ifndef __USART1_H_
  2. #define __USART1_H_

  3. #include "stm32f10x.h"

  4. void USART1_Init(uint32_t BaudRate);

  5. #endif

复制代码
系统嘀嗒定时器代码

  1. #include "systick.h"
  2. #include "key.h"


  3. uint32_t mySysTick_Config(uint32_t ticks)
  4. {
  5.   if (ticks > SysTick_LOAD_RELOAD_Msk)  return (1);            /* Reload value impossible */
  6.                                                                
  7.   SysTick->LOAD  = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;      /* set reload register */
  8.   NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  /* set Priority for Cortex-M0 System Interrupts */
  9.   SysTick->VAL   = 0;                                          /* Load the SysTick Counter Value */
  10.   SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
  11.                    SysTick_CTRL_TICKINT_Msk   |
  12.                    SysTick_CTRL_ENABLE_Msk;                    /* Enable SysTick IRQ and SysTick Timer */
  13.   return (0);                                                  /* Function successful */
  14. }

  15. void SysTick_Init(void)
  16. {
  17.         //SystemInit();
  18.         SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  19.         mySysTick_Config(SystemCoreClock/1000);
  20. }
  21. void SysTick_Handler(void)
  22. {
  23.         SystemTick();
  24.         KeyScan();
  25. }
  26. static uint32_t Ticks=0;
  27. void SystemTick(void)
  28. {
  29.         Ticks++;
  30. }
  31. uint32_t GetSoftTimer(void)
  32. {
  33.         return Ticks;
  34. }
  35. uint8_t SoftTimer(uint32_t BaseTimer,uint32_t Timeout)
  36. {
  37.         if(Ticks>=BaseTimer)
  38.         return (Ticks)>=Timeout+BaseTimer;
  39.         return (Ticks+0xFFFFFF)>=Timeout+BaseTimer;
  40. }

复制代码


未完待续


机智云智能浇花器实战(2)-基础Demo实现-
继电器实现/功能测试/DHT11驱动代码实现/OLED屏幕显示传感器数据/中文字模制作等等


本帖被以下淘专辑推荐:

  • · .i|主题: 2, 订阅: 0

0

主题

9

帖子

850

积分

高级会员

Rank: 4

积分
850
沙发
发表于 2022-7-28 17:57:33 | 只看该作者
感谢机智云的平台和开源项目,感谢机智云的工程师们,让我们更好的学习机智云物联网

点评

一起努力进步哈  详情 回复 发表于 2022-8-15 10:33

610

主题

657

帖子

3万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
33415
板凳
 楼主| 发表于 2022-8-15 10:33:59 | 只看该作者
ysj 发表于 2022-7-28 17:57
感谢机智云的平台和开源项目,感谢机智云的工程师们,让我们更好的学习机智云物联网 ...

一起努力进步哈
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

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