1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-04 15:24:12 +01:00
Arduino/libraries/GSM/examples/GsmWebServer/GsmWebServer.ino

114 lines
2.7 KiB
Arduino
Raw Normal View History

2013-03-11 12:17:08 +01:00
/*
GSM Web Server
2013-03-11 12:17:08 +01:00
A simple web server that shows the value of the analog input pins.
using a GSM shield.
Circuit:
* GSM shield attached
* Analog inputs attached to pins A0 through A5 (optional)
2013-03-11 12:17:08 +01:00
created 8 Mar 2012
by Tom Igoe
*/
// libraries
#include <GSM.h>
// PIN Number
#define PINNUMBER ""
// APN data
#define GPRS_APN "GPRS_APN" // replace your GPRS APN
#define GPRS_LOGIN "login" // replace with your GPRS login
#define GPRS_PASSWORD "password" // replace with your GPRS password
// initialize the library instance
GPRS gprs;
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSMServer server(80); // port 80 (http default)
// timeout
const unsigned long __TIMEOUT__ = 10 * 1000;
2013-03-11 12:17:08 +01:00
void setup() {
2013-03-11 12:17:08 +01:00
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
2013-03-11 12:17:08 +01:00
// connection state
boolean notConnected = true;
2013-03-11 12:17:08 +01:00
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
2013-03-11 12:17:08 +01:00
notConnected = false;
} else {
2013-03-11 12:17:08 +01:00
Serial.println("Not connected");
delay(1000);
}
}
2013-03-11 12:17:08 +01:00
Serial.println("Connected to GPRS network");
2013-03-11 12:17:08 +01:00
// start server
server.begin();
2013-03-11 12:17:08 +01:00
//Get IP.
IPAddress LocalIP = gprs.getIPAddress();
Serial.println("Server IP address=");
Serial.println(LocalIP);
}
void loop() {
// listen for incoming clients
GSMClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
2013-03-11 12:17:08 +01:00
Serial.println("Receiving request!");
bool sendResponse = false;
while (char c = client.read()) {
if (c == '\n') {
sendResponse = true;
}
2013-03-11 12:17:08 +01:00
}
// if you've gotten to the end of the line (received a newline
// character)
if (sendResponse) {
2013-03-11 12:17:08 +01:00
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(analogRead(analogChannel));
client.println("<br />");
2013-03-11 12:17:08 +01:00
}
client.println("</html>");
//necessary delay
delay(1000);
client.stop();
}
}
}
}
}