机智云智能浇花器实战(1)-基础Demo实现

一,系统总计设计

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

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


机智云智能浇花器实战(1)-基础Demo实现

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

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

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

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

机智云智能浇花器实战(1)-基础Demo实现


四,项目文件夹框架

机智云智能浇花器实战(1)-基础Demo实现


机智云智能浇花器实战(1)-基础Demo实现

五,核心板原理图

机智云智能浇花器实战(1)-基础Demo实现

六,LED灯的驱动代码编写

原理图

机智云智能浇花器实战(1)-基础Demo实现


七,代码实现

  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. LED_InitStruct.GPIO_Pin = LED1_PIN;
  10. LED_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  11. LED_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  12. GPIO_Init(LED1_PORT, &LED_InitStruct);
  13. LED_InitStruct.GPIO_Pin = LED2_PIN;
  14. GPIO_Init(LED2_PORT, &LED_InitStruct);
  15. LED_InitStruct.GPIO_Pin = LED3_PIN;
  16. GPIO_Init(LED3_PORT, &LED_InitStruct);
  17. LED1(0);
  18. LED2(0);
  19. LED3(0);
  20. }
  21. void LED_Task(void)
  22. {
  23. static uint32_t Timer = 0;
  24. static uint8_t Sta = 0;
  25. if(SoftTimer(Timer,500))
  26. {
  27. Timer=GetSoftTimer();
  28. Sta?(Sta=0):(Sta=1);
  29. LED1(Sta);
  30. LED2(Sta);
  31. LED3(Sta);
  32. }
  33. }
  34. void GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  35. {
  36. uint32_t ODR;
  37. ODR = GPIOx->ODR;
  38. GPIOx->BSRR = ((ODR & GPIO_Pin) << 16U) | (~ODR & GPIO_Pin);
  39. }

复制代码

  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)-基础Demo实现


代码实现

  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. GPIO_InitTypeDef KEY_InitStruct;
  9. KEY_InitStruct.GPIO_Pin = KEY0_PIN|KEY1_PIN|KEY2_PIN;
  10. KEY_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  11. KEY_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  12. GPIO_Init(KEY0_PORT ,&KEY_InitStruct);
  13. GPIO_Init(KEY1_PORT ,&KEY_InitStruct);
  14. GPIO_Init(KEY2_PORT ,&KEY_InitStruct);
  15. }
  16. static uint8_t Key0Value=0;
  17. static uint8_t Key1Value=0;
  18. static uint8_t Key2Value=0;
  19. void KeyScan(void)
  20. {
  21. static uint16_t Key0Timer=0;
  22. static uint16_t Key1Timer=0;
  23. static uint16_t Key2Timer=0;
  24. if(KEY0==0)
  25. {
  26. if(Key0Timer<10)
  27. {
  28. Key0Timer++;
  29. if(Key0Timer>=10)
  30. Key0Value=1;
  31. }
  32. }
  33. else
  34. {
  35. Key0Timer = 0;
  36. }
  37. if(KEY1==0)
  38. {
  39. if(Key1Timer<10)
  40. {
  41. Key1Timer++;
  42. if(Key1Timer>=10)
  43. Key1Value=1;
  44. }
  45. }
  46. else
  47. {
  48. Key1Timer = 0;
  49. }
  50. if(KEY2==0)
  51. {
  52. if(Key2Timer<10)
  53. {
  54. Key2Timer++;
  55. if(Key2Timer>=10)
  56. Key2Value=1;
  57. }
  58. }
  59. else
  60. {
  61. Key2Timer = 0;
  62. }
  63. }
  64. uint8_t GetKey0(void)
  65. {
  66. uint8_t Key=Key0Value;
  67. Key0Value=0;
  68. return Key;
  69. }
  70. uint8_t GetKey1(void)
  71. {
  72. uint8_t Key=Key1Value;
  73. Key1Value=0;
  74. return Key;
  75. }
  76. uint8_t GetKey2(void)
  77. {
  78. uint8_t Key=Key2Value;
  79. Key2Value=0;
  80. return Key;
  81. }

复制代码

  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 !rn”);
  7. SysTick_Init();
  8. //printf(“系统嘀嗒初始化 OK !rn”);
  9. LED_Init();
  10. //printf(“状态指示初始化 OK !rn”);
  11. KEY_Init();
  12. //printf(“按键配置初始化 OK !rn”);
  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)-基础Demo实现


串口1 驱动代码

  1. #include “usart1.h”
  2. 声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年6月19日
下一篇 2022年6月19日

相关推荐