background image

const char sparkfunDomain[]

 

= "sparkfun.com";

 

//

 

Domain

 

we

 

wan

t

 

to

 

look

 

up

IPAddress

 

sparkfunIP;

 

//

 

IPAddress

 

variable

 

where

 

we'll

 

store

 

the

 

domain's

 

IP

gprs.hostByName(sparkfunDomain,

 

&sparkfunIP);

Serial.print("sparkfun.com's

 

IP

 

address

 

is:

 

");

Serial.println(sparkfunIP);

hostByName()

returns an error code as well. It’ll be a positive number if the 

lookup was successful, and a negative number if there was an error or 
timeout.

Connecting

To connect to a remote IP address, use either 

gprs.connect(IPAddress

 

ip,

 

unsigned

 

int

 

port)

or 

gprs.connect(const

 

char

 

*

 

domain,

 

unsigned

 

int

 

port)

to connect to 

an IP address or domain string.

For example, to connect to SparkFun on port 80 (the good, old, HTTP port), 
send this:

const char sparkFunDomain[]

 

= "sparkfun.com";

int connectStatus;
connectStatus

 

= gprs.connect(sparkFunDomain,

 

80);

if (connectStatus

 

> 0)

    

Serial.println("Connected

 

to

 

SparkFun,

 

port

 

80");

else if (connectStatus

 

==

­

1)

    

Serial.println("Timed

 

out

 

trying

 

to

 

connect

 

to

 

SparkFu

n.");
else if (connectStatus

 

==

­

2)

    

Serial.println("Received

 

an

 

error

 

trying

 

to

 

connect.");

Sending and Receiving

As with other stream classes, 

print()

write()

available()

, and 

read()

functions are defined for 

gprs

. You can use 

print()

to send just 

about any, defined variable type in Arduino to a connected server.

Each 

print()

takes a long-ish time to execute (about 1-2 seconds), so if 

speed is a factor we recommend using as few separate 

print()

calls as 

possible. For example, these blocks of code do the same thing:

//

 

You

 

can

 

do

 

this

 

to

 

send

 

a

 

GET

 

string..

gprs.println("GET

 

/

 

HTTP/1.1");

gprs.print("Host:

 

");

gprs.print(server);
gprs.println();
gprs.println();

//

 

But

 

this

 

one

 

is

 

faster:

gprs.print("GET

 

/

 

HTTP/1.1\nHost:

 

example.com\n\n");

But the latter finishes about 8 seconds faster.

To check for data coming from a connected server, back to the MG2639, 
use 

gprs.available()

and 

gprs.read()

gprs.available()

returns the 

number of bytes currently stored in the library’s receive buffer. If it’s greater 
than 0, there’s at least one character there – use 

gprs.read()

to read the 

first available character in the buffer.

As an example from the sketch:

Page 17 of 22

Содержание MG2639 Cellular Shield

Страница 1: ...whether it s a text message triggered MP3 player an environmental monitor logging to data sparkfun com or a new iteration of the Port O Rotary telephone Covered In This Tutorial This tutorial aims to...

Страница 2: ...requires more power than an Arduino can supply when it s powered over USB We recommend this 9V wall adapter but any power supply within your Arduino s specified input voltage range should work SIM Ca...

Страница 3: ...eld if you ve never installed a library before check this guide out Hardware Overview Before you get to connecting the MG2639 Cellular Shield to your Arduino you should familiarize yourself with the f...

Страница 4: ...use with SoftwareSerial Instead we recommend using the AltSoftSerial library with this part of the module Either of the two UARTS the cell or GPS can be switched over to the Arduino s hardware UART Ch...

Страница 5: ...B0104 level shifter It defaults to 5V but can be switched to 3 3V if your application requires Cellular UART RX and TX These jumpers allow you to switch the cellular module s RX and TX between either...

Страница 6: ...rduino s unused pins You can also instead use male headers to connect it to the Arduino If you re looking to use the shield as more of a breakout board you can solder wires directly to the pins you ne...

Страница 7: ...e SIM card into the shield The workings of the SIM card socket can take some getting used to To unlock the latch push the top part of the assembly towards the battery then lift it up Slide the SIM car...

Страница 8: ...nding a suitable SIM card the most important part of getting the MG2639 Shield working is supplying it with enough power Power Supply Requirements Depending on which state it s in the MG2639 module ca...

Страница 9: ...d to cut SJ2 and short SJ3 Jumper configuration for battery power Cut SJ2 and short SJ3 then clean it up better than we did SJ2 will prevent the 3 8V regulator from supplying a competing voltage to th...

Страница 10: ...lar Shield library depends on Download and Install the Arduino Library We ve written an Arduino library specifically for the MG2639 Cellular Shield Click the button below to download it or clone it fr...

Страница 11: ...s include SFE_MG2639_CellShield h To begin communication the cellular module and perform some set up call the cell begin function This initializing function will return with the status of the module a...

Страница 12: ...ch for help using those functions Example 1 Text Messages The library includes a fun example sketch the demonstrates how to send and receive text messages If your cellular plan has SMS ability give th...

Страница 13: ...rduino will send an SMS back to the sender with the desired information Using the Library SMS To use the SMS functionality of the library there is a second object defined as sms There are a variety of...

Страница 14: ...message sending phone and timestamp the message was sent These values are only updated after an sms read Here s an example based on the sketch Get first available unread message int messageIndex sms a...

Страница 15: ...ad allows you to go through the logs of your text messages and delete any or save them Example 2 GPRS TCP One of the most powerful aspects of the MG2639 is its ability to connect to a GPRS network and...

Страница 16: ...ion can take a while to return successfully upwards of 30 seconds if the module is just getting warmed up so be patient If you ever need to turn GPRS off use the gprs close function This function has...

Страница 17: ...connect to SparkFu n else if connectStatus 2 Serial println Received an error trying to connect Sending and Receiving As with other stream classes print write available and read functions are defined...

Страница 18: ...ve voice calls If you re using a passive microphone and speaker you can simply connect them to the differential pins on the audio port Connect the microphone pins to MIC and MIC and speaker wires to S...

Страница 19: ...G An outgoing call is in the process of dialing This status precedes ringing CALL_DIALED_RINGING An outgoing call has been dialed and is ringing on the other end CALL_INCOMING A call is coming in If a...

Страница 20: ...t while phoneStatus CALL_ACTIVE phoneStatus CALL_ IDLE if callStatus CALL_ACTIVE Serial println Other end picked up Again you can use phone hangUp to end the call unless the other party does so first...

Страница 21: ...e character at a time and don t abuse the strem Memory Limitations The MG2639 library can really test the limits of the Arduino s memory You ll notice that in most of these examples we put large const...

Страница 22: ...ar Shield should serve as a solid launching point Serial Graphic LCD Hookup Learn how to use the Serial Graphic LCD Graphic LCD Hookup Guide How to add some flashy graphics to your project with a 84x4...

Отзывы: