Hi !
I would like to ligth on a LED conected to my Arduino, by internet.
But I don't want to use Arduino as a server beacuse I don't want to need to open the different port.
So I d'ont want to use Arduino as a server but as a client. It means that frequently Arduino load a internet webpage in wich one it can find "1" or "0". If its "1", puts LED on.
First of all, I would like to know your opinion about this solution and maybe do you know some projects that works on the same idea.
Now, I have a problem in my code. During the first loop, all works good, the led ligths on and all, but at the second loop, it can't read the number, as the web page cursor didn't come back at the top of the page. Can you help me ? :~
Thanks a lot,
#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(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.------m"; // name address for Google (using DNS)
int led = 8;
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);
// 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;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 60*100; // delay between updates, in milliseconds
void setup() {
pinMode(led, OUTPUT);
// 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)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /Ard-----.php HTTP/1.1");
client.println("Host: www.--------om");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
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 (c=='?'){
Serial.print("-step1----");
char c = client.read();
Serial.print(c);
if (c=='1'){
Serial.print("-step2----");
char c = client.read();
Serial.print(c);
digitalWrite(led, HIGH);
delay(5000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
client.stop();
client.flush();
Serial.println("disconnecting.");
}
}
}
// if the server's disconnected, stop the client:
if (!client.connected() && lastConnected ) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
client.flush();
//do nothing forevermore:
//while(true);
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /---.php HTTP/1.1");
client.println("Host: www.f-----om");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
client.flush();
}
}