当前位置:网站首页>Skill sorting [email protected]+ Alibaba cloud +nbiot+dht11+bh1750+ soil moisture sensor +oled
Skill sorting [email protected]+ Alibaba cloud +nbiot+dht11+bh1750+ soil moisture sensor +oled
2022-06-30 10:32:00 【Sky_ Lannister】
Use DHT11 The temperature and humidity sensor measures temperature and humidity , Use BH1750 Measure the light intensity , Use soil moisture sensor to measure soil moisture . Need to do PCB The board ,32+pcb+ sensor +oled Display these data
1、 Project brief introduction

2、 Implementation logic
# adopt dht11 Detect temperature and humidity
# adopt bh1750 Check the light intensity
# adopt adc Detect soil moisture
# Display the data to oled And send to through serial port nbiot modular , Then go to alicloud
3、 Application scenarios
# Remote detection of environmental parameters
4、 Sort out the core code
// The code is reused several times before it is used. It doesn't matter , It is important to nbiot Module configuration
//temp hump
void DHT11_IO_IN(void) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = DHT11_Pin;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void DHT11_IO_OUT(void) {
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = DHT11_Pin;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void DHT11_Rst(void) {
DHT11_IO_OUT(); //
DHT11_DQ_OUT_LOW; //
HAL_Delay(20); //
DHT11_DQ_OUT_HIGH; //
delay_us(30); //
}
uint8_t DHT11_Check(void) {
uint8_t retry=0;
DHT11_IO_IN();
while (DHT11_DQ_IN && retry<100) {
retry++;
delay_us(1);
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_IN&&retry<100) {
retry++;
delay_us(1);
};
if(retry>=100)return 1;
return 0; //DHT11
}
uint8_t DHT11_Read_Bit(void) {
uint8_t retry=0;
while(DHT11_DQ_IN&&retry<100) {
retry++;
delay_us(1);
}
retry=0;
while(!DHT11_DQ_IN&&retry<100) {
retry++;
delay_us(1);
}
delay_us(40);
if(DHT11_DQ_IN)return 1;
else return 0;
}
uint8_t DHT11_Read_Byte(void) {
uint8_t i,dat;
dat=0;
for (i=0; i<8; i++) {
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
uint8_t DHT11_Read_Data(uint16_t *temp,uint16_t *humi) {
uint8_t buf[5];
uint8_t i;
DHT11_Rst();
if(DHT11_Check()==0) {
for(i=0; i<5; i++) {
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]) {
*humi=(buf[0]<<8) + buf[1];
*temp=(buf[2]<<8) + buf[3];
}
} else return 1;
return 0;
}
uint8_t DHT11_Init(void) {
DHT11_Rst();
return DHT11_Check();
}
bh1750****************//
/*** Start signal ***/
void BH1750_Start()
{
HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_SET); // Pull up the data line
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_SET); // Pull up the clock line
delay_us(5); // Time delay
HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_RESET); // Produce a falling edge
delay_us(5); // Time delay
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_RESET); // Pull down the clock line
}
/***** Stop signal ******/
void BH1750_Stop()
{
HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_RESET); // Pull down the data line
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_SET); // Pull up the clock line
delay_us(5); // Time delay
HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_SET); // To produce a rising edge
delay_us(5); // Time delay
}
/************************************** Send reply signal Entrance parameters :ack (0:ACK 1:NAK) **************************************/
void BH1750_SendACK(int ack)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
if(ack == 1) // Write reply signal
HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_SET);
else if(ack == 0)
HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_RESET);
else
return;
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_SET); // Pull up the clock line
delay_us(5); // Time delay
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_RESET); // Pull down the clock line
delay_us(5); // Time delay
}
/************************************** Receive a reply signal **************************************/
int BH1750_RecvACK()
{
int mcy;
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; /* This must be set to input pull-up , Otherwise, the data cannot be read out */
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = BH1750_SDA_Pin;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_SET); // Pull up the clock line
delay_us(5); // Time delay
if(HAL_GPIO_ReadPin( GPIOB, BH1750_SDA_Pin ) == 1 )// Read the answer signal
mcy = 1 ;
else
mcy = 0 ;
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_RESET); // Pull down the clock line
delay_us(5); // Time delay
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init( GPIOB, &GPIO_InitStruct );
return mcy;
}
/************************************** towards IIC The bus sends a byte of data **************************************/
void BH1750_SendByte(uint8_t dat)
{
uint8_t i;
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_RESET); // Pull down the clock line
delay_us(2); // Time delay
for (i=0; i<8; i++) //8 Bit counter
{
if( 0X80 & dat )
HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_SET);
else
HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_RESET);
dat <<= 1;
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_SET); // Pull up the clock line
delay_us(2); // Time delay
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_RESET); // Pull down the clock line
delay_us(2); // Time delay
}
//BH1750_RecvACK();
}
/************************************** from IIC The bus reads a byte of data **************************************/
uint8_t BH1750_RecvByte()
{
uint8_t i;
uint8_t dat = 0;
uint8_t bit;
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; /* This must be set to input pull-up , Otherwise, the data cannot be read out */
GPIO_InitStruct.Pin = BH1750_SDA_Pin;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init( GPIOB, &GPIO_InitStruct );
//HAL_GPIO_WritePin(GPIOB, BH1750_SDA_Pin,GPIO_PIN_SET); // Enable internal pull-up , Ready to read data ,
for (i=0; i<8; i++) //8 Bit counter
{
//dat <<= 1;
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_RESET); // Pull up the clock line
delay_us(2); // Time delay
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_SET);
dat <<= 1;
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_SET);
if( SET == HAL_GPIO_ReadPin( GPIOB, BH1750_SDA_Pin ) )
dat |= 0X01;
// Pull down the clock line
delay_us(1); // Time delay
}
HAL_GPIO_WritePin(GPIOB, BH1750_SCL_Pin,GPIO_PIN_RESET);
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init( GPIOB, &GPIO_InitStruct );
return dat;
}
void Init_BH1750()
{
BH1750_Start();
BH1750_SendByte(0x46);
BH1750_SendByte(0x01);
BH1750_Stop();
}
void Single_Write_BH1750(u8 REG_Address)
{
BH1750_Start();// Start signal
BH1750_SendByte(0x46);// Send device address + Write the signal
BH1750_RecvACK();
BH1750_SendByte(REG_Address);// Internal address , Please refer to Chinese pdf22 page
BH1750_RecvACK();
BH1750_Stop();// Send stop signal
}
/********************************************************* Continuous reading BH1750 internal data *********************************************************/
void Multiple_Read_BH1750()
{
BH1750_Start();// Start signal
BH1750_SendByte(0x47);// Send device address + Read the signal
BH1750_RecvACK();
BUF[0] = BH1750_RecvByte();//BUF[0] Storage 0x32 The data in the address
BH1750_SendACK(0);
BUF[1] = BH1750_RecvByte();//BUF[0] Storage 0x32 The data in the address
BH1750_SendACK(1);
BH1750_Stop();// Stop signal
HAL_Delay(5);
}
// Continuous reading BH1750 internal data
void mread(void)
{
u8 i=0;
Single_Write_BH1750(0x01);// power on
Single_Write_BH1750(0x10);// H- resolution mode
HAL_Delay(18);// Time delay 18ms
Multiple_Read_BH1750();// Read data continuously , Stored in BUF in
}
/* towards onenet The platform sends data functions * return 1 ok; 0 fail */
uint8_t send_onenet(void)
{
char text[200];
uint8_t len;
memset(text, 0, sizeof(text));
// Package
memset(text, 0, sizeof(text));
sprintf(text, "bbbWenDu:%.2f C; ShiDu: %.2f %; TuRang:%.2f %; GuangZhao:%d Lux;",temp, hump, check_hump, light);
// Packet
len = strlen(text);
HAL_UART_Transmit(&huart1, (uint8_t *)text, len, 0xFFFF); // send out
HAL_Delay(1000);
return 1;
}
/* USER CODE END 0 */
/** * @brief The application entry point. * @retval int */
int main(void)
{
/* USER CODE BEGIN 1 */
uint32_t clk;
tx_nbiot[0] = 0xff;
tx_nbiot[1] = 0x74;
tx_nbiot[13] = 0xff;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_ADC1_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
OLED_Init();
OLED_ColorTurn(0);//
OLED_DisplayTurn(0);//
OLED_Refresh();
OLED_Clear();
OLED_ShowString(0,0,"TEMP: . C",16);
OLED_ShowString(0,14,"HUMP: . %",16);
OLED_ShowString(0,28,"GROUND: . %",16);
OLED_ShowString(0,42,"LIGHT: LUX",16);
OLED_Refresh();
/****dht11*****/
uint8_t int_num = 3;
while(int_num--) {
HAL_Delay(500);
DHT11_Init();
}
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);//open uart1 RXNE
//Init_BH1750();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
check_hump = 4092-ADC_num;
check_hump = (float)(check_hump/3292*100);//ad->hump%
if((check_hump >= 0) && (check_hump <= 100))
{
intH = (int)check_hump;
float hum_tmp = check_hump - intH;
hum_tmp *= 100;
decH = hum_tmp;
tx_nbiot[6] = intH;
tx_nbiot[7] = decH;
}
HAL_ADC_Start_IT(&hadc1);
DHT11_Read_Data(&temperature,&humidity);
// OLED_ShowNum(39,0,temperature>>8,4,12);
// OLED_ShowNum(80,0,temperature&0xff,2,12);
// OLED_ShowNum(39,2,humidity>>8,4,12);
// OLED_ShowNum(80,2,humidity&0xff,2,12);
tx_nbiot[2] = temperature>>8;
tx_nbiot[3] = temperature&0xff;
tx_nbiot[4] = humidity>>8;
tx_nbiot[5] = humidity&0xff;
temp = tx_nbiot[2]*100 + tx_nbiot[3];
temp = temp/100;
hump = tx_nbiot[4]*100 + tx_nbiot[5];
hump = hump/100;
// light_temp = GetLight();
// light = light_temp;
mread();
light_temp = BUF[0];
light_temp=(light_temp<<8)+BUF[1];// Synthetic data
light=light_temp/1.2;
OLED_ShowNum(53,0,tx_nbiot[2],2,16); //temp
OLED_ShowNum(77,0,tx_nbiot[3],2,16);
OLED_ShowNum(53,14,tx_nbiot[4],2,16);//hump
OLED_ShowNum(77,14,tx_nbiot[5],2,16);
OLED_ShowNum(53,28,tx_nbiot[6],2,16);//ground
OLED_ShowNum(77,28,tx_nbiot[7],2,16);
OLED_ShowNum(53,42,light,5,16);//LIGHT
OLED_Refresh();
tx_nbiot[8] = (light>>8)&0xff;
tx_nbiot[9] = light&0xff;
if(clk % 10 == 0) //
send_onenet();
// if(rx_ok)
// {
// //rx_order++;
// rx_ok = 0;
// }
HAL_Delay(500);
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
clk++;
}
/* USER CODE END 3 */
}
5、 Some references
Official website of Hezhou system http://erp.openluat.com/
DTU Management system :https://dtu.yinerda.com/#/user/login
Physical network card management :http://sim.openluat.com/

