综合技术交流
直播中

kiki

8年用户 1604经验值
擅长:存储技术 存储技术 存储技术
私信 关注
[资料]

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

GPIO_InitTypeDef            LED_InitStruct = {0};

LED_InitStruct.GPIO_Pin = LED1_PIN;


LED_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;

LED_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;


GPIO_Init(LED1_PORT, &LED_InitStruct);

LED_InitStruct.GPIO_Pin = LED2_PIN;


GPIO_Init(LED2_PORT, &LED_InitStruct);

LED_InitStruct.GPIO_Pin = LED3_PIN;


GPIO_Init(LED3_PORT, &LED_InitStruct);

LED1(0);


25. LED2(0);
26. ```
    
    ```

LED3(0);

  1. }
  2. void LED_Task(void)
  3. {

static uint32_t Timer = 0;


static uint8_t  Sta   = 0;

if(SoftTimer(Timer,500))


{

Timer=GetSoftTimer();


Sta?(Sta=0):(Sta=1);

LED1(Sta);


LED2(Sta);

LED3(Sta);


}

  1. }
  2. void GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  3. {

uint32_t ODR;


ODR = GPIOx->ODR;

GPIOx->BSRR = ((ODR & GPIO_Pin) << 16U) | (~ODR & GPIO_Pin);


48. }
49. 
50. 

*复制代码*

1. #ifndef _*LED_H*
2. #define _*LED_H*
3. 
4. #include "[STM32](http://club.gizwits.com/thread-5326-1-1.html)f10x.h"
5. 
6. 
7. //PB9  --- LED1   --- 低有效
8. //PB8  --- LED2   --- 低有效
9. //PA3  --- LED3   --- 低有效
10. 
11. //基于STM32标准库  芯片是 STM32F103C8T6
12. 
13. 
14. 
15. #define LED1_CLK  RCC_APB2Periph_GPIOB
16. #define LED1_PORT GPIOB
17. #define LED1_PIN  GPIO_Pin_9
18. 
19. #define LED2_CLK  RCC_APB2Periph_GPIOB
20. #define LED2_PORT GPIOB
21. #define LED2_PIN  GPIO_Pin_8
22. 
23. #define LED3_CLK  RCC_APB2Periph_GPIOA
24. #define LED3_PORT GPIOA
25. #define LED3_PIN  GPIO_Pin_3
26. 
27. 
28. //宏定义的一个开关
29. #define LED1(X)  X?(GPIO_ResetBits(LED1_PORT,LED1_PIN)):(GPIO_SetBits(LED1_PORT,LED1_PIN))
30. #define LED2(X)  X?(GPIO_ResetBits(LED2_PORT,LED2_PIN)):(GPIO_SetBits(LED2_PORT,LED2_PIN))
31. #define LED3(X)  X?(GPIO_ResetBits(LED3_PORT,LED3_PIN)):(GPIO_SetBits(LED3_PORT,LED3_PIN))
32. 
33. void LED_Init(void);
34. void GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
35. void LED_Task(void);
36. 
37. #endif
38. 
39. 

*复制代码*

按键驱动的代码编写

原理图

代码实现

