Hello,
I am trying to read several DHT sensors and update them to a server that is online. The first time through the loop it runs fine. Makes the GET call as required and posts the information. It waits the specified amount of time and starts a second loop, this time though it goes through the DHT loop twice until, I think, it overuns the char spacing I allot for the statement and then forces a restart of the Arduino. Upon restart, it will do one good call, then repeat.
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>
#include <MemoryFree.h>
// REAL TIME CLOCK DS 1307
RTC_DS1307 rtc;
// Local Network Settings
byte mac[] = {0xab, 0xb3, 0xef, 0x2F, 0x1a, 0x2d}; // Must be unique on local network
// Emoncms Settings
char EmoncmsAddress[] = "emoncms.org";
char writeAPIKey[] = "&apikey=xxxx4fbaaa359xxxxdb0xxxx1dxxxxaa";
char writeNode1[] = "node=1&csv=";
char writeNode2[] = "node=2&csv=";
char Emoncmsbegin[] = "/input/post.json?";
char transmission1[375];
//char transmission2[240];
const long updateEmoncmsInterval = 20 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
//DHT Settings
#define DHTTYPE DHT21
DHT dht01(23, DHTTYPE);DHT dht02(22, DHTTYPE);DHT dht03(31, DHTTYPE);DHT dht04(30, DHTTYPE);
DHT dht05(25, DHTTYPE);DHT dht06(24, DHTTYPE);DHT dht07(29, DHTTYPE);DHT dht08(28, DHTTYPE);
DHT dht09(27, DHTTYPE);DHT dht10(26, DHTTYPE);DHT dht11(36, DHTTYPE);DHT dht12(37, DHTTYPE);
DHT dht13(34, DHTTYPE);DHT dht14(35, DHTTYPE);DHT dht15(38, DHTTYPE);DHT dht16(39, DHTTYPE);
DHT dht17(32, DHTTYPE);DHT dht18(33, DHTTYPE);DHT dht19(40, DHTTYPE);DHT dht20(41, DHTTYPE);
DHT* dhtList[] = {&dht01, &dht02, &dht03, &dht04, &dht05, &dht06, &dht07, &dht08,
&dht09, &dht10, &dht11, &dht12, &dht13, &dht14, &dht15, &dht16, &dht17, &dht18, &dht19, &dht20};
#define NUM_DHTS (sizeof(dhtList)/sizeof(DHT*))
typedef char humStr[7]; //4 digits + decimal point + 1 decimal + null terminator
typedef char tempStr[7]; //4 digits + decimal point + 1 decimal + null terminator
humStr numhum[NUM_DHTS];
tempStr numtemp[NUM_DHTS];
char exttemp[7];
char exthum[7];
////////////////////////////////////////////////////////////////////////////
//
// SETUP
//
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
pinMode(SS, OUTPUT);
// Start Ethernet on Arduino
startEthernet();
delay(1000);
// Start DHT22
dht01.begin(); dht02.begin(); dht03.begin(); dht04.begin();
dht05.begin(); dht06.begin(); dht07.begin(); dht08.begin();
dht09.begin(); dht10.begin(); dht11.begin(); dht12.begin();
dht13.begin(); dht14.begin(); dht15.begin(); dht16.begin();
dht17.begin(); dht18.begin(); dht19.begin(); dht20.begin();
}
/////////////////////////////////////////////////////////////////////////
//
// LOOP
//
void loop()
{
long currentTime = millis();
if (currentTime - lastConnectionTime > updateEmoncmsInterval)
{
//memset(transmission1, '\0', 350);
strcat(transmission1,Emoncmsbegin);
strcat(transmission1,writeNode1);
Serial.print(freeMemory());
for (int i = 0; i < NUM_DHTS; i++)
{
// Read value from DHT Sensor
float h = dhtList[i]->readHumidity();
float t = dhtList[i]->readTemperature();
if (isnan(h))
{
h = 0.0;
t = 0.0;
dtostrf(h, 3, 1, numhum[i]);
dtostrf(t, 3, 1, numtemp[i]);
}
else
{
dtostrf(h, 4, 1, numhum[i]);
dtostrf(t, 4, 1, numtemp[i]);
}
strcat(transmission1,numtemp[i]);
strcat(transmission1, ",");
strcat(transmission1, numhum[i]);
if (i < NUM_DHTS-1) strcat(transmission1, ",");
Serial.print(i); Serial.print(" ");
Serial.print((char*)numhum[i]); Serial.print(" ");
Serial.println((char*)numtemp[i]);
}
strcat(transmission1, writeAPIKey);
//Serial.println(transmission1);
//strcat(transmission2, writeAPIKey);
}
// Disconnect from Emoncms
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update Emoncms
if(!client.connected() && (currentTime - lastConnectionTime > updateEmoncmsInterval))
{
Serial.println(transmission1);
//Serial.println(transmission2);
updateEmoncms(transmission1);
Serial.println(F("Sent Transmission"));
memset(transmission1, '\0', 300);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {startEthernet();}
lastConnected = client.connected();
}
/////////////////////////////////////////////////////////////////////////////
//
// UPDATE Emoncms
//
void updateEmoncms(String tsData)
{
if (client.connect(EmoncmsAddress, 80))
{
client.print("GET ");
client.print(tsData);
client.println(" HTTP/1.0");
client.println("Host: emoncms.org");
client.print("User-Agent: Arduino-ethernet");
client.println("Connection: close");
client.println();
//client.stop();
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to Emoncms...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to Emoncms failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to Emoncms Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
/////////////////////////////////////////////////////////////////////////
//
// START ETHERNET
//
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}
Here is the text from the serial monitor:
...disconnected
Connecting Arduino to network...
Connecting Arduino to network...
Arduino connected to network using DHCP
49380 17.7 23.9
1 17.8 23.7
2 18.2 23.6
3 18.0 23.7
4 18.4 23.6
Read fail5 0.0 0.0
6 18.0 24.0
7 18.7 23.4
8 20.9 22.8
9 20.8 22.8
10 18.2 22.9
11 19.6 23.0
12 21.3 22.2
13 22.3 22.4
14 21.1 22.3
15 18.9 22.4
Read fail16 0.0 0.0
Read fail17 0.0 0.0
Read fail18 0.0 0.0
Read fail19 0.0 0.0
/input/post.json?node=1&csv=23.9,17.7,23.7,17.8,23.6,18.2,23.7,18.0,23.6,18.4,0.0,0.0,24.0,18.0,23.4,18.7,22.8,20.9,22.8,20.8,22.9,18.2,23.0,19.6,22.2,21.3,22.4,22.3,22.3,21.1,22.4,18.9,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0&apikey=xx114faa0xx5xxxx753db0x9dxxdexxxxx8
Connecting to Emoncms...
Sent Transmission
49380 17.7 24.1
1 17.7 23.8
2 18.2 23.6
3 18.0 23.8
4 18.4 23.7
Read fail5 0.0 0.0
6 18.0 24.1
7 18.7 23.3
8 21.0 22.9
9 20.8 22.9
10 18.1 23.0
11 19.6 23.1
12 21.3 22.2
13 22.3 22.4
14 21.1 22.4
15 19.0 22.4
Read fail16 0.0 0.0
Read fail17 0.0 0.0
Read fail18 0.0 0.0
Read fail19 0.0 0.0
49380 17.4 24.1
1 17.7 23.9
2 18.1 23.6
3 18.0 23.9
4 18.2 23.7
Read fail5 0.0 0.0
6 17.8 24.1
7 18.6 23.4
8 20.9 23.0
9 20.8 22.9
10 18.1 23.1
11 19.6 23.0
12 21.2 22.3
13 22.2 22.4
14 21.1 22.4
15 19.0 22.5
Read fail16 0.0 0.0
Read fail17 0.0 0.0
Read fail18 0.0 0.0
Read fail19 0.0 0.0
...disconnected
Connecting Arduino to network...
Connecting Arduino to network...
Arduino connected to network using DHCP
Anyway, thanks for looking. Let me know if I need to clarify anything.
Tim