踏入一个充满可能性的世界,体验我们的蜂窝网络通信解决方案。我们提供从网络指示到完整的TCP/UDP堆栈以及先进的HTTP/HTTPS传输功能,确保无与伦比的连接性和通信效率。
A
A
硬件概览
它是如何工作的?
3G SARA Click基于来自u-blox的紧凑型3.75G UMTS/HSPA调制解调器SARA U-201。SARA U-201模块可在全球范围内使用。有关模块的完整频率列表和其他相关信息可在SARA U-201数据手册中找到。该设备是点击板的主要组件,由几个内部块或部分组成,如天线切换部分、射频收发器部分、存储器、电源管理和最重要的 - 蜂窝基带处理器。此部分包含管理其他部分所需的逻辑,并提供与主机MCU的接口。此接口由几条线组成,报告天线状态、SIM卡状态、SIM卡接口线、GPIO、UART接口线、USB接口线、I2C总线线、设备控制线和I2S总线线。这些线路被路由到点击板的相应元素。SARA U-201模块必须由干净稳定的电源供电。模块正常工作所需的电压约为4V,通过Microchip的1A低压降输出(LDO)调节器MCP1826从5V mikroBUS™轨道派生。尽管SARA U-201是低功耗设备,但通常情况下,蜂窝网络模块以高功耗著称,因此必须使用1A LDO。SARA U-201的数字部分由内部供电1.8V,因此需要对连接主机MCU与模块的通信总线进行调理。出于这个原因,另一个小型LDO被使用,为来自Texas
Instruments的具有自动方向感测的6位双向电平转换和电压转换器TXB0106的一侧提供所需的参考电压。级联器的另一侧的参考电压来自板载SMD跳线,标记为PWR SEL。此跳线从mikroBUS™中选择3.3V和5V,取决于使用的MCU类型及其逻辑电压水平要求。SARA U-201模块的UART总线连接到级联器的一侧,而另一侧(转换后)连接到相应的mikroBUS™ UART引脚。然而,SARA U-201模块被设计为传统的DCE设备(Data Communication Equipment),提供完整的UART引脚数,包括硬件流控制引脚(CTS、RTS)。这些引脚被路由到mikroBUS™ CS (RTS)和INT引脚(CTS),如果需要硬件流控制,则可以在MCU软件中使用这些引脚。RI引脚是路由到mikroBUS™ PWM引脚的振铃指示器。STAT引脚用于信号网络连接状态。此引脚通过级联器路由到mikroBUS™ AN引脚,并使用黄色LED可视化地指示网络连接状态。红色TX LED用于指示传输状态,位于STAT LED旁边。PWRKEY引脚被路由到mikroBUS™ RST引脚,并在上电序列期间使用。如果提供了有效的供电电压,则在此引脚
上的低脉冲将启动设备。为了正确地与网络分离并将工作参数存储在其非易失性存储器中,模块应该通过发出AT+CPWROFF命令或在断开电源之前在PWRKEY引脚上产生至少1秒的低脉冲来安全地关闭电源。如前所述,SARA U-201提供了I2S接口,主要用于数字音频。来自Maxim Integrated的16位单声道音频语音编解码器IC MAX9860为3G SARA click板提供语音通信。此IC使用I2S和I2C接口与SARA U-201模块通信。它还通过LDO供电1.8V,因此不需要电平转换。此IC为连接耳机提供了清晰可听的模拟接口,具有一个音频输出通道和一个麦克风输入通道。耳机可以通过板载的3.5mm音频插孔连接。SARA 3G click配备了一个微型USB连接器。它允许通过个人计算机(PC)为模块供电和配置。u-blox公司提供了一个软件套件,可用于配置SARA U-201模块。点击板背面的Micro SIM卡槽用于安装SIM卡。没有有效的SIM卡,该设备无法使用,而SIM卡允许连接到蜂窝网络。支持1.8V和3V SIM卡类型。
功能概述
开发板
Nucleo-64搭载STM32L073RZ MCU提供了一个经济实惠且灵活的平台,供开发人员探索新的想法并原型化其设计。该板利用了STM32微控制器的多功能性,使用户能够为其项目选择性能和功耗之间的最佳平衡。它采用LQFP64封装的STM32微控制器,并包括一些必要的组件,例如用户LED,可以同时作为ARDUINO®信号使用,以及用户和复位按钮,以及用于精准定时操作的32.768kHz晶体振荡器。设计时考虑了扩展性和灵活性,Nucleo-64板具有ARDUINO®
Uno V3扩展连接器和ST morpho扩展引脚标头,为全面项目集成提供了对STM32 I/O的完全访问权限。电源选项具有适应性,支持ST-LINK USB VBUS或外部电源,确保在各种开发环境中的适应性。该板还配备了一个内置的ST-LINK调试器/编程器,具有USB重新枚举功能,简化了编程和调试过程。此外,该板还设计了外部SMPS,以实现有效的Vcore逻辑供电,支持USB设备全速或USB SNK/UFP全速,以及内置的加密功能,增强了项目的功耗效率和安全性。通过专用
连接器提供了额外的连接性,用于外部SMPS实验、ST-LINK的USB连接器和MIPI®调试连接器,扩展了硬件接口和实验的可能性。开发人员将通过STM32Cube MCU软件包中全面的免费软件库和示例得到广泛的支持。这与与各种集成开发环境(IDE)的兼容性相结合,包括IAR Embedded Workbench®、MDK-ARM和STM32CubeIDE,确保了平稳高效的开发体验,使用户能够充分发挥Nucleo-64板在其项目中的功能。
微控制器概述
MCU卡片 / MCU

建筑
ARM Cortex-M0
MCU 内存 (KB)
192
硅供应商
STMicroelectronics
引脚数
64
RAM (字节)
20480
你完善了我!
配件
Click Shield for Nucleo-64 配备了两个专有的 mikroBUS™ 插座,使得所有的 Click board™ 设备都可以轻松地与 STM32 Nucleo-64 开发板连接。这样,Mikroe 允许其用户从不断增长的 Click boards™ 范围中添加任何功能,如 WiFi、GSM、GPS、蓝牙、ZigBee、环境传感器、LED、语音识别、电机控制、运动传感器等。您可以使用超过 1537 个 Click boards™,这些 Click boards™ 可以堆叠和集成。STM32 Nucleo-64 开发板基于 64 引脚封装的微控制器,采用 32 位 MCU,配备 ARM Cortex M4 处理器,运行速度为 84MHz,具有 512Kb Flash 和 96KB SRAM,分为两个区域,顶部区域代表 ST-Link/V2 调试器和编程器,而底部区域是一个实际的开发板。通过 USB 连接方便地控制和供电这些板子,以便直接对 Nucleo-64 开发板进行编程和高效调试,其中还需要额外的 USB 线连接到板子上的 USB 迷你接口。大多数 STM32 微控制器引脚都连接到了板子左右边缘的 IO 引脚上,然后连接到两个现有的 mikroBUS™ 插座上。该 Click Shield 还有几个开关,用于选择 mikroBUS™ 插座上模拟信号的逻辑电平和 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换器,使用任何 Click board™,无论 Click board™ 是否在 3.3V 或 5V 逻辑电压电平下运行。一旦将 STM32 Nucleo-64 开发板与我们的 Click Shield for Nucleo-64 连接,您就可以访问数百个工作于 3.3V 或 5V 逻辑电压电平的 Click boards™。
橡胶天线 GSM/GPRS 直角天线是我们广泛产品线中所有 GSM Click board™ 的完美伴侣。这款专用天线旨在优化您的无线连接,具有令人印象深刻的特性。其广泛的频率范围涵盖 824-894/1710-1990MHz 或 890-960/1710-1890MHz,可以处理各种频段,确保无缝可靠的连接。该天线拥有 50 欧姆的阻抗和 2dB 的增益,增强了信号接收和发送。其 70/180MHz 带宽为不同应用提供了灵活性。垂直极化进一步提高了其性能。该天线具有最大输入功率容量为 50W,即使在苛刻的条件下也能确保强大的通信。该天线长 50mm,体积小巧,并配备 SMA 母头连接器,是您无线通信需求的多功能紧凑解决方案。
使用的MCU引脚
mikroBUS™映射器
“仔细看看!”
Click board™ 原理图

一步一步来
项目组装
实时跟踪您的结果
应用程序输出
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”替换为要显示的参数。

软件支持
库描述
这个库包含了3G SARA Click驱动程序的API。
关键函数:
c3gsara_set_sim_apn
- 此函数设置SIM卡的APNc3gsara_send_sms_text
- 此函数向电话号码发送文本消息c3gsara_send_sms_pdu
- 此函数以PDU模式向电话号码发送文本消息
开源
代码示例
完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio。 应用程序代码也可以在MIKROE的GitHub账户中找到。
/*!
* @file main.c
* @brief 3G SARA 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:
* - C3GSARA_CONFIGURE_FOR_NETWORK:
* Sets configuration to device to be able to connect to the network.
*
* - C3GSARA_WAIT_FOR_CONNECTION:
* Waits for the network registration indicated via CREG URC event and then checks
* the connection status.
*
* - C3GSARA_CONFIGURE_FOR_EXAMPLE:
* Sets the device configuration for sending SMS or TCP/UDP messages depending on the selected demo example.
*
* - C3GSARA_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 c3gsara_clear_app_buf ( void )
* - static err_t c3gsara_process ( void )
* - static void c3gsara_error_check( err_t error_flag )
* - static void c3gsara_log_app_buf ( void )
* - static err_t c3gsara_rsp_check ( uint8_t *rsp )
* - static err_t c3gsara_configure_for_connection( void )
* - static err_t c3gsara_check_connection( void )
* - static err_t c3gsara_configure_for_messages( void )
* - static err_t c3gsara_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 "c3gsara.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 "" // 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 "3G SARA 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
{
C3GSARA_CONFIGURE_FOR_NETWORK = 1,
C3GSARA_WAIT_FOR_CONNECTION,
C3GSARA_CONFIGURE_FOR_EXAMPLE,
C3GSARA_EXAMPLE
} c3gsara_example_state_t;
static c3gsara_t c3gsara;
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 c3gsara_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 c3gsara_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 c3gsara_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 c3gsara_error_check ( err_t error_flag );
/**
* @brief Logs application buffer.
* @details This function logs data from application buffer.
*/
static void c3gsara_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 c3gsara_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 c3gsara_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 c3gsara_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 c3gsara_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 c3gsara_example ( void );
void application_init ( void )
{
log_cfg_t log_cfg; /**< Logger config object. */
c3gsara_cfg_t c3gsara_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.
c3gsara_cfg_setup( &c3gsara_cfg );
C3GSARA_MAP_MIKROBUS( c3gsara_cfg, MIKROBUS_1 );
if ( UART_ERROR == c3gsara_init( &c3gsara, &c3gsara_cfg ) )
{
log_error( &logger, " Application Init Error. " );
log_info( &logger, " Please, run program again... " );
for ( ; ; );
}
c3gsara_set_power_state ( &c3gsara, C3GSARA_POWER_STATE_ON );
c3gsara_process( );
c3gsara_clear_app_buf( );
// Check communication
c3gsara_send_cmd( &c3gsara, C3GSARA_CMD_AT );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
c3gsara_error_check( error_flag );
// Restart device
#define RESTART_DEVICE "1,1"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CFUN, RESTART_DEVICE );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
c3gsara_error_check( error_flag );
log_info( &logger, " Application Task " );
example_state = C3GSARA_CONFIGURE_FOR_NETWORK;
}
void application_task ( void )
{
switch ( example_state )
{
case C3GSARA_CONFIGURE_FOR_NETWORK:
{
if ( C3GSARA_OK == c3gsara_configure_for_network( ) )
{
example_state = C3GSARA_WAIT_FOR_CONNECTION;
}
break;
}
case C3GSARA_WAIT_FOR_CONNECTION:
{
if ( C3GSARA_OK == c3gsara_check_connection( ) )
{
example_state = C3GSARA_CONFIGURE_FOR_EXAMPLE;
}
break;
}
case C3GSARA_CONFIGURE_FOR_EXAMPLE:
{
if ( C3GSARA_OK == c3gsara_configure_for_example( ) )
{
example_state = C3GSARA_EXAMPLE;
}
break;
}
case C3GSARA_EXAMPLE:
{
c3gsara_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 c3gsara_clear_app_buf ( void )
{
memset( app_buf, 0, app_buf_len );
app_buf_len = 0;
}
static err_t c3gsara_process ( void )
{
uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
int32_t rx_size = 0;
rx_size = c3gsara_generic_read( &c3gsara, 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 C3GSARA_OK;
}
return C3GSARA_ERROR;
}
static err_t c3gsara_rsp_check ( uint8_t *rsp )
{
uint32_t timeout_cnt = 0;
uint32_t timeout = 120000;
c3gsara_clear_app_buf( );
c3gsara_process( );
while ( ( 0 == strstr( app_buf, rsp ) ) &&
( 0 == strstr( app_buf, C3GSARA_RSP_ERROR ) ) )
{
c3gsara_process( );
if ( timeout_cnt++ > timeout )
{
c3gsara_clear_app_buf( );
return C3GSARA_ERROR_TIMEOUT;
}
Delay_ms ( 1 );
}
Delay_ms ( 100 );
c3gsara_process( );
if ( strstr( app_buf, rsp ) )
{
return C3GSARA_OK;
}
else if ( strstr( app_buf, C3GSARA_RSP_ERROR ) )
{
return C3GSARA_ERROR_CMD;
}
else
{
return C3GSARA_ERROR_UNKNOWN;
}
}
static void c3gsara_error_check ( err_t error_flag )
{
switch ( error_flag )
{
case C3GSARA_OK:
{
c3gsara_log_app_buf( );
break;
}
case C3GSARA_ERROR:
{
log_error( &logger, " Overflow!" );
break;
}
case C3GSARA_ERROR_TIMEOUT:
{
log_error( &logger, " Timeout!" );
break;
}
case C3GSARA_ERROR_CMD:
{
log_error( &logger, " CMD!" );
break;
}
case C3GSARA_ERROR_UNKNOWN:
default:
{
log_error( &logger, " Unknown!" );
break;
}
}
Delay_ms ( 500 );
}
static void c3gsara_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 c3gsara_configure_for_network ( void )
{
err_t func_error = C3GSARA_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"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_COPS, DEREGISTER_FROM_NETWORK );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Set SIM APN
c3gsara_set_sim_apn( &c3gsara, SIM_APN );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Enable full functionality
#define FULL_FUNCTIONALITY "1"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CFUN, FULL_FUNCTIONALITY );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Enable network registartion
#define ENABLE_REG "2"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CREG, ENABLE_REG );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Automatic registration
#define AUTOMATIC_REGISTRATION "0"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_COPS, AUTOMATIC_REGISTRATION );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
#endif
return func_error;
}
static err_t c3gsara_check_connection ( void )
{
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
#define CONNECTED "+CREG: 2,1"
c3gsara_send_cmd_check ( &c3gsara, C3GSARA_CMD_CREG );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
c3gsara_error_check( error_flag );
if ( strstr( app_buf, CONNECTED ) )
{
Delay_ms ( 100 );
c3gsara_process( );
c3gsara_log_app_buf( );
log_printf( &logger, "\r\n" );
c3gsara_clear_app_buf( );
// Check signal quality
c3gsara_send_cmd( &c3gsara, C3GSARA_CMD_CSQ );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
c3gsara_error_check( error_flag );
return error_flag;
}
Delay_ms ( 1000 );
return C3GSARA_ERROR;
#endif
return C3GSARA_OK;
}
static err_t c3gsara_configure_for_example ( void )
{
err_t func_error = C3GSARA_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
#define ACTIVATE_PDP_CONTEXT "1,1"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CGACT, ACTIVATE_PDP_CONTEXT );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
#define ACTIVATE_PDP_PROFILE "1,3"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_UPSDA, ACTIVATE_PDP_PROFILE );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CMGF, SMS_MODE );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
#else
#error "No demo example selected"
#endif
return func_error;
}
static err_t c3gsara_example ( void )
{
err_t func_error = C3GSARA_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
uint8_t cmd_buf[ 100 ] = { 0 };
uint8_t urc_buf[ 20 ] = { 0 };
uint16_t timeout_cnt = 0;
uint16_t timeout = 30000;
uint8_t * __generic_ptr socket_num_buf = 0;
uint8_t tcp_socket_num[ 2 ] = { 0 };
uint8_t udp_socket_num[ 2 ] = { 0 };
// Create TCP socket
#define RSP_USOCR "+USOCR: "
#define TCP_PROTOCOL "6"
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCR, TCP_PROTOCOL );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
tcp_socket_num[ 0 ] = *socket_num_buf;
c3gsara_error_check( error_flag );
// Create UDP socket
#define UDP_PROTOCOL "17"
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCR, UDP_PROTOCOL );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
udp_socket_num[ 0 ] = *socket_num_buf;
c3gsara_error_check( error_flag );
// Connect TCP socket to remote IP and port
strcpy( cmd_buf, tcp_socket_num );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, REMOTE_IP );
strcat( cmd_buf, "\"," );
strcat( cmd_buf, REMOTE_PORT );
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_USOCO, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Connect UDP socket to remote IP and port
strcpy( cmd_buf, udp_socket_num );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, REMOTE_IP );
strcat( cmd_buf, "\"," );
strcat( cmd_buf, REMOTE_PORT );
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCO, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Get message length
uint8_t message_len_buf[ 5 ] = { 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
strcpy( cmd_buf, tcp_socket_num );
strcat( cmd_buf, "," );
strcat( cmd_buf, message_len_buf );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, MESSAGE_CONTENT );
strcat( cmd_buf, "\"" );
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOWR, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Read response message from TCP socket
#define URC_READ_SOCKET_DATA_TCP "+UUSORD: "
strcpy( urc_buf, URC_READ_SOCKET_DATA_TCP );
strcat( urc_buf, tcp_socket_num );
for ( ; ; )
{
c3gsara_process( );
uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
if ( start_response_buf )
{
Delay_ms ( 100 );
c3gsara_process( );
uint8_t response_len_buf[ 5 ] = { 0 };
uint8_t * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
strcpy( cmd_buf, tcp_socket_num );
strcat( cmd_buf, "," );
strcat( cmd_buf, response_len_buf );
c3gsara_log_app_buf( );
c3gsara_clear_app_buf( );
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USORD, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
break;
}
if ( timeout_cnt++ > timeout )
{
break;
}
Delay_ms ( 1 );
}
timeout_cnt = 0;
// Write message to UDP socket
strcpy( cmd_buf, udp_socket_num );
strcat( cmd_buf, "," );
strcat( cmd_buf, message_len_buf );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, MESSAGE_CONTENT );
strcat( cmd_buf, "\"" );
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_USOWR, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Read response message from UDP socket
#define URC_READ_SOCKET_DATA_UDP "+UUSORD: "
strcpy( urc_buf, URC_READ_SOCKET_DATA_UDP );
strcat( urc_buf, udp_socket_num );
for ( ; ; )
{
c3gsara_process( );
uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
if ( start_response_buf )
{
Delay_ms ( 100 );
c3gsara_process( );
uint8_t response_len_buf[ 5 ] = { 0 };
uint8_t * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
strcpy( cmd_buf, udp_socket_num );
strcat( cmd_buf, "," );
strcat( cmd_buf, response_len_buf );
c3gsara_log_app_buf( );
c3gsara_clear_app_buf( );
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USORF, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
break;
}
if ( timeout_cnt++ > timeout )
{
break;
}
Delay_ms ( 1 );
}
// Close TCP socket
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCL, tcp_socket_num );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Close UDP socket
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCL, udp_socket_num );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_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"
c3gsara_send_cmd_check( &c3gsara, C3GSARA_CMD_CMGF );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
if ( strstr( app_buf, CMGF_PDU ) )
{
// Send SMS in PDU mode
c3gsara_send_sms_pdu( &c3gsara, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
}
else if ( strstr( app_buf, CMGF_TXT ) )
{
// Send SMS in TXT mode
c3gsara_send_sms_text ( &c3gsara, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_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
/*!
* @file main.c
* @brief 3G SARA 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:
* - C3GSARA_CONFIGURE_FOR_NETWORK:
* Sets configuration to device to be able to connect to the network.
*
* - C3GSARA_WAIT_FOR_CONNECTION:
* Waits for the network registration indicated via CREG URC event and then checks
* the connection status.
*
* - C3GSARA_CONFIGURE_FOR_EXAMPLE:
* Sets the device configuration for sending SMS or TCP/UDP messages depending on the selected demo example.
*
* - C3GSARA_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 c3gsara_clear_app_buf ( void )
* - static err_t c3gsara_process ( void )
* - static void c3gsara_error_check( err_t error_flag )
* - static void c3gsara_log_app_buf ( void )
* - static err_t c3gsara_rsp_check ( uint8_t *rsp )
* - static err_t c3gsara_configure_for_connection( void )
* - static err_t c3gsara_check_connection( void )
* - static err_t c3gsara_configure_for_messages( void )
* - static err_t c3gsara_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 "c3gsara.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 "" // 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 "3G SARA 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
{
C3GSARA_CONFIGURE_FOR_NETWORK = 1,
C3GSARA_WAIT_FOR_CONNECTION,
C3GSARA_CONFIGURE_FOR_EXAMPLE,
C3GSARA_EXAMPLE
} c3gsara_example_state_t;
static c3gsara_t c3gsara;
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 c3gsara_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 c3gsara_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 c3gsara_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 c3gsara_error_check ( err_t error_flag );
/**
* @brief Logs application buffer.
* @details This function logs data from application buffer.
*/
static void c3gsara_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 c3gsara_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 c3gsara_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 c3gsara_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 c3gsara_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 c3gsara_example ( void );
void application_init ( void )
{
log_cfg_t log_cfg; /**< Logger config object. */
c3gsara_cfg_t c3gsara_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.
c3gsara_cfg_setup( &c3gsara_cfg );
C3GSARA_MAP_MIKROBUS( c3gsara_cfg, MIKROBUS_1 );
if ( UART_ERROR == c3gsara_init( &c3gsara, &c3gsara_cfg ) )
{
log_error( &logger, " Application Init Error. " );
log_info( &logger, " Please, run program again... " );
for ( ; ; );
}
c3gsara_set_power_state ( &c3gsara, C3GSARA_POWER_STATE_ON );
c3gsara_process( );
c3gsara_clear_app_buf( );
// Check communication
c3gsara_send_cmd( &c3gsara, C3GSARA_CMD_AT );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
c3gsara_error_check( error_flag );
// Restart device
#define RESTART_DEVICE "1,1"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CFUN, RESTART_DEVICE );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
c3gsara_error_check( error_flag );
log_info( &logger, " Application Task " );
example_state = C3GSARA_CONFIGURE_FOR_NETWORK;
}
void application_task ( void )
{
switch ( example_state )
{
case C3GSARA_CONFIGURE_FOR_NETWORK:
{
if ( C3GSARA_OK == c3gsara_configure_for_network( ) )
{
example_state = C3GSARA_WAIT_FOR_CONNECTION;
}
break;
}
case C3GSARA_WAIT_FOR_CONNECTION:
{
if ( C3GSARA_OK == c3gsara_check_connection( ) )
{
example_state = C3GSARA_CONFIGURE_FOR_EXAMPLE;
}
break;
}
case C3GSARA_CONFIGURE_FOR_EXAMPLE:
{
if ( C3GSARA_OK == c3gsara_configure_for_example( ) )
{
example_state = C3GSARA_EXAMPLE;
}
break;
}
case C3GSARA_EXAMPLE:
{
c3gsara_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 c3gsara_clear_app_buf ( void )
{
memset( app_buf, 0, app_buf_len );
app_buf_len = 0;
}
static err_t c3gsara_process ( void )
{
uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
int32_t rx_size = 0;
rx_size = c3gsara_generic_read( &c3gsara, 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 C3GSARA_OK;
}
return C3GSARA_ERROR;
}
static err_t c3gsara_rsp_check ( uint8_t *rsp )
{
uint32_t timeout_cnt = 0;
uint32_t timeout = 120000;
c3gsara_clear_app_buf( );
c3gsara_process( );
while ( ( 0 == strstr( app_buf, rsp ) ) &&
( 0 == strstr( app_buf, C3GSARA_RSP_ERROR ) ) )
{
c3gsara_process( );
if ( timeout_cnt++ > timeout )
{
c3gsara_clear_app_buf( );
return C3GSARA_ERROR_TIMEOUT;
}
Delay_ms ( 1 );
}
Delay_ms ( 100 );
c3gsara_process( );
if ( strstr( app_buf, rsp ) )
{
return C3GSARA_OK;
}
else if ( strstr( app_buf, C3GSARA_RSP_ERROR ) )
{
return C3GSARA_ERROR_CMD;
}
else
{
return C3GSARA_ERROR_UNKNOWN;
}
}
static void c3gsara_error_check ( err_t error_flag )
{
switch ( error_flag )
{
case C3GSARA_OK:
{
c3gsara_log_app_buf( );
break;
}
case C3GSARA_ERROR:
{
log_error( &logger, " Overflow!" );
break;
}
case C3GSARA_ERROR_TIMEOUT:
{
log_error( &logger, " Timeout!" );
break;
}
case C3GSARA_ERROR_CMD:
{
log_error( &logger, " CMD!" );
break;
}
case C3GSARA_ERROR_UNKNOWN:
default:
{
log_error( &logger, " Unknown!" );
break;
}
}
Delay_ms ( 500 );
}
static void c3gsara_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 c3gsara_configure_for_network ( void )
{
err_t func_error = C3GSARA_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"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_COPS, DEREGISTER_FROM_NETWORK );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Set SIM APN
c3gsara_set_sim_apn( &c3gsara, SIM_APN );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Enable full functionality
#define FULL_FUNCTIONALITY "1"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CFUN, FULL_FUNCTIONALITY );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Enable network registartion
#define ENABLE_REG "2"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CREG, ENABLE_REG );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Automatic registration
#define AUTOMATIC_REGISTRATION "0"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_COPS, AUTOMATIC_REGISTRATION );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
#endif
return func_error;
}
static err_t c3gsara_check_connection ( void )
{
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
#define CONNECTED "+CREG: 2,1"
c3gsara_send_cmd_check ( &c3gsara, C3GSARA_CMD_CREG );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
c3gsara_error_check( error_flag );
if ( strstr( app_buf, CONNECTED ) )
{
Delay_ms ( 100 );
c3gsara_process( );
c3gsara_log_app_buf( );
log_printf( &logger, "\r\n" );
c3gsara_clear_app_buf( );
// Check signal quality
c3gsara_send_cmd( &c3gsara, C3GSARA_CMD_CSQ );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
c3gsara_error_check( error_flag );
return error_flag;
}
Delay_ms ( 1000 );
return C3GSARA_ERROR;
#endif
return C3GSARA_OK;
}
static err_t c3gsara_configure_for_example ( void )
{
err_t func_error = C3GSARA_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
#define ACTIVATE_PDP_CONTEXT "1,1"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CGACT, ACTIVATE_PDP_CONTEXT );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
#define ACTIVATE_PDP_PROFILE "1,3"
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_UPSDA, ACTIVATE_PDP_PROFILE );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_CMGF, SMS_MODE );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
#else
#error "No demo example selected"
#endif
return func_error;
}
static err_t c3gsara_example ( void )
{
err_t func_error = C3GSARA_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
uint8_t cmd_buf[ 100 ] = { 0 };
uint8_t urc_buf[ 20 ] = { 0 };
uint16_t timeout_cnt = 0;
uint16_t timeout = 30000;
uint8_t * __generic_ptr socket_num_buf = 0;
uint8_t tcp_socket_num[ 2 ] = { 0 };
uint8_t udp_socket_num[ 2 ] = { 0 };
// Create TCP socket
#define RSP_USOCR "+USOCR: "
#define TCP_PROTOCOL "6"
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCR, TCP_PROTOCOL );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
tcp_socket_num[ 0 ] = *socket_num_buf;
c3gsara_error_check( error_flag );
// Create UDP socket
#define UDP_PROTOCOL "17"
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCR, UDP_PROTOCOL );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
udp_socket_num[ 0 ] = *socket_num_buf;
c3gsara_error_check( error_flag );
// Connect TCP socket to remote IP and port
strcpy( cmd_buf, tcp_socket_num );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, REMOTE_IP );
strcat( cmd_buf, "\"," );
strcat( cmd_buf, REMOTE_PORT );
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_USOCO, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Connect UDP socket to remote IP and port
strcpy( cmd_buf, udp_socket_num );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, REMOTE_IP );
strcat( cmd_buf, "\"," );
strcat( cmd_buf, REMOTE_PORT );
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCO, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Get message length
uint8_t message_len_buf[ 5 ] = { 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
strcpy( cmd_buf, tcp_socket_num );
strcat( cmd_buf, "," );
strcat( cmd_buf, message_len_buf );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, MESSAGE_CONTENT );
strcat( cmd_buf, "\"" );
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOWR, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Read response message from TCP socket
#define URC_READ_SOCKET_DATA_TCP "+UUSORD: "
strcpy( urc_buf, URC_READ_SOCKET_DATA_TCP );
strcat( urc_buf, tcp_socket_num );
for ( ; ; )
{
c3gsara_process( );
uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
if ( start_response_buf )
{
Delay_ms ( 100 );
c3gsara_process( );
uint8_t response_len_buf[ 5 ] = { 0 };
uint8_t * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
strcpy( cmd_buf, tcp_socket_num );
strcat( cmd_buf, "," );
strcat( cmd_buf, response_len_buf );
c3gsara_log_app_buf( );
c3gsara_clear_app_buf( );
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USORD, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
break;
}
if ( timeout_cnt++ > timeout )
{
break;
}
Delay_ms ( 1 );
}
timeout_cnt = 0;
// Write message to UDP socket
strcpy( cmd_buf, udp_socket_num );
strcat( cmd_buf, "," );
strcat( cmd_buf, message_len_buf );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, MESSAGE_CONTENT );
strcat( cmd_buf, "\"" );
c3gsara_send_cmd_with_par( &c3gsara, C3GSARA_CMD_USOWR, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Read response message from UDP socket
#define URC_READ_SOCKET_DATA_UDP "+UUSORD: "
strcpy( urc_buf, URC_READ_SOCKET_DATA_UDP );
strcat( urc_buf, udp_socket_num );
for ( ; ; )
{
c3gsara_process( );
uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
if ( start_response_buf )
{
Delay_ms ( 100 );
c3gsara_process( );
uint8_t response_len_buf[ 5 ] = { 0 };
uint8_t * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
strcpy( cmd_buf, udp_socket_num );
strcat( cmd_buf, "," );
strcat( cmd_buf, response_len_buf );
c3gsara_log_app_buf( );
c3gsara_clear_app_buf( );
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USORF, cmd_buf );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
break;
}
if ( timeout_cnt++ > timeout )
{
break;
}
Delay_ms ( 1 );
}
// Close TCP socket
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCL, tcp_socket_num );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
// Close UDP socket
c3gsara_send_cmd_with_par ( &c3gsara, C3GSARA_CMD_USOCL, udp_socket_num );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_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"
c3gsara_send_cmd_check( &c3gsara, C3GSARA_CMD_CMGF );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
if ( strstr( app_buf, CMGF_PDU ) )
{
// Send SMS in PDU mode
c3gsara_send_sms_pdu( &c3gsara, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_error_check( error_flag );
}
else if ( strstr( app_buf, CMGF_TXT ) )
{
// Send SMS in TXT mode
c3gsara_send_sms_text ( &c3gsara, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
error_flag = c3gsara_rsp_check( C3GSARA_RSP_OK );
func_error |= error_flag;
c3gsara_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