Tutorial.GSMExamplesPachubeClientString History

Hide minor edits - Show changes to markup

November 08, 2013, at 04:18 PM by Scott Fitzgerald -
Changed lines 1-411 from:
to:
March 11, 2013, at 10:34 AM by Scott Fitzgerald -
Changed lines 397-411 from:
to:
February 04, 2013, at 08:05 AM by Scott Fitzgerald -
Changed lines 5-8 from:

A simple server that distributes any incoming messages to all connected clients. To use, open a terminal window, telnet to your devices IP address, and type away. Any incoming text will be sent to all connected clients (including the one typing). Additionally, you will be able to see the client's input in your serial monitor as well.

Not all network operators allow incoming data requests from outside their network. This means you can create a web server with the GSM shield, but you may not be able to connect to it from the public internet; only from another data enabled device from the same provider on the same network. You should check with your provider to see what specific policies they have in place regarding incoming data connections.

to:

This example shows you how to answer a HTTP request using a GSM shield attached to an Arduino. Specifically, it connects to pachube.com, a free datalogging site and sends data as a string. The example requires that you set up a pachube.com account, as well as a pachube feed (for more information on setting up an input feed, please click here). Your GSM shield will then connect to that feed and upload sensor data.

Added lines 14-19:
  • Analog sensors attached to A0 and A1

Software Required

  • pachube.com account
  • pachube.com feed that accepts two data items
Added lines 35-42:
Changed lines 55-56 from:

Initialize instances of the classes you're going to use. You're going to need the GSM, GPRS, GSMServer, and GSMClient classes. When you instantiate the GSMServer class, you'll need to tell it which port to listen for incoming connections. Port 80 is the default port for HTTP requests.

to:

Initialize instances of the classes you're going to use. You're going to need the GSM, GPRS, GSMServer, and GSMClient classes.

Deleted line 60:

GSMServer server(80);

Changed lines 63-64 from:

Create some variables to set a timeout period, flag incoming client messages, and a buffer to read incoming bytes. to hold the server, path, and port you wish to connect to.

to:

Create some variables to store information you're going to need in the sketch; a char array to hold the URL, and some variables for setting timing intervals.

Changed lines 66-68 from:

char server[] = "arduino.cc"; char path[] = "/"; int port = 80;

to:

char server[] = "api.pachube.com";

unsigned long lastConnectionTime = 0; boolean lastConnected = false; const unsigned long postingInterval = 10*1000;

Changed lines 73-74 from:

In setup, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.

to:

In setup, open a serial connection to the computer.

Deleted line 77:
  Serial.println("Starting Arduino web client.");
Changed lines 88-89 from:

When the modem does connect nd had attached itself to the GPRS network, gsmAccess() will return GSM_READY. Use this as a flag to set the notConnected variable to true or false. Once connected, the remainder of setup will run.

to:

When the modem does connect and has attached itself to the GPRS network, gsmAccess() will return GSM_READY. Use this as a flag to set the notConnected variable to true or false. Once connected, sned a status message to the serial monitor and close setup.

Added lines 102-103:
  Serial.println("Connected to GPRS network");

}

Changed lines 106-107 from:

Attempt to connect to the server using client.connect(). connect() requires two arguments, the server and port. Once connected to the server, make a HTTP request by calling client.print(). A typical web request looks like "GET pathname HTTP/1.0". print will send the message, just as a browser would.

to:

You're going to use a custom function to make the HTTP connection to the server and send any data. Declare a function named sendData() that accepts a String as an argument.

Changed lines 109-116 from:
  if (client.connect(server, port))
to:
Changed lines 118-123 from:
    Serial.println("connected");
    client.print("GET ");
    client.print(path);
    client.println(" HTTP/1.0");
    client.println();
  } 
to:
    Serial.println("connecting...");
Changed lines 121-122 from:

If the connection to the server failed, make a note of it in the serial monitor, and close up setup.

to:

When connected, send a HTTP PUT request with the information about your feed. length() will return the length of the data you're sending.

Changed lines 124-128 from:
  else
  {
    Serial.println("connection failed");
  }

}

to:
    client.print("PUT /v2/feeds/");
    client.print(FEEDID);
    client.println(".csv HTTP/1.1");
    client.print("Host: api.pachube.com\n");
    client.print("X-ApiKey: ");
    client.println(APIKEY);
    client.print("User-Agent: ");
    client.println(USERAGENT);
    client.print("Content-Length: ");
    client.println(thisData.length());
Changed lines 136-137 from:

Inside loop, check to see if there are bytes returned from the server. If so, read them and print them to the serial monitor.

to:

Once the length has been sent, pass on the last pieces of the PUT request, and the data to the server.

Changed lines 139-143 from:
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
to:
    client.print("Content-Type: text/csv\n");
    client.println("Connection: close\n");
    client.println();

    client.println(thisData);
 }
Changed lines 148-149 from:

If the server disconnects, which it will typically do once it has completed the HTTP request, stop the client locally and close the loop.

to:

If the connection attempt was not successful, send a status message to the serial monitor, and disconnect from the server.

Changed line 151 from:
  if (!client.available() && !client.connected())
to:
  else
Added line 153:
    Serial.println("connection failed");
Deleted lines 156-159:
    // do nothing forevermore:
    for(;;)
      ;
Deleted line 157:

}

Changed lines 160-163 from:
to:

Update the time that the last connection, or attempt, happened before closing the function.

Changed lines 163-164 from:

