I am trying to connect arduino uno and ethernet shield and try to send some data to an asp.net web api application. When i upload the sketch, i get in the serial monitor connection failed. I don't understand what's going wrong ? Below is the code i am using
#include "cactus_io_DHT22.h"
#include <SPI.h>
#include <Ethernet.h>
#include <Client.h>
#define DHT22_PIN 2
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDD };
IPAddress ip(192,168,0,177);
IPAddress server(192,168,0,11);
EthernetClient client;
DHT22 dht(DHT22_PIN);
float heatindex1 =0.00;
float heatindex2 = 0.00;
void setup() {
Ethernet.begin(mac,ip);
delay(1000); Serial.begin(9600);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected :D ");
}
Serial.print("Arduino server IP address: ");
Serial.println(Ethernet.localIP());
Serial.println("DHT22 Humidity - Temperature Sensor");
Serial.println("RH\t\tTemp (C)\tTemp (F)\tHeat Index (C)\t Heat Index
(F)\t Max(C)\t\t Min(C)");
dht.begin();
dht.readHumidity();
dht.readTemperature();
}
float max1=dht.temperature_C;
float min1=100;
void loop() {
dht.readHumidity();
dht.readTemperature();
heatindex1=dht.computeHeatIndex_C();
heatindex2=dht.computeHeatIndex_F();
if(dht.temperature_C>max1)
{max1=dht.temperature_C;}
if(dht.temperature_C<min1)
{min1=dht.temperature_C;}
// Check if any reads failed and exit early (to try again).
if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
Serial.println("DHT sensor read failure!");
return;
}
if (client) {
// When a client sends a request to a webserver, that request ends with a blank line
boolean currentLineIsBlank = true;
Serial.println("OK"); }
if (client.connect(server, 80)) {
client.print("GET /api/todo?humidity=");
client.print(dht.humidity);
client.print("&temperature1=");
client.print(dht.temperature_C);
client.print("&temperature2=");
client.print(dht.temperature_F);
client.print("&heatindex1=");
client.print(heatindex1);
client.print("&heatindex2=");
client.print(heatindex2);
client.print("&max1=");
client.print(max1);
client.print("&min1=");
client.print(min1);// And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
client.println(" HTTP/1.1"); // Part of the GET request
client.println("Host: 192.168.0.11"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
}
else {
//If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
Serial.print(dht.humidity); Serial.print(" %\t\t");
Serial.print(dht.temperature_C); Serial.print(" *C\t");
Serial.print(dht.temperature_F); Serial.print(" *F\t");
Serial.print(dht.computeHeatIndex_C()); Serial.print(" *C\t ");
Serial.print(dht.computeHeatIndex_F()); Serial.print(" *F\t ");
Serial.print(max1); Serial.print(" *C\t ");
Serial.print(min1); Serial.println(" *C\t");
delay(5000);
}