/*CountFlashesToWeb
Intended to give only a flavour of the Arduino script. Several lines have been omitted and the exact destination file on my website has been hidden.

Intended to increment a counter when a new flash is seen from meter
Should send a record of total to date but only every tenth flash
each send group will be appended with a letter showing its status
e.g. leter A means import meter (and hopefully day rate)
Intended to send these results to a new dataBase on OK website
The circuit:
* Phototransistor or photoresistor connected to +5V
* Analogue pin A0 also connected via resistor to GND
* LED connected from digital pin 13 to ground
*/

// setup ethernet parameters
#include
#include
#include
//File MyFile;
String ReportString ="";
byte mac[] = { 0x78, 0x2e, 0xef, 0x16, 0xec, 0xeb };
char server[] = "www.khormaksarschool.org.uk";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(***,***,***,**); // OK site address suppressed
// Initialize the Ethernet client library with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
int ConnectedOK = 0;
String LogString ="";
// These constants won't change:
const int analogPin = A5; // pin that the sensor is attached to
const int ledPin = 8; // pin that the LED is attached to
const int threshold = 800; // an arbitrary threshold level that's in the range of the analog input

// other declarations
int LEDstate ;
long mycounter = 0 ;
// Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647.
int lastStringLength ;
String stringZeroes = String("00000000A"); // effectively a constant of eight zeroes
// Not sure I will ever actually USE this constant.
// an eight digit number could represent 99, 999, 999 Watt-hours or 99,999 kWh
// which is about £10,000 worth of electricity.
// the final A is proposed as a marker to Excel that string is complete
// official end of a string is null which is not reliably sent !
String stringResult = String("00000000A"); // this one is proposed for actual use.
char SendChar ;
//char TestChar1 ;
//char TestChar2 = "0";
int TestDivTen ;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
if (analogValue >= threshold) {
digitalWrite(ledPin, HIGH);
// next line intended to increment counter only once if meter flash lasts longer than delay period
if ( LEDstate == 0) {
mycounter = mycounter + 1;
String stringResult = String(mycounter);
TestDivTen = mycounter % 10 ;
if (TestDivTen == 0) {
stringResult = stringResult + "A";
ReportString = "GET /********.php?ArdInp=";
ReportString = ReportString + stringResult;

EthernetClient client;
// Ethernet connection was started above with DHCP
if (Ethernet.begin(mac) == 0) {

// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);

}

// give the Ethernet shield one second to initialize:
ConnectedOK = 0;

delay(1000); // delay 1 sec

if (client.connect(server, 80)) {
ConnectedOK = 1;

// Make a HTTP request:
client.print(ReportString);
client.println(" HTTP/1.1");
client.println("Host: khormaksarschool.org.uk");
client.println("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; es-MX; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 GTB6");
client.println();

delay(1000);
}
if (client.available()) {

}

}

LEDstate = 1 ;
}
} else {
digitalWrite(ledPin,LOW);
LEDstate =0;
}
if (LEDstate == 0) {
// delay (in msecs) if pulse wasn't seen
delay(5);
}else{
delay(100); // another pulse in less than 100ms would imply using a lot more than 100 Amps !
}
}