华为云IoT智慧物流案例04 | 4G网卡拨号(广和通L610-CAT.1模组)与北斗定位模组(中科微电子)数据解析
回顾
衔接上个章节,稍微调整代码,令1个IIC接口同时接入2个设备并获取数据。 程序修改     
//作者:实柏
//日期:2021-6-30
/************************************************************/
//包含头文件
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/select.h>
#include<sys/time.h>
#include<errno.h>
//宏定义
#define SHT30_Address 0x44 //1000100
#define BH1750_Address 0x23 //0100011
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_SLAVE 0x0703 //IIC从器件的地址设置
#define I2C_BUS_MODE 0x0780
typedef unsigned char uint8;
int fd = -1;
//函数声明
static uint8 dev_Init(void);
static uint8 BH1750_IIC(void);
static uint8 SHT30_IIC(void);
static uint8 BH1750_Dat_To_Lux(uint8* buf);
//
static uint8 dev_Init(void)
{
fd = open("/dev/i2c-0", O_RDWR); // open file and enable read and write
if (fd < 0)
{
perror("Can't open /dev/BH1750 \n"); // open i2c dev file fail
exit(1);
}
printf("open /dev/i2c-0 success !\n"); // open i2c dev file succes
}
static uint8 BH1750_IIC(void)
{
if (ioctl(fd, I2C_SLAVE,BH1750_Address) < 0)
{ //set i2c address
printf("fail to set i2c device slave address!\n");
close(fd);
return - 1;
}
printf("set slave address to 0x%x success!\n", BH1750_Address);
return (1);
}
static uint8 SHT30_IIC(void)
{
if (ioctl(fd, I2C_SLAVE,SHT30_Address) < 0)
{ //set i2c address
printf("fail to set i2c device slave address!\n");
close(fd);
return - 1;
}
printf("set slave address to 0x%x success!\n", SHT30_Address);
return (1);
}
static uint8 BH1750_Dat_To_Lux(uint8* buf)
{
uint8 lux = 0;
lux = buf[0];
lux <<=8;
lux += buf[1];
lux = (int)(lux / 1.2);
}
// main
int main(int argc, char * argv[])
{
unsigned char buf[8] = {0};
dev_Init();
usleep(1000 * 100);
uint8 light = 0;
float temperature=0;
float humidity=0;
while (1)
{
BH1750_IIC();
usleep(1000 * 100);
buf[0] = 0x20;
if ( write(fd, buf, 1) != 1 ) {
perror("faile to write config.\n");
return -1;
}
buf[0] = 0;
if (read(fd, buf, 2) != 2 ) {
perror("faile to read back configure data.\n");
return -1;
}
printf("BH1750 0x%x 0x%x \n", buf[0], buf[1] );
light = BH1750_Dat_To_Lux(buf);
printf("light: %5d lux\r\n",light);
usleep(1000 * 200);
SHT30_IIC();
usleep(1000 * 100);
buf[0] = 0x2C;
buf[1] = 0x0D;
if ( write(fd, buf, 2) != 2 ) {
perror("faile to write config.\n");
return -1;
}
buf[0] = 0;
buf[1] = 0;
if (read(fd, buf, 6) != 6 ) {
perror("faile to read back configure data.\n");
return -1;
}
printf("SHT30---->: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x \n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5] );
temperature = ((float)((buf[0]<<8)+buf[1]))*175/65535-45;
humidity = ((float)((buf[3]<<8)+buf[4]))*100/65535;
printf("temperature:%f humidity:%f\r\n",temperature,humidity);
usleep(1000 * 200);
}
close(fd);
}
编译运行 
第一章 广和通L610-CAT.1模组调测和拨号脚本编写并验证网络
1.连接广和通L610-CAT.1模组,背面插入SIM物联网卡(缺口朝外)  
2.AT指令测试L610模组,将右上角AT开关拨至左边与PC通信  AT测试命令:
-
AT+CPIN? +CPIN: READY //确保 SIM 卡就绪 -
AT+CGSN? 查询 IMEI,每个模块唯一 -
AT+CSQ(信号强度:数值越高,信号越强) +CSQ: 21,99 //确保模块能接收到信号 -
AT+CFUN? (回复+CFUN: 1 确认模块正常工作模式。不是 1 则设置成 1) +CFUN: 1 -
AT+CEREG?(查询驻网状态,1表示驻网成功,2/3,未注册或正在尝试搜索操作) +CEREG: 0,1 -
AT+GTUSBMODE?(查询USB模式,31为PPP,32为ECM) +GTUSBMODE: 31 -
AT+MIPCALL? (查询模块是否启用自动拨号功能,0未启用,1启用) +MIPCALL: 0
3.1开发板lsusb 命令对USB / UART 枚举确认vid/pid (AT开关拨回右侧)  3.2执行加载驱动命令sudo modprobe option 及lsusb 和 ls /dev/ttyUSB* 命令,查看USB串口驱动是否加载成功  
4.PPP脚本编写拨号实现网络功能
4.1 检查终端是否安装 pppd (ppp拨号驱动) 命令:pppd -h  4.2 pppd连接脚本 存放路径:/etc/ppp/peers  
4.3 连接建立规则脚本 存放路径:/etc/ppp/peers  
4.4 PPP拨号 在终端输入命令:pppd call 连接脚本名称 我这里是:pppd call L610_cat1  获取IP地址:pppd call L610_cat1 & 
4.5 ifconfig 查询ppp网络接口是否存在 
ping 一下公网地址验证网络功能 ping www.baidu.com -I ppp0 (-I用来指定使用的网络设备)  发现ping 不通且会弹出这样一个问题(不能访问域名,DNS的问题)
 修改配置文件:vi /etc/resolv.conf 添加语句:nameserver 114.114.114.114  source /etc/resolv.conf 再度ping 华为云MQTT服务器地址 ping iot-mqtts.cn-north-4.myhuaweicloud.com  4G网络成功ping通公网
4.6 PPP 拨号参考流程图 
第二章 北斗定位模组(中科微电子)数据解析
1.连接北斗定位模组(串口通信方式,PPS引脚(时间脉冲信号)可不接),要接上陶瓷天线 连接后红色LED发光提示   2.查看模组发送的数据(模组连接后会自动发送数据)
-
北斗模组是接到E53模块的串口部分  -
通过原理图我们得知连接的是串口5 -
查询设备树文件我们知道其在Linux设备中为ttySTM3 -
在终端进入/dev目录 查找对应设备并查看其发送的数据 -
命令:cat ttySTM3  可以看到北斗定位模组一直在发送各种前缀的数据信息  3.查询手册将上报的数据通过程序(抓取关键字符串的算法)获取准确的定位信息(具体程序开发待后续开发QT应用) 手册:CASIC 多模卫星导航接收机协议规范 RMC头标识代表含义   总结: -
广和通L610-CAT.1模组提供低功耗4G网络与华为云进行通讯,作为物联网网络的桥梁。 -
北斗定位模组作为数据采集终端之一,可提供设备的位置信息。
这篇文章耽搁了许久因为我的模组硬件出了点问题,两位技术指导也是线上软件硬件和我一同排查问题(学到了排查的经验,阿正前辈直接把他的L610模块寄给我进行测试,流泪猫猫头.jpg),很感谢他们的热心帮助。
技术指导: 华为云社区与华为云学院:https://edu.huaweicloud.com/ 人员:阿正前辈 广和通技术指导 王海亮大佬
|