connection fail web hosting
http://www.autocontrol.in.th/Electical/user/status.php?user_home=boss
help me !!
#include <SPI.h>
#include <Ethernet.h>
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(172,20,14,99);
// fill in your Domain Name Server address here:
IPAddress myDns(122,155,187,95);
// initialize the library instance:
EthernetClient client;
char server[] = "www.autocontrol.in.th";
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 = 1*1000; // delay between updates, in milliseconds
boolean startRead = false;
int ledPins[] ={2, 3, 4, 5, 6, 7, 8, };
char buffer[16];
void setup() {
// start serial port:
Serial.begin(9600);
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac, ip, myDns);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
dataReceived();
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
startRead=false;
}
// 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:
//char url[64];
//sprintf(url, "/test/status.php?device1=1&try=%u", lastConnectionTime);
//sprintf(url, "GET %s HTTP/1.1", url);
//Serial.print(url);
startRead=false;
client.println("GET /Electical/user/status.php?user_home=boss HTTP/1.1");
//client.println("GET /test/status.php? HTTP/1.1");
client.println("Host: www.autocontrol.in.th");
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();
}
}
void dataReceived(){
char c = client.read();
if (c == '[' ) {
startRead = true;
}else if(startRead){
static int buffer_idx=0;
if ( c!=']' && buffer_idx < 15 ) {
buffer[buffer_idx] = c;
buffer_idx++;
}
else
{
buffer[buffer_idx] = '\0';
buffer_idx = 0;
Serial.println(buffer);
char * s1 = strtok(buffer, ",");
doWork(1, s1);
char * s2 = strtok(NULL, ",");
doWork(2, s2);
char * s3 = strtok(NULL, ",");
doWork(3, s3);
char * s4 = strtok(NULL, ",");
doWork(4, s4);
char * s5 = strtok(NULL, ",");
doWork(5, s5);
char * s6 = strtok(NULL, ",");
doWork(6, s6);
char * s7 = strtok(NULL, ",");
doWork(7, s7);
char* s8 = strtok(NULL, ",");
doWork(8, s8);
}
}
}
void doWork(int index, char* val){
int state=atoi(val);
if(state==2) TurnOn(index);
else if(state==1) TurnOff(index);
}
void TurnOn(int index){
digitalWrite(ledPins[index], HIGH);
Serial.print("LED ");
Serial.print(index);
Serial.println(" ON");
}
void TurnOff(int index){
digitalWrite(ledPins[index], LOW);
Serial.print("LED ");
Serial.print(index);
Serial.println(" OFF");
}
