Showing posts with label Arduino Ethernet Shield. Show all posts
Showing posts with label Arduino Ethernet Shield. Show all posts

Friday, December 25, 2015

Arduino Ethernet Shield and Xbee

This summer I worked with the folks at the Chattanooga Public Library's 4th Floor Lab and we've now got a basic sketch that integrates the Arduino Ethernet Shield with XBee Communication.  I've searched high and low for someone to post an Arduino sketch that integrates them, but I haven't found one.  Well, if you too have been looking, here it is!  I used this some a few months ago to post some sensor data that was being logged several hundred feet away in the field and sent via XBee 1 to XBee 2 stacked with the Arduino Ethernet Shield.  There's plenty of html code changes you can add or change to make your 1990's-looking webpage better.  Hope it's useful!  Merry Christmas!

*/
#include <SPI.h>
#include <Ethernet.h>
#include <SoftwareSerial.h>


// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)


// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)

SoftwareSerial XBee(2, 3); // RX, TXint ReadingNumber=0;

char link[]="wirelessearthwatchdawgs.blogspot.com"; //link data

// Enter a MAC address and IP address for your controller below.


// The IP address will be dependent on your local network:

byte mac[] = {

   0x90, 0xA2, 0xDA, 0x0D, 0xA8, 0x6B

};

IPAddress ip(192,168,1,120

);

//IPAddress ip(172, 16, 100, 120);


// Initialize the Ethernet server library


// with the IP address and port you want to use


// (port 80 is default for HTTP):

EthernetServer server(8081);


void setup() {

  // Open serial communications and wait for port to open:

  Serial.begin(1200);

   XBee.begin(1200);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for Leonardo only

  }

  // start the Ethernet connection and the server:

  Ethernet.begin(mac, ip);

  server.begin();

  Serial.print("server is at ");

  Serial.println(Ethernet.localIP());

}

void loop() {


  // listen for incoming clients

  EthernetClient client = server.available();

  if (client) {

    Serial.println("new client");

    // an http request ends with a blank line

    boolean currentLineIsBlank = true;

    while (client.connected()) {

      if (client.available()) {

        char c = client.read();

        Serial.write(c);

        // if you've gotten to the end of the line (received a newline

        // character) and the line is blank, the http request has ended,

        // so you can send a reply

        if (c == '\n' && currentLineIsBlank) {

          // send a standard http response header

          client.println("HTTP/1.1 200 OK");

          client.println("Content-Type: text/html");

          client.println("Connection: close");  // the connection will be closed after completion of the response

          client.println("Refresh: 5");  // refresh the page automatically every 5 sec

          client.println();

          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println();

          //set background to yellow

          client.print("<body style=background-color:yellow>");

          //send first heading

          client.println("<font color='red'><h1>My Web Title</font></h1>");

          client.println("<hr />");

          client.println("<hr />");

          //output some sample data to browser

          client.println("<font color='blue' size='5'>Sample data:w/m2");


          // output the value of each analog input pin

          boolean xbeenewline = false; // initialize boolean variable to keep track if a new line has been seen.

          while (xbeenewline == false) {          //when this is false; a new line hasn't been seen; 

            char c = XBee.read ();        // render it as characters, rather than as integers.

            if (c == '\n') {          // if c hits the ASCII symbol for new line

              xbeenewline = true;   // it's true that the server sees a new line from the xbee feed

              boolean newxbeenewline = false;  // initialize a variable that will serve as the recognition of the end of a line of data

              while (newxbeenewline == false) {  // keep reading data, we arent' at the end of the line yet

                char c = XBee.read();

                if (c == '\n') {            // but when we see the symbol for end of line

                  newxbeenewline = true;    // we're done reading data

                } else if (c != -1) {       // -1 is the value for nothing sent by xbee, so don't print it

                 

                  client.print(c);

                  delay (1);

                }

              }


                client.println ("<br/>");                                    //(“<br />”);

          client.println ("<hr/>");   

      

          client.println ("<font color='blue' size='5'>Link: ");         //(“<font color=’blue’ size=’5′>Link: “);

          client.print ("<a href=");            //(“<a href=”);

          client.print(link);

          client.println(  ">Visit our Project Site!</a>");          //(“>Visit Scienceprog!</a>”);

          client.println ("<br/>");                                    //(“<br />”);

          client.println ("<hr/>");                             //(“<hr />”);

            }

          }


          client.println("</html>");

          break;

        }

        if (c == '\n') {

          // you're starting a new line

          currentLineIsBlank = true;

        }

        else if (c != '\r') {

          // you've gotten a character on the current line

          currentLineIsBlank = false;

        }

      }

    }

    // give the web browser time to receive the data

    delay(1);

    // close the connection:

    client.stop();

    Serial.println("client disconnected");

  }

}