Hi all
I have setup an energy logger, reading an s0 output, using an interrupt.
During test i had a local apache/php/mysql server running, processing the http-requests from my Arduino.
This worked perfect, i did a http-req every minute and every entry was correctly logged.
Now, i switched the same code to my online server/database (same config) and i am experiencing the following:
But, when everything seems to go allright, some requests never reach my online server, i see a gap between the minutes.
When running everything on local network, i never had this.
My code below:
#include <SPI.h>
#include <Ethernet.h>
// Ethernet variables:
byte myMac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte myIp[] = {
10, 0, 0, 13 };
byte myDns[] = {
10, 0, 0, 1 };
byte myGateway[] = {
10, 0, 0, 1 };
EthernetClient client;
char server[] = "myserver.com";
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
long postingInterval = 59380;
float KWh, Wh;
float timeBetweenPulses = 0;
float millisPrev = 0;
int s0state, s0statePrev;
float countPulses, countPulsesToSend;
boolean startRead;
String httpResponse;
// Setup:
void setup() {
Serial.begin(9600);
Serial.println("Start!");
// disable w5100 SPI while starting SD
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
//Serial.println("SD: Starting..");
//if(!SD.begin(4)) Serial.println("SD: Failed!"); // SD.begin() return with its SPI disabled, so nothing need to be done here.
//else Serial.println("SD: Ok!");
Serial.println("Ethernet: Starting...");
//Ethernet.begin(mac, ip, dns, gateway, subnet);
Ethernet.begin(myMac, myIp, myDns, myGateway);
Serial.print("Ethernet: IP address: ");
Serial.println(Ethernet.localIP());
// Ethernet begin returns with its SPI enabled, so disable it.
digitalWrite(10,HIGH);
pinMode(2,INPUT);
digitalWrite(2,HIGH);
attachInterrupt(0, pulse, RISING);
}
// Loop:
void loop() {
if(client.available()){
httpResponse = "";
while (client.available() > 0) {
char c = client.read();
if (c == '[' ) { //'<' is our begining character
startRead = true; //Ready to start reading the part
c = client.read();
}
if(startRead){
if(c != ']'){ //'>' is our ending character
httpResponse += c;
}
else{
//got what we need here! We can disconnect now
startRead = false;
}
}
}
if(httpResponse == "1"){
Serial.print("EthernetClient: ACK Received, ");
Serial.print(countPulsesToSend);
Serial.println(" pulses sent. Resetting \'countPulsesToSend\'...");
countPulsesToSend = 0;
disconnect();
}
else{
Serial.println("EthernetClient: FAILED ACK Received, pulses NOT sent. NOT resetting \'countPulsesToSend\'...");
disconnect();
}
}
if(millis() - lastConnectionTime > postingInterval){
timeBetweenPulses = (millis() - millisPrev)/ 1000;
millisPrev = millis();
Wh = countPulsesToSend * 1.25 / timeBetweenPulses * 3600;
KWh = (float)countPulses / 800;
Serial.print("KWh measurement: ");
Serial.print(timeBetweenPulses);
Serial.print(" s / ");
Serial.print("Total: ");
Serial.print(countPulses);
Serial.print(" pulses / ");
Serial.print("To send: ");
Serial.print(countPulsesToSend);
Serial.print(" pulses / ");
Serial.print(KWh);
Serial.print(" KWh / ");
Serial.print(Wh);
Serial.println(" W");
httpRequest();
lastConnectionTime = millis();
}
}
void pulse(){
countPulses ++;
countPulsesToSend ++;
}
void disconnect(){
client.flush();
client.stop();
Serial.println("EthernetClient: Disconnecting...");
while(client.connected()) {
client.flush();
client.stop();
delay(500);
Serial.println("EthernetClient: client.stop()");
}
}
void httpRequest() {
// Send data to webserver:
if (client.connect(server, 80)) {
//Serial.print("*** countPulsesToSend: ");
//Serial.println(countPulsesToSend);
//client.print("GET http://10.0.0.2/storekwh/value=");
client.print("GET http://myserver.com/storekwh/value=");
client.print(countPulsesToSend);
client.print("/key=www/");
client.println(" HTTP/1.1");
//client.println("Host: 10.0.0.2");
client.println("Host: myserver.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
delay(500);
//countPulsesToSend = 0;
}
else {
// if you couldn't make a connection:
Serial.println("EthernetClient: Connection failed!");
Serial.println();
Serial.println("EthernetClient: Disconnecting...");
disconnect();
}
}