Tutorial.GSMExamplesPachubeClient History

Hide minor edits - Show changes to markup

November 08, 2013, at 04:26 PM by Scott Fitzgerald -
Changed lines 1-445 from:
to:
March 11, 2013, at 10:35 AM by Scott Fitzgerald -
Changed lines 432-445 from:
to:
February 04, 2013, at 07:40 AM by Scott Fitzgerald -
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, you can close setup.

Added line 102:

}

Changed lines 105-106 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 some custom functions in this sketch. The first will make a HTTP connection to the server and send any data. Declare a function named sendData() that accepts an argument of type int

Changed lines 108-115 from:
  if (client.connect(server, port))
to:
Changed lines 117-122 from:
    Serial.println("connected");
    client.print("GET ");
    client.print(path);
    client.println(" HTTP/1.0");
    client.println();
  } 
to:
    Serial.println("connecting...");
Changed lines 120-121 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

Changed lines 123-127 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: ");
Changed lines 134-135 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:

You'll need to calculate the length of the data you're sending. The length will be 8 bytes for the term "sensor1," plus the number of digits of the data. You'll create a new function named getLength() in a moment to handle this information. Once calculated, send the length to the server, and the last pieces of the PUT.

Last, send the data to the server.

Changed lines 139-143 from:
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
to:
    int thisLength = 8 + getLength(thisData);
    client.println(thisLength);

    client.print("Content-Type: text/csv\n");
    client.println("Connection: close");
    client.println();

    client.print("sensor1,");
    client.println(thisData);
 }
Changed lines 151-152 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 154 from:
  if (!client.available() && !client.connected())
to:
  else
Added line 156:
    Serial.println("connection failed");
Deleted lines 159-162:
    // do nothing forevermore:
    for(;;)
      ;
Deleted line 160:

}

Changed lines 163-166 from:
to:

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

Changed lines 166-167 from:

/*

  Pachube sensor client
to:
Changed lines 181-240 from:
 This sketch connects an analog sensor to Pachube (http://www.pachube.com)
 using a Telefonica GSM/GPRS shield.

 This example has been updated to use version 2.0 of the Pachube.com API. 
 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.

 Circuit:
 * Analog sensor attached to analog in 0
 * GSM shield attached to pins 10, 11, 12, 13

 created 4 March 2012
 by Tom Igoe
 and adapted for GSM shield by David Del Peral

 This code is in the public domain.

 */

// libraries

  1. include <GSM.h>

// Pachube Client data

  1. define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
  2. define FEEDID 00000 // replace your feed ID
  3. define USERAGENT "My Project" // user agent is the project name

// PIN Number

  1. define PINNUMBER ""

// APN data

  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: GSMClient client; GPRS gprs; GSM gsmAccess; // include a 'true' parameter for debug enabled

// if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: //IPAddress server(216,52,233,121); // numeric IP for api.pachube.com char server[] = "api.pachube.com"; // name address for pachube API

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com

