[136] | 1 | /*
|
---|
| 2 |
|
---|
| 3 | This sketch test the GSM shield's ability to connect to a
|
---|
| 4 | GPERS network. It asks for APN information through the
|
---|
| 5 | serial monitor and tries to connect to arduino.cc.
|
---|
| 6 |
|
---|
| 7 | Circuit:
|
---|
| 8 | * GSM shield attached
|
---|
| 9 | * SIM card with data plan
|
---|
| 10 |
|
---|
| 11 | Created 18 Jun 2012
|
---|
| 12 | by David del Peral
|
---|
| 13 |
|
---|
| 14 | This example code is part of the public domain
|
---|
| 15 |
|
---|
| 16 | http://arduino.cc/en/Tutorial/GSMToolsTestGPRS
|
---|
| 17 |
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | // libraries
|
---|
| 21 | #include <GSM.h>
|
---|
| 22 |
|
---|
| 23 | // PIN Number
|
---|
| 24 | #define PINNUMBER ""
|
---|
| 25 |
|
---|
| 26 | // initialize the library instance
|
---|
| 27 | GSM gsmAccess; // GSM access: include a 'true' parameter for debug enabled
|
---|
| 28 | GPRS gprsAccess; // GPRS access
|
---|
| 29 | GSMClient client; // Client service for TCP connection
|
---|
| 30 |
|
---|
| 31 | // messages for serial monitor response
|
---|
| 32 | String oktext = "OK";
|
---|
| 33 | String errortext = "ERROR";
|
---|
| 34 |
|
---|
| 35 | // URL and path (for example: arduino.cc)
|
---|
| 36 | char url[] = "arduino.cc";
|
---|
| 37 | char urlproxy[] = "http://arduino.cc";
|
---|
| 38 | char path[] = "/";
|
---|
| 39 |
|
---|
| 40 | // variable for save response obtained
|
---|
| 41 | String response = "";
|
---|
| 42 |
|
---|
| 43 | // use a proxy
|
---|
| 44 | boolean use_proxy = false;
|
---|
| 45 |
|
---|
| 46 | void setup()
|
---|
| 47 | {
|
---|
| 48 | // initialize serial communications and wait for port to open:
|
---|
| 49 | Serial.begin(9600);
|
---|
| 50 | while (!Serial) {
|
---|
| 51 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void loop()
|
---|
| 56 | {
|
---|
| 57 | use_proxy = false;
|
---|
| 58 |
|
---|
| 59 | // start GSM shield
|
---|
| 60 | // if your SIM has PIN, pass it as a parameter of begin() in quotes
|
---|
| 61 | Serial.print("Connecting GSM network...");
|
---|
| 62 | if (gsmAccess.begin(PINNUMBER) != GSM_READY)
|
---|
| 63 | {
|
---|
| 64 | Serial.println(errortext);
|
---|
| 65 | while (true);
|
---|
| 66 | }
|
---|
| 67 | Serial.println(oktext);
|
---|
| 68 |
|
---|
| 69 | // read APN introduced by user
|
---|
| 70 | char apn[50];
|
---|
| 71 | Serial.print("Enter your APN: ");
|
---|
| 72 | readSerial(apn);
|
---|
| 73 | Serial.println(apn);
|
---|
| 74 |
|
---|
| 75 | // Read APN login introduced by user
|
---|
| 76 | char login[50];
|
---|
| 77 | Serial.print("Now, enter your login: ");
|
---|
| 78 | readSerial(login);
|
---|
| 79 | Serial.println(login);
|
---|
| 80 |
|
---|
| 81 | // read APN password introduced by user
|
---|
| 82 | char password[20];
|
---|
| 83 | Serial.print("Finally, enter your password: ");
|
---|
| 84 | readSerial(password);
|
---|
| 85 |
|
---|
| 86 | // attach GPRS
|
---|
| 87 | Serial.println("Attaching to GPRS with your APN...");
|
---|
| 88 | if (gprsAccess.attachGPRS(apn, login, password) != GPRS_READY)
|
---|
| 89 | {
|
---|
| 90 | Serial.println(errortext);
|
---|
| 91 | }
|
---|
| 92 | else {
|
---|
| 93 |
|
---|
| 94 | Serial.println(oktext);
|
---|
| 95 |
|
---|
| 96 | // read proxy introduced by user
|
---|
| 97 | char proxy[100];
|
---|
| 98 | Serial.print("If your carrier uses a proxy, enter it, if not press enter: ");
|
---|
| 99 | readSerial(proxy);
|
---|
| 100 | Serial.println(proxy);
|
---|
| 101 |
|
---|
| 102 | // if user introduced a proxy, asks him for proxy port
|
---|
| 103 | int pport;
|
---|
| 104 | if (proxy[0] != '\0') {
|
---|
| 105 | // read proxy port introduced by user
|
---|
| 106 | char proxyport[10];
|
---|
| 107 | Serial.print("Enter the proxy port: ");
|
---|
| 108 | readSerial(proxyport);
|
---|
| 109 | // cast proxy port introduced to integer
|
---|
| 110 | pport = (int) proxyport;
|
---|
| 111 | use_proxy = true;
|
---|
| 112 | Serial.println(proxyport);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | // connection with arduino.cc and realize HTTP request
|
---|
| 116 | Serial.print("Connecting and sending GET request to arduino.cc...");
|
---|
| 117 | int res_connect;
|
---|
| 118 |
|
---|
| 119 | // if use a proxy, connect with it
|
---|
| 120 | if (use_proxy)
|
---|
| 121 | res_connect = client.connect(proxy, pport);
|
---|
| 122 | else
|
---|
| 123 | res_connect = client.connect(url, 80);
|
---|
| 124 |
|
---|
| 125 | if (res_connect)
|
---|
| 126 | {
|
---|
| 127 | // make a HTTP 1.0 GET request (client sends the request)
|
---|
| 128 | client.print("GET ");
|
---|
| 129 |
|
---|
| 130 | // if use a proxy, the path is arduino.cc URL
|
---|
| 131 | if (use_proxy)
|
---|
| 132 | client.print(urlproxy);
|
---|
| 133 | else
|
---|
| 134 | client.print(path);
|
---|
| 135 |
|
---|
| 136 | client.println(" HTTP/1.0");
|
---|
| 137 | client.println();
|
---|
| 138 | Serial.println(oktext);
|
---|
| 139 | }
|
---|
| 140 | else
|
---|
| 141 | {
|
---|
| 142 | // if you didn't get a connection to the server
|
---|
| 143 | Serial.println(errortext);
|
---|
| 144 | }
|
---|
| 145 | Serial.print("Receiving response...");
|
---|
| 146 |
|
---|
| 147 | boolean test = true;
|
---|
| 148 | while (test)
|
---|
| 149 | {
|
---|
| 150 | // if there are incoming bytes available
|
---|
| 151 | // from the server, read and check them
|
---|
| 152 | if (client.available())
|
---|
| 153 | {
|
---|
| 154 | char c = client.read();
|
---|
| 155 | response += c;
|
---|
| 156 |
|
---|
| 157 | // cast response obtained from string to char array
|
---|
| 158 | char responsechar[response.length() + 1];
|
---|
| 159 | response.toCharArray(responsechar, response.length() + 1);
|
---|
| 160 |
|
---|
| 161 | // if response includes a "200 OK" substring
|
---|
| 162 | if (strstr(responsechar, "200 OK") != NULL) {
|
---|
| 163 | Serial.println(oktext);
|
---|
| 164 | Serial.println("TEST COMPLETE!");
|
---|
| 165 | test = false;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | // if the server's disconnected, stop the client:
|
---|
| 170 | if (!client.connected())
|
---|
| 171 | {
|
---|
| 172 | Serial.println();
|
---|
| 173 | Serial.println("disconnecting.");
|
---|
| 174 | client.stop();
|
---|
| 175 | test = false;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /*
|
---|
| 182 | Read input serial
|
---|
| 183 | */
|
---|
| 184 | int readSerial(char result[])
|
---|
| 185 | {
|
---|
| 186 | int i = 0;
|
---|
| 187 | while (1)
|
---|
| 188 | {
|
---|
| 189 | while (Serial.available() > 0)
|
---|
| 190 | {
|
---|
| 191 | char inChar = Serial.read();
|
---|
| 192 | if (inChar == '\n')
|
---|
| 193 | {
|
---|
| 194 | result[i] = '\0';
|
---|
| 195 | return 0;
|
---|
| 196 | }
|
---|
| 197 | if (inChar != '\r')
|
---|
| 198 | {
|
---|
| 199 | result[i] = inChar;
|
---|
| 200 | i++;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|