I am trying to connect server with using arduino uno, ethernet shield and hc-sr06 sensors. It connects to server without using sensor codes but when I add sensor codes it is not connected.
Here is the code without sensor codes; I see "In loop2" log on serial port.
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
delay(10000); // GIVE THE SENSOR SOME TIME TO START
}
void loop(){
Serial.println("In loop");
if (client.connect("www.xxxxx.com",80)) {
Serial.println("In loop2");
client.println("POST /update_data2.php HTTP/1.1");
client.println("Host: www.xxxxx.com"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(txData.length());
client.println();
client.print(txData);
}
if (client.connected()) {
client.stop();
}
delay(30000);
}
Here is the code with using sensor codes; I dont see "In loop2" log on serial port.
#include <Ethernet.h>
#include <SPI.h>
const int trigger_pin_1 = 12;
const int echo_pin_1 = 13;
const int trigger_pin_2 = 11;
const int echo_pin_2 = 10;
const int trigger_pin_3 = 9;
const int echo_pin_3 = 8;
const int led_1_r = 2;
const int led_1_g = 3;
const int led_2_r = 4;
const int led_2_g = 5;
const int led_3_r = 6;
const int led_3_g = 7;
int sure_1 ;
int mesafe_1 ;
int sure_2 ;
int mesafe_2 ;
int sure_3 ;
int mesafe_3 ;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
String txData ="";
void setup() {
Serial.begin(9600);
Serial.println("In setup");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
delay(5000);
pinMode(led_1_r , OUTPUT);
pinMode(led_1_g , OUTPUT);
pinMode(led_2_r , OUTPUT);
pinMode(led_2_g , OUTPUT);
pinMode(led_3_r , OUTPUT);
pinMode(led_3_g , OUTPUT);
pinMode(trigger_pin_1 , OUTPUT);
pinMode(trigger_pin_2 , OUTPUT);
pinMode(trigger_pin_3 , OUTPUT);
pinMode(echo_pin_1 ,INPUT);
pinMode(echo_pin_2 ,INPUT);
pinMode(echo_pin_3 ,INPUT);
delay(3000);
}
void loop(){
Serial.println("In loop");
digitalWrite(trigger_pin_1 , HIGH);
digitalWrite(trigger_pin_2 , HIGH);
digitalWrite(trigger_pin_3 , HIGH);
delayMicroseconds(1000);
digitalWrite(trigger_pin_1 , LOW);
digitalWrite(trigger_pin_2 , LOW);
digitalWrite(trigger_pin_3 , LOW);
sure_1 = pulseIn(echo_pin_1 , HIGH);
mesafe_1 = (sure_1/2) / 29.1;
sure_2 = pulseIn(echo_pin_2 , HIGH);
mesafe_2 = (sure_2/2) / 29.1;
sure_3 = pulseIn(echo_pin_3 , HIGH);
mesafe_3 = (sure_3/2) / 29.1;
delay(100);
Serial.print("mesafe_1:");
Serial.println(mesafe_1);
Serial.print("mesafe_2:");
Serial.println(mesafe_2);
Serial.print("mesafe_3:");
Serial.println(mesafe_3);
if (mesafe_1 <= 10)
{
digitalWrite(led_1_r , HIGH);
delay(100);
clienConnect(1,0);
}else{
digitalWrite(led_1_g , HIGH);
delay(100);
clienConnect(1,1);
}
if (mesafe_2 <= 10)
{
digitalWrite(led_2_r , HIGH);
delay(100);
clienConnect(2,0);
}else{
digitalWrite(led_2_g , HIGH);
delay(100);
clienConnect(2,1);
}
if (mesafe_3 <= 10)
{
digitalWrite(led_3_r , HIGH);
delay(100);
clienConnect(3,0);
}else{
digitalWrite(led_3_g , HIGH);
delay(100);
clienConnect(3,1);
}
delay(100);
}
void clienConnect(int id, int valuex){
Serial.println("In clienConnect");
txData = "senid=" + (String)id + "&senvalue=" + (String)valuex;
Serial.print(" txData:");
Serial.println(txData);
Serial.println("In clienConnect2");
if (client.connect("www.xxxxx.com",80))
{
Serial.println("In loop2");
client.println("POST /update_data.php HTTP/1.1");
client.println("Host: www.xxxxx.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(txData.length());
client.println();
client.print(txData);
}
Serial.println("In loop3");
if (client.connected()) {
Serial.println("In loop4");
client.stop();
Serial.println("In loop5");
}
Serial.println("In loop6");
delay(1000);
}
Why is not client connect to server when I add sensor codes? Could you please help me in this subject? Thanks a lot.