当前位置:网站首页>Smart street lamp based on stm32+ Huawei cloud IOT design

Smart street lamp based on stm32+ Huawei cloud IOT design

2022-07-06 17:48:00 InfoQ

1.  Preface

With the rapid development of industry and cities , The industry of road lighting system is also developing rapidly , And tend to complicate , In order to control and maintain complex street lamp system more economically , Major technology companies in the market have developed various street lamp control systems , And the street lights are connected to the Internet of things cloud , Connected to the government network , You can know the working condition of street lamps remotely , Operation of the . In order to understand the Internet of things platform , Learn about Internet of things development , Understand the development of smart street lamps , This paper uses Huawei cloud IOT Internet of things platform , choice STM32 As the main control chip , Then cooperate with relevant sensors to complete the development of smart street lamps .

2.  The specific functions and related hardware

The intelligent street lamp currently designed adopts NBIOT modular -BC20 Connect Huawei cloud Internet of things server , Upload various parameter information of street lamps : Ambient light intensity 、 Solar charging panel voltage 、 Lithium battery power and other information . Designed a mobile phone APP, Through the development interface provided by the application side of Huawei cloud Internet of things platform , Get the parameter information uploaded by the street lamp , And it can be on the mobile phone APP On the remote manual control street lamp switch , Set the opening and closing time of street lamps, etc .  The main control chip of smart street lamp adopts STM32F103C8T6、NBIOT Networking module adopts BC20,BC20 Built in MQTT agreement , Can pass AT The command connects the Internet of things platform to realize communication . Smart street lamps are powered by lithium batteries , And equipped with solar charging panel , The sun can be used to charge and compensate the power . Power monitor is used , Battery power detection chip , The charging efficiency can be detected 、 electric current 、 voltage 、 Battery capacity and other information .BC20 With GPS function , You can report the of street lights GPS Location to cloud server , mobile phone APP After acquisition , You can call Baidu map to display the position of street lights .

null
null
null
null
null
null

3.  Hardware selection

3.1 STM32F103C8T6

null
null

3.2  Solar panels

null

3.3  Lithium battery charging module

null

3.4  Power detection module

null

3.5 BH1750 Photosensitive sensors

null

3.6 LED The lamp

null

3.7 BC20-NBIOT modular

model : BC20+BD+GPS
brand :  Creative thinking
Place of Origin :  The Chinese mainland
Interface type : TTL
Applicable scenario : NBIOT
Size : 40x40x12mm
Working current : 0.5A
Support TCP/IP agreement : 
Support transmission rate : 115200Kbps
Working voltage : 5V
Whether voice call is supported :  no
Module type :  other
Whether SMS is supported :  no
Support standard : GSM/GPRS(2G)
Whether the phone book is supported :  no

null

4.  Create products and devices

4.1  Create products

Address :https://www.huaweicloud.com/?locale=zh-cn

null
null
null
null

4.2  Custom model

Address : https://console.huaweicloud.com/iotdm/?region=cn-north-4#/dm-dev/all-product/7211833377cf435c8c0580de390eedbe/product-detail/6276134223aaf461a0f6e515

null
null
null
null
null
null
null
null
null

4.3  Create device

null
null
{
 "device_id": "6276134223aaf461a0f6e515_1126626497",
 "secret": "12345678"
}

4.4 MQTT Key generation

After creating the product 、 After the device , Then you need to know how to pass MQTT Log in to the Huawei ECS according to the protocol . The official details are here :https://support.huaweicloud.com/devg-iothub/iot_01_2127.html#ZH-CN_TOPIC_0240834853__zh-cn_topic_0251997880_li365284516112

Attribute reporting format :https://support.huaweicloud.com/api-iothub/iot_06_v5_3010.html

null
null
MQTT Device login key generation address : https://iot-tool.obs-website.cn-north-4.myhuaweicloud.com/

null
null
DeviceId 6276134223aaf461a0f6e515_1126626497
DeviceSecret 12345678
ClientId 6276134223aaf461a0f6e515_1126626497_0_0_2022050706
Username 6276134223aaf461a0f6e515_1126626497
Password 73ebe0779dbd5b2e2fd3db8ab8f642b78d7a532576f2e14d2799d4f78d37bcc8

