当前位置:网站首页>Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
Alcohol driving monitoring system based on stm32+ Huawei cloud IOT design
2022-07-04 14:32:00 【InfoQ】
1. Preface





2. Log in to Huawei cloud to create cloud devices
2.1 Create products





MQTT (1883) a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com



2.2 Create device



equipment ID
625cd29cecf9c41c38215ba5_1126626497
Device key
1126626497
{
"device_id": "625cd29cecf9c41c38215ba5_1126626497",
"secret": "1126626497"
}
2.3 Product model definition




2.4 Generate MQTT Login key



2.5 Use MQTT Client software login
a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com
121.36.42.100
2.6 Report data


// Subscribe to topics : The platform sends a message to the device
$oc/devices/625cd29cecf9c41c38215ba5_1126626497/sys/messages/down
// Equipment report data
$oc/devices/625cd29cecf9c41c38215ba5_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": "MQ3","properties":{"MQ3":50}}]}
3. STM32 Code implementation
3.1 System schematic diagram


3.2 Program download


3.3 Font template

3.4 main.c Code
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include <string.h>
#include "timer.h"
#include "oled.h"
#include "adc.h"
#include <string.h>
#include <stdlib.h>
#include "font.h"
#include "sim800c.h"
#include "mqtt.h"
/*
Hardware connection mode :
1. TFT 1.44 Inch color screen wiring
GND Power ground
VCC Pick up 5V or 3.3v Power Supply
SCL Pick up PC8(SCL)
SDA Pick up PC9(SDA)
RST Pick up PC10
DC Pick up PB7
CS Pick up PB8
BL Pick up PB11
2. Buzzer
VCC--->3.3V
DAT--->PA6
GND--->GND
3. Alcohol detection sensor MQ3
VCC--->3.3V
GND--->GND
DAT--->PA1
4. On board LED Lamp wiring
LED1---PA8
LED2---PD2
5. On board key wiring
K0---PA0
K1---PC5
K2---PA15
6. SIM800C--GSM modular
GND----GND
VCC--->3.3V
PA2----SIM800C_RXD
PA3----SIM800C_TXD
*/
// Equipment information of Huawei Internet of things server
#define MQTT_ClientID "62381267575fb713ee164ad2_xl_1_0_0_2022032106"
#define MQTT_UserName "62381267575fb713ee164ad2_xl_1"
#define MQTT_PassWord "124344feff3e3d96ff6af13cf36af36766619ff1eeee40e99cbae9b7b9739fe4"
// Topics subscribed and published
#define SET_TOPIC "$oc/devices/62381267575fb713ee164ad2_xl_1/sys/messages/down" // subscribe
#define POST_TOPIC "$oc/devices/62381267575fb713ee164ad2_xl_1/sys/properties/report" // Release
// Set the connected router information
#define CONNECT_WIFI "Xiaomi_meizi6" // The name of the router to be connected -- Do not appear in Chinese 、 Special characters such as spaces
#define CONNECT_PASS "12170307yu" // The password of the router to be connected
#define CONNECT_SERVER_IP "a161a58a78.iot-mqtts.cn-north-4.myhuaweicloud.com" // The server IP Address
#define CONNECT_SERVER_PORT 1883 // Server port
//JTAG Mode setting , Used for setting up JTAG The pattern of
//mode:jtag,swd Mode setting ;00, All enable ;01, Can make SWD;10, All closed ;
#define JTAG_SWD_DISABLE 0X02
#define SWD_ENABLE 0X01
#define JTAG_SWD_ENABLE 0X00
void JTAG_Set(u8 mode)
{
u32 temp;
temp=mode;
temp<<=25;
RCC->APB2ENR|=1<<0; // Turn on the auxiliary clock
AFIO->MAPR&=0XF8FFFFFF; // eliminate MAPR Of [26:24]
AFIO->MAPR|=temp; // Set up jtag Pattern
}
u16 MQ3_data=0; // Alcohol concentration
float MQ3_data_float=0; //mg/l
char data_buff[100];
// Excessive alcohol indicates
u8 sim800c_buff[100]="Drunk driving reminder";
int main()
{
u8 state=0;
u8 key=0;
u32 time_cnt=0;
JTAG_Set(JTAG_SWD_DISABLE); // Release PA15
LED_Init(); //LED Lamp initialization
BEEP_Init(); // Buzzer initialization
KEY_Init(); // Key initialization
USART1_Init(115200); // A serial port 1 initialization - Print debug information
Lcd_Init(); //LCD initialization
Lcd_Clear(0); // The screen is black
LCD_LED_SET; // adopt IO Control back light
AdcInit(); //ADC initialization -- Detect the value of the alcohol sensor
TIMER2_Init(72,20000);// Auxiliary serial port 2 receive , The timeout is 20ms
USART2_Init(115200); // Possible baud rate ( test ): 57600 、9600、115200
USART3_Init(115200);// A serial port -WIFI
TIMER3_Init(72,20000); // Timeout time 20ms
// Preheat video
LCD_ShowChineseFont(0,16*3,16,HZ_FONT_16[20],RED,0);
LCD_ShowChineseFont(16,16*3,16,HZ_FONT_16[21],RED,0);
LCD_ShowChineseFont(16*2,16*3,16,HZ_FONT_16[22],RED,0);
LCD_ShowChineseFont(16*3,16*3,16,HZ_FONT_16[23],RED,0);
LCD_ShowChineseFont(16*4,16*3,16,HZ_FONT_16[24],RED,0);
LCD_ShowChineseFont(16*5,16*3,16,HZ_FONT_16[25],RED,0);
LCD_ShowChineseFont(16*6,16*3,16,HZ_FONT_16[26],RED,0);
LCD_ShowChineseFont(16*7,16*3,16,HZ_FONT_16[27],RED,0);
//delay_ms(30000); // Initialization time 30 second
delay_ms(5000); // Initialization time 5 second
Lcd_Clear(0); // The screen is black
// Real data of collected data
LCD_ShowChineseFont(0,16*2,16,HZ_FONT_16[0],RED,0);
LCD_ShowChineseFont(16,16*2,16,HZ_FONT_16[1],RED,0);
LCD_ShowChineseFont(16*2,16*2,16,HZ_FONT_16[2],RED,0);
LCD_ShowChineseFont(16*3,16*2,16,HZ_FONT_16[3],RED,0);
// Alcohol content calculation results
LCD_ShowChineseFont(0,16*3,16,HZ_FONT_16[4],RED,0);
LCD_ShowChineseFont(16,16*3,16,HZ_FONT_16[5],RED,0);
LCD_ShowChineseFont(16*2,16*3,16,HZ_FONT_16[6],RED,0);
LCD_ShowChineseFont(16*3,16*3,16,HZ_FONT_16[7],RED,0);
// Excessive alcohol shows
Gui_DrawFont_GBK16(0,16*4,RED,0,(u8*)" ");
LCD_ShowChineseFont(0,16*4,16,HZ_FONT_16[16],RED,0);
LCD_ShowChineseFont(16,16*4,16,HZ_FONT_16[17],RED,0);
LCD_ShowChineseFont(16*2,16*4,16,HZ_FONT_16[18],RED,0);
LCD_ShowChineseFont(16*3,16*4,16,HZ_FONT_16[19],RED,0);
// initialization SIM800C
state=SIM800C_InitCheck();
printf("SIM800C Initialization status :%d\r\n",state);
DelayMs(1000);
// Set text mode
state=SIM800C_SetNoteTextMode();
printf(" Set text mode status :%d\r\n",state);
DelayMs(1000);
//
printf(" Initializing WIFI One moment please .\r\n");
for(i=0;i<5;i++)
{
if(ESP8266_Init()==0)
{
esp8266_state=1;
break;
}
else
{
esp8266_state=0;
printf("ESP8266 Hardware detection error .\n");
}
}
if(esp8266_state)
{
printf(" Ready to connect to the server ....\r\n");
// Unencrypted port
printf("WIFI:%d\n",ESP8266_STA_TCP_Client_Mode(CONNECT_WIFI,CONNECT_PASS,CONNECT_SERVER_IP,CONNECT_SERVER_PORT,1));
//2. MQTT Protocol initialization
MQTT_Init();
//3. Connect to server
for(i=0;i<5;i++)
{
if(MQTT_Connect(MQTT_ClientID,MQTT_UserName,MQTT_PassWord)==0)
{
esp8266_state=1;
break;
}
esp8266_state=0;
printf(" Server connection failed , Retrying ...\n");
delay_ms(500);
}
printf(" Server connection successful .\n");
//3. Subscribe to topics
if(MQTT_SubscribeTopic(SET_TOPIC,0,1))
{
printf(" Topic subscription failed .\n");
}
else
{
printf(" Topic subscription succeeded .\n");
}
}
while(1)
{
key=KEY_Scan();
if(key)
{
printf("key=%d\r\n",key);
}
// control LED The lamp
if(key==2)
{
LED2=!LED2;
}
// Manual control buzzer test
// Send SMS test manually
if(key==1)
{
BEEP=1;
delay_ms(100);
BEEP=0;
sprintf((char*)sim800c_buff,"Drunk driving reminder:%d",MQ3_data);
// Send a text message
if(SIM800C_SendNote((u8*)"13800138000",sim800c_buff,strlen((char*)sim800c_buff))==0)
printf(" Message sent successfully \r\n");
else
printf(" SMS sending failed \r\n");
}
// Polling time arrives
if(time_cnt>300)
{
time_cnt=0;
LED1=!LED1;
// Obtain air quality
MQ3_data=GetAdcCHxDATA(1);
printf(" collection MQ3 data :%d\r\n",MQ3_data);
// Report data
sprintf(data_buff,"{\"services\": [{\"service_id\":\"STM32\",\"properties\":{\"MQ3\":%d}]}",
MQ3_data);
MQTT_PublishData(POST_TOPIC,data_buff,0);
// Report data
sprintf(data_buff,"{\"services\": [{\"service_id\":\"STM32\",\"properties\":{\"MQ3\":%d}]}",
MQ3_data);
MQTT_PublishData(POST_TOPIC,data_buff,0);
//LCD The screen displays the alcohol concentration in real time
// Collect data
sprintf(data_buff,"%d",MQ3_data);
Gui_DrawFont_GBK16(72,16*2,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*2,RED,0,(u8*)data_buff);
// alcohol content
//MQ3_data_float=((float)MQ3_data*(5.0/4096))*0.36-1.08;
MQ3_data_float=((float)MQ3_data*(5.0/4096))*0.36;
sprintf(data_buff,"%0.2f mg/L",MQ3_data_float);
printf(" Calculate the alcohol concentration :%s\r\n",data_buff);
Gui_DrawFont_GBK16(72,16*3,RED,0,(u8*)" ");
Gui_DrawFont_GBK16(72,16*3,RED,0,(u8*)data_buff);
// Judge whether the alcohol concentration exceeds the standard , Set the threshold
if(MQ3_data>2000)
{
sprintf((char*)sim800c_buff," :%d",MQ3_data);
// Send a text message
if(SIM800C_SendNote((u8*)"18171571217",sim800c_buff,strlen((char*)sim800c_buff))==0)
printf(" Message sent successfully \r\n");
else
printf(" SMS sending failed \r\n");
}
// The smaller the value collected , Indicates that the higher the alcohol concentration . On the contrary, the bigger .
// Display different text prompts according to the threshold value
if(MQ3_data>2000)
{
// You have drunk driving
Gui_DrawFont_GBK16(0,16*4,RED,0,(u8*)" ");
LCD_ShowChineseFont(0,16*4,16,HZ_FONT_16[8],RED,0);
LCD_ShowChineseFont(16,16*4,16,HZ_FONT_16[9],RED,0);
LCD_ShowChineseFont(16*2,16*4,16,HZ_FONT_16[10],RED,0);
LCD_ShowChineseFont(16*3,16*4,16,HZ_FONT_16[11],RED,0);
}
// The value collected in the normal stable range is 500 about
else
{
// Safe range
Gui_DrawFont_GBK16(0,16*4,RED,0,(u8*)" ");
LCD_ShowChineseFont(0,16*4,16,HZ_FONT_16[16],RED,0);
LCD_ShowChineseFont(16,16*4,16,HZ_FONT_16[17],RED,0);
LCD_ShowChineseFont(16*2,16*4,16,HZ_FONT_16[18],RED,0);
LCD_ShowChineseFont(16*3,16*4,16,HZ_FONT_16[19],RED,0);
}
}
DelayMs(10);
time_cnt++;
}
}
边栏推荐
- Count the running time of PHP program and set the maximum running time of PHP
- AI与生命科学
- Industrial Internet has greater development potential and more industry scenarios
- Talk about 10 tips to ensure thread safety
- GCC [6] - 4 stages of compilation
- SqlServer函数,存储过程的创建和使用
- Leetcode T48:旋转图像
- Excel quickly merges multiple rows of data
- The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
- 第十七章 进程内存
猜你喜欢
No servers available for service: xxxx
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Scratch Castle Adventure Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Leetcode T48:旋转图像
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on GPIO)
Ultrasonic distance meter based on 51 single chip microcomputer
Codeforce:c. sum of substrings
flink sql-client.sh 使用教程
随机推荐
实战解惑 | OpenCV中如何提取不规则ROI区域
(1) The standard of performance tuning and the correct posture for tuning - if you have performance problems, go to the heapdump performance community!
LVGL 8.2 Line
Ultrasonic distance meter based on 51 single chip microcomputer
使用CLion编译OGLPG-9th-Edition源码
LVGL 8.2 Menu
Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
Respect others' behavior
商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
How to operate and invest games on behalf of others at sea
R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
Red envelope activity design in e-commerce system
Detailed analysis of pytorch's automatic derivation mechanism, pytorch's core magic
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
Data center concept
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
【信息检索】分类和聚类的实验
opencv3.2 和opencv2.4安装
Oppo find N2 product form first exposure: supplement all short boards
DDD application and practice of domestic hotel transactions -- Code