Hallo *,
so ich bin dann mal weiter gekommen (mehr oder weniger).
Glaube der Code wäre so erst einmal nutzbar aber bestimmt noch optimierungsfähig..
Die Annahme der Daten auf dem Server muss ich noch umsetzen aber da habe ich auch schon etwas gefunden (emoncms oder Splunk(würde mich ein Kollege kurz zusammen schreiben) nehme aber auch alternativen).
Soweit geht der Code auch schon bis nach dem starten der Ethernet Verbindung, diese wird noch aufgebaut aber dann scheint der Code zuhängen. (IP bekommt das Shield auch schon und ist Pingbar).
Anmerkung die "Serial.println" im Setup Teil sind fürs Debuggen.
Hier endet die Ausgabe nach dem durchführen des Ethernet.begin Befehls mit einem "M" (vom Serial.println( "My ..."); .
//libs
#include <SPI.h>
#include <Ethernet.h>
#include <avr/dtostrf.h>
//Adapted from a sample Arduino sketch and openenergymonitor.com, by chris at tinkerfailure.com
//Mac-Adresse
byte mac[] = {
0x00,0x50,0x56, 0x0A, 0xD8, 0x01 };
//IP-Adresse
IPAddress ip(192, 168, 0, 6);
IPAddress gateway(192, 168,0, 1);
IPAddress subnet(255, 255, 255, 0);
// fill in your Domain Name Server address here:
IPAddress dnServer(192, 168, 0, 1);
// initialize the library instance:
EthernetClient client;
char server[] = "192.168.0.5";
unsigned long lastConnectionTime = 0; // last time connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 60*1000; // delay between updates, in milliseconds
//Number of pulses, used to measure energy.
long pulseCount = 0;
//Used to measure power.
unsigned long pulseTime,lastTime;
//power and energy
double power, elapsedkWh;
//Number of pulses per wh - found or set on the meter.
int ppwh = 0.5; //2000 pulses/kwh = 0.5 pulse per wh
//pin definitons
int z1 = 22;
int z2 = 23;
int z3 = 24;
int z4 = 25;
int z5 = 26;
int z6 = 27;
int z7 = 28;
int z8 = 29;
int z9 = 30;
int z10 = 31;
int z11 = 32;
int z12 = 33;
int z13 = 34;
int z14 = 35;
int z15 = 36;
int z16 = 37;
void setup()
{
Serial.begin(9600);
Serial.println("delay start");
// give the ethernet module time to boot up:
delay(500);
Serial.println("delay fertig");
// start the Ethernet connection using a fixed IP address and DNS server:
Serial.println("IP start");
Ethernet.begin(mac, ip, subnet, gateway, dnServer);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
// Serial.println(Ethernet.localIP());
Serial.println("IP fertig");
// interrupt
Serial.println("Interrupts start");
attachInterrupt(digitalPinToInterrupt(z1), onPulse, FALLING);
attachInterrupt(z2, onPulse, FALLING);
attachInterrupt(z3, onPulse, FALLING);
attachInterrupt(z4, onPulse, FALLING);
attachInterrupt(z5, onPulse, FALLING);
attachInterrupt(z6, onPulse, FALLING);
attachInterrupt(z7, onPulse, FALLING);
attachInterrupt(z8, onPulse, FALLING);
attachInterrupt(z9, onPulse, FALLING);
attachInterrupt(z10, onPulse, FALLING);
attachInterrupt(z11, onPulse, FALLING);
attachInterrupt(z12, onPulse, FALLING);
attachInterrupt(z13, onPulse, FALLING);
attachInterrupt(z14, onPulse, FALLING);
attachInterrupt(z15, onPulse, FALLING);
attachInterrupt(z16, onPulse, FALLING);
Serial.println("Interrupts end");
//pins as Input
Serial.println("PinMode start");
pinMode(z1,INPUT);
pinMode(z2,INPUT);
pinMode(z3,INPUT);
pinMode(z4,INPUT);
pinMode(z5,INPUT);
pinMode(z6,INPUT);
pinMode(z7,INPUT);
pinMode(z8,INPUT);
pinMode(z9,INPUT);
pinMode(z10,INPUT);
pinMode(z11,INPUT);
pinMode(z12,INPUT);
pinMode(z13,INPUT);
pinMode(z14,INPUT);
pinMode(z15,INPUT);
pinMode(z16,INPUT);
Serial.println("PinMode end");
//set Pins at HIGH
digitalWrite(z1,HIGH);
digitalWrite(z2,HIGH);
digitalWrite(z3,HIGH);
digitalWrite(z4,HIGH);
digitalWrite(z5,HIGH);
digitalWrite(z6,HIGH);
digitalWrite(z7,HIGH);
digitalWrite(z8,HIGH);
digitalWrite(z9,HIGH);
digitalWrite(z10,HIGH);
digitalWrite(z11,HIGH);
digitalWrite(z12,HIGH);
digitalWrite(z13,HIGH);
digitalWrite(z14,HIGH);
digitalWrite(z15,HIGH);
digitalWrite(z16,HIGH);
Serial.print("Pins auf HIGH");
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// prepare the variables
char powerString[8] = "";
char elapsedString[8] = "";
//convert to strings
dtostrf(power, 7, 4, powerString);
dtostrf(elapsedkWh, 4, 3, elapsedString);
String valueOne = powerString;
String valueTwo = elapsedString;
//assemble query
String stringOne = "GET /energymonitorscript.php/?power=";
String stringTwo = "&elapsed=";
String stringThree = " HTTP/1.0 \r\n";
String stringOut = stringOne + valueOne + stringTwo + valueTwo + stringThree;
// send the HTTP PUT request:
client.println(stringOut);
client.println("User-Agent: arduino-ethernet \r\n");
client.println("Connection: close \r\n");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}
// The interrupt routine
void onPulse()
{
//used to measure time between pulses.
lastTime = pulseTime;
pulseTime = micros();
//pulseCounter
pulseCount++;
//Calculate power
power = (3600000000.0 / (pulseTime - lastTime))/ppwh;
//Find kwh elapsed
elapsedkWh = (1.0*pulseCount/(ppwh*1000)); //multiply by 1000 to pulses per wh to kwh convert wh to kwh
//Print the values.
Serial.print(power,4);
Serial.print(" ");
Serial.println(elapsedkWh,3);
//make the http request
httpRequest();
}
Sprich wenn ich das richtig sehe, komme ich nicht mal bis zum "loop".
Grüße,