第一部分:总体设计/系统功能介绍/智能云自助开发平台-利器开发GAgent等等点击下载:【Io机智云智能浇花器实战(1)-基础Demo实现 第二篇内容: 继电器实现/功能测试/DHT11驱动代码实现/OLED屏幕显示传感器数据/中文字模制作等点击下载:智能智能浇花器实战(2)-基础Demo实现 一,BH1750光照传感器原理图
二,BH1750传感器代码
- #include "bh1750.h"
- #include "delay.h"
- uint8_t BUF[8]; //接收数据缓存区
- int mcy; ///进位标志
- /**开始信号**/
- void BH1750_Start()
- {
- BH1750_SDA_H; ///提高数据线
- BH1750_SCL_H; //提高时钟线
- Delay_nus(5); //延时
- GPIO_ResetBits(BH1750_PORT, BH1750_SDA_PIN); //产生下降边
- Delay_nus(5); //延时
- GPIO_ResetBits(BH1750_PORT, BH1750_SCL_PIN); //拉低时钟线
- }
- /***停止信号***/
- void BH1750_Stop()
- {
- BH1750_SDA_L; ///拉低数据线
- BH1750_SCL_H; //提高时钟线
- Delay_nus(5); //延时
- GPIO_SetBits(BH1750_PORT, BH1750_SDA_PIN); //产生上升沿
- Delay_nus(5); //延时
- }
- /******************
- 发送响应信号
- 入口参数:ack (0:ACK 1:NAK)
- ******************/
- void BH1750_SendACK(int ack)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_P;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN;
- GPIO_Init(BH1750_PORT, &GPIO_InitStruct);
- if(ack == 1) //写应答信号
- BH1750_SDA_H;
- else if(ack == 0)
- BH1750_SDA_L;
- else
- return;
- BH1750_SCL_H; //拉高时钟线
- Delay_nus(5); //延时
- BH1750_SCL_L; //拉低时钟线
- Delay_nus(5); //延时
- }
- /******************
- 接收应答信号
- ******************/
- int BH1750_RecvACK()
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; /*这里一定要设成输入上拉,否则不能读出数据*/
- GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Pin=BH1750_SDA_PIN;
- GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
- BH1750_SCL_H; //拉高时钟线
- Delay_nus(5); //延时
- if(GPIO_ReadInputDataBit(BH1750_PORT,BH1750_SDA_PIN)==1)//读应答信号
- mcy = 1 ;
- else
- mcy = 0 ;
- BH1750_SCL_L; //拉低时钟线
- Delay_nus(5); //延时
- GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
- return mcy;
- }
- /******************
- 向IIC总线发送一个字节数据
- ******************/
- void BH1750_SendByte(uint8_t dat)
- {
- uint8_t i;
- for (i=0; i<8; i++) //8位计数器
- {
- if( 0X80 & dat )
- GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN);
- else
- GPIO_ResetBits(BH1750_PORT,BH1750_SDA_PIN);
- dat <<= 1;
- BH1750_SCL_H; //拉高时钟线
- Delay_nus(5); //延时
- BH1750_SCL_L; //拉低时钟线
- Delay_nus(5); //延时
- }
- BH1750_RecvACK();
- }
- uint8_t BH1750_RecvByte()
- {
- uint8_t i;
- uint8_t dat = 0;
- uint8_t bit;
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; /*这里一定要设成输入上拉,否则不能读出数据*/
- GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(BH1750_PORT,&GPIO_InitStruct );
- GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN); //使能内部上拉,准备读取数据,
- for (i=0; i<8; i++) //8位计数器
- {
- dat <<= 1;
- BH1750_SCL_H; //拉高时钟线
- Delay_nus(5); //延时
- if( SET == GPIO_ReadInputDataBit(BH1750_PORT,BH1750_SDA_PIN))
- bit = 0X01;
- else
- bit = 0x00;
- dat |= bit; //读数据
- BH1750_SCL_L; //拉低时钟线
- Delay_nus(5); //延时
- }
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(BH1750_PORT, &GPIO_InitStruct );
- return dat;
- }
- void Single_Write_BH1750(uint8_t REG_Address)
- {
- BH1750_Start(); //起始信号
- BH1750_SendByte(SlaveAddress); //发送设备地址+写信号
- BH1750_SendByte(REG_Address); //内部寄存器地址,请参考中文pdf22页
- // BH1750_SendByte(REG_data); //内部寄存器数据,请参考中文pdf22页
- BH1750_Stop(); //发送停止信号
- }
- //初始化BH1750,根据需要请参考pdf进行修改**
- void BH1750_Init()
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /*开启GPIOB的外设时钟*/
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Pin = BH1750_SDA_PIN | BH1750_SCL_PIN ;
- GPIO_Init(BH1750_PORT,&GPIO_InitStruct);
- Single_Write_BH1750(0x01);
- Delay_nms(180); //延时180ms
- }
- //连续读出BH1750内部数据
- void mread(void)
- {
- uint8_t i;
- BH1750_Start(); //起始信号
- BH1750_SendByte(SlaveAddress+1); //发送设备地址+读信号
- for (i=0; i<3; i++) //连续读取6个地址数据,存储中BUF
- {
- BUF[i] = BH1750_RecvByte(); //BUF[0]存储0x32地址中的数据
- if (i == 3)
- {
- BH1750_SendACK(1); //最后一个数据需要回NOACK
- }
- else
- {
- BH1750_SendACK(0); //回应ACK
- }
- }
- BH1750_Stop(); //停止信号
- Delay_nms(5);
- }
- float Read_BH1750(void)
- {
- int dis_data; //变量
- float temp1;
- float temp2;
- Single_Write_BH1750(0x01); // power on
- Single_Write_BH1750(0x10); // H- resolution mode
- Delay_nms(180); //延时180ms
- mread(); //连续读出数据,存储在BUF中
- dis_data=BUF[0];
- dis_data=(dis_data<<8)+BUF[1]; //合成数据
- temp1=dis_data/1.2;
- temp2=10*dis_data/1.2;
- temp2=(int)temp2%10;
- return temp1;
- }
- #ifndef __BH1750_H__
- #define __BH1750_H__
- #include "STM32f10x.h"
- /*
- 定义器件在IIC总线中的从地址,根据ALT ADDRESS地址引脚不同修改
- ALT ADDRESS引脚接地时地址为0x46, 接电源时地址为0xB8
- */
- #define SlaveAddress 0x46
- #define BH1750_PORT GPIOB
- #define BH1750_SCL_PIN GPIO_Pin_1
- #define BH1750_SDA_PIN GPIO_Pin_0
- #define BH1750_SCL_H GPIO_SetBits(BH1750_PORT,BH1750_SCL_PIN)
- #define BH1750_SCL_L GPIO_ResetBits(BH1750_PORT,BH1750_SCL_PIN)
- #define BH1750_SDA_H GPIO_SetBits(BH1750_PORT,BH1750_SDA_PIN)
- #define BH1750_SDA_L GPIO_ResetBits(BH1750_PORT,BH1750_SDA_PIN)
- extern uint8_t BUF[8]; //接收数据缓存区
- extern int dis_data; //变量
- extern int mcy; //表示进位标志位
- void BH1750_Init(void);
- void conversion(uint32_t temp_data);
- void Single_Write_BH1750(uint8_t REG_Address); //单个写入数据
- uint8_t Single_Read_BH1750(uint8_t REG_Address); //单个读取内部寄存器数据
- void mread(void); //连续的读取内部寄存器数据
- float Read_BH1750(void);
- #endif
BH1750传感器代码说明 核心板单独测试程序在PB0PB1管脚是完全正常,不知道是不是核心板的PB2上接了什么暂时还未排查出来问题,如果你是用开发板或者是自己设计的项目板的话,那么程序是直接可以使用的程序依然按照PB0PB1保留。 三,机智云自助开发平台数据点创建 机智云官方网站:https://www.gizwits.com/ 步骤1,创建产品 创建好后就会有基本信息 步骤2,填写机智云产品ProductKey这两个信息比较重要最好是保存下来
- Product Key :9c8a5a8e38344fb4af14b6db0f5b1df7
- Product Secret :45c86d8c6a2a4b1dac7d68df54f6e4f0
步骤3,定义自己的数据点 只读:就是只允许赋值数据传感器赋值给平台平台只能读取 可写:就是数据可以被修改继电器的开关状态平台可以修改 四,MCU开发 mcu开发注意事项平台选Common其实就是STM32F103x平台 1,生成代码包 2,下载自动生成的代码包 3,机智云Gizwits协议移植这两个文件夹要添加到自己的工程 这是添加的文件夹以及文件的目录 4,修改gizwits_product.c
- #include <stdio.h>
- #include <string.h>
- #include "gizwits_product.h"
- #include "usart3.h"
- static uint32_t timerMsCount;
- uint8_t aRxBuffer;
- dataPoint_t currentDataPoint;
- uint8_t wifi_flag;
- //存放事件处理API接口函数
- int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *gizdata, uint32_t len)
- {
- uint8_t i = 0;
- dataPoint_t *dataPointPtr = (dataPoint_t *)gizdata;
- moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)gizdata;
- protocolTime_t *ptime = (protocolTime_t *)gizdata;
- #if MODULE_TYPE
- gprsInfo_t *gprsInfoData = (gprsInfo_t *)gizdata;
- #else
- moduleInfo_t *ptModuleInfo = (moduleInfo_t *)gizdata;
- #endif
- if((NULL == info) || (NULL == gizdata))
- {
- return -1;
- }
- for(i=0; i<info->num; i++)
- {
- switch(info->event[i])
- {
- case EVENT_Relay_1:
- currentDataPoint.valueRelay_1 = dataPointPtr->valueRelay_1;
- GIZWITS_LOG("Evt: EVENT_Relay_1 %d \n", currentDataPoint.valueRelay_1);
- if(0x01 == currentDataPoint.valueRelay_1)
- {
- currentDataPoint.valueRelay_1 = 1;
- }
- else
- {
- currentDataPoint.valueRelay_1 = 0;
- }
- break;
- case WIFI_SOFTAP:
- break;
- case WIFI_AIRLINK:
- break;
- case WIFI_STATION:
- break;
- case WIFI_CON_ROUTER:
- break;
- case WIFI_DISCON_ROUTER:
- break;
- case WIFI_CON_M2M:
- wifi_flag = 1; //WiFi连接标志
- break;
- case WIFI_DISCON_M2M:
- wifi_flag = 0; //WiFi断开标志
- break;
- case WIFI_RSSI:
- GIZWITS_LOG("RSSI %d\n", wifiData->rssi);
- break;
- case TRANSPARENT_DATA:
- GIZWITS_LOG("TRANSPARENT_DATA \n");
- //user handle , Fetch data from [data] , size is [len]
- break;
- case WIFI_NTP:
- GIZWITS_LOG("WIFI_NTP : [%d-%d-%d %02d:%02d:%02d][%d] \n",ptime->year,ptime->month,ptime->day,ptime->hour,ptime->minute,ptime->second,ptime->ntp);
- break;
- case MODULE_INFO:
- GIZWITS_LOG("MODULE INFO ...\n");
- #if MODULE_TYPE
- GIZWITS_LOG("GPRS MODULE ...\n");
- //Format By gprsInfo_t
- #else
- GIZWITS_LOG("WIF MODULE ...\n");
- //Format By moduleInfo_t
- GIZWITS_LOG("moduleType : [%d] \n",ptModuleInfo->moduleType);
- #endif
- break;
- default:
- break;
- }
- }
- return 0;
- }
- void userHandle(void)
- {
- /*
- currentDataPoint.valueTemp = ;//Add Sensor Data Collection
- currentDataPoint.valueHumi = ;//Add Sensor Data Collection
- currentDataPoint.valueLight_Intensity = ;//Add Sensor Data Collection
- */
- }
- void userInit(void)
- {
- memset((uint8_t*)¤tDataPoint, 0, sizeof(dataPoint_t));
- currentDataPoint.valueRelay_1 = 0;
- currentDataPoint.valueTemp = 0;
- currentDataPoint.valueHumi = 0;
- currentDataPoint.valueLight_Intensity = 0;
- }
- void gizTimerMs(void)
- {
- timerMsCount++;
- }
- uint32_t gizGetTimerCount(void)
- {
- return timerMsCount;
- }
- void mcuRestart(void)
- {
- __set_FAULTMASK(1);
- NVIC_SystemReset();
- }
- void TIMER_IRQ_FUN(void)
- {
- gizTimerMs();
- }
- void UART_IRQ_FUN(void)
- {
- uint8_t value = 0;
- gizPutData(&value, 1);
- }
- int32_t uartWrite(uint8_t *buf, uint32_t len)
- {
- uint32_t i = 0;
- if(NULL == buf)
- {
- return -1;
- }
- for(i=0; i<len; i++)
- {
- USART_SendData(USART3,buf[i]);
- while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //循环发送,直到发送完毕
- if(i >=2 && buf[i] == 0xFF)
- {
- USART_SendData(USART3, 0x55);
- while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //循环发送,直到发送完毕
- }
- }
- return len;
- }
5,修改
- #ifndef _GIZWITS_PRODUCT_H
- #define _GIZWITS_PRODUCT_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <stdint.h>
- //#include "stm32f1xx.h"
- #include "gizwits_protocol.h"
- /**
- * MCU software version
- */
- #define SOFTWARE_VERSION "03030000"
- /**
- * MCU hardware version
- */
- #define HARDWARE_VERSION "03010100"
- /**
- * Communication module model
- */
- #define MODULE_TYPE 0 //0,WIFI ;1,GPRS
- /**@name TIM3 related macro definition
- * @{
- */
- #define TIMER TIM3
- #define TIMER_IRQ TIM3_IRQn
- #define TIMER_RCC RCC_APB1Periph_TIM3
- #define TIMER_IRQ_FUN TIM3_IRQHandler
- /**@} */
- /**@name USART related macro definition
- * @{
- */
- #define UART_BAUDRATE 9600
- #define UART_PORT 2
- #define UART USART2
- #define UART_IRQ USART2_IRQn
- #define UART_IRQ_FUN USART2_IRQHandler
- #if (UART_PORT == 1)
- #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_GPIO_CLK RCC_APB2Periph_GPIOA
- #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_AFIO_CLK RCC_APB2Periph_AFIO
- #define UART_CLK_Cmd RCC_APB2PeriphClockCmd
- #define UART_CLK RCC_APB2Periph_USART1
- #define UART_GPIO_PORT GPIOA
- #define UART_RxPin GPIO_Pin_10
- #define UART_TxPin GPIO_Pin_9
- #endif
- #if (UART_PORT == 2)
- #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_GPIO_CLK RCC_APB2Periph_GPIOA
- #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_AFIO_CLK RCC_APB2Periph_AFIO
- #define UART_CLK_Cmd RCC_APB1PeriphClockCmd
- #define UART_CLK RCC_APB1Periph_USART2
- #define UART_GPIO_PORT GPIOA
- #define UART_RxPin GPIO_Pin_3
- #define UART_TxPin GPIO_Pin_2
- #endif
- #if (UART_PORT == 3)
- #define UART_GPIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_GPIO_CLK RCC_APB2Periph_GPIOC
- #define UART_AFIO_Cmd RCC_APB2PeriphClockCmd
- #define UART_AFIO_CLK RCC_APB2Periph_AFIO
- #define UART_CLK_Cmd RCC_APB1PeriphClockCmd
- #define UART_CLK RCC_APB1Periph_USART3
- #define UART_GPIO_PORT GPIOC
- #define UART_RxPin GPIO_Pin_11
- #define UART_TxPin GPIO_Pin_10
- #endif
- /**@} */
- /** User area the current device state structure*/
- extern dataPoint_t currentDataPoint;
- void gizTimerMs(void);
- uint32_t gizGetTimerCount(void);
- void timerInit(void);
- void uartInit(void);
- void userInit(void);
- void userHandle(void);
- void mcuRestart(void);
- int32_t uartWrite(uint8_t *buf, uint32_t len);
- int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *data, uint32_t len);
- #ifdef __cplusplus
- }
- #endif
- #endif
5,修改gizwits_product.h Listitem Listitem Listitem Listitem Listitem