1. #include "key.h"
2. #include "systick.h"
3. 
4. void KEY_Init(void)
5. {
6. ```
   RCC_APB2PeriphClockCmd(KEY0_CLK,ENABLE);
  1. RCC_APB2PeriphClockCmd(KEY1_CLK,ENABLE);
    
  2. RCC_APB2PeriphClockCmd(KEY2_CLK,ENABLE);
    
  3. 
    

GPIO_InitTypeDef            KEY_InitStruct;

KEY_InitStruct.GPIO_Pin = KEY0_PIN|KEY1_PIN|KEY2_PIN;


KEY_InitStruct.GPIO_Mode  = GPIO_Mode_IPU;

KEY_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;


GPIO_Init(KEY0_PORT ,&KEY_InitStruct);

GPIO_Init(KEY1_PORT ,&KEY_InitStruct);


GPIO_Init(KEY2_PORT ,&KEY_InitStruct);

  1. }
  2. static uint8_t Key0Value=0;
  3. static uint8_t Key1Value=0;
  4. static uint8_t Key2Value=0;
  5. void KeyScan(void)
  6. {

static uint16_t Key0Timer=0;


static uint16_t Key1Timer=0;

static uint16_t Key2Timer=0;


if(KEY0==0)

{


if(Key0Timer<10)

{


Key0Timer++;

if(Key0Timer>=10)


Key0Value=1;

}


}

else


{

Key0Timer = 0;


}

if(KEY1==0)


{

if(Key1Timer<10)


{

Key1Timer++;


if(Key1Timer>=10)

Key1Value=1;


}

}


else

{


Key1Timer = 0;

}


if(KEY2==0)

{


if(Key2Timer<10)

{


Key2Timer++;

if(Key2Timer>=10)


Key2Value=1;

}


}

else


{

Key2Timer = 0;


}

  1. }
  2. uint8_t GetKey0(void)
  3. {

uint8_t Key=Key0Value;


Key0Value=0;

return Key;


78. }
79. 
80. uint8_t GetKey1(void)
81. {
82. ```
    
    ```

uint8_t Key=Key1Value;

Key1Value=0;


return Key;

  1. }
  2. uint8_t GetKey2(void)
  3. {

uint8_t Key=Key2Value;


Key2Value=0;

return Key;


92. }
93. 
94. 

*复制代码*

1. #ifndef _*KEY_H*
2. #define _*KEY_H*
3. 
4. #include "stm32f10x.h"
5. 
6. #define KEY0_CLK         RCC_APB2Periph_GPIOA
7. #define KEY0_PORT                     GPIOA
8. #define KEY0_PIN                     GPIO_Pin_0
9. 
10. #define KEY1_CLK         RCC_APB2Periph_GPIOA
11. #define KEY1_PORT                     GPIOA
12. #define KEY1_PIN                     GPIO_Pin_1
13. 
14. #define KEY2_CLK         RCC_APB2Periph_GPIOA
15. #define KEY2_PORT                     GPIOA
16. #define KEY2_PIN                     GPIO_Pin_2
17. 
18. #define KEY0             GPIO_ReadInputDataBit(KEY0_PORT,KEY0_PIN)
19. #define KEY1             GPIO_ReadInputDataBit(KEY1_PORT,KEY1_PIN)
20. #define KEY2             GPIO_ReadInputDataBit(KEY2_PORT,KEY2_PIN)
21. 
22. void KEY_Init(void);
23. void KeyScan(void);
24. uint8_t GetKey0(void);
25. uint8_t GetKey1(void);
26. uint8_t GetKey2(void);
27. #endif
28. 
29. 

*复制代码*

**测试驱动程序的正确性**

1. #include "main.h"
2. int main(void)
3. {
4. ```
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  1. USART1_Init(9600);
    
  2. //printf("打印串口初始化 OK !
    ");
    
  3. SysTick_Init();
  4. //printf("系统嘀嗒初始化 OK !
    ");
  5. LED_Init();
    
  6. //printf("状态指示初始化 OK !
    ");
  7. KEY_Init();
  8. //printf("按键配置初始化 OK !
    ");
  9. 
    

while(1)

{


LED_Task();

if(GetKey0())


{

GPIO_TogglePin(LED1_PORT,LED1_PIN);


//JiaoHua(1-currentDataPoint.valueRelay_1);

}


if(GetKey1())

{


GPIO_TogglePin(LED2_PORT,LED2_PIN);

//gizwitsSetMode(WIFI_AIRLINK_MODE);


//按键进入配网模式

}


if(GetKey2())

{


GPIO_TogglePin(LED3_PORT,LED3_PIN);

}


}

  1. }

复制代码