The domain name of Huawei cloud Internet of things platform is : 
161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com
Of Huawei's cloud Internet of things platform IP The address is :
121.36.42.100
After the parameters are filled correctly in the software , You can see that the device has been successfully connected . Next, open the device page , You can see that the device is online .

null
null

4.5  Topic subscription and Publishing

// Subscribe to topics :  The platform sends a message to the device
$oc/devices/6276134223aaf461a0f6e515_1126626497/sys/messages/down
// Equipment report data
$oc/devices/6276134223aaf461a0f6e515_1126626497/sys/properties/report
// Reported attribute message  ( Multiple attributes can be reported at a time , stay json Just add it to the )
{"services": [{"service_id": "led","properties":{"GPS":"lat:12.345,lng:45.678"}}]}

adopt MQTT Client software simulation report test :

null
null

4.6  Application side development

In order to display equipment data more conveniently , Interact with the device , We also need to develop a supporting host computer , The official provides the application side development API Interface 、SDK Interface , For convenience and versatility , I'm using API Interface to complete data interaction , The upper computer software adopts QT Development .

Help document address : ttps://support.huaweicloud.com/api-iothub/iot_06_v5_0034.html

The device attribute is the sensor status data information uploaded by the device , The application side provides API Interface , It can actively send request instructions to the device end ; After receiving the instruction, the device needs to report the data according to the agreed data format ; therefore , To realize the data interaction between the application layer and the device side , It needs the cooperation between the application layer and the device end to complete .

null
null

5. STM32 Programming

STM32 Connecting Huawei cloud IOT Engineering cases : https://download.csdn.net/download/xiaolong1126626497/81993720

null

5.1 BC20 Connect Huawei cloud Internet of things server - debugging

null
Connect MQTT The server
AT+QMTOPEN=0,"a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com",1883

OK

+QMTOPEN: 0,0


Sign in MQTT The server
Command format : AT+QMTCONN=<tcpconnectID>,<clientID>,<username>,<password>
AT+QMTCONN=0,&quot;6210e8acde9933029be8facf_dev1_0_0_2022021913&quot;,&quot;6210e8acde9933029be8facf_dev1&quot;,&quot;6cea55404b463e666cd7a6060daba745bbaa17fe7078dfef45f8151cdf19673d&quot;

OK

+QMTCONN: 0,0,0


Subscribe to topics
Command format : AT+QMTSUB=<tcpconnectID>,<msgID>,&quot;<topic1>”,<qos1>[,&quot;<topic2>”,<qos2>…]

AT+QMTSUB=0,1,&quot;$oc/devices/6210e8acde9933029be8facf_dev1/sys/messages/down&quot;,2

OK

+QMTSUB: 0,1,0,2


Release theme
Command format :AT+QMTPUB=<tcpconnectID>,<msgID>,<qos>,<retain>,&quot;<topic>&quot;,&quot;<msg>&quot;

Send instructions first : 
AT+QMTPUB=0,0,0,0,&quot;$oc/devices/6210e8acde9933029be8facf_dev1/sys/properties/repor&quot;

Waiting to return  &quot;>&quot; 
And then send the data . You don't need to return the car .
&quot;{&quot;services&quot;: [{&quot;service_id&quot;: &quot;gps&quot;,&quot;properties&quot;:{&quot;longitude&quot;:12.345,&quot;latitude&quot;:33.345}}]}&quot;
Data sent , Then send the Terminator .  Hexadecimal value --0x1a . Some serial debugging assistants can adapt to ctrl+z  Shortcut key input 0xA
Wait for the module to return &quot;OK&quot;, This data transmission is complete . 
OK

+QMTPUB: 0,0,0

5.2  Test module

After the first step is connected , The serial port debugging assistant selects the baud rate as 115200, Check the send new line option on the software . send out
AT
In the past , The normal module will return
OK
.

Check whether the module is normal
AT

OK


Get the card number , Check whether the card is inserted properly
AT+CIMI

