Medidor de consumo de energia - Projeto TI
Headlines News :

.

Home » » Medidor de consumo de energia

Medidor de consumo de energia

Written By x86_g on 2012-09-20 | 4:45 PM


Medidor de consumo de energia



Livraria
https://github.com/openenergymonitor/EmonLib

Interface com o arduino:


Source code

CÓDIGO:

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include "EmonLib.h"                  // Include Emon Library
//#include "RTClib.h"
#include <avr/eeprom.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

#define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block(dst_p, (void *)offsetof(__eeprom_data, eeprom_field), MIN(dst_size, sizeof((__eeprom_data*)0)->eeprom_field))
#define eeprom_read(dst, eeprom_field) eeprom_read_to(&dst, eeprom_field, sizeof(dst))
#define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block(src_p, (void *)offsetof(__eeprom_data, eeprom_field), MIN(src_size, sizeof((__eeprom_data*)0)->eeprom_field))
#define eeprom_write(src, eeprom_field) { typeof(src) x = src; eeprom_write_from(&x, eeprom_field, sizeof(x)); }
#define MIN(x,y) ( x > y ? y : x )
/*
 * __eeprom_data is the magic name that maps all of the data we are
 * storing in our EEPROM
 */
struct __eeprom_data {
  double flash_kwhtotal;
};

// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
//Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 6, 7);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 

//RTC_DS1307 RTC;            // Create an instance
EnergyMonitor emon1;      // Create an instance

//Cria variaveis globais
double kwhTotal;
double vlreais;
unsigned long ltmillis, tmillis, timems, previousMillis;
char charBuf[30];
void setup()
{  
  Serial.begin(9600);
  display.begin();
  display.setContrast(37);
  /*if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
  }*/
  //Theoretical CT sensor calibration
  //CT Ratio / Burden resistance = (100A / 0.05A) / 64 Ohms = 50
  emon1.current(1, 29.41); // Current: input pin, calibration for 24 Ohms
  eeprom_read(kwhTotal, flash_kwhtotal);
  previousMillis = millis();
  
    // text display tests
  /*display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  display.println("Hello, world!");
  display.setTextColor(WHITE, BLACK); // 'inverted' text
  display.println(3.141592);
  display.setTextSize(2);
  display.setTextColor(BLACK);
  display.print("0x"); display.println(0xDEADBEEF, HEX);
  display.display();
  delay(2000);*/

}

void loop()
{
  //Calculate amount of time since last realpower measurment.
  ltmillis = tmillis;
  tmillis = millis();
  timems = tmillis - ltmillis;
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  
  //Calculate todays number of kwh consumed.
  //kwhTotal = kwhTotal + ((realPower/1000.0) * 1.0/3600.0 * (timems/1000.0));
  
  //Calculate todays number of kwh consumed.
  kwhTotal = kwhTotal + (((Irms*127.0)/1000.0) * 1.0/3600.0 * (timems/1000.0));
  
  Serial.print("Watts: ");
  Serial.println(Irms*127.0);         // Apparent power
  Serial.print("Current: ");
  Serial.println(Irms);            // Irms
  Serial.print("kwhTotal: ");
  printFloat(kwhTotal, 10);
  Serial.println("");
  
  //grava na memoria a cada 1 minuto
  if ((millis() - previousMillis)>4000)
      {
        Serial.println("Gravando na EEprom");
        eeprom_write(kwhTotal, flash_kwhtotal);
        previousMillis=millis();
      }
  //convert double em string
  dtostrf(kwhTotal, 8, 7, charBuf);  
  //Multiplica pelo valor kilowatt hora R$ 0.35 Reais
  vlreais = kwhTotal * 0.35;
  // text display tests
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  //display.println("Hello, world!");
  display.print("KW/h:");
  display.println(charBuf);
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.print("Consumo Watts:");
  //dtostrf(Irms, 10, 8, charBuf); 
  display.print(Irms*126);
  display.println("Watts:");
  display.println("");
  display.print("R$: ");
  dtostrf(vlreais, 8, 7, charBuf);
  display.print(charBuf);
  display.display();  
  //delay(2500);
}


void printFloat(float value, int places) {
  // this is used to cast digits
  int digit;
  float tens = 0.1;
  int tenscount = 0;
  int i;
  float tempfloat = value;

    // make sure we round properly. this could use pow from <math.h>, but doesn't seem worth the import
  // if this rounding step isn't here, the value  54.321 prints as 54.3209

  // calculate rounding term d:  0.5/pow(10,places)  
  float d = 0.5;
  if (value < 0)
    d *= -1.0;
  // divide by ten for each decimal place
  for (i = 0; i < places; i++)
    d/= 10.0;    
  // this small addition, combined with truncation will round our values properly
  tempfloat +=  d;

  // first get value tens to be the large power of ten less than value
  // tenscount isn't necessary but it would be useful if you wanted to know after this how many chars the number will take

  if (value < 0)
    tempfloat *= -1.0;
  while ((tens * 10.0) <= tempfloat) {
    tens *= 10.0;
    tenscount += 1;
  }


  // write out the negative if needed
  if (value < 0)
    Serial.print('-');

  if (tenscount == 0)
    Serial.print(0, DEC);

  for (i=0; i< tenscount; i++) {
    digit = (int) (tempfloat/tens);
    Serial.print(digit, DEC);
    tempfloat = tempfloat - ((float)digit * tens);
    tens /= 10.0;
  }

  // if no places after decimal, stop now and return
  if (places <= 0)
    return;

  // otherwise, write the point and continue on
  Serial.print('.');  

  // now write out each decimal place by shifting digits one by one into the ones place and writing the truncated value
  for (i = 0; i < places; i++) {
    tempfloat *= 10.0;
    digit = (int) tempfloat;
    Serial.print(digit,DEC);  
    // once written, subtract off that digit
    tempfloat = tempfloat - (float) digit;
  }
}
Share this article :

0 comentários:

Postar um comentário