Go Down

Topic: GSM/GPRS shield example for xively? (Read 2774 times) previous topic - next topic

pietkuip

Aug 02, 2013, 11:08 pm Last Edit: Aug 04, 2013, 11:55 am by pietkuip Reason: 1
Could anybody please provide me with a basic sketch that works with the official GSM/GPRS shield and Xively?

The 1.0.5 IDE contains example programs for pachube, which was replaced by Cosm, and now by Xively. And I have had no luck in updating the old code. And there is no example program on the Xively site either.

PS: I got it working. I will post the code here later today (needs some tidying up).

pietkuip

Ok, here is code that works for me. But I did not implement controlling the LED from Xively.

Code: [Select]
/*
GSM Xively client

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

This example has been updated to use the Xively.com API.
To make it work, create a feed with a datastream, or
change the code below to match your feed.

Circuit:
* Analog sensor attached to analog in 0
* GSM shield attached to an Arduino
* SIM card with a data plan

Merged by Pieter Kuiper in August 2013 from
https://xively.com/dev/tutorials/arduino_wi-fi/
http://arduino.cc/en/Tutorial/GSMExamplesPachubeClient
*/

// libraries
#include <GSM.h>
#include <HttpClient.h>
#include <Xively.h>

// Your Xively key to let you upload data
char xivelyKey[] = "tqXVm5egTqFwZNYGAenaJFS12gjY5AHI4pFlJs64s7A0q9Hd";  // replace your xively api key here
//your xively feed ID
#define xivelyFeed 1426183629

// PIN Number
#define PINNUMBER ""

// Analog pin which we're monitoring
#define sensorPin A0
//led connected pin controlled from Xively (not implemented in this version)
#define ledPin 9

// APN data (access point name)
#define GPRS_APN       "internet.tele2.se" // replace with the APN of the GPRS provider of your SIM card
#define GPRS_LOGIN     "" // empty on bluevia, replace with your GPRS login if needed
#define GPRS_PASSWORD  "" // empty on bluevia, replace with your GPRS password if needed


// initialize the library instance:
GSMClient client;
GPRS gprs;
GSM gsmAccess;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server
char server[] = "api.xively.com";      // name address for xively 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

//datastreams
char sensorID[] = "Light";
char ledID[] = "LED";

// Define the strings for our datastream IDs
XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorID, strlen(sensorID), DATASTREAM_FLOAT),
  XivelyDatastream(ledID, strlen(ledID), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(xivelyFeed, datastreams, 2 /* number of datastreams */);

XivelyClient xivelyclient(client);

void setup()
{
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  Serial.println("Starting Xively client.");
  // connection state
  boolean notConnected = true;
 
  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while(notConnected)
  {
    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);
    }
  }
  pinMode(sensorPin, INPUT);
}

void loop()

  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available())
  {
     char c = client.read();
     Serial.print(c);
  }

//read sensor values
  int sensorValue = analogRead(sensorPin);
  datastreams[0].setFloat(sensorValue);
 
  //print the sensor valye
  Serial.print("Read sensor value ");
  Serial.println(datastreams[0].getFloat());

  //send value to xively
  Serial.println("Uploading it to Xively");
  int ret = xivelyclient.put(feed, xivelyKey);
  //return message
  Serial.print("xivelyclient.put returned ");
  Serial.println(ret);
  Serial.println("");
 
 
  //delay between calls
  delay(15000); 
}


KraakBal

Would have registered soon anyway, but did it now to say thanks!  XD

shermannyp


pifouni

Hi,

very interresting but how did you manage to create a free personnal account on Xively ?

Thank you in advance

KASSIMSAMJI

Ok, here is code that works for me. But I did not implement controlling the LED from Xively.

Code: [Select]
/*
 GSM Xively client
 
 This sketch connects an analog sensor to Xively (http://www.xively.com)
 using a Telefonica GSM/GPRS shield.

 This example has been updated to use the Xively.com API.
 To make it work, create a feed with a datastream, or
 change the code below to match your feed.
 
 Circuit:
 * Analog sensor attached to analog in 0
 * GSM shield attached to an Arduino
 * SIM card with a data plan
 
 Merged by Pieter Kuiper in August 2013 from
 https://xively.com/dev/tutorials/arduino_wi-fi/
 http://arduino.cc/en/Tutorial/GSMExamplesPachubeClient
 */

// libraries
#include <GSM.h>
#include <HttpClient.h>
#include <Xively.h>

// Your Xively key to let you upload data
char xivelyKey[] = "tqXVm5egTqFwZNYGAenaJFS12gjY5AHI4pFlJs64s7A0q9Hd";  // replace your xively api key here
//your xively feed ID
#define xivelyFeed 1426183629

// PIN Number
#define PINNUMBER ""

// Analog pin which we're monitoring
#define sensorPin A0
//led connected pin controlled from Xively (not implemented in this version)
#define ledPin 9

// APN data (access point name)
#define GPRS_APN       "internet.tele2.se" // replace with the APN of the GPRS provider of your SIM card
#define GPRS_LOGIN     "" // empty on bluevia, replace with your GPRS login if needed
#define GPRS_PASSWORD  "" // empty on bluevia, replace with your GPRS password if needed


// initialize the library instance:
GSMClient client;
GPRS gprs;
GSM gsmAccess;

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server
char server[] = "api.xively.com";      // name address for xively 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

//datastreams
char sensorID[] = "Light";
char ledID[] = "LED";

// Define the strings for our datastream IDs
XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorID, strlen(sensorID), DATASTREAM_FLOAT),
  XivelyDatastream(ledID, strlen(ledID), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(xivelyFeed, datastreams, 2 /* number of datastreams */);
 
XivelyClient xivelyclient(client);

void setup()
{
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  Serial.println("Starting Xively client.");
  // connection state
  boolean notConnected = true;
 
  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while(notConnected)
  {
    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);
    }
  }
  pinMode(sensorPin, INPUT);
}

void loop()

  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available())
  {
     char c = client.read();
     Serial.print(c);
  }

//read sensor values
  int sensorValue = analogRead(sensorPin);
  datastreams[0].setFloat(sensorValue);
 
  //print the sensor valye
  Serial.print("Read sensor value ");
  Serial.println(datastreams[0].getFloat());
 
  //send value to xively
  Serial.println("Uploading it to Xively");
  int ret = xivelyclient.put(feed, xivelyKey);
  //return message
  Serial.print("xivelyclient.put returned ");
  Serial.println(ret);
  Serial.println("");
 
 
  //delay between calls
  delay(15000); 
}


hello...how cai i control the led from xively..thank you

Go Up