Go Down

Topic: S0 kwh uitlezen, afstellen. (Read 632 times) previous topic - next topic

arnaudarduino

Feb 11, 2018, 11:05 am Last Edit: Feb 22, 2018, 06:50 pm by arnaudarduino
Moet er toch maar een vraag van maken, beginstand van de kwh.
Hoe kan ik in de sketch
Code: [Select]
/*
 *    FILE: KWh monitoring
 *  AUTHOR: Rob Tillaart
 *    DATE: 2010 03 31
 *     URL: http://playground.arduino.cc/Main/EEM12L-32AKWhMonitoring
 *
 * PURPOSE: prototype KWh monitoring 
 *
 * Digital Pin layout ARDUINO
 * =============================
 *  2     IRQ 0    - to KW meter EEM12L-32A
 *
 * This KWh meter gives a pulse every 0.5 Watt
 */

//
// KWH SENSOR CODE
//
// TODO - make a class
//
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xAF, 0x82, 0x31, 0x30};
IPAddress ip(192, 168, 2, 7);
IPAddress dnServer(8, 8, 8, 8);
IPAddress gateway(192,168,2,254);
IPAddress subnet(255, 255, 255, 0);
IPAddress server(0,0,0,0);
char serverName[] = "www.mijnwebsite.com";

EthernetClient client;


char input;                     // incoming serial data (byte)
bool readnextLine = false;
#define BUFSIZE 500
char buffer[BUFSIZE];           //Buffer for serial data to find \n .

unsigned long kwh = 0;          // pulse
unsigned long sum = 0;          // sum pulse
unsigned long lastTime = 0;     // will store last time
unsigned long interval = 60000; // interval at which to blink (milliseconds)60000

volatile unsigned long rpk = 0;               // raw pulse KWH counter (private)
volatile unsigned long rpk_old = 0;           // value of last read (private)
volatile boolean CS_kwh = false;              // Critical Section for KWH meter (private)

// The interrupt routine
void Kirq()
{
  rpk++;                    // just increment raw pulse counter.
  if (rpk > 1000000000)     // reset pulse counter after 10e9 pulse = 500.000 KW
  {
    if (false == CS_kwh)    // in critical section?  // assumption IRQ-call is handled atomic on arduino.
    {
      rpk -= rpk_old;
      rpk_old = 0;
    }
  }
}

// returns kwh's since last reset
float readkwh1()
{
 return rpk / 1000.0;        // one pulse = 0.5 watt.
}

// returns kwh's since last call
float readkwh()
{
  CS_kwh = true;           // Start Critical Section - prevent interrupt Kirq() from changing rpk & rpk_old ;
      long t = rpk;            // store the raw pulse counter in a temp var.
      long k = t - rpk_old;    // subtract last measure to get delta
      rpk_old = t;             // remember old value
  CS_kwh = false;          // End Critical Section
  return k/2000.0;         // return delta, one pulse = 0.5 watt.
}

//
// SETUP
//
void setup()
{
   Serial.begin(115200);
 Serial.print("Start ");
 Serial.println(__FILE__);
 delay(1000);
//Ethernet.begin(mac, ip);
 Ethernet.begin(mac);
 delay(1000);
 pinMode(4, OUTPUT);         // SD select pin
 digitalWrite(4, HIGH);      // Explicitly disable SD
  // KWH interrupt attached to IRQ 0  = pin2
  attachInterrupt(0, Kirq, FALLING);
}
//
// MAIN LOOP
//
void loop()
{
 if (millis() - lastTime > interval) {
   lastTime = millis();
   // read the pulses
   kwh = readkwh1();
   sum += kwh;

   // send new data
   httpRequest();
 
    delay(1000);
  float kwh = readkwh();
  float sum = readkwh1();
  Serial.print("kwh: ");   
  Serial.print(kwh, 5);
  Serial.print("    ");
  Serial.println(sum, 5);
}
}
void httpRequest()
{
 // if there's a successful connection:
 if (client.connect(serverName, 80))
 { float kwh = readkwh();
   float sum = readkwh1();
   Serial.print("GET /datalogger/p32.php?kwh=");
   Serial.print(kwh, 4);
   Serial.print("&sum=");
   Serial.print(sum, 4);
   Serial.println(" HTTP/1.1");
   Serial.println("Host: mijnwebsite.com");
   Serial.println("User-Agent: arduino-ethernet");
   Serial.println("Connection: stop/close");
   Serial.println();
   Serial.println("web ");

   client.print("GET /datalogger/p32.php?kwh=");
   client.print(kwh, 4);
   client.print("&sum=");
   client.print(sum, 4);;
   client.println(" HTTP/1.1");
   client.println("Host: mijnwebsite.com");
   client.println("User-Agent: arduino-ethernet");
   client.println("Connection: close");
    //Request complete; empty recieve buffer
   while (client.available())
   {
     char c = client.read(); //gets byte from ethernet buffer

     if (client.available() == false)
     {
       delay(25); // give some extra time for next packet to arrive
     }
   }
   client.println();
   Serial.println("Success!");
 }
 else
 {
   Serial.println("Failed");
 }
 client.stop();
}

     

//
// END OF PROGRAM
//
of in de bijbehorende bestanden de beginstand aanpassen.
Want wanneer de arduino opnieuw opstart is de tellerstand weg en begint die weer opnieuw op 0.

Heb geprobeerd om in de weergave ,en of in de database de rekenen om zodoende de juiste tellerstand te verkrijgen, mij niet gelukt.
Want mij kwh meter staat niet op nul maar heeft tellerstand 00061,5.

nicoverduin

Opslaan in eeprom. Daarna check je of het eeprom leeg is (0xff).
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

arnaudarduino

#2
Feb 13, 2018, 09:11 pm Last Edit: Feb 22, 2018, 06:49 pm by arnaudarduino
Ik pas de tellerstand nu aan via insert database, dat werkt.

Code: [Select]
{
$SQL = ("INSERT INTO md12458745.pellets (kwh, sum) VALUES ('".$_GET["kwh"]."','".($_GET["sum"]+61.2)."')"); 
mysql_query($SQL);
}

Teller stand 61.2
Echter de gaat de teller te hard Op 2kwh  0,300 kwh teveel.
Pull down weerstand is 10k.

Hoe kan ik dat verschil aanpassen, moet de weerstand dan anders ?
Opstelling is arduino uno met w5100 shield stukje kabel 3x0.8 ongeveer  2m met 10k weerstand kwhmeter s0 .

arnaudarduino

#3
Feb 22, 2018, 08:37 pm Last Edit: Mar 05, 2018, 06:16 pm by arnaudarduino
Eindelijk lopen de gegevens gelijk. Waar lag het nou aan gewoon tikfout.
Pfff. 
Nu verder, proberen een sketch met temperatuur en luchtvochtigheid eraan toevoegen.

Go Up