初学者
10 分钟

基于SIM808和PIC32MZ2048EFM100的全方位定位和连接解决方案

GPS和GSM/GPRS结合:终极跟踪解决方案

GSM-GPS Click with Curiosity PIC32 MZ EF

已发布 6月 26, 2024

点击板

GSM-GPS Click

开发板

Curiosity PIC32 MZ EF

编译器

NECTO Studio

微控制器单元

PIC32MZ2048EFM100

着眼于简化连接和位置跟踪,我们旨在为各种企业提供一种单一的集成设备,该设备提供实时GPS数据以及可靠的GSM/GPRS通信功能,从而增强其实力。

A

A

硬件概览

它是如何工作的?

GSM-GPS Click基于SIMCom的SIM808模块,是一款支持GSM/GPRS和GPS的高度集成模块。SIM808是一款高度集成的模块,支持0710 MUX协议、TCP/UDP协议、FTP/HTTP、MMS、POP3/SMTP、DTMF、干扰检测、音频记录、SSL、可选的蓝牙3.0、TTS CN,并具有嵌入式AT。对于与外界的连接,该Click板具有两个SMA连接器,用于GSM/GPRS和GPS收音机,MIKROE公司提供了相应的天线,以提高范围和接收信号强度。此外,GSM-GPS Click板下方有一个微型SIM卡槽和一个微型USB连接器,可与PC连接。SIM808有三种操作模式。正常模式有几个功能:GSM/GPRS休眠、GSM空闲、GSM通话、GPRS待机、GPRS数据和充电。电源关闭模式关闭模块的基带部分,软件不活动,串口不可访问,而RTC保持活动状态。最低功能模式下,模块的射频部分将

不工作,SIM卡将无法访问,或者两者都不工作,但串口仍可访问。AT命令集设置了最后两种操作模式。SIM808的GPRS类12可以实现高达85.6Kbps的数据速率(下行/上行),同时使用CS-4编码方案。它可以通过GSM/GPRS处理短信,支持点对点MO和MT,短信小区广播,文本和PDU模式。还支持PBCCH、PP堆栈和USSD。GPS接收机具有22个跟踪和66个获取通道,跟踪灵敏度为-165dBm。冷启动对-148dBm的敏感度较低。重要的是TTFF,在热启动时小于一秒,但在冷启动时为32秒。水平位置的精度在2.5m CEP以下。还有几个LED用于视觉状态显示。PPS代表每秒脉冲,与接收机时间有关。TXD LED表示网络状态,STA LED表示上电状态。GSM-GPS具有4针标头,用于差分音频输出的接口,并输入连接扬声器和麦克风。SIM808使用UART接口,其中常用的

UART RX和TX引脚作为其默认通信协议与主机MCU进行通信,支持从1200bps到11520bps的波特率。UART接口还配有RTS(请求发送)和CTS(清除发送)引脚。此外,Click板还具有通过mikroBUS™信号访问的其他功能,例如STA引脚上的上电状态(与STA LED上的状态相同)。可以通过拉低PWK引脚(电源键)1秒钟来关闭SIM808的电源。有一个环指示器(RI)用于明显的目的。对于逻辑电平转换,此Click板使用了来自德州仪器的TXB0106,这是一款具有自动方向感知和ESD保护的6位双向电平转换和电压转换器。此Click板可以通过I/O SEL跳线选择3.3V或5V逻辑电压电平进行操作。这样,既可以使用3.3V又可以使用5V的MCU来正确使用通信线路。但是,Click板配备了一个包含易于使用的功能和示例代码的库,可用作进一步开发的参考。

GSM-GPS Click hardware overview image

功能概述

开发板

Curiosity PIC32 MZ EF 开发板是一个完全集成的 32 位开发平台,特点是高性能的 PIC32MZ EF 系列(PIC32MZ2048EFM),该系列具有 2MB Flash、512KB RAM、集成的浮点单元(FPU)、加密加速器和出色的连接选项。它包括一个集成的程序员和调试器,无需额外硬件。用户可以通过 MIKROE 

