I am working on my first Arduino project which is an electricity demand meter. The basic setup is that it counts pulses on one pin does some simple math and shows the demand on lcd the screen. It also logs this demand number and uses a web server to show the demand and allow you to download the logs. It is very much a hodgepodge of different scripts I have found online. My current problem I can't seem to figure out is that it keeps crashing and restarting. Originally I had the logging working but when I tried to add the file serving portion with the webserver the logging broke and the rebooting started. If anyone can take a look and see any obvious problems (and my awful coding practice) that would be a huge help. Thanks in advance.
#include <LiquidCrystal.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>
#include <SPI.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <SD.h>
#define BUFSIZ 128
// Setup NIC
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x30, 0x92 };
byte ip[] = { 192,168,100,90 };
char rootFileName[] = "index.htm";
Server server(80);
// SD Card
const int chipSelect = 4;
Sd2Card card;
SdVolume volume;
SdFile file;
SdFile root;
// NTP Time stuff
unsigned int localPort = 8888; // local port to listen for UDP packets
//byte timeServer[] = {192, 43, 244, 18}; // time.nist.gov NTP server
byte timeServer[] = {192, 168, 100, 5}; // time server ip address
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
const int buttonPin = 2; // the pin that the pushbutton is attached to
const double demandConstant = .72 * 4; //constant from utility to multiply count
const int demandChargeTime = 15; //number of minutes utility bases demand
const int demandLimit = 100; //number to warn when demand goes higher
double currentDemand = 0; // Current kW in last XX minutes
int demandLoopCount = 0;
int countArray[demandChargeTime];
LiquidCrystal lcd(9, 3, 8, 14, 5, 6, 7); //RS,RW,E,d4,d5,d6,d7
//int backLight = 2; // will control the backlight
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
//setup LCD
lcd.begin(16, 2);//setup coloumn and rows
lcd.clear();
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
server.begin();
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
// make sure that the default chip select pin is set to
// output, even if you don't use it:
lcd.setCursor(0,0);
lcd.clear();
lcd.print("Initial SD");
lcd.clear();
PgmPrint("Free RAM: ");
Serial.println(FreeRam());
// Initialize SD Card
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
if (!card.init(SPI_HALF_SPEED, 4))
{
lcd.print("SD Error - Card");
}
if (!volume.init(&card))
{
lcd.print("SD Error - Vol");
}
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) Serial.print("openRoot failed");
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);
lcd.clear();
lcd.print("SD card OK");
delay(1000);
lcd.clear();
lcd.print("Syncing Time...");
//sync time to NTP
getNTP();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(month());
lcd.print("/");
lcd.print(day());
lcd.print("/");
lcd.print(year());
lcd.setCursor(0,1);
lcd.print(hour());
lcd.print(":");
lcd.print(minute());
lcd.print(" EST");
delay(3000);
lcd.clear();
Alarm.timerRepeat(5, Demand); //Run demand loop every 60 secs, 5 for testing
Alarm.timerRepeat(8, logData); //Run LogData every 5min = 300
}
Code continued on next post