Hello,
I'm trying to control a bulb using ardunio + ethernet shield connected with relay
I hooked them up and everything is working properly to turn on/off the light while the arduino is the server using this code
//simple button GET server code to control servo and arduino pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//for use with W5100 based ethernet shields
//Powering a servo from the arduino usually DOES NOT WORK.
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
//////////////////////
void setup(){
pinMode(6, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
Serial.begin(9600);
Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
client.println("<TITLE>Home Automation</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Home Automation</H1>");
client.println("<hr />");
client.println("
");
client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
client.println("<a href=\"/?lightoff\"\">Turn Off Light</a>
");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("?lighton") >0)//checks for on
{
digitalWrite(6, HIGH); // set pin 4 high
Serial.println("Led On");
}
else{
if(readString.indexOf("?lightoff") >0)//checks for off
{
digitalWrite(6, LOW); // set pin 4 low
Serial.println("Led Off");
}
}
//clearing string for next read
readString="";
}
}
}
}
}
Now what I want to do is to make arduino work as a client connected to my website to control it through a php webpage using this code for arduino (I made it as a combination of a several codes) and I was able to make connection to my website ip
#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[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x3C, 0x0D };
// 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(xxx,xxx,xxx,xxx); // numeric IP for Google (no DNS)
IPAddress server(xxx,xxx,xxx,xxx); // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(xxx,xxx,xxx,xxx);
// 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;
String readString;
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...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
client.println();
Serial.println("connected");
client.println();
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
pinMode(6, OUTPUT);
}
void loop()
{
// 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 (readString.length() < 100) {
readString += c;
}
if(readString.indexOf("?lighton") >0){
digitalWrite(6, HIGH);
Serial.println("Led On");
}
else{
if(readString.indexOf("?lightoff") >100)
{
digitalWrite(6, LOW);
Serial.println("Led Off");
}
}
readString="";
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
while(true);
}
}
}
and using this php code uploaded to my website to control the light through 2 buttons but it didn't work, nothing happen with button click
<?php
if(isset($_POST['select']))
{
select();
}
else
{
insert();
}
?>
<html>
<body>
<a href="/?lighton">Turn On Light</a>
<a href="/?lightoff">Turn Off Light</a>
</form>
<?php
function select()
{
$what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);
echo '<'.$what_the_arduino_reads.'>';
}
function insert()
{
$what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);
echo '<'.$what_the_arduino_reads.'>';
}
?>
I'm sure there is a certain problem with either arduino code or the php code or both cause I made them as a combination without specific knowledge specially with php
I would be very grateful if anyone can help me to get this thing done