当前位置:网站首页>Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
2022-07-29 04:01:00 【Forever with 1】
List of articles
One 、Lua Language to extract the desired data from the string
Lua Link to the use of regular expressions in
1、 problem
lua Language string extraction is in 2G Module and 4G Modular lua Used in code writing , It mainly deals with the data received by the serial port , Then extract each value , Assign to the corresponding variable , Form a new key value pair , Package and upload the data to Alibaba cloud
2、 Preliminary code
STM32+AIR800 Simple control of temperature and humidity collection and uploading to Alibaba cloud and issuing instructions from the cloud - link
String extraction used ( Yes STM32 The sent data is used to extract the temperature, humidity and smoke concentration ): Although data is often not extracted , But the function can be realized , The code is like this ghost :
3、 Code improvement
Occasionally used some time ago Cosmopolitan 4G modular when , Time interval 2 In, we need to extract the data received by the serial port , Use serial port to receive STM32 Data sent , For example, there is temperature , humidity , Carbon dioxide concentration 、 Status of each switch , This is definitely not enough , So take time to improve it
LUA Multiple data can be returned in the language , But there is also a hole in the use of regular expressions , Even the number of returned data is limited , Beyond the limit, there will be inexplicable errors ; When only fourorfive data were extracted in the early stage, everything was normal , When I need to send the status of each switch to 4g modular , When all written in one piece , It is impossible to extract data normally , There will also be problems with the data upload that could have been achieved , It took a while for this problem to be found and solved , Final solution : That is to extract data twice to achieve your desired goal ( The following code )
// Raw data :
//temp:20,hum:30,co2:20,water_weight:100,food_weight:200.......
// To extract the value corresponding to the corresponding key
data = uart.read(UART_ID,"*l")-- Data received by serial port
a,b,c,d,e=string.match(data,'temp:(%d+),hum:(%d+),co2:(%d+),water_weight:(%d+),food_weight:(%d+)')
f,g,h,i,j=string.match(data,'Led_switch_STA:(%d),Air_switch_STA:(%d),Water_switch_STA:(%d),Food_switch_STA:(%d),Temp_switch_STA:(%d)')
if a~=nil and b~=nil and c~=nil and d~=nil and e~=nil then
-- Execute corresponding statement
end
if f~=nil and g~=nil and i~=nil and j~=nil and h~=nil then
-- Execute corresponding statement
end
Two 、C Language - Extract the data you want from the string
1、 Have a problem
3-STM32+ESP8266 Connect onenet Upload data + Remote control (MQTT)- link
The reason why this string extraction is used , Because I'm using ESP8266 subscribe onenet When analyzing the data distributed by the cloud platform , If you use json analysis , Compiled code will exceed memory , Can't download to STM32 in ( chip STM32F103C6T6), If you use STM32F103ZET6 or C8T6 When the master chip , Use json Data analysis is completely OK . To solve this problem , Therefore, string matching and string extraction methods are used to extract numerical values
sscanf stay c Usage in language - link
2、 Problem solving inspiration
The following code extracts the numbers and prints them :
#include <stdio.h>
#include<string.h>
int main()
{
int a,b,c;
sscanf("2015.04.05", "%d.%d.%d", &a,&b,&c); // Get the required string
printf("a=%d,b=%d,c=%d\n",a,b,c); // a=2015,b=4,c=5
}
3、 resolvent
Extract some numbers from a string ( It is mainly used to extract the value corresponding to the key in the key value pair )
For example, subscribe to the message , It exists in the form of key value pairs , We need to judge the value corresponding to the specified key
such as :
(1) data “LED1”:1 in key “LED” Of value yes 1( Judge LED1 Perform the open or close operation according to this instruction )
(2) data “PWM”:245 In the key “PWM” The value is 245( It is used to adjust the duty cycle and then adjust the light brightness )
The following program is used to return PWM The corresponding value ,req And print the corresponding value : Use two methods to extract ( Maybe there is a more suitable method , If you know something, you can add it in the comment area )
#include <stdio.h>
#include<string.h>
int main()
{
//char *st="{"PWM":200},req:129";// Error definition : stay C If you want to advance quotation marks in a language, you need to use escape characters
char *st="{\"PWM\":200},req:129";// Correct definition : Define a string
char a,b,c,d,e,f;// Store characters
int num1,num2,num3,num4;
// Method 1 :
sscanf(st, "{\"%c%c%c\":%d},%c%c%c:%d",&a,&b,&c,&num1,&d,&e,&f,&num2); // Get the required string
printf("num1=%d,num2=%d\n",num1,num2);
// Method 2
sscanf(st, "{\"PWM\":%d},req:%d", &num3,&num4); // Take the corresponding number directly
printf("num3=%d,num4=%d\n",num3,num4);
}
STM32 Using prototypes in :
Subscribed data :{“PWM”:123},req:11
Just extract PWM The value of
if(strstr((char *)req_payload, "PWM"))
{
sscanf((char *)req_payload, "{\"%c%c%c\":%d", &a,&b,&c,&PWM_value); // Get the required string
printf("pwm_get_value=%d\r\n",PWM_value);
}
边栏推荐
- mmdetection初步使用
- How to write SQL statements about field conversion
- EMD 经验模态分解
- 【redis系列】字符串数据结构
- [introduction to C language] zzulioj 1031-1035
- 大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
- [deep learning CPU (part outside) - virtual memory]
- Nacos registry
- Deep understanding of Base64 underlying principles
- C language to achieve three chess game (detailed explanation)
猜你喜欢

面试必备!TCP协议经典十五连问!

SQL窗口函数

数据挖掘——关联分析例题代码实现(下)

华为天才少年稚晖君做了一把模块化机械键盘,引起极客圈地震,网友:这才是真正的客制化...

Nacos registry

RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption
![[deep learning CPU (part outside) - virtual memory]](/img/f7/4c72d583456f6f68c52424602ff5d9.png)
[deep learning CPU (part outside) - virtual memory]

Ribbon principle analysis namedcontextfactory

LVS+KeepAlived高可用部署实战应用

消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!
随机推荐
路由 知识
CUB_ Visualization of key points in 200 bird dataset
小马智行进军前装量产,从自研域控制器入手?
Deep understanding of browser caching mechanism (HTTP)
【C语言入门】ZZULIOJ 1031-1035
Microcomputer principle and interface technology
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
Raft protocol - process demonstration
一文学透MySQL表的创建和约束
新零售O2O 电商模式解析
Arrow function of new features of ES6
Analysis of new retail o2o e-commerce model
Extended operator of new features in ES6
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
Typescript from getting started to mastering (19) enumeration types
Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
With more than 5 years of work experience and a salary of 15K, would you accept it if you were me?
代码 ~ 隐藏或禁用状态栏和虚拟按键
Why does the 20 bit address bus determine the storage space of 1MB
Deep understanding of Base64 underlying principles