CODE: temperature and motion – server

July 10, 2011 by

Look at this code, bishes – for the wiznet based ethernet shield.

LiquidCrystal Library - display() and noDisplay()
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
The circuit:
* LCD RX pin to digital pin d2
* temp sensor analog pins a2,a3,a4,a5
* Light Sensor analog pin a0 (TEMT6000)
* xbee tx d0
* xbee rx d1
* Ethernet shield attached to pins 10, 11, 12, 13
* led Pin D3
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 4 Sep 2010
by Tom Igoe
// include the library code:
//------------------Start web server Setup -mac and IP address---------
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10,0,0,174 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
//----------------- End Web Server Setup--------
//------------------Start i2c LCD backpack setup----------
// since the LCD does not send data back to the Arduino, we should only define the txPin
#define txPin 2 //pin used for i2c based LCD backpack
SoftwareSerial LCD = SoftwareSerial(2, txPin);
const int LCDdelay=10; // conservative, 2 actually works
//------------------End i2c LCD backpack setup
//-----------------Setup the Variables for sensors-------
int sensorPin1 = A0; // select the input pin for the light sensor
int sensorPin2 = A1; // select the input pin for the PIR sensor
int sensorValue1 = 0; // variable to store the value coming from the Light-sensor, default 0
int sensorValue2 = 0; // variable to store the value coming from the PIR-sensor, default 0
float tempF = 0; // variable to store the value coming from the temp sensor after converted to F degrees
const int ledpin3 = 3; //pin corresponding to pin 3 for LED
//-----------------End sensor variable madness------------
//-----------------Start LCD controlls-------------------
// wbp: goto with row & column
void goTo(int row, int col) {
LCD.print(0xFE, BYTE); //command flag
LCD.print((col + row*64 + 128), BYTE); //position
void clearLCD(){
LCD.print(0xFE, BYTE); //command flag
LCD.print(0x01, BYTE); //clear command.
void backlightOn() { //turns on the backlight
LCD.print(0x7C, BYTE); //command flag for backlight stuff
LCD.print(157, BYTE); //light level.
void backlightOff(){ //turns off the backlight
LCD.print(0x7C, BYTE); //command flag for backlight stuff
LCD.print(128, BYTE); //light level for off.
void serCommand(){ //a general function to call the command flag for issuing all other commands
LCD.print(0xFE, BYTE);
//---------------------End LCD controlls-----------------
//---------------------Begin LCD setup -----------------
void setup() {
pinMode(txPin, OUTPUT); //sets up Pint as output to LCD
//--------------------End LCD Setup-----------------------
//---------------------Begin LED Output-------------------
pinMode(3, OUTPUT); //setup pin for digital out - pin 3
//---------------------End led Output---------------------
//------------------Begin Web SErver Setup---------------
//void setup()
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
//------------------End Web SErver Setup----------------
//-------------------Begin Sensor read and LCD control----------------------
void loop() {
// read the value from the sensors:
sensorValue1 = analogRead(sensorPin1); //light sensor
sensorValue2 = analogRead(sensorPin2); //PIR
LibTemperature temp = LibTemperature(0); // reads temperature in Celcius
// convert to degF
tempF = (temp.GetTemperature() * 9 / 5) + 32;
//stores float to int because serlcd wont send floats
int tempfd = tempF;
// Print sensor value to the LCD.
LCD.print(" ");
LCD.print(" ");
LCD.print("Temp F");
//Iluminate LED if motion.
digitalWrite(3, HIGH); // set the LED on
{if (sensorValue2 < 10) {
// no motion
digitalWrite(ledpin3, LOW); // set the LED off
else {
// motion
digitalWrite(ledpin3, HIGH); // set the LED ON
//-----------------End Sensor read and LCD control------------------------
//-----------------Web Server code---------------------------
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char 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");
// output the value of each analog input pin
//for (int analogChannel = 0; analogChannel < 1; analogChannel++) {
int analogChannel = 0; //select channell 1
client.print("Light Level from channel ");
client.print(" is ");
client.print("Temperature is ");
client.print(" Degrees F");
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
// close the connection:
//--------------------End web server code ----------------------