Hi evey one ,
my project is a "smart house" i want to control the door of a house with arduino with ethernet shield and send data from arduino to a hosted database .
here is my 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, 64 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(8081); //server port
String readString;
//////////////////////
void setup(){
pinMode(6, OUTPUT); //pin selected to control
pinMode(7, 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=\"/?venton\"\">Turn On Vent</a>");
client.println("<a href=\"/?ventoff\"\">Turn Off Vent</a>
");
client.println("<a href=\"/?moton\"\">Turn On Moteur</a>");
client.println("<a href=\"/?motoff\"\">Turn Off Moteur</a>
");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("?venton") >0)//checks for on
{
digitalWrite(6, HIGH); // set pin 4 high
Serial.println("vent On");
}
else{
if(readString.indexOf("?ventoff") >0)//checks for off
{
digitalWrite(6, LOW); // set pin 4 low
Serial.println("vent Off");
}
else{ if(readString.indexOf("?moton") >0)//checks for off
{
digitalWrite(7, HIGH); // set pin 4 low
Serial.println("Moteur on");
}
else{ if(readString.indexOf("?motoff") >0)//checks for off
{
digitalWrite(7, LOW); // set pin 4 low
Serial.println("Moteur off");
}}
}
}
//clearing string for next read
readString="";
}
}
}
}
}
here is my problems :
How to send data to my database ?
I want that if a led is on the motor stop : How to receive the state of a led ? because when the door is totally opened a led will turn on so i have to know the state of the led to stop the motor .
is this code right ?
else{ if(readString.indexOf("?moton") >0)//checks for off
{
digitalWrite(7, HIGH); // set pin 4 low
Serial.println("Moteur on");
if (digitalRead(pin_led,HIGH){
digitalWrite(7,LOW);
// I want here that the state of the led will be sent to my database
}
}
Note :
*my motor turn like a led but i will use a stepper motor i will change the code
*Is there any other idea to my databse ? can i put it in the sd card and send data to the sd card
Think you
To send data to another server you also need client code. Below is code with both server and client functions.
//zoomkat 7-03-12, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pins high/low
//use the ' in html instead of " to prevent having to escape the "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = {192, 168, 1, 102 }; // ip in lan assigned to arduino
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
char serverName[] = "web.comporium.net"; // (DNS) zoomkat's test web page server
//byte serverName[] = { 208, 104, 2, 86 }; // (IP) zoomkat web page server IP address
String readString; //used by server to capture GET request
//////////////////////
void setup(){
pinMode(5, OUTPUT); //pin selected to control
pinMode(6, OUTPUT); //pin selected to control
pinMode(7, OUTPUT); //pin selected to control
pinMode(8, OUTPUT); //pin selected to control
//pinMode(5, OUTPUT); //pin 5 selected to control
Ethernet.begin(mac,ip,gateway,gateway,subnet);
server.begin();
Serial.begin(9600);
Serial.println(F("server/client 1.0 test 7/03/12")); // keep track of what is loaded
Serial.println(F("Send an g in serial monitor to test client")); // what to do to test client
}
void loop(){
// check for serial input
if (Serial.available() > 0)
{
byte inChar;
inChar = Serial.read();
if(inChar == 'g')
{
sendGET(); // call client sendGET function
}
}
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.print(readString); //print to serial monitor for debuging
//now output HTML data header
if(readString.indexOf('?') >=0) { //don't send new page
client.println(F("HTTP/1.1 204 Zoomkat"));
client.println();
client.println();
}
else {
client.println(F("HTTP/1.1 200 OK")); //send new page on browser request
client.println(F("Content-Type: text/html"));
client.println();
client.println(F("<HTML>"));
client.println(F("<HEAD>"));
client.println(F("<TITLE>Arduino GET test page</TITLE>"));
client.println(F("</HEAD>"));
client.println(F("<BODY>"));
client.println(F("<H1>Zoomkat's simple Arduino 1.0 button</H1>"));
// DIY buttons
client.println(F("Pin5"));
client.println(F("<a href=/?on2 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off3 target=inlineframe>OFF</a>
"));
client.println(F("Pin6"));
client.println(F("<a href=/?on4 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off5 target=inlineframe>OFF</a>
"));
client.println(F("Pin7"));
client.println(F("<a href=/?on6 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off7 target=inlineframe>OFF</a>
"));
client.println(F("Pin8"));
client.println(F("<a href=/?on8 target=inlineframe>ON</a>"));
client.println(F("<a href=/?off9 target=inlineframe>OFF</a>
"));
client.println(F("Pins"));
client.println(F(" <a href=/?off2468 target=inlineframe>ALL ON</a>"));
client.println(F(" <a href=/?off3579 target=inlineframe>ALL OFF</a>"));
client.println(F("<IFRAME name=inlineframe style='display:none'>"));
client.println(F("</IFRAME>"));
client.println(F("</BODY>"));
client.println(F("</HTML>"));
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf('2') >0)//checks for 2
{
digitalWrite(5, HIGH); // set pin 5 high
Serial.println(F("Led 5 On"));
Serial.println();
}
if(readString.indexOf('3') >0)//checks for 3
{
digitalWrite(5, LOW); // set pin 5 low
Serial.println(F("Led 5 Off"));
Serial.println();
}
if(readString.indexOf('4') >0)//checks for 4
{
digitalWrite(6, HIGH); // set pin 6 high
Serial.println(F("Led 6 On"));
Serial.println();
}
if(readString.indexOf('5') >0)//checks for 5
{
digitalWrite(6, LOW); // set pin 6 low
Serial.println(F("Led 6 Off"));
Serial.println();
}
if(readString.indexOf('6') >0)//checks for 6
{
digitalWrite(7, HIGH); // set pin 7 high
Serial.println(F("Led 7 On"));
Serial.println();
}
if(readString.indexOf('7') >0)//checks for 7
{
digitalWrite(7, LOW); // set pin 7 low
Serial.println(F("Led 7 Off"));
Serial.println();
}
if(readString.indexOf('8') >0)//checks for 8
{
digitalWrite(8, HIGH); // set pin 8 high
Serial.println(F("Led 8 On"));
Serial.println();
}
if(readString.indexOf('9') >0)//checks for 9
{
digitalWrite(8, LOW); // set pin 8 low
Serial.println(F("Led 8 Off"));
Serial.println();
}
//clearing string for next read
readString="";
}
}
}
}
}
//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
if (client.connect(serverName, 80)) {
Serial.println(F("connected"));
client.println(F("GET /~shb/arduino.txt HTTP/1.1"));
client.println(F("Host: web.comporium.net"));
client.println(F("Connection: close"));
client.println();
}
else {
Serial.println(F("connection failed"));
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read();
Serial.print(c);
}
Serial.println();
Serial.println(F("disconnecting."));
Serial.println(F("=================="));
Serial.println();
client.stop();
}
xSeriaLx:
my project is a "smart house" i want to control the door of a house with arduino with ethernet shield and send data from arduino to a hosted database .
How to send data to my database ?
*Is there any other idea to my databse ?
You can send the data to Excel. This is done with a free macro PLX-DAQ. Here is some background
The connection is by USB cable, no ethernet needed, and the method is only slightly different form sending data to the serial monitor. There are other options to Excel too.
can i put it in the sd card and send data to the sd card
Yes. You can either
Send data direct Excel
Send data to SD for subsequent retrieval
Both
If you are serious about what you are doing, it is always a good idea to send data to on-board SD, if only for backup.
Think you for answering me ,
I want to know how can i receive the state of a led that i turn on from a website
How to connect the led and what's the code ?
I use digitalRead , analogRead ?
Think you
I don't know much about LEDs, and the logic of what you are saying is unclear, but LEDs should be connected via a current-limiting resistor, say 560 ohm or 1k. The Blink example shows how they are switched, using digitalWrite. If you want to read the state of the pin to which the LED is connected, I guess you can use digitalRead but the LED is then redundant.