Hello,
I'm building a weather station with Mega 2560 R3 as my first arduino project. I'm using a W5500 network module. Everything works perfectly with days without problems without ethernet. Even sending data to Wunderground works but ... Mega always freezes while sending sample # 56, every time. I tried changing the delay between sending, nothing changes. I tried not using DHCP and setting ip address in router to avoid end of lease time ... nothing change.
I'm using IDE 1.6.9 (1.6.10 does not work with my RTC library) and the Wiznet library 1.5 which was copied over the Arduino Ethernet library. Sketch occupies 12 % program space and 20 % memory space. It should not be a problem. I also ensured power supply voltage was ok.
As my sketch is close to 900 lines, as it perfectly works if I comment the main loop line calling the function sending data, I will only include the lines related to ethernet, it should be easier this way.
Define part, etc ...
#include <SPI.h> // Ethernet module communication
#include <Ethernet.h> // wiznet library
EthernetClient client;
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; // Enter a MAC address for your controller
IPAddress SERVER = { 38,102,137,157 }; // numeric IP for "weatherstation.wunderground.com"
//char SERVER [] = "weatherstation.wunderground.com" ; // name address for uploading data
char ID [] = "IQUBECVI10" ; // wunderground id
char PASSWORD [] = "XXXXX" ; // wunderground password
char WEBPAGE [] = "GET /weatherstation/updateweatherstation.php?" ;
int wundgCycles ; // delay before sending Wunderground data
unsigned int Sample = 0; // Data sent sample number
#define IDLE_TIMEOUT_MS 5000 // Amount of time to wait (in milliseconds) with no data
Setup, initEther(), wundgSendData()
Mega freezes at that line : if (!client.connect(SERVER, 80))
// --- setup ----
initEther() ; // initialize W5500
// ----- initEther function ----
void initEther()
{
if (Ethernet.begin(mac) == 0)
{ Serial.println(F("Failed to configure Ethernet using DHCP"));
lcd.print(F("Ethernet failed"));
delay(10000); // this will trigger the watchdog timer
}
delay(1000) ;
Serial.print(F("Connected - IP address is : ")); // print your local IP address:
lcd.setCursor(0,0);
Serial.print(Ethernet.localIP()) ;
lcd.print(F("Ethernet OK"));
Serial.println();
}
// ------ wundgSendData function ------
void sendWundgData(void)
{ Serial.println(F("in SendWundgData()"));
Sample ++; // to check # sent
float windSpdMph = (windSpdKmH_avg2m* kmhToMph) ;
float windGustMph = (windGustKmh_10m * kmhToMph) ;
float tempF = ((outTempC*1.8)+32);
float dewF = ((dewptC*1.8)+32);
float pressIn = (pressSea*0.0295299830714) ;
float rainIn = (dailyRainmm * mmToInch);
float rainLastHrin = (rainLastHrmm * mmToInch);
long start = millis() ; // to monitor time delay to send data
Serial.println(F("before if connect "));
if (!client.connect(SERVER, 80)) // mega freezes here
{Serial.println(F("Connection failed"));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Connection failed"));
delay(2000);
initEther() ;
}
else
{
Serial.println(F("connected "));
Serial.print(F("Sample # "));
Serial.println(Sample);
client.print(WEBPAGE);
client.print(F("ID="));
client.print(ID);
client.print(F("&PASSWORD="));
client.print(PASSWORD);
client.print(F("&dateutc=now"));
client.print(F("&windspeedmph="));
client.print(windSpdMph,2);
Serial.print(F("&windspeedmph = "));
Serial.println(windSpdMph,2);
if (windGust)
{ client.print(F("&windgustmph="));
client.print(windGustMph,2);
}
client.print(F("&dailyrainin="));
client.print(rainIn,3) ;
client.print(F("&rainin="));
client.print(rainLastHrin,3) ;
client.print(F("&tempf="));
client.print(tempF,2);
client.print(F("&baromin="));
client.print(pressIn,2);
client.print(F("&dewptf="));
client.print(dewF);
client.print(F("&humidity="));
client.print(humidity,1);
client.print(F("&action=updateraw"));
client.println();
Serial.println(F("&action=updateraw"));
digitalWrite(LED, LOW);
Serial.print (F("duree envoi : "));
Serial.println(millis()- start );
}
Serial.println(F("-------------"));
Serial.println(F("Server Responce!"));
unsigned long lastRead = millis();
while (client.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS))
{
while (client.available())
{
char c = client.read();
Serial.print(c);
lastRead = millis();
}
}
Serial.println(F("---------------------------"));
client.stop();
secondRTC = second(RTC.get() ) ; // To restart complete cycle next second
}
Main Loop code. The only part for Ethernet is the call to wundgSendData(). No problem at all if commented.
void loop()
{
secondRTC = second(RTC.get()) ;
minuteRTC = minute(RTC.get()) ;
dayRTC = day(RTC.get()) ;
if (secondRTC != lastSecondRTC) // to start retreiving data each new second
{ // digitalWrite(LED,HIGH); // internal LED on when cycle begins
if (lcdView> 2) // executes view0() or view1() or view2()
lcdView = 0;
if (lcdView == 0)
view0();
if (lcdView == 1)
view1();
if (lcdView == 2)
view2();
lastSecondRTC = secondRTC; // to start loop each new second
if (lastDayRTC != dayRTC ) // reset/change daily values
{ resetMaxMin() ; // min & max temp
lastDayRTC = dayRTC ; }
if(secondRTC == 0)
{ if(++min10Count > 9) min10Count = 0; // increment min10Count
rainHour[minuteRTC] = 0; // Zero out this minute's rainfall amount
windGustMax_10m[min10Count] = 0; // Zero out this minute's gust
windLullMin_10m[min10Count] = 99 ; } // Zero out this minute's lull
rainProcess(); // Calculates rain data
tempProcess(); // Calculates temp and Humidity data
pressureProcess(); // Calculates atmospheric pressure data
if(secondRTC == 59 )
{pOld[minuteRTC] = pressSea;}
windSpeedProcess(); // Calculates wind speed data
if(windSpeed > windGustToday) // Check to see if this is a gust for the day
{windGustToday = windSpeed;}
if(windSpeed > windGustMax_10m[min10Count]) // Check to see if this is a gust for the minute
{windGustMax_10m[min10Count] = windSpeed;}
if(windSpeed < windLullMin_10m[min10Count]) // Check to see if this is a lull for the minute
{windLullMin_10m[min10Count] = windSpeed;}
windSpdAvg[sec120Count] = (int)windSpeed;
if(++sec120Count > 119) // increment sec120Count to store wind speed every second for 120 seconds
{ sec120Count = 0;
}
wundgCycles ++ ;
if(wundgCycles >59)
{Serial.println("Avant sendWundgData()");
sendWundgData(); // upload data to wunderground
wundgCycles = 0 ;
}
tempFeelProcess(); // Calculates Windchill or Humidex
buttonProcess(); // Process button actions
// serialPrintWeather(); // optional to serial monitor all readings every second
// digitalWrite(LED,LOW); // internal led OFF after 1 cylce calc is done
wdt_reset(); // reset watchdog
}
delay(5); // delay to wait for second to change in RTC
} // end of main loop
Thanks in advance, I'm quite sure it's a stupid logic error as I'm not an expert with network and even Arduino ..
J Guy