# Schematic diagram of circuit board pcb See information package
6、 matters needing attention
#nbiot The module does not need a card , It needs to be well configured
Full operational project address
边栏推荐
- Musk has more than 100 million twitter fans, but he has been lost online for a week
- 《锦绣中华》中老年公益文旅游-走进佛山敬老院
- The human agent of kDa, Jinbei kd6, takes you to explore the metauniverse
- Robot system dynamics - inertia parameters
- Questions about cookies and sessions
- Compare the maximum computing power of the Cenozoic top ant s19xp and the existing s19pro in bitland
- Why can't you rob scientists of NFT
- What is the real performance of CK5, the king machine of CKB?
- NLopt--非线性优化--原理介绍及使用方法
- 技能梳理[email protected]+adxl345+电机震动+串口输出
猜你喜欢

Guolin was crowned the third place of global popularity of perfect master in the third quarter of 2022

MySQL index, transaction and storage engine of database (1)

CSDN博客运营团队2022年H1总结

著名画家史国良《丰收时节》数字藏品上线长城数艺

MySQL advanced SQL statement of database (2)

Dyson design award, changing the world with sustainable design

ArcGIS Pro + PS 矢量化用地规划图

GeoffreyHinton:我的五十年深度学习生涯与研究心法

Nlopt -- Nonlinear Optimization -- principle introduction and application method

