//******************************************************************************
// www.gilicon.cn
//
// Copyright © 2020, NANCHANG Co.,Ltd.
// All rights reserved.
//
// FileName : NB_BC28.c
// Date : 2020-01-07
// Version : V0001
// 历史记录 : 1.第一次创建
//
// 说明:
// NB_IOT与平台相关的配置变量定义
//******************************************************************************
#include “NB_BC28.h”
#include “NB_Board.h”
#include “systick.h”
#include “lcd12864.h”
#include “Iwdg.h”
//#define REMOTE_SERVER_IP “183.230.40.39”
//#define REMOTE_DEVICE_ID “533756583”
//#define REMOTE_SERVER_PASS “252854”
//#define REMOTE_MQTT_INFO “NBIOTGILICON”
//#define REMOTE_MQTT_VER “version”
//#define REMOTE_MQTT_SUB_TOPIC “/gilicon/device/001”
//#define REMOTE_MQTT_PUB_TOPIC “/gilicon/device/002”
#define REMOTE_SERVER_IP “183.230.40.39”
//#define REMOTE_DEVICE_ID “581594300”
//#define REMOTE_DEVICE_ID “581594301”
//#define REMOTE_DEVICE_ID “581594302”
//#define REMOTE_DEVICE_ID “581594303”
//#define REMOTE_DEVICE_ID “581594304”
#define REMOTE_DEVICE_ID “581594306”
//char MAC_DEVICE_ID[12] = “000000000004”;
//char MAC_DEVICE_ID[12] = “202603530001”;
//char MAC_DEVICE_ID[12] = “202607910010”;
char MAC_DEVICE_ID[12] = “202607980007”;
#define REMOTE_SERVER_PASS “252854”
//#define REMOTE_MQTT_INFO “GILICONDEV001”
//#define REMOTE_MQTT_INFO “GILICONDEV002”
//#define REMOTE_MQTT_INFO “GILICONDEV003”
//#define REMOTE_MQTT_INFO “GILICONDEV004”
//#define REMOTE_MQTT_INFO “GILICONDEV005”
#define REMOTE_MQTT_INFO “GILICONDEV007”
#define REMOTE_MQTT_VER “version”
#define REMOTE_MQTT_SUB_TOPIC “”/gilicon/device/001""
#define REMOTE_MQTT_PUB_TOPIC “/gilicon/device/002”
bool NB_Connect_Err = false;
NETWORK_DEVICE_INFO NBIOT_MODULE_INFO ;
bool NB_OrderInfo(void);
bool NBIOT_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime );
//创建TCP Socket===>成功返回1,失败返回0
u8 NB_Create_TCP(char *server_ip, int port)
{
char buffer[64] = {0};
bool status = true ;
sprintf(buffer, "AT+QMTOPEN=0,%s,%d", server_ip, port);
//连接MQ
status = NBIOT_Cmd(buffer, "OK", NULL, 1500);
//连接ONENET MQTT服务器
if(true == status)
{
// fnWDT_Restart();
sprintf(buffer, “AT+QMTCONN=0,%s,%s,%s”, REMOTE_DEVICE_ID, REMOTE_SERVER_PASS,REMOTE_MQTT_INFO);
status = NBIOT_Cmd(buffer, “OK”, NULL, 3000);
if(true == status)
return 1 ;
else
return 0 ;
}
return 0 ;
}
//订阅信息
bool NB_OrderInfo(void)
{
bool status = true ;
char data_buffer[64] = {0};
sprintf(data_buffer, “AT+QMTSUB=0,1,%s,0”, REMOTE_MQTT_SUB_TOPIC);
status = NBIOT_Cmd(data_buffer, “+QMTSUB”, NULL, 3000);
return status ;
}
//NBIOT基础配置
bool BASE_Config_NBIOT(void)
{
char buffer[50] = {0};
bool status = true ;
//按照获取的产品、设备信息,配置阿里云设备信息。AT+QMTCFG=“version”,0,4
sprintf(buffer, “AT+QMTCFG=%s,0,4”, REMOTE_MQTT_VER);
status = NBIOT_Cmd(buffer, “OK”, NULL, 500);
return status ;
}
//关闭Socket连接
bool Close_Socket(void)
{
bool status = true ;
status = NBIOT_Cmd(“AT+QMTCLOSE=0”, “OK”, NULL, 2000);
return status ;
}
/*
函数名:NBIOT_Cmd
描述 :对NBIOT模块发送AT指令
输入 :cmd,待发送的指令
reply1,reply2,期待的响应,为NULL表不需响应,两者为或逻辑关系
waittime,等待响应的时间
返回 : 1,指令发送成功
0,指令发送失败
调用 :被外部调用
*/
bool NBIOT_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime )
{
bool status = false ;
USART1_RX_STA = 0 ;//从新开始接收新的数据包
// memset(NB_RXBuffer, 0, MAX_COMPACK_SIZE); //清空接收缓冲
u1_printf(“%s\r\n”, cmd);
if ( ( reply1 == 0 ) && ( reply2 == 0 ) ) //不需要接收数据
return true;
delay_ms ( waittime ); //延时
// NB_RXBuffer[NB_FramLength] = ‘\0’;
if(USART1_RX_STA&0X8000)//接收到期待的应答结果
{
NB_RXBuffer[USART1_RX_STA&0X7FFF]=0;//添加结束符
// printf(“RXBuffer:%s\n”, NB_RXBuffer);
USART1_RX_STA = 0;
if(strstr((char *)NB_RXBuffer, reply1) != NULL)
{
// printf(“ackerror:%s\n”, reply1);
return true ;
}
if (strstr((char *)NB_RXBuffer, reply2) != NULL)
{
// printf(“acksuccess:%s\n”, NB_RXBuffer);
//如果匹配到断连标志,就重新建立连接
NB_Connect_Err = true;
// BASE_Config_NBIOT();
status = NB_Create_TCP(REMOTE_SERVER_IP, 6002);
// fnWDT_Restart();
if (false == status)
{
// fnWDT_Restart();
Close_Socket();
NB_Create_TCP(REMOTE_SERVER_IP, 6002);
NB_Connect_Err = false;
}
NB_OrderInfo();
u1_printf(“%s\r\n”, cmd); //重新发一次数据
return true ;
}
}
return false ;
}
void NB_TCP_Connect(void)
{
bool status = false ;
NB_Connect_Err = true;
status = NB_Create_TCP(REMOTE_SERVER_IP, 6002);
if (false == status)
{
Close_Socket();
NB_Create_TCP(REMOTE_SERVER_IP, 6002);
NB_Connect_Err = false;
}
NB_OrderInfo();
}
//NBIOT发送数据到后台服务器
/*
Data_Length:要发送到后台的原始数据的长度
hex_data:要发送到后台的,但必须将原始数据转换成十六进制编码的数据
*/
bool NB_Send_Data_To_Server(uint8_t *send_data)
{
bool status = true;
char data_buffer[NB_MAX_SEND_LEN] = {0};
// printf(“数据上传:\n”);
// printf(“%s\n”, data_buffer);
// NB_OrderInfo();
sprintf(data_buffer, “AT+QMTPUBEX=0,0,0,0,%s,%s”, REMOTE_MQTT_PUB_TOPIC, send_data);
status = NBIOT_Cmd(data_buffer, “OK”,“ERROR”, 1000);
// if(status == true)
// {
// u1_printf(“%s\r\n”, data_buffer);
// USART_SendData(USART1,0x1A);
// }
return status ;
}
//bool NB_Send_Data_To_Server(uint8_t *send_data)
//{
// bool status = true;
// char data_buffer[128] = {0};
printf(“数据上传:\n”);
printf(“%s\n”, data_buffer);
//
// sprintf(data_buffer, “AT+QMTPUB=0,0,0,1,%s”, REMOTE_MQTT_PUB_TOPIC);
// status = NBIOT_Cmd(data_buffer, “<”,NULL, 500);
// if(status == true)
// {
// u1_printf(“%s\r\n”, data_buffer);
// USART_SendData(USART1,0x1A);
// }
//
// return status ;
//}
//获取信号强度
int Get_CSQ(void)
{
int signal_value = -1;
bool status = true ;
char csq_buffer[50] = {0};
status = NBIOT_Cmd(“AT+CSQ”, “+CSQ”, NULL, 500);
if(status == true)
{
memcpy(csq_buffer, NB_RXBuffer + 7, 2);
signal_value = atoi(csq_buffer);
return signal_value ;
}
return 99 ;
}
//获取入网状态
bool Get_Enter_Net_Status(void)
{
int status = 0 ;
bool cmd_status = false ;
char buffer[30] = {0};
cmd_status = NBIOT_Cmd(“AT+CGATT?”, “OK”, NULL, 2000);
// cmd_status = NBIOT_Cmd(“AT+CGATT?”, “OK”, NULL, 60000);
memcpy(buffer, NB_RXBuffer + 9, 1);
status = atoi(buffer);
if(1 == status)
return true;
else
return false ;
}
//检查模组是否在线
bool checkNBIOT(void)
{
return NBIOT_Cmd(“AT”, “OK”, NULL, 500);
}
bool NBIOT_ClosePSM(void)
{
return NBIOT_Cmd(“AT+CPSMS=0”, “OK”, NULL, 500);
}
bool NBIOT_CloseDRX(void)
{
return NBIOT_Cmd(“AT+CEDRXS=0,5”, “OK”, NULL, 500);
}
int nb_reconnect_cnt = 0;
/**
功能:初始化NBIOT
参数:None
返回值:初始化结果,非0为初始化成功,0为失败
*/
int initNBIOT(void)
{
bool status_return = false ;
while(1)
{
switch(NBIOT_MODULE_INFO.Init_NB_Status)
{
//1、检查NBIOT模块是否在线
case 0:
status_return = checkNBIOT();
if(false == status_return)
{
// printf(“NB模块硬件故障…\n”);
display_string_8x16(5,16,“NB ERROR”);
NBIOT_MODULE_INFO.Init_NB_Status = 0 ;
nb_reconnect_cnt++;
if (nb_reconnect_cnt > 5)
{
nb_reconnect_cnt = 0;
NBIOT_MODULE_INFO.Init_NB_Status = 7 ;
}
}
else
{
// printf(“NB模块正常连接\n”);
display_string_8x16(5,16,“NB ONLINE”);
NBIOT_MODULE_INFO.Init_NB_Status = 1 ;
nb_reconnect_cnt = 0;
}
delay_ms ( 2000 );
break ;
case 1:
status_return = Get_Enter_Net_Status();
if(false == status_return)
{
// printf(“入网失败,请重新复位模块\n”);
display_string_8x16(5,16,“NET ERROR”);
NBIOT_MODULE_INFO.Init_NB_Status = 1 ;
nb_reconnect_cnt++;
if (nb_reconnect_cnt > 5)
{
nb_reconnect_cnt = 0;
NBIOT_MODULE_INFO.Init_NB_Status = 2 ;
}
break ;
}
else
{
// printf(“模组入网成功…\n”);
display_string_8x16(5,16,“NET OK”);
NBIOT_MODULE_INFO.Init_NB_Status = 2 ;
nb_reconnect_cnt = 0;
}
break ;
case 2:
NBIOT_MODULE_INFO.signalCSQ = Get_CSQ();
if(99 == NBIOT_MODULE_INFO.signalCSQ)
{
// printf(“设备无信号…\n”);
display_string_8x16(5,16,“NB CSQ LOW”);
NBIOT_MODULE_INFO.Init_NB_Status = 2 ;
nb_reconnect_cnt++;
if (nb_reconnect_cnt > 5)
{
nb_reconnect_cnt = 0;
NBIOT_MODULE_INFO.Init_NB_Status = 7 ;
}
}
else
{
// printf(“信号级别:%d\n”, NBIOT_MODULE_INFO.signalCSQ);
display_string_8x16(5,16,“NB CSQ OK”);
NBIOT_MODULE_INFO.Init_NB_Status = 3 ;
nb_reconnect_cnt = 0;
}
break ;
case 3:
//按照获取的产品、设备信息,配置阿里云设备信息。AT+QMTCFG="version",0,4
status_return = BASE_Config_NBIOT();
if(false == status_return)
{
NBIOT_MODULE_INFO.Init_NB_Status = 3 ;
nb_reconnect_cnt++;
if (nb_reconnect_cnt > 5)
{
nb_reconnect_cnt = 0;
NBIOT_MODULE_INFO.Init_NB_Status = 7 ;
}
}
else
{
NBIOT_MODULE_INFO.Init_NB_Status = 4 ;
nb_reconnect_cnt = 0;
}
delay_ms ( 1000 );
break ;
case 4:
Close_Socket();
status_return = NB_Create_TCP(REMOTE_SERVER_IP, 6002);
if(false == status_return)
{
// printf(“重新创建TCP连接失败\n”);
NBIOT_MODULE_INFO.Init_NB_Status = 5 ;
display_string_8x16(5,16,“NB CONNECT ERR”);
break ;
}
else
{
// printf(“重新创建TCP连接成功\n”);
NBIOT_MODULE_INFO.Init_NB_Status = 5 ;
display_string_8x16(5,16,“NB CONNECT OK”);
}
delay_ms ( 2000 );
break ;
case 5:
status_return = NB_OrderInfo();
if(false == status_return)
{
NBIOT_MODULE_INFO.Init_NB_Status = 7 ;
}
else
{
NBIOT_MODULE_INFO.Init_NB_Status = 7 ;
}
delay_ms ( 1000 );
break ;
default:
break ;
}
if(7 == NBIOT_MODULE_INFO.Init_NB_Status)
{
NBIOT_ClosePSM();
NBIOT_CloseDRX();
break ;
}
}
return 1;
}
char* nb_itoa(int num, char* str, int radix)
{
/索引表/
char temp;
char index[] = “0123456789ABCDEF”;
unsigned unum;/中间变量/
int i = 0, j, k;
/*确定unum的值*/
if(radix == 10 && num < 0) /*十进制负数*/
{
unum = (unsigned) - num;
str[i++] = '-';
}
else unum = (unsigned)num; /*其他情况*/
/*转换*/
do
{
str[i++] = index[unum % (unsigned)radix];
unum /= radix;
}
while(unum);
str[i] = '\0';
/*逆序*/
if(str[0] == '-')
k = 1; /*十进制负数*/
else
k = 0;
for(j = k; j <= (i - 1) / 2; j++)
{
temp = str[j];
str[j] = str[i - 1 + k - j];
str[i - 1 + k - j] = temp;
}
return str;
}
int nb_Str2Hex(char *str, char *hex)
{
int high = 0;
int low = 0;
int temp = 0;
if (NULL == str || NULL == hex)
{
return -1;
}
if (0 == strlen(str))
{
return -2;
}
while(*str)
{
temp = (int)(*str);
high = temp >> 4;
low = temp & 15;
nb_itoa(high, hex, 16);
hex++;
nb_itoa(low, hex, 16);
hex++;
str++;
}
*hex = '\0';
return 0;
}