460041052911195

OK


Activate the network
AT+CGATT=1

OK


Get network activation status
AT+CGATT?

+CGATT: 1

OK


Query network quality
AT+CSQ

+CSQ: 26,0

OK
 
AT+CEREG=? // Check network status
+CEREG: 0,1 // Find network successfully
OK

5.3 keil Project code

MQTT The protocol connects Huawei cloud IOT Source project : https://download.csdn.net/download/xiaolong1126626497/81993720

null
null
null

5.4  Power detection

#include &quot;INA226.h&quot;
#include &quot;delay.h&quot;
//  Wiring instructions :
//  simulation IIC:
//IIC_SCL --  Clock line PB6( push-pull 、 Open drain output )
//IIC_SDA --  Two way data line PB7
INA226 ina226_data;
// initialization INA226
void INA226_Init(void)


 IIC_Init();
 INA226_SendData(INA226_ADDR1,CFG_REG,0x8000); // Restart
 
 INA226_SendData(INA226_ADDR1,CFG_REG,0x484f); // Set the conversion time 204us, The average number of times 128, The sampling time is 204*128, Set the mode to shunt and bus continuous mode
 INA226_SendData(INA226_ADDR1,CAL_REG,CAL); // Set resolution
 //INA226_SendData(INA226_ADDR1,CAL_REG,0x0012);// Set shunt voltage to current conversion parameters  
 INA226_Get_ID(INA226_ADDR1); // obtain ina226 Of id
}

// Set register pointer
void INA226_SetRegPointer(u8 addr,u8 reg)
{
 IIC_Start();

 IIC_Send_Byte(addr);
 IIC_Wait_Ack();

 IIC_Send_Byte(reg);
 IIC_Wait_Ack();

 IIC_Stop();
}

// send out , Write data
void INA226_SendData(u8 addr,u8 reg,u16 data)
{
 u8 temp=0;
 IIC_Start();

 IIC_Send_Byte(addr);
 IIC_Wait_Ack();

 IIC_Send_Byte(reg);
 IIC_Wait_Ack();
 
 temp = (u8)(data>>8);
 IIC_Send_Byte(temp);
 IIC_Wait_Ack();

 temp = (u8)(data&0x00FF);
 IIC_Send_Byte(temp);
 IIC_Wait_Ack();
 
 IIC_Stop();
}

// Reading data
u16 INA226_ReadData(u8 addr)
{
 u16 temp=0;
 IIC_Start();

 IIC_Send_Byte(addr+1);
 IIC_Wait_Ack();
 
 temp = IIC_Read_Byte(1);
 temp<<=8; 
 temp |= IIC_Read_Byte(0);
 
 IIC_Stop();
 return temp;
}
//1mA/bit
u16 INA226_GetShunt_Current(u8 addr)
{
 u16 temp=0; 
 INA226_SetRegPointer(addr,CUR_REG);
 temp = INA226_ReadData(addr);
 if(temp&0x8000) temp = ~(temp - 1); 
 return temp;
}

// obtain id
void INA226_Get_ID(u8 addr)
{
 u32 temp=0;
 INA226_SetRegPointer(addr,INA226_GET_ADDR);
 temp = INA226_ReadData(addr);
 ina226_data.ina226_id = temp;
}

// Obtain the calibration value
u16 INA226_GET_CAL_REG(u8 addr)

 u32 temp=0;
 INA226_SetRegPointer(addr,CAL_REG);
 temp = INA226_ReadData(addr);
 return (u16)temp;
}

//1.25mV/bit
u16 INA226_GetVoltage(u8 addr)
{
 u32 temp = 0;
 INA226_SetRegPointer(addr,BV_REG);
 temp = INA226_ReadData(addr);
 return (u16)temp; 
}

//2.5uV/bit
u16 INA226_GetShuntVoltage(u8 addr)
{
 int16_t temp = 0;
 INA226_SetRegPointer(addr,SV_REG);
 temp = INA226_ReadData(addr);
 if(temp&0x8000) temp = ~(temp - 1); 
 return (u16)temp; 
}