mikroBUS™ Click™ 适配器板扩展功能,通过 Microchip PHY 女儿板添加以太网连接功能,使用 Microchip 扩展板添加 WiFi 连接能力,并通过 Microchip 音频女儿板添加音频输入和输出功能。这些板完全集成到 PIC32 强大的软件框架 MPLAB Harmony 中,该框架提供了一个灵活且模块化的接口

来应用开发、一套丰富的互操作软件堆栈(TCP-IP、USB)和易于使用的功能。Curiosity PIC32 MZ EF 开发板提供了扩展能力,使其成为连接性、物联网和通用应用中快速原型设计的绝佳选择。

Curiosity PIC32MZ EF double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC32

MCU 内存 (KB)

2048

硅供应商

Microchip

引脚数

100

RAM (字节)

524288

你完善了我!

配件

橡胶天线 GSM/GPRS 直角型是我们广泛系列 GSM Click board™ 的理想伴侣。这款专用天线旨在优化您的无线连接,具有令人印象深刻的特点。其宽频范围涵盖 824-894/1710-1990MHz 或 890-960/1710-1890MHz,可处理各种频段,确保无缝可靠的连接。该天线具有 50 欧姆的阻抗和 2dB 的增益,增强了信号接收和传输。其 70/180MHz 的带宽为各种应用提供了灵活性。垂直极化进一步提升了其性能。这款天线的最大输入功率容量为 50W,即使在苛刻条件下也能保证强大的通信。尺寸紧凑,长度为 50mm,带有 SMA 母头连接器,是您无线通信需求的多功能紧凑解决方案。

GSM-GPS Click accessories image

使用的MCU引脚

mikroBUS™映射器

Module Status
RPB4
AN
Module Power-Up
RA9
RST
UART RTS
RPD4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
UART RI
RPE8
PWM
UART CTS
RF13
INT
UART TX
RPD10
TX
UART RX
RPD15
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

GSM-GPS Click Schematic schematic

一步一步来

项目组装

Curiosity PIC32MZ EF front image hardware assembly

从选择您的开发板和Click板™开始。以Curiosity PIC32 MZ EF作为您的开发板开始。

Curiosity PIC32MZ EF front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
Board mapper by product7 hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Curiosity PIC32 MZ EF MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

实时跟踪您的结果

应用程序输出

1. 应用程序输出 - 在调试模式下,“应用程序输出”窗口支持实时数据监控,直接提供执行结果的可视化。请按照提供的教程正确配置环境,以确保数据正确显示。

2. UART 终端 - 使用UART Terminal通过USB to UART converter监视数据传输,实现Click board™与开发系统之间的直接通信。请根据项目需求配置波特率和其他串行设置,以确保正常运行。有关分步设置说明,请参考提供的教程

3. Plot 输出 - Plot功能提供了一种强大的方式来可视化实时传感器数据,使趋势分析、调试和多个数据点的对比变得更加直观。要正确设置,请按照提供的教程,其中包含使用Plot功能显示Click board™读数的分步示例。在代码中使用Plot功能时,请使用以下函数:plot(insert_graph_name, variable_name);。这是一个通用格式,用户需要将“insert_graph_name”替换为实际图表名称,并将“variable_name”替换为要显示的参数。

软件支持

库描述

这个库包含 GSM-GPS Click 驱动程序的 API。

关键函数:

  • gsmgps_send_cmd_with_parameter - 带参数的发送命令函数

  • gsmgps_send_sms_pdu - 以 PDU 模式发送短信的 GSM-GPS 函数

  • gsmgps_generic_parser - 通用解析函数

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * \file 
 * \brief Gsmgps Click example
 * 
 * # Description
 * This example reads and processes data from GSM-GPS click.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes the driver and powers up the module, then sets default configuration 
 * for connecting the device to network.
 * 
 * ## Application Task  
 * Waits for the device to connect to network, then waits for the GPS position fix. Once it get a fix, 
 * it sends an SMS with GPS info to the selected phone number approximately every 40 seconds.
 * 
 * ## Additional Function
 * - static void gsmgps_clear_app_buf ( void )
 * - static void gsmgps_error_check( err_t error_flag )
 * - static void gsmgps_log_app_buf ( void )
 * - static void gsmgps_check_connection( void )
 * - static err_t gsmgps_rsp_check ( void )
 * - static err_t gsmgps_process ( void )
 * - static void gps_parser_application ( void )
 * 
 * @note 
 * In order for the example to work, user needs to set the phone number to which he wants 
 * to send an SMS, and also will need to set an APN and SMSC (required for PDU mode only) of entered SIM card.
 * Enter valid data for the following macros: SIM_APN, SIM_SMSC and PHONE_NUMBER_TO_MESSAGE.
 * E.g. 
    SIM_APN "vipmobile"
    SIM_SMSC "+381610401"
    PHONE_NUMBER_TO_MESSAGE "+381659999999"
 * 
 * @author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "gsmgps.h"
#include "string.h"

#define APP_OK                              0
#define APP_ERROR_DRIVER                    -1
#define APP_ERROR_OVERFLOW                  -2
#define APP_ERROR_TIMEOUT                   -3

#define RSP_OK                              "OK"
#define RSP_ERROR                           "ERROR"

#define SIM_APN                             ""  // Set valid SIM APN
#define SIM_SMSC                            ""  // Set valid SMS Service Center Address - only in PDU mode
#define PHONE_NUMBER_TO_MESSAGE             "" // Set Phone number to message

#define PROCESS_BUFFER_SIZE                 256

#define WAIT_FOR_CONNECTION                 0
#define CONNECTED_TO_NETWORK                1

static gsmgps_t gsmgps;
static log_t logger;

static char app_buf[ PROCESS_BUFFER_SIZE ]  = { 0 };
static int32_t app_buf_len                  = 0;
static int32_t app_buf_cnt                  = 0;

static uint8_t app_connection_status        = WAIT_FOR_CONNECTION;

static err_t app_error_flag;
static uint8_t gps_parser_flag = 0;
static uint8_t gps_info_message[ 200 ] = { 0 };

/**
 * @brief GSM-GPS clearing application buffer.
 * @details This function clears memory of application buffer and reset its length and counter.
 * @note None.
 */
static void gsmgps_clear_app_buf ( void );

/**
 * @brief GSM-GPS data reading function.
 * @details This function reads data from device and concats data to application buffer.
 * 
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 *
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t gsmgps_process ( void );

/**
 * @brief GSM-GPS check for errors.
 * @details This function checks for different types of errors and logs them on UART.
 * @note None.
 */
static void gsmgps_error_check( err_t error_flag );

/**
 * @brief GSM-GPS logs application buffer.
 * @details This function logs data from application buffer.
 * @note None.
 */
static void gsmgps_log_app_buf ( void );

/**
 * @brief GSM-GPS response check.
 * @details This function checks for response and returns the status of response.
 * 
 * @return application status.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t gsmgps_rsp_check ( void );

/**
 * @brief GSM-GPS check connection.
 * @details This function checks connection to the network and 
 *          logs that status to UART.
 * 
 * @note None.
 */
static void gsmgps_check_connection( void );

/**
 * @brief GPS parser application.
 * @param rsp Response buffer.
 * @details This function logs GPS data on the USB UART and stores data in gps_info_message buffer.
 * 
 * @note None.
 */
static void gps_parser_application ( char *rsp );

// ------------------------------------------------------ APPLICATION FUNCTIONS

