Hi! Can you please help me with my code? I attach a copy below.
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int piezoPin = 6;
int ledPin = 13;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 254); // ip arduino
char server[] = "192.168.1.180"; // ip ethernet
EthernetClient client;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial){
;//wait for serial port to connect
}
Serial.println("DHT11 testing");
dht.begin();
pinMode (piezoPin, OUTPUT);
pinMode(ledPin,OUTPUT);
//start ethernet connection
Ethernet.begin(mac, ip);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
float h = dht.readHumidity();
//pembacaan dalam celcius
float c = dht.readTemperature();
if (isnan(h) || isnan(c)){
Serial.println("Sensor reading failed");
return;
}
float htoc = dht.computeHeatIndex(c, h, false);
if(htoc > 40 ){
digitalWrite(ledPin, HIGH); //HIGH = ON
delay(1000);
digitalWrite(ledPin, LOW); //LOW = OFF
delay(1000);
digitalWrite(piezoPin,HIGH);
{
tone(6,3047,400);
delay(100);
noTone(7);
delay(100);
}
Serial.println("Warning! ");
Serial.print("Temperature : ");
Serial.print(htoc);
Serial.print(" *C\t");
Serial.print("Humidity : ");
Serial.print(h);
Serial.println(" %\t");
}
else{
digitalWrite(piezoPin,LOW);
digitalWrite(ledPin, LOW);
Serial.print("Temperature : ");
Serial.print(htoc);
Serial.print(" *C\t");
Serial.print("Humidity : ");
Serial.print(h);
Serial.println(" %");
}
// Connect to the server (your computer or web page)
if (client.connect(server, 80)) {
Serial.println("--> connection ok\n");
client.print("GET /write_data.php?"); // This
client.print("Temperature: ");
client.print(htoc);
client.print("Humidity: ");
client.print(h); // 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.10.180"); // 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");
}
// Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
delay(10000);
}
I'm using Arduino Uno R3 as main controller, Wiznet Ethernet W5100, 5v piezo and DHT11 temperature and humidity sensor. When I opened my serial monitor it keeps giving me "connection failed" message but the sensor can read the temperature and humidity. I don't know what is wrong with my code or with my arduino , because it's run successfully in that reference project. Thanks