当前位置:网站首页>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
边栏推荐
- Who should the newly admitted miners bow to in front of the chip machine and the graphics card machine
- 6.Redis新数据类型
- 2022第六季完美童模 合肥赛区 初赛圆满落幕
- Memorize the text and remember the words. Read the text and remember the words. Read the article and remember the words; 40 articles with 3500 words; 71 articles broke through the words in the middle
- "Kunming City coffee map" activity was launched again
- 技能梳理[email protected]体感机械臂
- NLopt--非线性优化--原理介绍及使用方法
- How to seize the opportunity of NFT's "chaos"?
- Arm新CPU性能提升22%,最高可组合12核,GPU首配硬件光追,网友:跟苹果的差距越来越大了...
- Enter the world of helium (hNT) hotspot servers to bring you different benefits
猜你喜欢

6. Redis new data type

Dyson design award, changing the world with sustainable design

今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计

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

记一次实习的经历,趟坑必备(一)

GNN hands on practice (II): reproduction graph attention network gat

光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述

Didn't receive robot state (joint angles) with recent timestamp within 1 seconds

Xinguan has no lover, and all the people benefit from loving deeds to warm the world -- donation to the public welfare action of Shangqiu children's welfare home

I found a wave of "alchemy artifact" in the goose factory. The developer should pack it quickly
随机推荐
The AOV function of R language was used for repeated measures ANOVA (one intra group factor and one inter group factor) and interaction Plot function and boxplot to visualize the interaction
Curl --- the request fails when the post request parameter is too long (more than 1024b)
Eth is not connected to the ore pool
【Rust每周一库】num-bigint - 大整数
那个程序员,被打了。
mysql数据库基础:存储过程和函数
Leetcode question brushing (IV) -- greedy thought (go Implementation)
The famous painter shiguoliang's "harvest season" digital collection was launched on the Great Wall Digital Art
Detailed explanation of commissioning methods and techniques
2022 Season 6 perfect children's model toxon division finals came to a successful conclusion
Foster design method
Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】
Questions about cookies and sessions
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
ArcGIS Pro脚本工具(6)——修复CAD图层数据源
Detailed explanation of SolidWorks mass characteristics (inertia tensor, moment of inertia, inertia spindle)
"Kunming City coffee map" activity was launched again
MIT-6874-Deep Learning in the Life Sciences Week6
Yixian e - commerce publie un rapport trimestriel: adhérer à la R & D et à l’investissement de la marque, réaliser un développement durable et de haute qualité
GD32 RT-Thread RTC驱动函数