void setup() {

  // initialize serial communications
  Serial.begin(9600);

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
to:
Changed lines 187-194 from:
    if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
to:
    dividend = dividend /10;
    digits++;
Added lines 190-195:
Changed lines 197-205 from:

void loop() {

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

  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
to:
Changed lines 214-216 from:
  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
to:
Changed lines 223-225 from:
  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
to:
Changed lines 232-234 from:
  // store the state of the connection for next time through
  // the loop:
to:
Changed lines 239-245 from:
to:
Changed lines 247-249 from:
  This method makes a HTTP connection to the server.
  • /

void sendData(int thisData)

to:
  Pachube sensor client

 This sketch connects an analog sensor to Pachube (http://www.pachube.com)
 using a Telefonica GSM/GPRS shield.

 This example has been updated to use version 2.0 of the Pachube.com API. 
 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.

 Circuit:
 * Analog sensor attached to analog in 0
 * GSM shield attached to pins 10, 11, 12, 13

 created 4 March 2012
 by Tom Igoe
 and adapted for GSM shield by David Del Peral

 This code is in the public domain.

 */

// libraries

  1. include <GSM.h>

// Pachube Client data

  1. define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
  2. define FEEDID 00000 // replace your feed ID
  3. define USERAGENT "My Project" // user agent is the project name

// PIN Number

  1. define PINNUMBER ""

// APN data

  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: GSMClient client; GPRS gprs; GSM gsmAccess; // include a 'true' parameter for debug enabled

// if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: //IPAddress server(216,52,233,121); // numeric IP for api.pachube.com char server[] = "api.pachube.com"; // name address for pachube API

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com

void setup()

Changed lines 300-301 from:
  // if there's a successful connection:
  if (client.connect(server, 80))
to:
  // initialize serial communications
  Serial.begin(9600);

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
Changed lines 310-343 from:
    Serial.println("connecting...");

    // send the HTTP PUT request:
    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: ");

    // 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:
    client.print("Content-Type: text/csv\n");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.print("sensor1,");
    client.println(thisData);
  } 
  else
  {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
to:
    if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
Deleted lines 318-319:
  // note the time that the connection was made or attempted
  lastConnectionTime = millis();
Changed lines 321-336 from:

/*

  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)
to:

void loop() {

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

  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available())
Changed lines 331-332 from:
    dividend = dividend /10;
    digits++;
to:
     char c = client.read();
     Serial.print(c);
Added lines 334-340:
  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected)
  {
    client.stop();
  }
Changed lines 342-343 from:
  // return the number of digits:
  return digits;
to:
  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval))
  {
  sendData(sensorReading);
  }

  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
Added lines 354-425:

/*

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

void sendData(int thisData) {

  // if there's a successful connection:
  if (client.connect(server, 80))
  {
    Serial.println("connecting...");

    // send the HTTP PUT request:
    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: ");

    // 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:
    client.print("Content-Type: text/csv\n");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.print("sensor1,");
    client.println(thisData);
  } 
  else
  {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  // note the time that the connection was made or attempted
  lastConnectionTime = millis();

}

/*

  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 04, 2013, at 06:57 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. 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 sensor attached to A0

Software Required

  • pachube.com account
  • pachube.com feed that accepts two data items
Added lines 35-42:
February 03, 2013, at 07:18 AM by Scott Fitzgerald -
Changed lines 3-6 from:

GSM Web Client

This sketch connects an Arduino to the Arduino homepage, http://arduino.cc, through a GSM shield. It then prints the content of the page through the serial monitor.

to:

GSM Pachube Client

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 lines 43-44 from:

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

to:

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.

Added line 49:

GSMServer server(80);

Changed lines 52-53 from:

Create some variables to hold the server, path, and port you wish to connect to.

to:

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.

Changed line 147 from:
 GSM Web client
to:
  Pachube sensor client
Changed lines 149-154 from:
 This sketch connects to a website using a GSM shield.
to:
 This sketch connects an analog sensor to Pachube (http://www.pachube.com)
 using a Telefonica GSM/GPRS shield.

 This example has been updated to use version 2.0 of the Pachube.com API. 
 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.
Changed lines 157-158 from:
 * GSM shield attached
to:
 * Analog sensor attached to analog in 0
 * GSM shield attached to pins 10, 11, 12, 13
Changed line 160 from:
 created 8 Mar 2012
to:
 created 4 March 2012
Added lines 162-165:
 and adapted for GSM shield by David Del Peral

 This code is in the public domain.
Added lines 171-175:

// Pachube Client data

  1. define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
  2. define FEEDID 00000 // replace your feed ID
  3. define USERAGENT "My Project" // user agent is the project name
Changed lines 180-184 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 189-195 from:

// This example downloads the URL "http://arduino.cc/"

// URL, path & port (for example: arduino.cc) char server[] = "arduino.cc"; char path[] = "/"; int port = 80; // 80 for HTTP

to:

// if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: //IPAddress server(216,52,233,121); // numeric IP for api.pachube.com char server[] = "api.pachube.com"; // name address for pachube API

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com

Changed line 202 from:
  Serial.println("Starting Arduino web client.");
to:
Changed lines 219-223 from:
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port))
to:

}

void loop() {

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

  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available())
Changed lines 231-241 from:
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET ");
    client.print(path);
    client.println(" HTTP/1.0");
    client.println();
  } 
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
to:
     char c = client.read();
     Serial.print(c);
Changed lines 234-240 from:

}

void loop() {

  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available())
to:
  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected)
Changed lines 239-240 from:
    char c = client.read();
    Serial.print(c);
to:
    client.stop();
Changed lines 242-243 from:
  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected())
to:
  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval))
Changed lines 246-252 from:
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
to:
  sendData(sensorReading);
Added lines 248-251:
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
Added lines 253-325:

/*

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

void sendData(int thisData) {

  // if there's a successful connection:
  if (client.connect(server, 80))
  {
    Serial.println("connecting...");

    // send the HTTP PUT request:
    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: ");

    // 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:
    client.print("Content-Type: text/csv\n");
    client.println("Connection: close");
    client.println();

    // here's the actual content of the PUT request:
    client.print("sensor1,");
    client.println(thisData);
  } 
  else
  {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  // note the time that the connection was made or attempted
  lastConnectionTime = millis();

}

/*

  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 02, 2013, at 11:04 AM by Scott Fitzgerald -
Added lines 1-245:

Share