void application_init ( void )
{
    log_cfg_t log_cfg;
    gsmgps_cfg_t cfg;

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, "---- Application Init ----" );

    //  Click initialization.

    gsmgps_cfg_setup( &cfg );
    GSMGPS_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    gsmgps_init( &gsmgps, &cfg );

    gsmgps_module_power( &gsmgps, GSMGPS_MODULE_POWER_ON );
    
    // dummy read
    gsmgps_process( );
    gsmgps_clear_app_buf( );
    // AT
    gsmgps_send_cmd( &gsmgps, GSMGPS_CMD_AT );
    app_error_flag = gsmgps_rsp_check( );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // ATI - product information
    gsmgps_send_cmd( &gsmgps, GSMGPS_CMD_ATI );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // CGMR - firmware version
    gsmgps_send_cmd( &gsmgps, GSMGPS_CMD_CGMR );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // COPS - deregister from network
    gsmgps_send_cmd_with_parameter( &gsmgps, GSMGPS_CMD_COPS, "2" );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // CGDCONT - set sim apn
    gsmgps_set_sim_apn( &gsmgps, SIM_APN );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
     
    // CFUN - full funtionality
    gsmgps_send_cmd_with_parameter( &gsmgps, GSMGPS_CMD_CFUN, "1" );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // COPS - automatic mode
    gsmgps_send_cmd_with_parameter( &gsmgps, GSMGPS_CMD_COPS, "0" );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 2000 );
    
    // CREG - network registration status
    gsmgps_send_cmd_with_parameter( &gsmgps, GSMGPS_CMD_CREG, "2" );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // CIMI - request IMSI
    gsmgps_send_cmd( &gsmgps, GSMGPS_CMD_CIMI );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
    
    // CGNSPWR - power ON GPS
    gsmgps_send_cmd_with_parameter( &gsmgps, GSMGPS_CMD_CGNSPWR, "1" );
    app_error_flag = gsmgps_rsp_check(  );
    gsmgps_error_check( app_error_flag );
    Delay_ms( 500 );
    
    app_buf_len = 0;
    app_buf_cnt = 0;
    app_connection_status = WAIT_FOR_CONNECTION;
    log_info( &logger, " Application Task " );
    Delay_ms( 5000 );
}

void application_task ( void )
{
    if ( app_connection_status == WAIT_FOR_CONNECTION )
    {
        // CGATT - request IMSI
        gsmgps_send_cmd_check( &gsmgps, GSMGPS_CMD_CGATT );
        app_error_flag = gsmgps_rsp_check(  );
        gsmgps_error_check( app_error_flag );
        Delay_ms( 500 );
        
        // CREG - network registration status
        gsmgps_send_cmd_check( &gsmgps, GSMGPS_CMD_CREG );
        app_error_flag = gsmgps_rsp_check(  );
        gsmgps_error_check( app_error_flag );
        Delay_ms( 500 );
        
        // CSQ - signal quality
        gsmgps_send_cmd( &gsmgps, GSMGPS_CMD_CSQ );
        app_error_flag = gsmgps_rsp_check(  );
        gsmgps_error_check( app_error_flag );
        Delay_ms( 5000 );
    }
    else
    {
        log_info( &logger, "CONNECTED TO NETWORK" );
        
        // SMS message format - PDU mode
        gsmgps_send_cmd_with_parameter( &gsmgps, GSMGPS_CMD_CMGF, "0" );
        app_error_flag = gsmgps_rsp_check(  );
        gsmgps_error_check( app_error_flag );
        Delay_ms( 3000 );
        
        for( ; ; )
        {   
            // Get GPS info
            gps_parser_flag = 1;
            gsmgps_send_cmd_with_parameter( &gsmgps, GSMGPS_CMD_CGPSINF, "2" );
            app_error_flag = gsmgps_rsp_check(  );
            gsmgps_error_check( app_error_flag );
            Delay_ms( 3000 );
            
            if ( gps_parser_flag == 0 )
            {
                log_printf( &logger, "> Sending message to phone number...\r\n" );
                gsmgps_send_sms_pdu ( &gsmgps, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, gps_info_message );
                app_error_flag = gsmgps_rsp_check(  );
                gsmgps_error_check( app_error_flag );
                Delay_ms( 10000 );
                Delay_ms( 10000 );
                Delay_ms( 10000 );
            }
        }
    }
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}

