当前位置:网站首页>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++;
}
}
边栏推荐
- Test evaluation of software testing
- Ultrasonic distance meter based on 51 single chip microcomputer
- Ruiji takeout notes
- Innovation and development of independent industrial software
- 92. (cesium chapter) cesium building layering
- 产业互联网则具备更大的发展潜能,具备更多的行业场景
- ViewModel 初体验
- Why should Base64 encoding be used for image transmission
- flink sql-client.sh 使用教程
- 2022 game going to sea practical release strategy
猜你喜欢
软件测试之测试评估
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
Nowcoder reverse linked list
Test evaluation of software testing
Data warehouse interview question preparation
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Why should Base64 encoding be used for image transmission
[information retrieval] link analysis
LVGL 8.2 text shadow
Data center concept
随机推荐
Leetcode T47: 全排列II
软件测试之测试评估
卷积神经网络经典论文集合(深度学习分类篇)
Innovation and development of independent industrial software
nowcoder重排链表
Xcode 异常图片导致ipa包增大问题
92. (cesium chapter) cesium building layering
LVGL 8.2 Menu
Respect others' behavior
Chapter 16 string localization and message Dictionary (2)
Ultrasonic distance meter based on 51 single chip microcomputer
[MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
Leetcode t49: grouping of alphabetic words
關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
flink sql-client. SH tutorial
尊重他人的行为
DDD application and practice of domestic hotel transactions -- Code
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
Digi重启XBee-Pro S2C生产,有些差别需要注意
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables