| ioPAC RTU Controllers C/C++ Sample Code Programming Guide

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Functions
cellular_connection.c File Reference

Establish 3G Network Sample More...

`#include <stdio.h>`
`#include <unistd.h>`
`#include <libmoxa_rtu.h>`

## Functions
int | main (int argc, char **const argv)

## Detailed Description

Establish 3G Network Sample

Copyright
  Moxa Inc.

Date
  06-04-2013

Author
  Changfu Hsieh

Version
  V1.0
  Cellular Network

    Introduction:
    This sample code shows how to establish a 3G connection by MOXA cellular API.
    Example:
    1. Execute the sample code with default settings: ./cellular_connection
    2. Execute the sample code with customized setting: ./cellular_connection -a internet -p 0000 -b 127
    Default:
    PIN = 0000
    Band = 127
    APN = internet
    Username = username
    Password = password
    Remote host = www.google.com
    Help:

    root@Moxa:/home#./cellular_connection -h
    Cellular 3G Network.

    Usage: ./cellular_connection [OPTIONS]

    Options:
            -t       To specify hostname for ping check. Default = www.google.com
            -a       To specify the APN. Default = internet
            -b       To specify bands. Default = 127
            -p       To specify PIN code. Default = 0000
            -u       To specify user name. Default = username
            -c       To specify user password. Default = password

Library:
Cellular APIs

## Function Documentation

int main | ( | int | argc,
|  | char **const | argv
| ) |  |

/*******************************************************************************
* Copyright Moxa Inc.
*
* 3G connection
*
* Date Author Comment
* 06-04-2013 Changfu Hsieh Created.
******************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <libmoxa_rtu.h>
int main(int argc, char **const argv)
{
INT32 rc = 0;
UINT8 ch;
UINT8 ipAddress[16];
UINT8 rssi;
INT8 imei[16];
INT8 pin[5];
INT8 apn[64];
INT8 username[64];
INT8 password[64];
UINT16 band = MODEM_BAND_PH8_AUTO;
CheckInfo autoConnect;
//set 3G connection auto-check parameters
autoConnect.autoCheckEnable = 1;//enable 3G netowrk AutoCheck function
autoConnect.pingIntervalS = 5;//ping www.google.com every 5 seconds for checking 3G network
autoConnect.pingMaxFail = 3;//state machine will re-establish 3G network after failing to ping www.google.com for 3 times
sprintf((char*)autoConnect.pingHostname, "www.google.com");
sprintf((char*)apn, "internet");//specify default APN settings
sprintf((char*)username, "username");//specify default username settings
sprintf((char*)password, "password");//specify default password settings
sprintf((char*)pin, "0000");//specify default PIN
for(;;)
{
rc = getopt(argc, argv, "a:b:p:ht:u:c:");
if(rc == EOF) break;
switch(rc)
{
case 'u':
sprintf(username, optarg);
break;
case 'c':
sprintf(password, optarg);
break;
case 'a':
sprintf(apn, optarg);
break;
case 't':
sprintf((char*)autoConnect.pingHostname, optarg);
break;
case 'p':
memcpy(pin, optarg, 4);
break;
case 'b':
band = atoi(optarg);
break;
case 'h':
default:
printf("Cellular 3G Network.\n\n");
printf("Usage: ./cellular_connection [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s To specify hostname for ping check. Default = %s\n", "-t", "www.google.com");
printf("\t%-8s To specify the APN. Default = %s\n", "-a", apn);
printf("\t%-8s To specify bands. Default = %d\n", "-b", MODEM_BAND_PH8_AUTO);
printf("\t%-8s To specify PIN code. Default = %s\n", "-p", pin);
printf("\t%-8s To specify user name. Default = %s\n", "-u", username);
printf("\t%-8s To specify user password. Default = %s\n", "-c", password);
printf("\n");
exit(1);
}
}
//enable verbose messages
MX_RTU_Cellular_Set_Debug(1);
//Init modem
printf("Cellular: Init cellular modem...\n");
rc = MX_RTU_Cellular_Modem_Init(pin, band);
printf("Cellular: MX_Cellular_Modem_Init:%d\r\n", rc);
if(rc != MODEM_ERR_OK)
{
return -1;
}
printf("\r\nCommands:\r\n");
printf("\tPress 'a' to get IP address.\r\n");
printf("\tPress 'c' to start 3G network without AutoCheck.\r\n");
printf("\tPress 'C' to start 3G network with AutoCheck.\r\n");
printf("\tPress 'm' to get current state machine.\r\n");
printf("\tPress 's' to stop 3G network.\r\n");
printf("\tPress 'h' to query RSSI.\r\n");
printf("\tPress 'i' to query IMEI.\r\n");
printf("\tPress 'q' to quit.\r\n");
while(1)
{
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'c':
case 'C':
case 's':
case 'q':
case 'h':
case 'i':
case 'm':
break;
case '\n':
default:
printf("\r\nCommands:\r\n");
printf("\tPress 'a' to get IP address.\r\n");
printf("\tPress 'c' to start 3G network without AutoCheck.\r\n");
printf("\tPress 'C' to start 3G network with AutoCheck.\r\n");
printf("\tPress 'm' to get current state machine.\r\n");
printf("\tPress 's' to stop 3G network.\r\n");
printf("\tPress 'h' to query RSSI.\r\n");
printf("\tPress 'i' to query IMEI.\r\n");
printf("\tPress 'q' to quit.\r\n");
continue;
}
switch(ch)
{
case 'a':
autoConnect.autoCheckEnable = 0;
rc = MX_RTU_Cellular_Net_IP_Address(ipAddress);
printf("Cellular: IP address:%s, rc:%d\n", ipAddress, rc);
continue;
case 'c':
autoConnect.autoCheckEnable = 0;
rc = MX_RTU_Cellular_Net_Start(apn, username, password, &autoConnect);
printf("Cellular: MX_Cellular_Net_Start without AutoCheck:%d\n", rc);
continue;
case 'C':
autoConnect.autoCheckEnable = 1;
rc = MX_RTU_Cellular_Net_Start(apn, username, password, &autoConnect);
printf("Cellular: MX_Cellular_Net_Start with AutoCheck:%d\n", rc);
continue;
case 's':
rc = MX_RTU_Cellular_Net_Stop();
printf("Cellular: MX_Cellular_Net_Stop:%d\n", rc);
continue;
case 'h':
rc = MX_RTU_Cellular_Modem_RSSI(&rssi);
printf("Cellular: MX_Cellular_Modem_RSSI:%d, rc:%d\n", rssi, rc);
continue;
case 'i':
MX_RTU_Cellular_Modem_IMEI(imei);
printf("Cellular: IMEI:%s\n", imei);
continue;
case 'm':
printf("Cellular: state machine = %d\n", MX_RTU_Cellular_Net_State());
continue;
case 'q':
case 'Q':
printf("Cellular: Quit\n");
return 0;
}
}
return 0;
}