Created
July 4, 2012 15:28
-
-
Save WassifAziz/3047895 to your computer and use it in GitHub Desktop.
Arduino GSM HTTP request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (c) Copyright 2010-2011 MCQN Ltd. | |
// Released under Apache License, version 2.0 | |
// | |
// Simple example to show how to use the HttpClient library | |
// Get's the web page given at http://<kHostname><kPath> and | |
// outputs the content to the serial port | |
// The example is adapted to run on the GSM/GPRS shield | |
// in asynchronous mode | |
// | |
// Circuit: | |
// * GSM shield attached | |
// | |
// libraries | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <SPI.h> | |
#include <HttpClient.h> | |
#include <b64.h> | |
#include <GSM3SMSService.h> | |
#include <GSM3ShieldV1AccessProvider.h> | |
#include <GSM3ShieldV1ClientProvider.h> | |
#include <GSM3ShieldV1SMSProvider.h> | |
#include <GSM3ShieldV1DataNetworkProvider.h> | |
#include <GSM3MobileClientService.h> | |
// initialize the gsm library instance | |
GSM3ShieldV1AccessProvider gsm; // include a 'true' parameter for debug enabled | |
GSM3MobileClientService client((bool)true); | |
GSM3ShieldV1ClientProvider s1client; | |
GSM3ShieldV1DataNetworkProvider gprs; | |
GSM3SMSService sms; | |
GSM3ShieldV1SMSProvider smsp; | |
//ip Address of the server you will connect to | |
int port = 80; | |
//The location to go to on the server | |
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is | |
String location = "/accounts/ HTTP/1.0"; | |
// APN data | |
#define GPRS_APN "mobile.o2.co.uk" // replace your GPRS APN | |
#define GPRS_LOGIN "web" // replace with your GPRS login | |
#define GPRS_PASSWORD "web" // replace with your GPRS password | |
// This example downloads the URL "http://arduino.cc/" | |
// Name of the server we want to connect to | |
byte server[] = { 74,125,224,72 }; | |
// Path to download (this is the bit after the hostname in the URL | |
// that you want to download | |
const char* kPath = "/accounts"; | |
// Number of milliseconds to wait without receiving any data before we give up | |
const int kNetworkTimeout = 30*1000; | |
// Number of milliseconds to wait if no data is available before trying again | |
const int kNetworkDelay = 1000; | |
// setup() changes against the Ethernet original example | |
void setup() | |
{ | |
// initialize serial communications | |
Serial.begin(9600); | |
// Asynchronous start GSM shield | |
// If your SIM has PIN, pass it as a parameter of begin() in quotes | |
// The second parameter is restart option | |
// The third parameter is zero because it means asynchronous mode | |
if(gsm.begin(0, 1, 0)) | |
while(gsm.ready()==0) | |
{ | |
// Your Code... | |
Serial.println("Waiting for GSM ready"); | |
delay(1000); | |
} | |
Serial.println("Connected to GSM network"); | |
// Asynchronous attach GPRS | |
gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD, 0); // The third parameter is zero because it means asynchronous mode | |
while(gprs.ready()==0) | |
{ | |
// Your code... | |
Serial.println("Waiting to GPRS attachment"); | |
delay(1000); | |
} | |
if (gprs.ready()==1) Serial.println("Attached"); | |
Serial.println("Connected to GPRS network"); | |
} | |
// ... but loop() does not change, and runs! | |
void loop(){ | |
String pageValue = connectAndRead(); //connect to the server and read the output | |
Serial.println(pageValue); //print out the findings. | |
delay(5000); //wait 5 seconds before connecting again | |
} | |
String connectAndRead(){ | |
//connect to the server | |
Serial.println("connecting..."); | |
//port 80 is typical of a www page | |
if (client.connect(server, port)) { | |
Serial.println("connected"); | |
client.print("GET "); | |
client.println(location); | |
client.println(); | |
//Connected - Read the page | |
Serial.println("wooo"); //go and read the output | |
} | |
else{ | |
return "connection failed"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I try to run this sample code on my GPRS Shield, it gets stuck in the "while(gsm.ready()==0)" loop
please help