/*

  Pachube sensor client
to:
Added lines 226-230:
Changed line 233 from:
to:
Changed lines 235-236 from:
 To make it work, create a feed with a datastream, and give it the ID
 sensor1. Or change the code below to match your feed.
to:
 To make it work, create a feed with two datastreams, and give them the IDs
 sensor1 and sensor2. Or change the code below to match your feed.
Added lines 238-240:
 This example uses the String library, which is part of the Arduino core from
 version 0019.  
Changed line 245 from:
 created 4 March 2012
to:
 created 8 March 2012
Changed line 256 from:

// Pachube Client data

to:

// Pachube data

Changed lines 265-269 from:
  1. define GPRS_APN "GPRS_APN" // replace your GPRS APN
  2. define GPRS_LOGIN "login" // replace with your GPRS login
  3. define GPRS_PASSWORD "password" // replace with your GPRS password

// initialize the library instance:

to:
  1. define GPRS_APN "GPRS_APN" // replace your GPRS APN
  2. define GPRS_LOGIN "login" // replace with your GPRS login
  3. define GPRS_PASSWORD "password" // replace with your GPRS password

// initialize the library instance

Changed lines 276-278 from:

//IPAddress server(216,52,233,121); // numeric IP for api.pachube.com char server[] = "api.pachube.com"; // name address for pachube API

to:

//IPAddress server(216,52,233,121); // numeric IP for api.pachube.com char server[] = "api.pachube.com"; // name address for Pachube API

Changed lines 281-282 from:

const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com

to:

const unsigned long postingInterval = 10*1000; // delay between updates to Pachube.com

Added lines 304-305:
  Serial.println("Connected to GPRS network");
Changed lines 309-312 from:

{

  // read the analog sensor:
  int sensorReading = analogRead(A0);   
to:

{

  // read the analog sensor
  int sensorReading = analogRead(A0); 

  // convert the data to a String to send it
  String dataString = "sensor1,";
  dataString += sensorReading;

  // you can append multiple readings to this String if your
  // pachube feed is set up to handle multiple values
  int otherSensorReading = analogRead(A1);
  dataString += "\nsensor2,";
  dataString += otherSensorReading;
Changed line 325 from:
  // purposes only:
to:
  // purposes only
Changed lines 328-329 from:
     char c = client.read();
     Serial.print(c);
to:
    char c = client.read();
    Serial.print(c);
Changed line 333 from:
  // through the loop, then stop the client:
to:
  // through the loop, then stop the client
Added lines 336-337:
    Serial.println();
    Serial.println("disconnecting.");
Changed line 340 from:
to:
Changed lines 342-343 from:
  // your last connection, then connect again and send data:
  if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval))
to:
  // your last connection, then connect again and send data
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval))
Changed line 345 from:
  sendData(sensorReading);
to:
    sendData(dataString);
Deleted line 346:
Changed line 348 from:
  // the loop:
to:
  // the loop
Changed lines 352-355 from:

/*

  This method makes a HTTP connection to the server.
  • /

void sendData(int thisData)

to:

// this method makes a HTTP connection to the server void sendData(String thisData)

Changed lines 370-376 from:
    // calculate the length of the sensor reading in bytes:
    // 8 bytes for "sensor1," + number of digits of the data:
    int thisLength = 8 + getLength(thisData);
    client.println(thisLength);

    // last pieces of the HTTP PUT request:
to:
    client.println(thisData.length());

    // last pieces of the HTTP PUT request
Changed line 374 from:
    client.println("Connection: close");
to:
    client.println("Connection: close\n");
Changed lines 377-378 from:
    // here's the actual content of the PUT request:
    client.print("sensor1,");
to:
    // here's the actual content of the PUT request
Changed line 382 from:
    // if you couldn't make a connection:
to:
    // if you couldn't make a connection
Changed line 388 from:
  // note the time that the connection was made or attempted
to:
  // note the time that the connection was made or attempted:
Deleted lines 390-415:

/*

  This method calculates the number of digits in the
  sensor reading.  Since each digit of the ASCII decimal
  representation is a byte, the number of digits equals
  the number of bytes.
  • /

int getLength(int someValue) {

  // there's at least one byte:
  int digits = 1;

  // continually divide the value by ten, 
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0)
  {
    dividend = dividend /10;
    digits++;
  }

  // return the number of digits:
  return digits;

}

February 03, 2013, at 07:23 PM by Scott Fitzgerald -
Changed lines 1-6 from:

Examples > Make Voice Call

Using the GSM Shield to Make Calls

This example shows how to use the GSM Shield and Library to make voice calls. Please click here for more information on the GSM library.

to:

Examples > GSM library

GSM Pachube Client String

A simple server that distributes any incoming messages to all connected clients. To use, open a terminal window, telnet to your devices IP address, and type away. Any incoming text will be sent to all connected clients (including the one typing). Additionally, you will be able to see the client's input in your serial monitor as well.

Not all network operators allow incoming data requests from outside their network. This means you can create a web server with the GSM shield, but you may not be able to connect to it from the public internet; only from another data enabled device from the same provider on the same network. You should check with your provider to see what specific policies they have in place regarding incoming data connections.

To use a data connection with the GSM shield, you'll need your provider's Access Point Name (APN), login, and password. To obtain this information, contact the network provider for the most up to date information. This page has some information about various carrier settings, but it may not be current.

Changed line 15 from:
  • SIM card
to:
  • SIM card with a data connection
Changed lines 27-29 from:

The code below is configured for use with a GSM/GPRS shield. In the variable declaration area, you need to instantiate both the GSM and GSMVoiceCall classes. Inside setup(), call GSM.begin(), with the PINNUMBER of your SIM card as parameter. GSM.begin() will return whether the shield managed to connect to the GSM network or not.

In the loop(), the input from the Serial Monitor is used to tell the GSM Shield a phone number to call to. The code waits until the call is over before performing any other actions.

to:
Changed lines 145-326 from:
to:
Changed line 332 from:
to:
January 30, 2013, at 10:53 AM by Scott Fitzgerald -
Added lines 1-34:

Share