[175] | 1 | /*
|
---|
| 2 | GSM Web Server
|
---|
| 3 |
|
---|
| 4 | A simple web server that shows the value of the analog input pins.
|
---|
| 5 | using a GSM shield.
|
---|
| 6 |
|
---|
| 7 | Circuit:
|
---|
| 8 | * GSM shield attached
|
---|
| 9 | * Analog inputs attached to pins A0 through A5 (optional)
|
---|
| 10 |
|
---|
| 11 | ATTENTION: to work correctly with M0/M0 pro you have to connect pin 2 to pin 4 of the shield
|
---|
| 12 |
|
---|
| 13 | created 8 Mar 2012
|
---|
| 14 | by Tom Igoe
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | // libraries
|
---|
| 18 | #include <GSM.h>
|
---|
| 19 |
|
---|
| 20 | // PIN Number
|
---|
| 21 | #define PINNUMBER ""
|
---|
| 22 |
|
---|
| 23 | // APN data
|
---|
| 24 | #define GPRS_APN "GPRS_APN" // replace your GPRS APN
|
---|
| 25 | #define GPRS_LOGIN "login" // replace with your GPRS login
|
---|
| 26 | #define GPRS_PASSWORD "password" // replace with your GPRS password
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | // initialize the library instance
|
---|
| 30 | GPRS gprs;
|
---|
| 31 | GSM gsmAccess; // include a 'true' parameter for debug enabled
|
---|
| 32 | GSMServer server(80); // port 80 (http default)
|
---|
| 33 |
|
---|
| 34 | // timeout
|
---|
| 35 | const unsigned long __TIMEOUT__ = 10 * 1000;
|
---|
| 36 |
|
---|
| 37 | void setup()
|
---|
| 38 | {
|
---|
| 39 | // initialize serial communications and wait for port to open:
|
---|
| 40 | SerialUSB.begin(9600);
|
---|
| 41 | while (!SerialUSB) {
|
---|
| 42 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | // connection state
|
---|
| 46 | boolean notConnected = true;
|
---|
| 47 |
|
---|
| 48 | // Start GSM shield
|
---|
| 49 | // If your SIM has PIN, pass it as a parameter of begin() in quotes
|
---|
| 50 | while (notConnected)
|
---|
| 51 | {
|
---|
| 52 | if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
|
---|
| 53 | (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY))
|
---|
| 54 | notConnected = false;
|
---|
| 55 | else
|
---|
| 56 | {
|
---|
| 57 | SerialUSB.println("Not connected");
|
---|
| 58 | delay(1000);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | SerialUSB.println("Connected to GPRS network");
|
---|
| 63 |
|
---|
| 64 | // start server
|
---|
| 65 | server.begin();
|
---|
| 66 |
|
---|
| 67 | //Get IP.
|
---|
| 68 | IPAddress LocalIP = gprs.getIPAddress();
|
---|
| 69 | SerialUSB.println("Server IP address=");
|
---|
| 70 | SerialUSB.println(LocalIP);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void loop() {
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | // listen for incoming clients
|
---|
| 77 | GSMClient client = server.available();
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | if (client)
|
---|
| 82 | {
|
---|
| 83 | while (client.connected())
|
---|
| 84 | {
|
---|
| 85 | if (client.available())
|
---|
| 86 | {
|
---|
| 87 | SerialUSB.println("Receiving request!");
|
---|
| 88 | bool sendResponse = false;
|
---|
| 89 | while (char c = client.read()) {
|
---|
| 90 | if (c == '\n') sendResponse = true;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | // if you've gotten to the end of the line (received a newline
|
---|
| 94 | // character)
|
---|
| 95 | if (sendResponse)
|
---|
| 96 | {
|
---|
| 97 | // send a standard http response header
|
---|
| 98 | client.println("HTTP/1.1 200 OK");
|
---|
| 99 | client.println("Content-Type: text/html");
|
---|
| 100 | client.println();
|
---|
| 101 | client.println("<html>");
|
---|
| 102 | // output the value of each analog input pin
|
---|
| 103 | for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
|
---|
| 104 | client.print("analog input ");
|
---|
| 105 | client.print(analogChannel);
|
---|
| 106 | client.print(" is ");
|
---|
| 107 | client.print(analogRead(analogChannel));
|
---|
| 108 | client.println("<br />");
|
---|
| 109 | }
|
---|
| 110 | client.println("</html>");
|
---|
| 111 | //necessary delay
|
---|
| 112 | delay(1000);
|
---|
| 113 | client.stop();
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|