Re: Lettura contatore GSE con arduino
Inviato: mer dic 14, 2011 5:27 pm
Bello!!!
Così hai una lettura istantanea e reale della produzione!
Ciao
Così hai una lettura istantanea e reale della produzione!
Ciao
Il forum dell'energia solare e del rinnovabile
http://forum.portalsole.it/
FabioR ha scritto:
sto pensando che potrei far calcore ad arduino i millisecondi fra un lampeggio e l'altro, facendoli calcolare la potenza istantanea ad ogni lampeggio
poi integro il dato facendo la media sul minuto e lo scrivo su db
Codice: Seleziona tutto
/*
Photovoltaic & Home Energy Monitor V 0.1
Fabio Roverso 16/12/11
based on:
//Basic energy monitoring sketch
//Authors: Trystan Lea, Eric Sandeen
//Licenced under GNU General Public Licence more details here
// openenergymonitor.org
//Sketch measures voltage and current.
//and then calculates useful values like real power,
//apparent power, powerfactor, Vrms, Irms.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <SFEbarGraph.h>
// debug switch
boolean blndebug=true;
/*
########################### LED BARGRAPH #########################
*/
// create a bargraph object
SFEbarGraph BG;
unsigned int LED;
boolean BLINK=true;
/*
########################### ENERGY MONITOR #########################
*/
//Used to convert integer to char for LCD
char chvalue [10];
//Setup variables
int numberOfSamples = 3000;
//Set Voltage and current input pins
int inPinV = 1;
int inPinI_FV = 2; //Photovoltaic
int inPinI_HO = 3; //Home
//Sanity check calibration method thanks to Eric Sandeen http://sandeen.net/wordpress
//See discussion here: http://openenergymonitor.org/emon/node/59
//Enter the values of your setup below
// Voltage is reduced both by wall xfmr & voltage divider
#define AC_WALL_VOLTAGE 230
#define AC_ADAPTER_VOLTAGE 9
// Ratio of the voltage divider in the circuit
#define AC_VOLTAGE_DIV_RATIO 11
// CT: Voltage depends on current, burden resistor, and turns
//Using SCT-013-030 reference sheet
#define CT_BURDEN_RESISTOR 62
#define CT_TURNS 1800
//Calibration coeficients
//These need to be set in order to obtain accurate results
//Set the above values first and then calibrate futher using normal calibration method described on how to build it page.
//double VCAL = 0.88928678;
//double ICAL = 0.069565217;
double VCAL = 1.0;
double ICAL_FV = 1.0;
double ICAL_HO = 1.0;
double PHASECAL = 2.3;
// Initial gueses for ratios, modified by VCAL/ICAL tweaks
#define AC_ADAPTER_RATIO (AC_WALL_VOLTAGE / AC_ADAPTER_VOLTAGE)
double V_RATIO = AC_ADAPTER_RATIO * AC_VOLTAGE_DIV_RATIO * 5 / 1024 * VCAL;
double I_RATIO_FV = (long double)CT_TURNS / CT_BURDEN_RESISTOR * 5 / 1024 * ICAL_FV;
double I_RATIO_HO = (long double)CT_TURNS / CT_BURDEN_RESISTOR * 5 / 1024 * ICAL_HO;
//Sample variables
int lastSampleV,
lastSampleI_FV,
lastSampleI_HO,
sampleV,
sampleI_FV,
sampleI_HO;
//Filter variables
double lastFilteredV,
lastFilteredI_FV,
lastFilteredI_HO,
filteredV,
filteredI_FV,
filteredI_HO;
double filterTemp;
//Stores the phase calibrated instantaneous voltage.
double shiftedV;
//Power calculation variables
double sqI_FV,
sqI_HO,
sqV,
instP_FV,
instP_HO,
sumI_FV,
sumI_HO,
sumV,
sumP_FV,
sumP_HO;
//Useful value variables
double realPower_FV,
realPower_HO,
balancePower,
apparentPower_FV,
apparentPower_HO,
powerFactor_FV,
powerFactor_HO,
Vrms,
Irms_FV,
Irms_HO;
/*
########################### NETWORK #########################
*/
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x65, 0xAB }; //Arduino MAC address
byte ip[] = {
192, 168, 1, 179 }; //Arduino IP address
byte gateway[] = {
192, 168, 1, 250 }; //Network gateway
byte subnet[] = {
255, 255, 255, 0 }; //Network subnet mask
//Web client stuff
byte server[] = {
192 ,168 ,12 ,8 }; //Web server address
unsigned int port = 82; //Web server http port (usually 80)
Client client(server, port); //Creates an http client handle
// On the Ethernet Shield, CS is pin 4.
const int chipSelect = 4;
/*
########################### LCD MONITOR #########################
*/
#define PIN_SCE 7 // LCD CS .... Pin 3
#define PIN_RESET 6 // LCD RST .... Pin 1
#define PIN_DC 5 // LCD Dat/Com. Pin 5
#define PIN_SDIN 2 // LCD SPIDat . Pin 6
#define PIN_SCLK 3 // LCD SPIClk . Pin 4
// LCD Gnd .... Pin 2
// LCD Vcc .... Pin 8
// LCD Vlcd ... Pin 7
#define LCD_C LOW
#define LCD_D HIGH
#define LCD_X 84
#define LCD_Y 48
#define LCD_CMD 0
int a,b;
static const byte ASCII[][5] =
{
{
0x00, 0x00, 0x00, 0x00, 0x00 }// 20
,{
0x00, 0x00, 0x5f, 0x00, 0x00 }// 21 !
,{
0x00, 0x07, 0x00, 0x07, 0x00 }// 22 "
,{
0x14, 0x7f, 0x14, 0x7f, 0x14 }// 23 #
,{
0x24, 0x2a, 0x7f, 0x2a, 0x12 }// 24 $
,{
0x23, 0x13, 0x08, 0x64, 0x62 }// 25 %
,{
0x36, 0x49, 0x55, 0x22, 0x50 }// 26 &
,{
0x00, 0x05, 0x03, 0x00, 0x00 }// 27 '
,{
0x00, 0x1c, 0x22, 0x41, 0x00 }// 28 (
,{
0x00, 0x41, 0x22, 0x1c, 0x00 }// 29 )
,{
0x14, 0x08, 0x3e, 0x08, 0x14 }// 2a *
,{
0x08, 0x08, 0x3e, 0x08, 0x08 }// 2b +
,{
0x00, 0x50, 0x30, 0x00, 0x00 }// 2c ,
,{
0x08, 0x08, 0x08, 0x08, 0x08 }// 2d -
,{
0x00, 0x60, 0x60, 0x00, 0x00 }// 2e .
,{
0x20, 0x10, 0x08, 0x04, 0x02 }// 2f /
,{
0x3e, 0x51, 0x49, 0x45, 0x3e }// 30 0
,{
0x00, 0x42, 0x7f, 0x40, 0x00 }// 31 1
,{
0x42, 0x61, 0x51, 0x49, 0x46 }// 32 2
,{
0x21, 0x41, 0x45, 0x4b, 0x31 }// 33 3
,{
0x18, 0x14, 0x12, 0x7f, 0x10 }// 34 4
,{
0x27, 0x45, 0x45, 0x45, 0x39 }// 35 5
,{
0x3c, 0x4a, 0x49, 0x49, 0x30 }// 36 6
,{
0x01, 0x71, 0x09, 0x05, 0x03 }// 37 7
,{
0x36, 0x49, 0x49, 0x49, 0x36 }// 38 8
,{
0x06, 0x49, 0x49, 0x29, 0x1e }// 39 9
,{
0x00, 0x36, 0x36, 0x00, 0x00 }// 3a :
,{
0x00, 0x56, 0x36, 0x00, 0x00 }// 3b ;
,{
0x08, 0x14, 0x22, 0x41, 0x00 }// 3c <
,{
0x14, 0x14, 0x14, 0x14, 0x14 }// 3d =
,{
0x00, 0x41, 0x22, 0x14, 0x08 }// 3e >
,{
0x02, 0x01, 0x51, 0x09, 0x06 }// 3f ?
,{
0x32, 0x49, 0x79, 0x41, 0x3e }// 40 @
,{
0x7e, 0x11, 0x11, 0x11, 0x7e }// 41 A
,{
0x7f, 0x49, 0x49, 0x49, 0x36 }// 42 B
,{
0x3e, 0x41, 0x41, 0x41, 0x22 }// 43 C
,{
0x7f, 0x41, 0x41, 0x22, 0x1c }// 44 D
,{
0x7f, 0x49, 0x49, 0x49, 0x41 }// 45 E
,{
0x7f, 0x09, 0x09, 0x09, 0x01 }// 46 F
,{
0x3e, 0x41, 0x49, 0x49, 0x7a }// 47 G
,{
0x7f, 0x08, 0x08, 0x08, 0x7f }// 48 H
,{
0x00, 0x41, 0x7f, 0x41, 0x00 }// 49 I
,{
0x20, 0x40, 0x41, 0x3f, 0x01 }// 4a J
,{
0x7f, 0x08, 0x14, 0x22, 0x41 }// 4b K
,{
0x7f, 0x40, 0x40, 0x40, 0x40 }// 4c L
,{
0x7f, 0x02, 0x0c, 0x02, 0x7f }// 4d M
,{
0x7f, 0x04, 0x08, 0x10, 0x7f }// 4e N
,{
0x3e, 0x41, 0x41, 0x41, 0x3e }// 4f O
,{
0x7f, 0x09, 0x09, 0x09, 0x06 }// 50 P
,{
0x3e, 0x41, 0x51, 0x21, 0x5e }// 51 Q
,{
0x7f, 0x09, 0x19, 0x29, 0x46 }// 52 R
,{
0x46, 0x49, 0x49, 0x49, 0x31 }// 53 S
,{
0x01, 0x01, 0x7f, 0x01, 0x01 }// 54 T
,{
0x3f, 0x40, 0x40, 0x40, 0x3f }// 55 U
,{
0x1f, 0x20, 0x40, 0x20, 0x1f }// 56 V
,{
0x3f, 0x40, 0x38, 0x40, 0x3f }// 57 W
,{
0x63, 0x14, 0x08, 0x14, 0x63 }// 58 X
,{
0x07, 0x08, 0x70, 0x08, 0x07 }// 59 Y
,{
0x61, 0x51, 0x49, 0x45, 0x43 }// 5a Z
,{
0x00, 0x7f, 0x41, 0x41, 0x00 }// 5b [
,{
0x02, 0x04, 0x08, 0x10, 0x20 }// 5c ¥
,{
0x00, 0x41, 0x41, 0x7f, 0x00 }// 5d ]
,{
0x04, 0x02, 0x01, 0x02, 0x04 }// 5e ^
,{
0x40, 0x40, 0x40, 0x40, 0x40 }// 5f _
,{
0x00, 0x01, 0x02, 0x04, 0x00 }// 60 `
,{
0x20, 0x54, 0x54, 0x54, 0x78 }// 61 a
,{
0x7f, 0x48, 0x44, 0x44, 0x38 }// 62 b
,{
0x38, 0x44, 0x44, 0x44, 0x20 }// 63 c
,{
0x38, 0x44, 0x44, 0x48, 0x7f }// 64 d
,{
0x38, 0x54, 0x54, 0x54, 0x18 }// 65 e
,{
0x08, 0x7e, 0x09, 0x01, 0x02 }// 66 f
,{
0x0c, 0x52, 0x52, 0x52, 0x3e }// 67 g
,{
0x7f, 0x08, 0x04, 0x04, 0x78 }// 68 h
,{
0x00, 0x44, 0x7d, 0x40, 0x00 }// 69 i
,{
0x20, 0x40, 0x44, 0x3d, 0x00 }// 6a j
,{
0x7f, 0x10, 0x28, 0x44, 0x00 }// 6b k
,{
0x00, 0x41, 0x7f, 0x40, 0x00 }// 6c l
,{
0x7c, 0x04, 0x18, 0x04, 0x78 }// 6d m
,{
0x7c, 0x08, 0x04, 0x04, 0x78 }// 6e n
,{
0x38, 0x44, 0x44, 0x44, 0x38 }// 6f o
,{
0x7c, 0x14, 0x14, 0x14, 0x08 }// 70 p
,{
0x08, 0x14, 0x14, 0x18, 0x7c }// 71 q
,{
0x7c, 0x08, 0x04, 0x04, 0x08 }// 72 r
,{
0x48, 0x54, 0x54, 0x54, 0x20 }// 73 s
,{
0x04, 0x3f, 0x44, 0x40, 0x20 }// 74 t
,{
0x3c, 0x40, 0x40, 0x20, 0x7c }// 75 u
,{
0x1c, 0x20, 0x40, 0x20, 0x1c }// 76 v
,{
0x3c, 0x40, 0x30, 0x40, 0x3c }// 77 w
,{
0x44, 0x28, 0x10, 0x28, 0x44 }// 78 x
,{
0x0c, 0x50, 0x50, 0x50, 0x3c }// 79 y
,{
0x44, 0x64, 0x54, 0x4c, 0x44 }// 7a z
,{
0x00, 0x08, 0x36, 0x41, 0x00 }// 7b {
,{
0x00, 0x00, 0x7f, 0x00, 0x00 }// 7c |
,{
0x00, 0x41, 0x36, 0x08, 0x00 }// 7d }
,{
0x10, 0x08, 0x08, 0x10, 0x08 }// 7e ?
,{
0x00, 0x06, 0x09, 0x09, 0x06 }// 7f ?
};
void LcdCharacter(char character)
{
LcdWrite(LCD_D, 0x00);
for (int index = 0; index < 5; index++)
{
LcdWrite(LCD_D, ASCII[character - 0x20][index]);
}
LcdWrite(LCD_D, 0x00);
}
void LcdClear(void)
{
for (int index = 0; index < LCD_X * LCD_Y / 8; index++)
{
LcdWrite(LCD_D, 0x00);
}
}
void LcdInitialise(void)
{
pinMode(PIN_SCE, OUTPUT);
pinMode(PIN_RESET, OUTPUT);
pinMode(PIN_DC, OUTPUT);
pinMode(PIN_SDIN, OUTPUT);
pinMode(PIN_SCLK, OUTPUT);
digitalWrite(PIN_RESET, LOW);
// delay(1);
digitalWrite(PIN_RESET, HIGH);
LcdWrite( LCD_CMD, 0x21 ); // LCD Extended Commands.
LcdWrite( LCD_CMD, 0xBf ); // Set LCD Vop (Contrast). //B1
LcdWrite( LCD_CMD, 0x04 ); // Set Temp coefficent. //0x04
LcdWrite( LCD_CMD, 0x14 ); // LCD bias mode 1:48. //0x13
LcdWrite( LCD_CMD, 0x0C ); // LCD in normal mode. 0x0d for inverse
LcdWrite(LCD_C, 0x20);
LcdWrite(LCD_C, 0x0C);
}
void LcdString(char *characters)
{
while (*characters)
{
LcdCharacter(*characters++);
}
}
void LcdWrite(byte dc, byte data)
{
digitalWrite(PIN_DC, dc);
digitalWrite(PIN_SCE, LOW);
shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
digitalWrite(PIN_SCE, HIGH);
}
// gotoXY routine to position cursor
// x - range: 0 to 84
// y - range: 0 to 5
void gotoXY(int x, int y)
{
LcdWrite( 0, 0x80 | x); // Column.
LcdWrite( 0, 0x40 | y); // Row.
}
void drawLine(void)
{
unsigned char j;
for(j=0; j<84; j++) // top
{
gotoXY (j,0);
LcdWrite (1,0x01);
}
for(j=0; j<84; j++) //Bottom
{
gotoXY (j,5);
LcdWrite (1,0x80);
}
for(j=0; j<6; j++) // Right
{
gotoXY (83,j);
LcdWrite (1,0xff);
}
for(j=0; j<6; j++) // Left
{
gotoXY (0,j);
LcdWrite (1,0xff);
}
}
//millis
unsigned long time;
unsigned long savetodb;
/*
###########################################################
########################### SETUP #########################
###########################################################
*/
void setup(void)
{
//start LCD
LcdInitialise();
LcdClear();
//define ethernet CS
pinMode(10, OUTPUT);
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//start serial
Serial.begin(9600);
// Let's initialize the bargraph library!
BG.begin(9,1);
// Turn all LEDs on then off using BG.paint()
for(LED=0;LED<(1*30);LED++) // Increment through all the LEDs from 0 to as many as we have
{
BG.paint(LED,HIGH); // Turn LED on
BG.send(); // Send the canvas to the bargraph
delay(50);
}
for(LED=0;LED<(1*30);LED++) // Increment through all the LEDs from 0 to as many as we have
{
BG.paint(LED,LOW); // Turn LED off
BG.send(); // Send the canvas to the bargraph
delay(50);
}
delay(500);
//Millis
time = millis();
savetodb = millis();
}
/*
###########################################################
########################### LOOP ##########################
###########################################################
*/
void loop(){
time=millis();
for (int n=0; n<numberOfSamples; n++)
{
//Used for offset removal
lastSampleV=sampleV;
lastSampleI_FV=sampleI_FV;
lastSampleI_HO=sampleI_HO;
//Read in voltage and current samples.
sampleV = analogRead(inPinV);
sampleI_FV = analogRead(inPinI_FV);
sampleI_HO = analogRead(inPinI_HO);
//Used for offset removal
lastFilteredV = filteredV;
lastFilteredI_FV = filteredI_FV;
lastFilteredI_HO = filteredI_HO;
//Digital high pass filters to remove 2.5V DC offset.
filteredV = 0.996*(lastFilteredV+sampleV-lastSampleV);
filteredI_FV = 0.996*(lastFilteredI_FV+sampleI_FV-lastSampleI_FV);
filteredI_HO = 0.996*(lastFilteredI_HO+sampleI_HO-lastSampleI_HO);
//Phase calibration goes here.
shiftedV = lastFilteredV + PHASECAL * (filteredV - lastFilteredV);
//Root-mean-square method voltage
//1) square voltage values
sqV= filteredV * filteredV;
//2) sum
sumV += sqV;
//Root-mean-square method current
//1) square current values
sqI_FV = filteredI_FV * filteredI_FV;
sqI_HO = filteredI_HO * filteredI_HO;
//2) sum
sumI_FV += sqI_FV;
sumI_HO += sqI_HO;
//Instantaneous Power
instP_FV = shiftedV * filteredI_FV;
instP_HO = shiftedV * filteredI_HO;
//Sum
sumP_FV +=instP_FV;
sumP_HO +=instP_HO;
}
//Calculation of the root of the mean of the voltage and current squared (rms)
//Calibration coeficients applied.
Vrms = V_RATIO*sqrt(sumV / numberOfSamples);
Irms_FV = I_RATIO_FV*sqrt(sumI_FV / numberOfSamples);
Irms_HO = I_RATIO_HO*sqrt(sumI_HO / numberOfSamples);
//Calculation power values
realPower_FV = V_RATIO*I_RATIO_FV*sumP_FV / numberOfSamples;
realPower_HO = V_RATIO*I_RATIO_HO*sumP_HO / numberOfSamples;
apparentPower_FV = Vrms * Irms_FV;
apparentPower_HO = Vrms * Irms_HO;
powerFactor_FV = realPower_FV / apparentPower_FV;
powerFactor_HO = realPower_HO / apparentPower_HO;
balancePower=realPower_FV-realPower_HO;
//balancePower=-3400;
//Reset accumulators
sumV = 0;
sumI_FV = 0;
sumI_HO = 0;
sumP_FV = 0;
sumP_HO = 0;
/*
########################### OUTPUT #########################
*/
if (blndebug){
//Output to serial
Serial.print(realPower_FV);
Serial.print(' ');
Serial.print(apparentPower_FV);
Serial.print(' ');
Serial.print(powerFactor_FV);
Serial.print(' ');
Serial.print(Vrms);
Serial.print(' ');
Serial.println(Irms_FV);
Serial.print(realPower_HO);
Serial.print(' ');
Serial.print(apparentPower_HO);
Serial.print(' ');
Serial.print(powerFactor_HO);
Serial.print(' ');
Serial.print(Vrms);
Serial.print(' ');
Serial.println(Irms_HO);
}
//Ouput Real Power to the LCD
LcdClear();
gotoXY(10,0);
itoa((int)(Vrms), chvalue,10);
LcdString (chvalue);
LcdString ("V");
gotoXY(2,1);
itoa((int)(realPower_FV), chvalue,10);
LcdString ("FV: ");
LcdString (chvalue);
LcdString ("W");
gotoXY(2,2);
itoa((int)(realPower_HO), chvalue,10);
LcdString ("HO: -");
LcdString (chvalue);
LcdString ("W");
gotoXY(0,4);
LcdString ("- Bilancio -");
gotoXY(2,5);
itoa((int)(balancePower), chvalue,10);
LcdString ("SUM: ");
LcdString (chvalue);
LcdString ("W");
//output to graph bar
if (balancePower<-3300) {
for(LED=20;LED<30;LED++)
{
BG.paint(LED,BLINK); // Turn LED on
BG.send(); // Send the canvas to the bargraph
}
BLINK=!BLINK;
}
else if (balancePower>=-3300 && balancePower<-3083){
BG.clear(); // Clear the canvas
BG.paint(29,HIGH);
BG.paint(28,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-3083 && balancePower<-2866){
BG.clear(); // Clear the canvas
BG.paint(28,HIGH);
BG.paint(27,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-2866 && balancePower<-2648){
BG.clear(); // Clear the canvas
BG.paint(27,HIGH);
BG.paint(26,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-2648 && balancePower<-2431){
BG.clear(); // Clear the canvas
BG.paint(26,HIGH);
BG.paint(25,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-2431 && balancePower<-2214){
BG.clear(); // Clear the canvas
BG.paint(25,HIGH);
BG.paint(24,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-2214 && balancePower<-1997){
BG.clear(); // Clear the canvas
BG.paint(24,HIGH);
BG.paint(23,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-1997 && balancePower<-1779){
BG.clear(); // Clear the canvas
BG.paint(23,HIGH);
BG.paint(22,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-1779 && balancePower<-1562){
BG.clear(); // Clear the canvas
BG.paint(22,HIGH);
BG.paint(21,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-1562 && balancePower<-1345){
BG.clear(); // Clear the canvas
BG.paint(21,HIGH);
BG.paint(20,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-1345 && balancePower<-1128){
BG.clear(); // Clear the canvas
BG.paint(20,HIGH);
BG.paint(19,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-1128 && balancePower<-910){
BG.clear(); // Clear the canvas
BG.paint(19,HIGH);
BG.paint(18,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-910 && balancePower<-693){
BG.clear(); // Clear the canvas
BG.paint(18,HIGH);
BG.paint(17,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-693 && balancePower<-476){
BG.clear(); // Clear the canvas
BG.paint(17,HIGH);
BG.paint(16,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-476 && balancePower<-259){
BG.clear(); // Clear the canvas
BG.paint(16,HIGH);
BG.paint(15,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-259 && balancePower<-41){
BG.clear(); // Clear the canvas
BG.paint(15,HIGH);
BG.paint(14,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=-41 && balancePower<176){
BG.clear(); // Clear the canvas
BG.paint(14,HIGH);
BG.paint(13,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=176 && balancePower<393){
BG.clear(); // Clear the canvas
BG.paint(13,HIGH);
BG.paint(12,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=393 && balancePower<610){
BG.clear(); // Clear the canvas
BG.paint(12,HIGH);
BG.paint(11,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=610 && balancePower<828){
BG.clear(); // Clear the canvas
BG.paint(11,HIGH);
BG.paint(10,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=828 && balancePower<1045){
BG.clear(); // Clear the canvas
BG.paint(10,HIGH);
BG.paint(9,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=1045 && balancePower<1262){
BG.clear(); // Clear the canvas
BG.paint(9,HIGH);
BG.paint(8,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=1262 && balancePower<1479){
BG.clear(); // Clear the canvas
BG.paint(8,HIGH);
BG.paint(7,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=1479 && balancePower<1697){
BG.clear(); // Clear the canvas
BG.paint(7,HIGH);
BG.paint(6,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=1697 && balancePower<1914){
BG.clear(); // Clear the canvas
BG.paint(6,HIGH);
BG.paint(5,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=1914 && balancePower<2131){
BG.clear(); // Clear the canvas
BG.paint(5,HIGH);
BG.paint(4,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=2131 && balancePower<2348){
BG.clear(); // Clear the canvas
BG.paint(4,HIGH);
BG.paint(3,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=2348 && balancePower<2566){
BG.clear(); // Clear the canvas
BG.paint(3,HIGH);
BG.paint(2,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=2566 && balancePower<2783){
BG.clear(); // Clear the canvas
BG.paint(2,HIGH);
BG.paint(1,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=2783 && balancePower<3000){
BG.clear(); // Clear the canvas
BG.paint(1,HIGH);
BG.paint(0,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower>=3000 ){
for(LED=0;LED<10;LED++)
{
BG.paint(LED,BLINK); // Turn LED on
BG.send(); // Send the canvas to the bargraph
}
BLINK=!BLINK;
}
//output to DB every minute
if(time>savetodb+60000){
WebClient();
savetodb=millis();
}
}
void WebClient(){
if (client.connect()){
client.flush();
client.print("GET ");
client.print("/fv.php?user=user&password=pass&data=");
client.print(realPower_FV);
client.print(";");
client.print(apparentPower_FV);
client.print(";");
client.print(powerFactor_FV);
client.print(";");
client.print(Irms_FV);
client.print(";");
client.print(realPower_HO);
client.print(";");
client.print(apparentPower_HO);
client.print(";");
client.print(powerFactor_HO);
client.print(";");
client.print(Irms_HO);
client.print(";");
client.print(Vrms);
client.println(" HTTP/1.0");
client.println();
while(!client.available()){
delay(1);
}
while (client.available()){
char c = client.read();
if (blndebug) Serial.print(c);
}
if (blndebug) Serial.println();
}
else {
if (blndebug) Serial.println("EXCEPTION: during HTTP GET. Could not connect");
return;
}
while(client.connected()){
if (blndebug) Serial.println("Waiting for server to disconnect");
}
client.stop();
}
/*
Photovoltaic & Home Energy Monitor V 0.2
Fabio Roverso 21/12/11
based on:
//Basic energy monitoring sketch
//Authors: Trystan Lea, Eric Sandeen
//Licenced under GNU General Public Licence more details here
// openenergymonitor.org
//Sketch measures voltage and current.
//and then calculates useful values like real power,
//apparent power, powerfactor, Vrms, Irms.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <SFEbarGraph.h>
// millis (for db saving)
unsigned long time;
unsigned long savetodb;
// debug output switch
boolean blndebug=true;
// power range for the ledbar
int Max_Production = 3000;
int Min_Production = -3100;
unsigned int lapses = 30;
int increment = (Max_Production - Min_Production) / lapses;
/*
########################### LED BARGRAPH #########################
*/
// create a bargraph object
SFEbarGraph BG;
unsigned int LED;
boolean BLINK=true;
/*
########################### ENERGY MONITOR #########################
*/
//Used to convert integer to char for LCD
char chvalue [10];
//Setup variables
int numberOfSamples = 3000;
//Set Voltage and current input pins
int inPinV = 1;
int inPinI_FV = 2; //Photovoltaic CT
int inPinI_HO = 3; //Home CT
//Sanity check calibration method thanks to Eric Sandeen http://sandeen.net/wordpress
//See discussion here: http://openenergymonitor.org/emon/node/59
//Enter the values of your setup below
// Voltage is reduced both by wall xfmr & voltage divider
#define AC_WALL_VOLTAGE 230
#define AC_ADAPTER_VOLTAGE 9
// Ratio of the voltage divider in the circuit #da verificare#
#define AC_VOLTAGE_DIV_RATIO 11
// CT: Voltage depends on current, burden resistor, and turns
//Using SCT-013-030 reference sheet
#define CT_BURDEN_RESISTOR 62
#define CT_TURNS 1800
//Calibration coeficients
//These need to be set in order to obtain accurate results
//Set the above values first and then calibrate futher using normal calibration method described on how to build it page.
double VCAL = 1.242937853107345;
double ICAL_FV = 0.9931972789115646;
double ICAL_HO = 0.9931972789115646;
double PHASECAL = 2.3;
// Initial gueses for ratios, modified by VCAL/ICAL tweaks
#define AC_ADAPTER_RATIO (AC_WALL_VOLTAGE / AC_ADAPTER_VOLTAGE)
double V_RATIO = AC_ADAPTER_RATIO * AC_VOLTAGE_DIV_RATIO * 5 / 1024 * VCAL;
double I_RATIO_FV = (long double)CT_TURNS / CT_BURDEN_RESISTOR * 5 / 1024 * ICAL_FV;
double I_RATIO_HO = (long double)CT_TURNS / CT_BURDEN_RESISTOR * 5 / 1024 * ICAL_HO;
//Sample variables
int lastSampleV,
lastSampleI_FV,
lastSampleI_HO,
sampleV,
sampleI_FV,
sampleI_HO;
//Filter variables
double lastFilteredV,
lastFilteredI_FV,
lastFilteredI_HO,
filteredV,
filteredI_FV,
filteredI_HO;
double filterTemp;
//Stores the phase calibrated instantaneous voltage.
double shiftedV;
//Power calculation variables
double sqI_FV,
sqI_HO,
sqV,
instP_FV,
instP_HO,
sumI_FV,
sumI_HO,
sumV,
sumP_FV,
sumP_HO;
//Useful value variables
double realPower_FV,
realPower_HO,
balancePower,
apparentPower_FV,
apparentPower_HO,
powerFactor_FV,
powerFactor_HO,
Vrms,
Irms_FV,
Irms_HO;
/*
########################### NETWORK #########################
*/
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x65, 0xAB }; //Arduino MAC address
byte ip[] = {
192, 168, 1, 179 }; //Arduino IP address
byte gateway[] = {
192, 168, 1, 250 }; //Network gateway
byte subnet[] = {
255, 255, 255, 0 }; //Network subnet mask
//Web client stuff
byte server[] = {
192 ,168 ,12 ,8 }; //Web server address
unsigned int port = 82; //Web server http port (usually 80)
Client client(server, port); //Creates an http client handle
/*
########################### LCD MONITOR #########################
This Code has extra features
including a XY positioning function on Display
and a Line Draw function on Nokia 3310 LCD
It is modded from the original
http://www.arduino.cc/playground/Code/PCD8544
*/
// Mods by Jim Park
// jim(^dOt^)buzz(^aT^)gmail(^dOt^)com
// hope it works for you
#define PIN_SCE 7 // LCD CS .... Pin 3
#define PIN_RESET 6 // LCD RST .... Pin 1
#define PIN_DC 5 // LCD Dat/Com. Pin 5
#define PIN_SDIN 2 // LCD SPIDat . Pin 6
#define PIN_SCLK 3 // LCD SPIClk . Pin 4
// LCD Gnd .... Pin 2
// LCD Vcc .... Pin 8
// LCD Vlcd ... Pin 7
#define LCD_C LOW
#define LCD_D HIGH
#define LCD_X 84
#define LCD_Y 48
#define LCD_CMD 0
static const byte ASCII[][5] =
{
{0x00, 0x00, 0x00, 0x00, 0x00} // 20
,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #
,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $
,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 '
,{0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (
,{0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )
,{0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *
,{0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +
,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d -
,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0
,{0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1
,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
,{0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3
,{0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4
,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6
,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
,{0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9
,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
,{0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @
,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A
,{0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B
,{0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C
,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D
,{0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E
,{0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F
,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G
,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H
,{0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I
,{0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J
,{0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K
,{0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L
,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M
,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N
,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O
,{0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P
,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q
,{0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R
,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
,{0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T
,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U
,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V
,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W
,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
,{0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [
,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c ¥
,{0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]
,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
,{0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b
,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
,{0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d
,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
,{0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f
,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g
,{0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h
,{0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i
,{0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j
,{0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k
,{0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l
,{0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m
,{0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n
,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
,{0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p
,{0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q
,{0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r
,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
,{0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t
,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u
,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v
,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w
,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y
,{0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z
,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
,{0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |
,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
,{0x10, 0x08, 0x08, 0x10, 0x08} // 7e ?
,{0x00, 0x06, 0x09, 0x09, 0x06} // 7f ?
};
void LcdCharacter(char character)
{
LcdWrite(LCD_D, 0x00);
for (int index = 0; index < 5; index++)
{
LcdWrite(LCD_D, ASCII[character - 0x20][index]);
}
LcdWrite(LCD_D, 0x00);
}
void LcdClear(void)
{
for (int index = 0; index < LCD_X * LCD_Y / 8; index++)
{
LcdWrite(LCD_D, 0x00);
}
}
void LcdInitialise(void)
{
pinMode(PIN_SCE, OUTPUT);
pinMode(PIN_RESET, OUTPUT);
pinMode(PIN_DC, OUTPUT);
pinMode(PIN_SDIN, OUTPUT);
pinMode(PIN_SCLK, OUTPUT);
digitalWrite(PIN_RESET, LOW);
// delay(1);
digitalWrite(PIN_RESET, HIGH);
LcdWrite( LCD_CMD, 0x21 ); // LCD Extended Commands.
LcdWrite( LCD_CMD, 0xBf ); // Set LCD Vop (Contrast). //B1
LcdWrite( LCD_CMD, 0x04 ); // Set Temp coefficent. //0x04
LcdWrite( LCD_CMD, 0x14 ); // LCD bias mode 1:48. //0x13
LcdWrite( LCD_CMD, 0x0C ); // LCD in normal mode. 0x0d for inverse
LcdWrite(LCD_C, 0x20);
LcdWrite(LCD_C, 0x0C);
}
void LcdString(char *characters)
{
while (*characters)
{
LcdCharacter(*characters++);
}
}
void LcdWrite(byte dc, byte data)
{
digitalWrite(PIN_DC, dc);
digitalWrite(PIN_SCE, LOW);
shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
digitalWrite(PIN_SCE, HIGH);
}
// gotoXY routine to position cursor
// x - range: 0 to 84
// y - range: 0 to 5
void gotoXY(int x, int y)
{
LcdWrite( 0, 0x80 | x); // Column.
LcdWrite( 0, 0x40 | y); // Row.
}
void drawLine(void)
{
unsigned char j;
for(j=0; j<84; j++) // top
{
gotoXY (j,0);
LcdWrite (1,0x01);
}
for(j=0; j<84; j++) //Bottom
{
gotoXY (j,5);
LcdWrite (1,0x80);
}
for(j=0; j<6; j++) // Right
{
gotoXY (83,j);
LcdWrite (1,0xff);
}
for(j=0; j<6; j++) // Left
{
gotoXY (0,j);
LcdWrite (1,0xff);
}
}
/*
###########################################################
########################### SETUP #########################
###########################################################
*/
void setup(void)
{
// start LCD
LcdInitialise();
LcdClear();
// define ethernet SS
pinMode(10, OUTPUT);
// start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
// start serial
Serial.begin(9600);
// initialize the bargraph library
BG.begin(9,1);
// Turn all LEDs on then off using BG.paint()
for(LED=0;LED<(1*30);LED++) // Increment through all the LEDs from 0 to as many as we have
{
BG.paint(LED,HIGH); // Turn LED on
BG.send(); // Send the canvas to the bargraph
delay(50);
}
for(LED=0;LED<(1*30);LED++) // Increment through all the LEDs from 0 to as many as we have
{
BG.paint(LED,LOW); // Turn LED off
BG.send(); // Send the canvas to the bargraph
delay(50);
}
// initialize timers
time = millis();
savetodb = millis();
}
/*
###########################################################
########################### LOOP ##########################
###########################################################
*/
void loop(){
time=millis();
for (int n=0; n<numberOfSamples; n++)
{
//Used for offset removal
lastSampleV=sampleV;
lastSampleI_FV=sampleI_FV;
lastSampleI_HO=sampleI_HO;
//Read in voltage and current samples.
sampleV = analogRead(inPinV);
sampleI_FV = analogRead(inPinI_FV);
sampleI_HO = analogRead(inPinI_HO);
//Used for offset removal
lastFilteredV = filteredV;
lastFilteredI_FV = filteredI_FV;
lastFilteredI_HO = filteredI_HO;
//Digital high pass filters to remove 2.5V DC offset.
filteredV = 0.996*(lastFilteredV+sampleV-lastSampleV);
filteredI_FV = 0.996*(lastFilteredI_FV+sampleI_FV-lastSampleI_FV);
filteredI_HO = 0.996*(lastFilteredI_HO+sampleI_HO-lastSampleI_HO);
//Phase calibration goes here.
shiftedV = lastFilteredV + PHASECAL * (filteredV - lastFilteredV);
//Root-mean-square method voltage
//1) square voltage values
sqV= filteredV * filteredV;
//2) sum
sumV += sqV;
//Root-mean-square method current
//1) square current values
sqI_FV = filteredI_FV * filteredI_FV;
sqI_HO = filteredI_HO * filteredI_HO;
//2) sum
sumI_FV += sqI_FV;
sumI_HO += sqI_HO;
//Instantaneous Power
instP_FV = shiftedV * filteredI_FV;
instP_HO = shiftedV * filteredI_HO;
//Sum
sumP_FV +=instP_FV;
sumP_HO +=instP_HO;
}
//Calculation of the root of the mean of the voltage and current squared (rms)
//Calibration coeficients applied.
Vrms = V_RATIO*sqrt(sumV / numberOfSamples);
Irms_FV = I_RATIO_FV*sqrt(sumI_FV / numberOfSamples);
Irms_HO = I_RATIO_HO*sqrt(sumI_HO / numberOfSamples);
//Calculation power values
realPower_FV = V_RATIO*I_RATIO_FV*sumP_FV / numberOfSamples;
realPower_HO = V_RATIO*I_RATIO_HO*sumP_HO / numberOfSamples;
apparentPower_FV = Vrms * Irms_FV;
apparentPower_HO = Vrms * Irms_HO;
powerFactor_FV = realPower_FV / apparentPower_FV;
powerFactor_HO = realPower_HO / apparentPower_HO;
//Home power balance
balancePower=realPower_FV-realPower_HO;
//Reset accumulators
sumV = 0;
sumI_FV = 0;
sumI_HO = 0;
sumP_FV = 0;
sumP_HO = 0;
/*
########################### OUTPUT #########################
*/
if (blndebug){
//Output to serial
Serial.println("Real_FV Apparent_FV P_Fact_FV I_FV Real_HO Apparent_HO P_Fact_HO I_HO V");
Serial.print(realPower_FV);
Serial.print(' ');
Serial.print(apparentPower_FV);
Serial.print(' ');
Serial.print(powerFactor_FV);
Serial.print(' ');
Serial.print(Irms_FV);
Serial.print(' ');
Serial.print(realPower_HO);
Serial.print(' ');
Serial.print(apparentPower_HO);
Serial.print(' ');
Serial.print(powerFactor_HO);
Serial.print(' ');
Serial.print(Irms_HO);
Serial.print(' ');
Serial.println(Vrms);
}
//Ouput Real Power to the LCD
LcdClear();
gotoXY(2,0);
LcdString ("Vrms: ");
itoa((int)(Vrms), chvalue,10);
LcdString (chvalue);
LcdString (" V");
gotoXY(2,1);
itoa(((int)(realPower_FV/10)*10), chvalue,10);
LcdString ("FV: +");
LcdString (chvalue);
LcdString (" W");
gotoXY(2,2);
itoa(((int)(realPower_HO/10)*10), chvalue,10);
LcdString ("HO: -");
LcdString (chvalue);
LcdString (" W");
gotoXY(0,4);
LcdString ("- Bilancio -");
gotoXY(10,5);
itoa(((int)(balancePower/10)*10), chvalue,10);
//LcdString ("BILANCIO: ");
LcdString (chvalue);
LcdString (" W");
//output to graph bar
if (balancePower < Min_Production - increment) {
for(LED=20;LED<30;LED++)
{
BG.paint(LED,BLINK); // Turn LED on
BG.send(); // Send the canvas to the bargraph
}
BLINK=!BLINK;
}
else if (balancePower >= (Min_Production - increment) && balancePower < Min_Production){
BG.clear(); // Clear the canvas
BG.paint(29,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= Min_Production && balancePower < (Min_Production + 1 * increment)){
BG.clear(); // Clear the canvas
BG.paint(29,HIGH);
BG.paint(28,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 1 * increment) && balancePower < (Min_Production + 2 * increment)){
BG.clear(); // Clear the canvas
BG.paint(28,HIGH);
BG.paint(27,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 2 * increment) && balancePower < (Min_Production + 3 * increment)){
BG.clear(); // Clear the canvas
BG.paint(27,HIGH);
BG.paint(26,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 3 * increment) && balancePower < (Min_Production + 4 * increment)){
BG.clear(); // Clear the canvas
BG.paint(26,HIGH);
BG.paint(25,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 4 * increment) && balancePower < (Min_Production + 5 * increment)){
BG.clear(); // Clear the canvas
BG.paint(25,HIGH);
BG.paint(24,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 5 * increment) && balancePower < (Min_Production + 6 * increment)){
BG.clear(); // Clear the canvas
BG.paint(24,HIGH);
BG.paint(23,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 6 * increment) && balancePower < (Min_Production + 7 * increment)){
BG.clear(); // Clear the canvas
BG.paint(23,HIGH);
BG.paint(22,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 7 * increment) && balancePower < (Min_Production + 8 * increment)){
BG.clear(); // Clear the canvas
BG.paint(22,HIGH);
BG.paint(21,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 8 * increment) && balancePower < (Min_Production + 9 * increment)){
BG.clear(); // Clear the canvas
BG.paint(21,HIGH);
BG.paint(20,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 9 * increment) && balancePower < (Min_Production + 10 * increment)){
BG.clear(); // Clear the canvas
BG.paint(20,HIGH);
BG.paint(19,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 10 * increment) && balancePower < (Min_Production + 11 * increment)){
BG.clear(); // Clear the canvas
BG.paint(19,HIGH);
BG.paint(18,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 11 * increment) && balancePower <- (Min_Production + 12 * increment)){
BG.clear(); // Clear the canvas
BG.paint(18,HIGH);
BG.paint(17,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 12 * increment) && balancePower < (Min_Production + 13 * increment)){
BG.clear(); // Clear the canvas
BG.paint(17,HIGH);
BG.paint(16,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 13 * increment) && balancePower < (Min_Production + 14 * increment)){
BG.clear(); // Clear the canvas
BG.paint(16,HIGH);
BG.paint(15,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 14 * increment) && balancePower < (Min_Production + 15 * increment)){
BG.clear(); // Clear the canvas
BG.paint(15,HIGH);
BG.paint(14,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 15 * increment) && balancePower < (Min_Production + 16 * increment)){
BG.clear(); // Clear the canvas
BG.paint(14,HIGH);
BG.paint(13,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 16 * increment) && balancePower < (Min_Production + 17 * increment)){
BG.clear(); // Clear the canvas
BG.paint(13,HIGH);
BG.paint(12,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 17 * increment) && balancePower < (Min_Production + 18 * increment)){
BG.clear(); // Clear the canvas
BG.paint(12,HIGH);
BG.paint(11,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 18 * increment) && balancePower < (Min_Production + 19 * increment)){
BG.clear(); // Clear the canvas
BG.paint(11,HIGH);
BG.paint(10,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 19 * increment) && balancePower < (Min_Production + 20 * increment)){
BG.clear(); // Clear the canvas
BG.paint(10,HIGH);
BG.paint(9,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 20 * increment) && balancePower < (Min_Production + 21 * increment)){
BG.clear(); // Clear the canvas
BG.paint(9,HIGH);
BG.paint(8,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 21 * increment) && balancePower < (Min_Production + 22 * increment)){
BG.clear(); // Clear the canvas
BG.paint(8,HIGH);
BG.paint(7,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 22 * increment) && balancePower < (Min_Production + 23 * increment)){
BG.clear(); // Clear the canvas
BG.paint(7,HIGH);
BG.paint(6,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 23 * increment) && balancePower < (Min_Production + 24 * increment)){
BG.clear(); // Clear the canvas
BG.paint(6,HIGH);
BG.paint(5,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 24 * increment) && balancePower < (Min_Production + 25 * increment)){
BG.clear(); // Clear the canvas
BG.paint(5,HIGH);
BG.paint(4,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 25 * increment) && balancePower < (Min_Production + 26 * increment)){
BG.clear(); // Clear the canvas
BG.paint(4,HIGH);
BG.paint(3,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 26 * increment) && balancePower < (Min_Production + 27 * increment)){
BG.clear(); // Clear the canvas
BG.paint(3,HIGH);
BG.paint(2,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 27 * increment) && balancePower < (Min_Production + 28 * increment)){
BG.clear(); // Clear the canvas
BG.paint(2,HIGH);
BG.paint(1,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 28 * increment) && balancePower < (Min_Production + 29 * increment)){
BG.clear(); // Clear the canvas
BG.paint(1,HIGH);
BG.paint(0,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 29 * increment) && balancePower < (Min_Production + 30 * increment)){
BG.clear(); // Clear the canvas
BG.paint(0,HIGH);
BG.send(); // Send the canvas to the bargraph
}
else if (balancePower >= (Min_Production + 30 * increment) ){
for(LED=0;LED<10;LED++)
{
BG.paint(LED,BLINK); // Turn LED on
BG.send(); // Send the canvas to the bargraph
}
BLINK=!BLINK;
}
//output to DB every minute
if(time>savetodb+60000){
WebClient();
savetodb=millis();
}
}
void WebClient(){
if (client.connect()){
client.flush();
client.print("GET ");
client.print("/fv.php?user=user&password=pass&data=");
client.print(realPower_FV);
client.print(";");
client.print(apparentPower_FV);
client.print(";");
client.print(powerFactor_FV);
client.print(";");
client.print(Irms_FV);
client.print(";");
client.print(realPower_HO);
client.print(";");
client.print(apparentPower_HO);
client.print(";");
client.print(powerFactor_HO);
client.print(";");
client.print(Irms_HO);
client.print(";");
client.print(Vrms);
client.println(" HTTP/1.0");
client.println();
while(!client.available()){
delay(1);
}
while (client.available()){
char c = client.read();
if (blndebug) Serial.print(c);
}
if (blndebug) Serial.println();
}
else {
if (blndebug) Serial.println("EXCEPTION: during HTTP GET. Could not connect");
return;
}
while(client.connected()){
if (blndebug) Serial.println("Waiting for server to disconnect");
}
client.stop();
}
cristianc ha scritto:Complimenti Fabio e grazie per la condivisione. Mi sto cimentando da poco anche io
con arduino, vorrei cominciare con il monitoraggio del solare ed ho una domanda
stupida: se l'arduino non è collegato al pc con l'USB ma lo metto in funzione con
un alimentatore, le istruzioni Serial.print o Serial.println bloccano l'esecuzione dello
sketch arduino?
Poi volevo chiederti, per monitorare il consumo di corrente o la produzione si
può utilizzarequesta schedache ho visto casualmente ieri?