Close

Arduino Sketch

A project log for Web based solar panel monitoring/managment

Arduino enable web server to monitor a small solar array and power management systems.

garyGary 10/12/2014 at 00:300 Comments

Here is the Arduino Sketch. Nothing fancy. It is a mix of my code, code fragments from other peoples projects as well as vendor sample code.

#include <SPI.h>

#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.

// The IP address will be dependent on your local network:

byte mac[] = {

0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xee

};

IPAddress ip(192, 168, 1, 178);

IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 255, 0);

// Initialize the Ethernet server library

// with the IP address and port you want to use

// (port 80 is default for HTTP):

EthernetServer server(80);

float current_input_value = 0; // value read from the carrier board

float amps = 0;

void setup() {

// Open serial communications and wait for port to open:

Serial.begin(9600);

while (!Serial) {

; // wait for serial port to connect. Needed for Leonardo only

}

// start the Ethernet connection and the server:

Ethernet.begin(mac, ip);

server.begin();

Serial.print("server is at ");

Serial.println(Ethernet.localIP());

}

void loop() {

float voltage;

float watts;

char buffer[10];

String S_label ;

String S_voltage ;

String S_amps ;

String S_watts ;

String S_print = S_label + S_voltage + S_amps + S_watts;

// listen for incoming clients

EthernetClient client = server.available();

if (client) {

Serial.println("new client");

// an http request ends with a blank line

boolean currentLineIsBlank = true;

while (client.connected()) {

if (client.available()) {

char c = client.read();

Serial.write(c);

// if you've gotten to the end of the line (received a newline

// character) and the line is blank, the http request has ended,

// so you can send a reply

if (c == '\n' && currentLineIsBlank) {

// send a standard http response header

client.println("HTTP/1.1 200 OK");

client.println("Content-Type: text/html");

client.println("Connection: close"); // the connection will be closed after completion of the response

client.println("Refresh: 5"); // refresh the page automatically every 5 sec

client.println();

client.println("<!DOCTYPE HTML>");

client.println("<html>");

//get the volatage

voltage=get_voltage(A0); //get volatge on A0

//Get the Current

current_input_value = analogRead(A1);

amps = float ((((long)current_input_value * 5000 / 1024) - 500 ) * 1000 / 133)/1000;

if (amps < 0 )

amps = 0 ; // Get rid of sensor noise when currentt is at/near zero.

watts=(amps * voltage);

S_label = "Panel" ;

S_voltage = dtostrf(voltage, 2,2, buffer );

S_amps = dtostrf(amps, 2,2, buffer );

S_watts = dtostrf(watts, 2,2, buffer );

S_print = S_label + " " + S_voltage + " " + S_amps + " " + S_watts;

client.println (S_print);

client.println ("<br>");

//get the volatage

voltage=get_voltage(A2); //get volatge on A5

//Get the Current

current_input_value = analogRead(A3);

amps = float ((((long)current_input_value * 5000 / 1024) - 500 ) * 1000 / 133)/1000;

if (amps < 0 )

amps = 0 ; // Get rid of sensor noise when currentt is at/near zero.

watts=(amps * voltage) ;

S_label = "Battery" ;

S_voltage = dtostrf(voltage, 2,2, buffer );

S_amps = dtostrf(amps, 2,2, buffer );

S_watts = dtostrf(watts, 2,2, buffer );

S_print = S_label + " " + S_voltage + " " + S_amps + " " + S_watts;

client.println (S_print);

client.println ("<br>");

//get the volatage

voltage=get_voltage(A4); //get volatge on A5

//Get the Current

current_input_value = analogRead(A5);

amps = float ((((long)current_input_value * 5000 / 1024) - 500 ) * 1000 / 133)/1000;

if (amps < 0 )

amps = 0 ; // Get rid of sensor noise when currentt is at/near zero.

watts=(amps * voltage);

S_label = "Load" ;

S_voltage = dtostrf(voltage, 2,2, buffer );

S_amps = dtostrf(amps, 2,2, buffer );

S_watts = dtostrf(watts, 2,2, buffer );

S_print = S_label + " " + S_voltage + " " + S_amps + " " + S_watts;

client.println (S_print);

client.println ("<br>");

client.println("</html>");

break;

}

if (c == '\n') {

// you're starting a new line

currentLineIsBlank = true;

}

else if (c != '\r') {

// you've gotten a character on the current line

currentLineIsBlank = false;

}

}

}

// give the web browser time to receive the data

delay(1);

// close the connection:

client.stop();

Serial.println("client disconnected");

}

}

float get_voltage ( int vpin ) {

float gvolt=(analogRead(vpin)/4.092)/10;

return gvolt;

}

Discussions