// Get the voltage
void GetVoltage(float *Voltage)//mV
{
 *Voltage = INA226_GetVoltage(INA226_ADDR1)*Voltage_LSB;
}

// Obtain shunt voltage
void Get_Shunt_voltage(float *Voltage)//uV
{
 *Voltage = (INA226_GetShuntVoltage(INA226_ADDR1)*INA226_VAL_LSB);// To correct the current shunt parameters, please change this to 2.5
}

// Get current
void Get_Shunt_Current(float *Current)//mA
{
 *Current = (INA226_GetShunt_Current(INA226_ADDR1)* CURRENT_LSB);
}

// Get power =  Bus voltage  *  electric current
void get_power()//W
{
 GetVoltage(&ina226_data.voltageVal); //mV
 Get_Shunt_voltage(&ina226_data.Shunt_voltage); //uV
 Get_Shunt_Current(&ina226_data.Shunt_Current); //mA
 Get_Power(&ina226_data.Power);
 ina226_data.Power_Val = ina226_data.voltageVal*0.001f * ina226_data.Shunt_Current*0.001f; //mV*mA
}

// Get the power loading value ,ina226 Internally calculated power , Because it is not calibrated , So... Is not used

u16 INA226_Get_Power(u8 addr)
{
 int16_t temp=0;
 INA226_SetRegPointer(addr,PWR_REG);
 temp = INA226_ReadData(addr);
 return (u16)temp;
}

// Get power ,ina226 Internal calculation , inaccurate , Do not use
void Get_Power(float *Power)//W
{
 *Power = (INA226_Get_Power(INA226_ADDR1)*POWER_LSB);
}

// No alarm is set , Abandon
/*
u8 INA226_AlertAddr()
{
 u8 temp;
 IIC_Start();

 IIC_Send_Byte(INA226_GETALADDR);
 IIC_Wait_Ack();
 
 temp = IIC_Read_Byte(1);
 
 IIC_Stop();
 return temp;
}
*/

5.5 BH1750 Ambient light intensity

#include &quot;bh1750.h&quot;
float Read_BH1750_Data()
{
 unsigned char t0;
 unsigned char t1;
 float t;
 u8 r_s=0;
 IIC_Start(); // Send the start signal
 IIC_WriteOneByteData(0x46);
 r_s=IIC_GetACK();// Get answers
 if(r_s)printf(&quot;error:1\r\n&quot;);
 IIC_WriteOneByteData(0x01);
 r_s=IIC_GetACK();// Get answers
 if(r_s)printf(&quot;error:2\r\n&quot;);
 IIC_Stop(); // Stop signal  
 
 IIC_Start(); // Send the start signal
 IIC_WriteOneByteData(0x46);
 r_s=IIC_GetACK();// Get answers
 if(r_s)printf(&quot;error:3\r\n&quot;);
 IIC_WriteOneByteData(0x01);
 r_s=IIC_GetACK();// Get answers
 if(r_s)printf(&quot;error:4\r\n&quot;);
 IIC_Stop(); // Stop signal  
 
 IIC_Start(); // Send the start signal
 IIC_WriteOneByteData(0x46);
 r_s=IIC_GetACK();// Get answers
 if(r_s)printf(&quot;error:5\r\n&quot;);
 IIC_WriteOneByteData(0x10);
 r_s=IIC_GetACK();// Get answers
 if(r_s)printf(&quot;error:6\r\n&quot;);
 IIC_Stop(); // Stop signal  
 
 DelayMs(100); // wait for
 
 IIC_Start(); // Send the start signal
 IIC_WriteOneByteData(0x47);
 r_s=IIC_GetACK();// Get answers
 if(r_s)printf(&quot;error:7\r\n&quot;);
 
 t0=IIC_ReadOneByteData(); // receive data
 IIC_SendACK(0); // Send reply signal
 t1=IIC_ReadOneByteData(); // receive data
 IIC_SendACK(1); // Send non reply signal
 IIC_Stop(); // Stop signal
 
 t=(((t0<<8)|t1)/1.2);
 return t; 
}

原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060938487923.html