0

I'm new to Arduino. For my first project, I've decided to build a weather station where data is collected via sensors and is then sent to Wunderground. I'm using an arduino uno, an ethernet shield as well as a DHT22 sensor for temperature and humidity.

Everything worked for an hour and then I'm getting "connection Failed" constantly.

I would really appreciate it if you could look into my code. I've tried everything and it doesn't explain the sudden failure of the project. I occasionally get a successful upload after dozens of connection failed messages.

Here's my code. I've removed my weather station password for security reasons.

#include <Wire.h>
#include <SPI.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "DHT.h"

// Sensor Pins 
#define DHTPIN 7
#define DHTTYPE DHT22
// Variables
float temp;
float hum;

byte mac[] = {
  "de,ad,be,ef,fe,ed"
};
EthernetClient ethernetClient;
unsigned int localPort = 8888;
// Wunderground server details
char SERVER[] = "rtupdate.wunderground.com";
char WEBPAGE[] = "GET /weatherstation/updateweatherstation.php?";
char ID[] = "ICHESTER24";
char PASSWORD[] = "iwqnrlex";

DHT dht(DHTPIN, DHTTYPE);
unsigned int connections = 1;
unsigned int timeout = 30000;

void setup(void) {
  Serial.begin(38400);
  Wire.begin();
  dht.begin();

  // Connect to internet
  Serial.print(F("\nInitialising Ethernet module"));
  if (Ethernet.begin(mac)){

    Serial.println("Initialisation completed successfully!");
  }
  else {
    Serial.println("Something went wrong during ethernet module setup!");
  }
} //END OF SETUP

void loop(void) {
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  float tempf = (temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  float dewptf = (dewPoint(tempf, hum));
  Serial.println("++++++++++++++++++++++++++");
  Serial.println("temp = ");
  Serial.println(tempf);
  Serial.println("Humidity = ");
  Serial.println(hum);
  Serial.println("DewPoint = ");
  Serial.println(dewptf);
  // Send all data to WUNDERGROUND
  if (ethernetClient.connect(SERVER, 80)) {
    ethernetClient.print(WEBPAGE);
    ethernetClient.print("ID=");
    ethernetClient.print(ID);
    ethernetClient.print("&PASSWORD=");
    ethernetClient.print(PASSWORD);
    ethernetClient.print("&dateutc=");
    ethernetClient.print("now");
    ethernetClient.print("&tempf=");
    ethernetClient.print(tempf);
    ethernetClient.print("&humidity=");
    ethernetClient.print(hum);
    ethernetClient.print("&dewptf=");
    ethernetClient.print(dewptf);
       ethernetClient.print("&softwaretype=Arduino%20UNO%20version1&action=updateraw&realtime=1&rtfreq=2.5");//Rapid Fire
    ethernetClient.println();
    Serial.println("New data uploaded");
  }else{
    Serial.println("Connection Failed...");
  }
  delay(2500);

}
void resetEthernet() {
 ethernetClient.stop();
 delay(1000);
 Ethernet.begin(mac);
 delay(1000);
 ethernetClient.available();
}
double dewPoint(double tempf, double humidity)
{
  double A0= 373.15/(273.15 + tempf);
  double SUM = -7.90298 * (A0-1);
  SUM += 5.02808 * log10(A0);
  SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
  SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
  SUM += log10(1013.246);
  double VP = pow(10, SUM-3) * humidity;
  double T = log(VP/0.61078);   
  return (241.88 * T) / (17.558-T);
}
1

I assume u used a W5100 shield. This shield is capable of having 4 connections open. If the connections are not closed for some reason the chip is unable to make another connection. This means after sending u need to close the connection. (else it will work 4 times).

ethernetClient.stop()

Just before or after

Serial.println("New data uploaded");

https://www.arduino.cc/en/Reference/ClientStop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.