static void gsmgps_clear_app_buf ( void )
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t gsmgps_process ( void )
{
    err_t return_flag = APP_ERROR_DRIVER;
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };
    
    rx_size = gsmgps_generic_read( &gsmgps, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 )
    { 
        int32_t buf_cnt = 0;
        return_flag = APP_OK;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE )
        {
            gsmgps_clear_app_buf(  );
            return_flag = APP_ERROR_OVERFLOW;
        }
        else
        {
            buf_cnt = app_buf_len;
            app_buf_len += rx_size;
        }

        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ )
        {
            if ( rx_buff[ rx_cnt ] != 0 ) 
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }
        }
    } 

    return return_flag;
}

static err_t gsmgps_rsp_check ( void )
{
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 10000;
    
    err_t error_flag = gsmgps_process(  );
    
    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        return error_flag;
    }
    
    while ( ( strstr( app_buf, RSP_OK ) == 0 ) && ( strstr( app_buf, RSP_ERROR ) == 0 ) )
    {
        error_flag = gsmgps_process(  );
        if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
        {
            return error_flag;
        }
        
        timeout_cnt++;
        if ( timeout_cnt > timeout )
        {
            while ( ( strstr( app_buf, RSP_OK ) == 0 ) && ( strstr( app_buf, RSP_ERROR ) == 0 ) )
            {
                gsmgps_send_cmd( &gsmgps, GSMGPS_CMD_AT );
                gsmgps_process(  );
                Delay_ms( 100 );
            }
            gsmgps_clear_app_buf(  );
            return APP_ERROR_TIMEOUT;
        }
        
        Delay_ms( 1 );
    }
    
    gsmgps_check_connection();
    
    gsmgps_log_app_buf();
    
    return APP_OK;
}

static void gsmgps_error_check( err_t error_flag )
{
    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        switch ( error_flag )
        {
            case -2:
                log_error( &logger, " Overflow!" );
                break;
            case -3:
                log_error( &logger, " Timeout!" );
                break;
            default:
                break;
        }
    }
}

static void gsmgps_log_app_buf ( void )
{
    if ( gps_parser_flag == 1 )
    {
        gps_parser_application( app_buf );
        
    }
    else
    {
        for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
        {
            log_printf( &logger, "%c", app_buf[ buf_cnt ] );
        }
        log_printf( &logger, "\r\n-----------------------------------\r\n" );
    }
    
    gsmgps_clear_app_buf(  );
}

static void gsmgps_check_connection( void )
{
    #define CONNECTED "+CGATT: 1"
    
    if ( strstr( app_buf, CONNECTED ) != 0 )
    {
        app_connection_status = CONNECTED_TO_NETWORK;
    }
}

static void gps_parser_application ( char *rsp )
{
    char element_buf[ 200 ] = { 0 };
    memset( gps_info_message, 0, 200 ); 
    gsmgps_generic_parser( rsp, GSMGPS_NEMA_GPGGA, GSMGPS_GPGGA_LATITUDE, element_buf );
    if ( strcmp( element_buf, "0000.0000" ) != 0 )
    {
        strcpy( gps_info_message, "GSM-GPS click - GPS info\n" );
        strcat( gps_info_message, "Latitude: " );
        strcat( gps_info_message, element_buf );
        log_printf( &logger, "Latitude:  %s\r\n", element_buf );
        gsmgps_generic_parser( rsp, GSMGPS_NEMA_GPGGA, GSMGPS_GPGGA_LONGITUDE, element_buf );
        strcat( gps_info_message, "\nLongitude: " );
        strcat( gps_info_message, element_buf );
        log_printf( &logger, "Longitude:  %s\r\n", element_buf );
        memset( element_buf, 0, sizeof( element_buf ) );
        gsmgps_generic_parser( rsp, GSMGPS_NEMA_GPGGA, GSMGPS_GPGGA_ALTITUDE, element_buf );
        strcat( gps_info_message, "\nAltitude: " );
        strcat( gps_info_message, element_buf );
        log_printf( &logger, "Altitude: %s m\r\n", element_buf );  
        gps_parser_flag = 0;
    }
    else
    {
        log_printf( &logger, "Waiting for the position fix..." );
    }
    log_printf( &logger, "\r\n-----------------------------------\r\n" );
}

// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。

想查看更多

类似项目