初学者
10 分钟

通过SIM800H-BT和ATmega328P揭示可靠通信的潜力

始终触手可及

GSM3 Click with Arduino UNO Rev3

已发布 6月 24, 2024

点击板

GSM3 Click

开发板

Arduino UNO Rev3

编译器

NECTO Studio

微控制器单元

ATmega328P

通过拥抱GSM网络的优势,我们赋予您自信和可靠的沟通能力,塑造了一个充满无限可能性的未来。

A

A

硬件概览

它是如何工作的?

GSM3 Click 基于 SIMCom 的 SIM800H,这是一款 GSM/GPRS 模块,符合 GSM 第 2/2+ 阶段标准,具备完整的蜂窝网络通信选项。它具有网络状态指示、干扰检测以及包括 TCP/IP、UDP、FTP、PPP、HTTP、电子邮件、MMS 等在内的嵌入式互联网协议。此外,它还具备先进的语音/音频功能,包括 FM 收音机接口。GPRS 多时隙等级 12 实现允许四个上行和四个下行时隙,开放五个时隙。数据通信速度在上行和下行连接中均为 85.6 kbps。此模块的一个突出特点是支持蓝牙 3.0+ EDR 协议。SIM800H 覆盖的频率为 850/900 MHz(2W 的 TX 功率)和 1800/1900 MHz(1W 的 TX 功率)。模块由几个内部模块组成,如天线切换部分、RF 收发部分(BT、FM 和 GSM)、内存、电源管理、模拟部分(音频、ADC)以及最重要的 - 蜂窝基带处理器。其接口由几条报告设备和网络状态的线路、SIM 卡接口线路、UART 接口线路和设备控制线路组成,这些线路被路由到 Click board™ 的相应元件。SIM800H 通过板载 LDO 以大约 4V 的电压供电,通过 5V 的  mikroBUS™ 电源轨,不论选择的 DATA LEVEL 如何。SIM800H 的数字部分内部供电为 2.8V,因此需要对连接宿主 MCU 的通信总线线路进行条件处理。为此,GSM3 配备了来自 Texas Instruments 的 

TXB0106,这是一个 6 位双向电平转换和电压转换 器,具有自动方向感应功能。与宿主 MCU 的通信,GSM3 Click 使用 UART 接口,支持从 1200bps 到 115.2kbps 的标准波特率,支持最高 57.6kbps 的自动波特率检测,并默认设置。波特率设置存储在内部非易失性存储器中,因此一旦存储,这些设置将在电源周期间保持不变。除 UART 接口外,GSM3 Click 还包括硬件流控制引脚 RTS 和 CTS。一个标记为  NET 的红色 LED 指示网络状态(网络搜索/未注册、已注册到网络、和通信建立)。一个标记为 STAT 的黄色 LED 用于指示设备状态。当它亮起时,设备处于运行状态。它还表示内部模块初始化完成,模块已准备好工作。除了 LED,此状态还通过 STA 引脚路由到宿主 MCU。PWRKEY 引脚路由到 mikroBUS™ 的 PWK 引脚,用于手动开启/关闭 Click board™。将此引脚拉低至低逻辑电平至少 1 秒将切换 SIM800H 模块的电源状态。为了正确地从网络断开并在非易失性存储器中存储工作参数,应使用 PWK(PWRKEY)引脚或通过发出 AT+CPOWD=1 命令安全地关闭模块。突然中断电源可能会导致当前参数设置丢失和网络断开不当。如果这些方法不起作用,RST 引脚在低逻辑状态下也可以重置模块。该 Click board™ 的突出特点之一是通过来自 Antenova 的 2.4GHz RUFA 

SMD 天线支持蓝牙 3.0+ EDR 协议。对于  GSM/GPRS 通信,GSM3 Click board 配备了 SMA 连接器,而适当的天线可在 MIKROE 商店购买。Click board™ 背面的 Micro SIM 卡插槽用于安装微型 SIM 卡。没有有效的 SIM 卡,设备将无法使用,这使其能够连接到蜂窝网络。支持 1.8V 和 3V 两种  SIM 卡类型,通信速度快达 64kbps(GSM Phase 2+)。SIM800H 模块还提供了丰富的音频功能,包括半速率、全速率、增强型全速率、自适应多速率语音编解码器、优越的回声消除,并且可以通过 AT 命令进行配置。音频 DSP 部分集成在模块中,仅需要一些外部组件。耳机可以通过 Click board™ 侧面的连接垫片连接。该垫片还提供 FM 天线连接,可用于收听 FM 广播。可以使用带有集成 FM 天线的耳机。通过 AT 命令可以调整 FM 收音机接收器的功能。来电会中断 FM 接收器的信号,并将其重定向到耳机。该 Click board™ 可通过 DATA LEVEL 跳线选择 3.3V 或 5V 逻辑电压电平进行操作。这样,既支持 3.3V 又支持 5V 的 MCU 可以正确使用通信线路。此外,该 Click board™ 配备了包含易于使用的函数和示例代码的库,可供进一步开发时参考。

GSM3 Click hardware overview image

功能概述

开发板

Arduino UNO 是围绕 ATmega328P 芯片构建的多功能微控制器板。它为各种项目提供了广泛的连接选项,具有 14 个数字输入/输出引脚,其中六个支持 PWM 输出,以及六个模拟输入。其核心组件包括一个 16MHz 的陶瓷谐振器、一个 USB 连接器、一个电

源插孔、一个 ICSP 头和一个复位按钮,提供了为板 子供电和编程所需的一切。UNO 可以通过 USB 连接到计算机,也可以通过 AC-to-DC 适配器或电池供电。作为第一个 USB Arduino 板,它成为 Arduino 平台的基准,"Uno" 符号化其作为系列首款产品的地

位。这个名称选择,意为意大利语中的 "一",是为了 纪念 Arduino Software(IDE)1.0 的推出。最初与 Arduino Software(IDE)版本1.0 同时推出,Uno 自此成为后续 Arduino 发布的基础模型,体现了该平台的演进。

Arduino UNO Rev3 double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

AVR

MCU 内存 (KB)

32

硅供应商

Microchip

引脚数

28

RAM (字节)

2048

你完善了我!

配件

Click Shield for Arduino UNO 具有两个专有的 mikroBUS™ 插座,使所有 Click board™ 设备能够轻松与 Arduino UNO 板进行接口连接。Arduino UNO 是一款基于 ATmega328P 的微控制器开发板,为用户提供了一种经济实惠且灵活的方式来测试新概念并构建基于 ATmega328P 微控制器的原型系统,结合了性能、功耗和功能的多种配置选择。Arduino UNO 具有 14 个数字输入/输出引脚(其中 6 个可用作 PWM 输出)、6 个模拟输入、16 MHz 陶瓷谐振器(CSTCE16M0V53-R0)、USB 接口、电源插座、ICSP 头和复位按钮。大多数 ATmega328P 微控制器的引脚都连接到开发板左右两侧的 IO 引脚,然后再连接到两个 mikroBUS™ 插座。这款 Click Shield 还配备了多个开关,可执行各种功能,例如选择 mikroBUS™ 插座上模拟信号的逻辑电平,以及选择 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换电压转换器使用任何 Click board™,无论 Click board™ 运行在 3.3V 还是 5V 逻辑电压电平。一旦将 Arduino UNO 板与 Click Shield for Arduino UNO 连接,用户即可访问数百种 Click board™,并兼容 3.3V 或 5V 逻辑电压电平的设备。

Click Shield for Arduino UNO accessories 1 image

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

GSM3 Click accessories image

使用的MCU引脚

mikroBUS™映射器

Status Indicator
PC0
AN
Reset
PD2
RST
UART RTS
PB2
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Power ON/OFF
PD6
PWM
UART CTS
PC3
INT
UART TX
PD0
TX
UART RX
PD1
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

GSM3 Click Schematic schematic

一步一步来

项目组装

Click Shield for Arduino UNO front image hardware assembly

从选择您的开发板和Click板™开始。以Arduino UNO Rev3作为您的开发板开始。

Click Shield for Arduino UNO front image hardware assembly
Arduino UNO Rev3 front image hardware assembly
Charger 27 Click front image hardware assembly
Prog-cut hardware assembly
Charger 27 Click complete accessories setup image hardware assembly
Arduino UNO Rev3 Access MB 1 - upright/background 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
Arduino UNO 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”替换为要显示的参数。

软件支持

库描述

这个库包含 GSM3 Click 驱动的 API。

关键函数:

  • gsm3_set_sim_apn - 设置 SIM 卡的 APN。

  • gsm3_send_sms_text - 向手机号发送文本消息。

  • gsm3_send_sms_pdu - 以 PDU 模式向手机号发送文本消息。

开源

代码示例

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

/*!
 * @file main.c
 * @brief GSM 3 Click Example.
 *
 * # Description
 * Application example shows device capability of connecting to the network and
 * sending SMS or TCP/UDP messages using standard "AT" commands.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver, tests the communication by sending "AT" command, and after that restarts the device.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - GSM3_CONFIGURE_FOR_NETWORK:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - GSM3_WAIT_FOR_CONNECTION:
 * Waits for the network registration indicated via CREG URC event and then checks
 * the connection status.
 *
 *  - GSM3_CONFIGURE_FOR_EXAMPLE:
 * Sets the device configuration for sending SMS or TCP/UDP messages depending on the selected demo example.
 *
 *  - GSM3_EXAMPLE:
 * Depending on the selected demo example, it sends an SMS message (in PDU or TXT mode) or TCP/UDP message.
 *
 * By default, the TCP/UDP example is selected.
 *
 * ## Additional Function
 * - static void gsm3_clear_app_buf ( void )
 * - static err_t gsm3_process ( void )
 * - static void gsm3_error_check( err_t error_flag )
 * - static void gsm3_log_app_buf ( void )
 * - static err_t gsm3_rsp_check ( uint8_t *rsp )
 * - static err_t gsm3_configure_for_connection( void )
 * - static err_t gsm3_check_connection( void )
 * - static err_t gsm3_configure_for_messages( void )
 * - static err_t gsm3_send_message( void )
 *
 * @note
 * In order for the examples to work, user needs to set the APN and SMSC (SMS PDU mode only)
 * of entered SIM card as well as the phone number (SMS mode only) to which he wants to send an SMS.
 * Enter valid values for the following macros: SIM_APN, SIM_SMSC and PHONE_NUMBER_TO_MESSAGE.
 * Example:
    SIM_APN "internet"
    SIM_SMSC "+381610401"
    PHONE_NUMBER_TO_MESSAGE "+381659999999"
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "gsm3.h"
#include "conversions.h"

// Example selection macros
#define EXAMPLE_TCP_UDP                     0               // Example of sending messages to a TCP/UDP echo server
#define EXAMPLE_SMS                         1               // Example of sending SMS to a phone number
#define DEMO_EXAMPLE                        EXAMPLE_TCP_UDP // Example selection macro

// SIM APN config
#define SIM_APN                             "internet"      // Set valid SIM APN

// SMS example parameters
#define SIM_SMSC                            ""              // Set valid SMS Service Center Address - only in SMS PDU mode
#define PHONE_NUMBER_TO_MESSAGE             ""              // Set Phone number to message
#define SMS_MODE                            "1"             // SMS mode: "0" - PDU, "1" - TXT

// TCP/UDP example parameters
#define REMOTE_IP                           "77.46.162.162" // TCP/UDP echo server IP address
#define REMOTE_PORT                         "51111"         // TCP/UDP echo server port

// Message content
#define MESSAGE_CONTENT                     "GSM 3 Click board - demo example."

// Application buffer size
#define APP_BUFFER_SIZE                     256
#define PROCESS_BUFFER_SIZE                 256

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    GSM3_CONFIGURE_FOR_NETWORK = 1,
    GSM3_WAIT_FOR_CONNECTION,
    GSM3_CONFIGURE_FOR_EXAMPLE,
    GSM3_EXAMPLE

} gsm3_example_state_t;

static gsm3_t gsm3;
static log_t logger;

/**
 * @brief Application example variables.
 * @details Variables used in application example.
 */
static uint8_t app_buf[ APP_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static err_t error_flag;
static gsm3_example_state_t example_state;

/**
 * @brief Clearing application buffer.
 * @details This function clears memory of application
 * buffer and reset its length and counter.
 */
static void gsm3_clear_app_buf ( void );

/**
 * @brief Data reading function.
 * @details This function reads data from device and
 * appends it to the application buffer.
 * @return @li @c  0 - Some data is read.
 *         @li @c -1 - Nothing is read.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm3_process ( void );

/**
 * @brief Check for errors.
 * @details This function checks for different types of
 * errors and logs them on UART or logs the response if no errors occured.
 * @param[in] error_flag  Error flag to check.
 */
static void gsm3_error_check ( err_t error_flag );

/**
 * @brief Logs application buffer.
 * @details This function logs data from application buffer.
 */
static void gsm3_log_app_buf ( void );

/**
 * @brief Response check.
 * @details This function checks for response and
 * returns the status of response.
 * @param[in] rsp  Expected response.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm3_rsp_check ( uint8_t *rsp );

/**
 * @brief Configure device for connection to the network.
 * @details Sends commands to configure and enable
 * connection to the specified network.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm3_configure_for_network ( void );

/**
 * @brief Wait for connection signal.
 * @details Wait for connection signal from CREG URC.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm3_check_connection ( void );

/**
 * @brief Configure device for example.
 * @details Configure device for the specified example.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm3_configure_for_example ( void );

/**
 * @brief Execute example.
 * @details This function executes SMS or TCP/UDP example depending on the DEMO_EXAMPLE macro.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t gsm3_example ( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    gsm3_cfg_t gsm3_cfg;  /**< Click config object. */

    /**
     * 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.
    gsm3_cfg_setup( &gsm3_cfg );
    GSM3_MAP_MIKROBUS( gsm3_cfg, MIKROBUS_1 );
    if ( UART_ERROR == gsm3_init( &gsm3, &gsm3_cfg ) )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );
        for ( ; ; );
    }
    
    gsm3_process( );
    gsm3_clear_app_buf( );

    // Check communication
    gsm3_send_cmd( &gsm3, GSM3_CMD_AT );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    gsm3_error_check( error_flag );
    
    // Restart device
    #define RESTART_DEVICE "1,1"
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CFUN, RESTART_DEVICE );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    gsm3_error_check( error_flag );
    
    log_info( &logger, " Application Task " );
    example_state = GSM3_CONFIGURE_FOR_NETWORK;
}

void application_task ( void )
{
    switch ( example_state )
    {
        case GSM3_CONFIGURE_FOR_NETWORK:
        {
            if ( GSM3_OK == gsm3_configure_for_network( ) )
            {
                example_state = GSM3_WAIT_FOR_CONNECTION;
            }
            break;
        }
        case GSM3_WAIT_FOR_CONNECTION:
        {
            if ( GSM3_OK == gsm3_check_connection( ) )
            {
                example_state = GSM3_CONFIGURE_FOR_EXAMPLE;
            }
            break;
        }
        case GSM3_CONFIGURE_FOR_EXAMPLE:
        {
            if ( GSM3_OK == gsm3_configure_for_example( ) )
            {
                example_state = GSM3_EXAMPLE;
            }
            break;
        }
        case GSM3_EXAMPLE:
        {
            gsm3_example( );
            break;
        }
        default:
        {
            log_error( &logger, " Example state." );
            break;
        }
    }
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

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

static err_t gsm3_process ( void )
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t rx_size = 0;
    rx_size = gsm3_generic_read( &gsm3, rx_buf, PROCESS_BUFFER_SIZE );
    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = app_buf_len;
        if ( ( ( app_buf_len + rx_size ) > APP_BUFFER_SIZE ) && ( app_buf_len > 0 ) ) 
        {
            buf_cnt = APP_BUFFER_SIZE - ( ( app_buf_len + rx_size ) - APP_BUFFER_SIZE );
            memmove ( app_buf, &app_buf[ APP_BUFFER_SIZE - buf_cnt ], buf_cnt );
        }
        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buf[ rx_cnt ] ) 
            {
                app_buf[ buf_cnt++ ] = rx_buf[ rx_cnt ];
                if ( app_buf_len < APP_BUFFER_SIZE )
                {
                    app_buf_len++;
                }
            }
        }
        return GSM3_OK;
    }
    return GSM3_ERROR;
}

static err_t gsm3_rsp_check ( uint8_t *rsp )
{
    uint32_t timeout_cnt = 0;
    uint32_t timeout = 120000;
    gsm3_clear_app_buf( );
    gsm3_process( );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, GSM3_RSP_ERROR ) ) )
    {
        gsm3_process( );
        if ( timeout_cnt++ > timeout )
        {
            gsm3_clear_app_buf( );
            return GSM3_ERROR_TIMEOUT;
        }
        Delay_ms ( 1 );
    }
    Delay_ms ( 100 );
    gsm3_process( );
    if ( strstr( app_buf, rsp ) )
    {
        return GSM3_OK;
    }
    else if ( strstr( app_buf, GSM3_RSP_ERROR ) )
    {
        return GSM3_ERROR_CMD;
    }
    else
    {
        return GSM3_ERROR_UNKNOWN;
    }
}

static void gsm3_error_check ( err_t error_flag )
{
    switch ( error_flag )
    {
        case GSM3_OK:
        {
            gsm3_log_app_buf( );
            break;
        }
        case GSM3_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case GSM3_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        case GSM3_ERROR_CMD:
        {
            log_error( &logger, " CMD!" );
            break;
        }
        case GSM3_ERROR_UNKNOWN:
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    Delay_ms ( 500 );
}

static void gsm3_log_app_buf ( void )
{
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }
}

static err_t gsm3_configure_for_network ( void )
{
    err_t func_error = GSM3_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    // Deregister from network
    #define DEREGISTER_FROM_NETWORK "2"
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_COPS, DEREGISTER_FROM_NETWORK );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    
    // Set SIM APN
    gsm3_set_sim_apn( &gsm3, SIM_APN );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );

    // Enable full functionality
    #define FULL_FUNCTIONALITY "1"
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CFUN, FULL_FUNCTIONALITY );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );

    // Enable network registartion
    #define ENABLE_REG "2"
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CREG, ENABLE_REG );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    
    // Automatic registration
    #define AUTOMATIC_REGISTRATION "0"
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_COPS, AUTOMATIC_REGISTRATION );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
#endif
    return func_error;
}

static err_t gsm3_check_connection ( void )
{
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    #define CONNECTED "+CREG: 2,1"
    gsm3_send_cmd_check ( &gsm3, GSM3_CMD_CREG );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    gsm3_error_check( error_flag );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 100 );
        // Check signal quality
        gsm3_send_cmd( &gsm3, GSM3_CMD_CSQ );
        error_flag = gsm3_rsp_check( GSM3_RSP_OK );
        gsm3_error_check( error_flag );
        #define NO_SIGNAL "99,99"
        if ( !strstr( app_buf, NO_SIGNAL ) )
        {
            Delay_ms ( 1000 );
            return error_flag;
        }
    }
    Delay_ms ( 1000 );
    return GSM3_ERROR;
#endif
    return GSM3_OK;
}

static err_t gsm3_configure_for_example ( void )
{
    err_t func_error = GSM3_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    #define ENABLE_RESPONSE_HEADER "1"
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CIPHEAD, ENABLE_RESPONSE_HEADER );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CMGF, SMS_MODE );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

static err_t gsm3_example ( void )
{
    err_t func_error = GSM3_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 100 ] = { 0 };

    // Open TCP socket.
    #define RESPONSE_CONNECT "CONNECT OK"
    #define TCP_SERVICE_TYPE "\"TCP\","
    strcpy( cmd_buf, TCP_SERVICE_TYPE );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_PORT );
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CIPSTART, cmd_buf );
    error_flag = gsm3_rsp_check( RESPONSE_CONNECT );
    func_error |= error_flag;
    gsm3_error_check( error_flag );

    // Get message length
    uint8_t message_len_buf[ 10 ] = { 0 };
    uint16_t message_len = strlen( MESSAGE_CONTENT );
    uint16_to_str( message_len, message_len_buf );
    l_trim( message_len_buf );
    r_trim( message_len_buf );

    // Write message to TCP socket
    uint8_t ctrl_z = 0x1A;
    strcpy( cmd_buf, message_len_buf );
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CIPSEND, cmd_buf );
    error_flag = gsm3_rsp_check( ">" );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    gsm3_generic_write ( &gsm3, MESSAGE_CONTENT, message_len );
    gsm3_generic_write ( &gsm3, &ctrl_z, 1 );
    
    // Read response
    #define RESPONSE_URC "+IPD"
    error_flag = gsm3_rsp_check( RESPONSE_URC );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    log_printf( &logger, "\r\n" );
    
    // Close TCP socket
    gsm3_send_cmd( &gsm3, GSM3_CMD_CIPCLOSE );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    
    // Open UDP socket.
    #define UDP_SERVICE_TYPE "\"UDP\","
    strcpy( cmd_buf, UDP_SERVICE_TYPE );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_PORT );
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CIPSTART, cmd_buf );
    error_flag = gsm3_rsp_check( RESPONSE_CONNECT );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    
    // Write message to UDP socket
    strcpy( cmd_buf, message_len_buf );
    gsm3_send_cmd_with_par( &gsm3, GSM3_CMD_CIPSEND, cmd_buf );
    error_flag = gsm3_rsp_check( ">" );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    gsm3_generic_write ( &gsm3, MESSAGE_CONTENT, message_len );
    gsm3_generic_write ( &gsm3, &ctrl_z, 1 );
    
    // Read response
    error_flag = gsm3_rsp_check( RESPONSE_URC );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    log_printf( &logger, "\r\n" );
    
    // Close UDP socket
    gsm3_send_cmd( &gsm3, GSM3_CMD_CIPCLOSE );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    // Check SMS mode
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    gsm3_send_cmd_check( &gsm3, GSM3_CMD_CMGF );
    error_flag = gsm3_rsp_check( GSM3_RSP_OK );
    func_error |= error_flag;
    gsm3_error_check( error_flag );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        // Send SMS in PDU mode
        gsm3_send_sms_pdu( &gsm3, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = gsm3_rsp_check( GSM3_RSP_OK );
        func_error |= error_flag;
        gsm3_error_check( error_flag );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        // Send SMS in TXT mode
        gsm3_send_sms_text ( &gsm3, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = gsm3_rsp_check( GSM3_RSP_OK );
        func_error |= error_flag;
        gsm3_error_check( error_flag );
    }
    // 30 seconds delay
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

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

额外支持

资源

喜欢这个项目吗?

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