I pieced together a data logger/webcllient sketch that was working well for me but I wanted to clean it up and format it better and that's when I got into trouble. To compound the problem I also accidentally deleted a working backup. The timer function is declared at the start of the loop and should wait for about 2 minutes before performing a 'get' that sends the data to my server. What really happens is that it loops at about one minute generally and sometimes happens much faster. I have checked that the braces seem to be where I need them and the loop seems to be declared properly but I am unable to determine what I did wrong. Can someone see what I messed up and help me fix it?
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <NewPing.h>
#define DHTPIN 37 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
#define TRIGGER_PIN 35 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 33 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
DHT dht(DHTPIN, DHTTYPE);
// this must be unique
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// change to your network settings
IPAddress gateway(192, 168, 209, 1);
IPAddress subnet(255, 255, 255, 0);
// change to your server
IPAddress server(xxx,xxx,xxx,xxx); // "your server.com"
//Change to your domain name for virtual servers
char serverName[] = "xxxxxxxx.com";
// If no domain name, use the ip address above
// char serverName[] = "xxx.xxx.xxx.xxx";
// change to your server's port
int serverPort = 80;
EthernetClient client;
char pageAdd[64];
// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 120000UL
unsigned long thisMillis = 0;
unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
dht.begin();
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// Start ethernet
Serial.println(F("Starting ethernet..."));
// Ethernet.begin(mac, ip, gateway, gateway, subnet);
// If using dhcp, comment out the line above
// and uncomment the next 2 lines
if(!Ethernet.begin(mac)) Serial.println(F("failed"));
else Serial.println(F("ok"));
digitalWrite(10,HIGH);
Serial.println(Ethernet.localIP());
delay(2000);
Serial.println(F("Ready"));
}
void loop()
{
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
int volt1 = digitalRead(31);
// if (volt1 < 2){volt1 = 11;}
int volt2 = analogRead(12);
volt2 = (volt2 / 68.12 ,1);
int humid1 = dht.readHumidity();
int level1 = (uS / US_ROUNDTRIP_CM);
int temp1 = dht.readTemperature();
int spare2 = digitalRead (38);
if (isnan(temp1) || isnan(level1))
{
Serial.println("Failed to read from DHT");
}
else
{
Serial.print("Humidity: ");
Serial.print(humid1);
Serial.println(" %\t");
Serial.print("Temperature: ");
Serial.print(temp1);
Serial.println(" *C");
Serial.print("Water Level: ");
Serial.println(level1);
Serial.print("House Volt: ");
Serial.println(volt1);
Serial.print("Battery: ");
Serial.println(volt2);
}
{
thisMillis = millis();
if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;
}
// Modify next line to load different page
// or pass values to server
sprintf(pageAdd,"/data.php?temp1=%d&level1=%d&volt1=%d&volt2=%d&humid1=%d&spare2=%d",temp1,level1,volt1,volt2,humid1,spare2);
if(!getPage(server,serverPort,pageAdd)) Serial.print(F("Fail "));
else Serial.println(F("Pass "));
}
}
byte getPage(IPAddress ipBuf,int thisPort, char *page)
{
int inChar;
char outBuf[128];
Serial.print(F("connecting..."));
if(client.connect(ipBuf,thisPort))
{
Serial.println(F("connected"));
sprintf(outBuf,"GET %s HTTP/1.0",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",serverName);
client.println(outBuf);
client.println(F("Connection: close\r\n"));
}
else
{
Serial.println(F("failed"));
return 0;
}
// connectLoop controls the hardware fail timeout
int connectLoop = 0;
while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}
connectLoop++;
// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
// this is a delay for the connectLoop timing
delay(1);
}
Serial.println();
Serial.println(F("disconnecting."));
// close client end
client.stop();
return 1;
}