*My first real post so forgive me - I couldn't decide if this should go here or the networking forum!
Firstly, let me explain, I have a webserver at home that is setup with some code I wrote to allow me to control my lights with a web call. For example:
Going to "http://server.local/lightwaverf/diningroom_light_on.php" will turn my dining room light on.
So, my idea is to have an Arduino with a PIR, LDR and Ethernet shield attached to it to do the following:
- Check system status - the system should be able to be turned on/off via the web - a value of 1/0.
- Check the light level coming in the room and keep it as a value.
- Check for motion.
- Check time.
-If the system is turned on, there is motion, and the light level is low, check the time.
If the time is between 11pm and 8am, turn the light on to 50% (go to: diningroom_light_on_50.php).
If the time is between 8am and 11pm, turn the light on fully (go to: diningroom_light_on.php).
So far I have been playing with some basic code, and have been messing with the "WebClient" example, and have managed to get it to incorporate the light sensor (A0). However, I am having some issues understanding how I incorporate the EthernetNetworking part into the if-statements I want to use.
Have a look at my code below:
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(10,0,1,2); // numeric IP for Google (no DNS)
//char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(10,0,1,27);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
//taken from here
}
void loop()
{
//This section pulled from Crazy Kit Manual
int val = analogRead(0); char i,j;
Serial.println(val);
if(val<600){
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /lightwaverf/diningroom_lamp_on.php HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while(true);
}
}
}
If anyone can help, I'd grately appreciate it!
I just saw that 'marque' on this post (http://forum.arduino.cc/index.php?topic=181814.0) has some similar code, so I've asked him to post it up! Hopefully he will!
P.S. If anyone uses LightWaveRF lights and they want my PHP code, hit me up!