其他辅助代码

串口打印实现

在开发板上用的是USB TO TTL 工具 串口1 打印 实际的项目板上没有设计该电路(失误1)

串口1 驱动代码

  1. #include "usart1.h"
  2. #include <stdio.h>
  3. void USART1_NVIC_Config(void)
  4. {
  5. //接收中断使能

NVIC_InitTypeDef NVIC_InitStruct;


8. ```
   /*NVIC控制器配置*/
  1. NVIC_InitStruct.NVIC_IRQChannel    = USART1_IRQn;//具体中断源名字
    
  2. 
    

NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//NVIC响应通道使能

NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级值


NVIC_InitStruct.NVIC_IRQChannelSubPriority        = 1;//响应优先级别值

  1. NVIC_Init(&NVIC_InitStruct);
  2. }
  3. /打印调试串口/
  4. void USART1_Init(uint32_t BaudRate)
  5. {

USART_DeInit(USART1);


//1.打开GPIO的时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);


//2.配置相关结构体

GPIO_InitTypeDef GPIO_InitStruct;


//串口发送引脚的配置         PA9->复用推挽输出

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;


GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;


GPIO_Init(GPIOA,&GPIO_InitStruct);

//串口接收引脚的配置 PA10->浮空输入模式


GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_10;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入模式


GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStruct);


//1.打开串口的时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//注意APB2


USART_InitTypeDef USART1_InitStruct;

//串口的参数配置 波特率可以更改


//无硬件流控制       收发模式

//1起始位 8数据位 无奇偶校验 1位停止位


USART1_InitStruct.USART_BaudRate            = BaudRate;

USART1_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;


USART1_InitStruct.USART_Mode                = USART_Mode_Rx|USART_Mode_Tx;

USART1_InitStruct.USART_Parity = USART_Parity_No;


USART1_InitStruct.USART_StopBits            = USART_StopBits_1;

USART1_InitStruct.USART_WordLength = USART_WordLength_8b;


//串口1初始化

USART_Init(USART1,&USART1_InitStruct);


/*******************/

//开串口中断


USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//接收中断

//USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//空闲中断


//中断优先级配置

USART1_NVIC_Config();


USART_Cmd(USART1,ENABLE);

  1. }
  2. /串口重定向函数 目的是让STM32支持printf("%d %x %c ")/
  3. int fputc(int ch , FILE *stream)
  4. {

while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);


USART_SendData(USART1,(uint16_t) ch); //数据通过串口发送

while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);


return ch;

  1. }

复制代码

  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. SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */
  7. NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */
  8. SysTick->VAL = 0; /* Load the SysTick Counter Value */
  9. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |

SysTick_CTRL_TICKINT_Msk |


SysTick_CTRL_ENABLE_Msk;                    /* Enable SysTick IRQ and SysTick Timer */

  1. return (0); /* Function successful */
  2. }
  3. void SysTick_Init(void)
  4. {

//SystemInit();


SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

mySysTick_Config(SystemCoreClock/1000);


23. }
24. void SysTick_Handler(void)
25. {
26. ```
    
    ```

SystemTick();

KeyScan();


28. }
29. static uint32_t Ticks=0;
30. void SystemTick(void)
31. {
32. ```
    
    ```

Ticks++;

  1. }
  2. uint32_t GetSoftTimer(void)
  3. {

return Ticks;


37. }
38. uint8_t SoftTimer(uint32_t BaseTimer,uint32_t Timeout)
39. {
40. ```
    
    ```

if(Ticks>=BaseTimer)

return (Ticks)>=Timeout+BaseTimer;


return (Ticks+0xFFFFFF)>=Timeout+BaseTimer;

  1. }

复制代码

未完待续

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


回帖(2)

jf_08165967

2022-8-20 22:43:35
好设计。。。。。。。。。。。。。
举报

jf_08165967

2022-8-22 20:28:13
太细了,看看。。。。。。。。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分