Arm新CPU性能提升22%,最高可组合12核,GPU首配硬件光追,网友:跟苹果的差距越来越大了...
随机推荐
半钢同轴射频线的史密斯圆图查看和网络分析仪E5071C的射频线匹配校准
技能梳理[email protected]+adxl345+电机震动+串口输出
CVPR 2022 | 清华&字节&京东提出BrT:用于视觉和点云3D目标检测的桥接Transformer
GD32 RT-Thread flash驱动函数
[AGC] build service 3- authentication service example
Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi
Great Wall digital art digital collection platform releases the creation Badge
Deployment of efficient and versatile clusters lvs+kept highly available clusters
【C语言快速上手】带你了解C语言,零基础入门③
背课文记单词,读课文记单词,读文章记单词;40篇文章搞定3500词;71篇文章突破中考单词;15篇文章贯通四级词汇;15篇文章贯通六级词汇
Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】
MySQL index, transaction and storage engine of database (2)
"Kunming City coffee map" was opened again, and coffee brought the city closer
Go -- standard library sort package
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
Configure Yii: display MySQL extension module verification failed
R语言aov函数进行重复测量方差分析(Repeated measures ANOVA、其中一个组内因素和一个组间因素)、分别使用interaction.plot函数和boxplot对交互作用进行可视化
6.Redis新数据类型
GeoffreyHinton:我的五十年深度学习生涯与研究心法
技能梳理[email protected]在oled上控制一条狗的奔跑