Beste Rob (robtillaart),
Bedankt voor je reactie. Het script ben ik naar aanleiding van je advies opnieuw aan het maken. De basis was al totaal anders, zodoende.
In de logbestanden (buffer) op de SD card zit de tijd nog niet verwerkt. Als ik de buffer op later tijdstip zou versturen naar db, is er niet meer bekend hoe laat de ernergie verbruikt is. Nu worden alle pulsen direct verwerkt, de tijd laat ik door PHP in de DB plaatsen.
Helaas loop ik al vast om de Arduino middels ntp de tijd op te laten halen (bij interne server of ntp op internet).
Zie bijgaand mijn code tot nu toe.
#include <Time.h>
#include <SD.h>
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
//int getNtpTime;
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x60, 0xD6 };
byte ip[] = { 192, 168, 1, 50 }; // Arduino IP-address.
byte gateway[] = { 192, 168, 1, 254 };
byte subnet[] = { 255, 255, 0, 0 };
byte server[] = { 192, 168, 1, 2 }; // Database server address.
//byte getNtpTime[] = { 192, 168, 1, 1 }; // NTP server address.
EthernetClient client;
void setup(){
Ethernet.begin(mac, ip, subnet, gateway);
Serial.begin(9600);
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
while(timeStatus()== timeNotSet); // wait until the time is set by the sync provider
delay(1000);
}
void loop(){
if (client.connect(server, 80)) {
Serial.println("Connected");
}else{
Serial.println("Connection failed");
}
if( now() != prevDisplay) //update the display only if the time has changed
{
prevDisplay = now();
digitalClockDisplay();
}
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10) {
Serial.print('0');
Serial.print(digits);
}
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
Heb jij enig idee wat ik mis doe? De volgende foutmelding blijf ik houden, bij verify code.
sketch_apr29a.cpp: In function 'void setup()':
sketch_apr29a:26: error: 'getNtpTime' was not declared in this scope
sketch_apr29a.cpp: In function 'void loop()':
sketch_apr29a:38: error: 'prevDisplay' was not declared in this scope
Heb getNtptime al als byte (ip) geprobeerd op te geven ook variable voor url? p.s. ben nog een echte beginner. Met veel script voorbeelden (examples) lukt het soms een klein beetje.
Het volgende script heb ik momenteel draaien. Erg simpel als de LED (kwh_meter) knippert stuur ik dit direct naar de PHP webserver.
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
//#include <Upd.h>
#include <SPI.h>
#include <SD.h>
#include <Time.h>
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
int analogPin1 = 1; // Kwh Sensor
int kwh_sensor1 = 0; // Kwh counter value
int kwh_pulsCount = 0; // variable to store the kwh Counter Pulse
// assign a MAC-, IP-, GW- and Subnetaddress for the ethernet controller.
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x60, 0x06};
byte ip[] = {192,168,1,50};
byte gateway[] = {192,168,1,254};
byte subnet[] = {255, 255, 0, 0};
byte server[] = {192,168,1,2 }; // The server (database) address to connect to.
Client client(server, 80); // initialize the library instance:
void setup()
{
Serial.begin(9600); // setup serial
Ethernet.begin(mac, ip); // start the ethernet connection
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
delay(15000); // wait 15 seconden for Ethernet connection
}
void loop()
{
kwh_sensor1 = analogRead(analogPin1); // read the input pin
Serial.println(kwh_sensor1); // debug value
// KWH Usage Script...
if (kwh_sensor1 > 200) {
kwh_pulsCount+=1;
Serial.print("kwh_pulsCount = ");
Serial.println(kwh_pulsCount);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect()) {
Serial.println("connected");
Serial.println();
client.println();
//Serial.print("PUT /energyusage/YOUR_FEED_HERE.csv HTTP/1.1\n");
//client.print("PUT /energyusage/YOUR_FEED_HERE.csv HTTP/1.1\n");
Serial.print("GET /energyusage/energy_upload.php");
client.print("GET /energyusage/energy_upload.php");
Serial.print("?kwh_pulsCount=");
client.print("?kwh_pulsCount=");
Serial.print(kwh_pulsCount);
client.print(kwh_pulsCount);
Serial.println();
client.println();
}
else {
Serial.println("connection failed");
}
if (kwh_pulsCount == 1000){kwh_pulsCount = 0;}
String data_sd = "";
time_t t = now(); // store the current time in time variable t
hour(t); // returns the hour for the given time t
minute(t); // returns the minute for the given time t
second(t); // returns the second for the given time t
day(t); // the day for the given time t
weekday(t); // day of the week for the given time t
month(t); // the month for the given time t
year(t); // the year for the given time t
data_sd += kwh_pulsCount;
data_sd += ",";
data_sd += hour(t);
data_sd += ":";
data_sd += minute(t);
data_sd += ":";
data_sd += second(t);
//data_sd += "\n";
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
Serial.println(data_sd);
dataFile.println(data_sd);
dataFile.close();
// print to the serial port too:
}
}
// GAS Usage Script...
//if (gas_sensor1 > ..){
// ....
//}
// if the server's disconnected, stop the client:
if (client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}