serial print to web server on sd

Hi all,

I am new to arduino and I dont know how to display the following current sensor to a webpage on sd using ajax beacuse I have to calculate whatis in loop and after to send it (sound silly?):

#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance

void setup()
{  
  Serial.begin(9600);
  
  emon1.current(1, 111.1);             // Current: input pin, calibration.
}

void loop()
{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  
  Serial.print(Irms*230.0);	       // Apparent power
  Serial.print(" ");
  Serial.println(Irms);		       // Irms
}

Please help me with an example!

an this is the web server... please help!

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1; 

// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ   50

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80
File webFile;               // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0;              // index into HTTP_req buffer

void setup()
{
    // disable Ethernet chip
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);

    Serial.begin(9600);       // for debugging

    emon1.current(3, 51.5);             // Current: input pin, calibration.

    
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm file.");
    
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    
}

void loop()
{
  
double Irms = emon1.calcIrms(1440);  // Calculate Irms only
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // buffer first part of HTTP request in HTTP_req array (string)
                // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
                if (req_index < (REQ_BUF_SZ - 1)) {
                    HTTP_req[req_index] = c;          // save HTTP request character
                    req_index++;
                }
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    // remainder of header follows below, depending on if
                    // web page or XML page is requested
                    // Ajax request - send XML file
                    if (StrContains(HTTP_req, "ajax_inputs")) {
                        // send rest of HTTP header
                        client.println("Content-Type: text/xml");
                        client.println("Connection: keep-alive");
                        client.println();
                        // send XML file containing input states
                        XML_response(client);
                    }
                    else {  // web page request
                        // send rest of HTTP header
                        client.println("Content-Type: text/html");
                        client.println("Connection: keep-alive");
                        client.println();
                        // send web page
                        webFile = SD.open("index.htm");        // open web page file
                        if (webFile) {
                            while(webFile.available()) {
                                client.write(webFile.read()); // send web page to client
                            }
                            webFile.close();
                        }
                    }
                    // display received HTTP request on serial port
                    Serial.print(HTTP_req);
                    // reset buffer index and all buffer elements to 0
                    req_index = 0;
                    StrClear(HTTP_req, REQ_BUF_SZ);
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}
// send the XML file containing analog value
void XML_response(EthernetClient cl)
{
    int analog_val;            // stores value read from analog inputs
    int count;                 // used by 'for' loops
    
    cl.print("<?xml version = \"1.0\" ?>");
    cl.print("<inputs>");
   // read analog inputs
    for (count = 3; count <= 5; count++) { // A3 to A5
        analog_val = analogRead(count);
        cl.print("<analog>");
        cl.print(analog_val);
        cl.println("</analog>");
    }
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;

    len = strlen(str);
    
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }

    return 0;
}

Thank you guys but I found it

You have to put in

void setup(){
the following print:

double Irms = emon1.calcIrms(2000);  // Calculate Irms only
  
cl.print("<analog>");
  cl.print(" ");
  cl.print(Irms*232.0/100);         // Apparent power
  cl.println("</analog>");
  cl.print("<analog>");
  cl.print(" ");
  cl.println(Irms);          // Irms
cl.println("</analog>");

But is there anyone who know how to monitor 3 phases?

firtst, I do not think your client print should be on setup function because it appennd only once, not on demand on your server... I'm maybe wrong without the whole code.

For that is measuring 3 phase, if it is on a single device like a 3 phase motor, eacch phase will have the same current, so measure only one phase, and to know the power, it is:

P = VI1.732

Regards.

Nitrof

Thank you very much Nitrof, for your fast answer.

Yes, you have right. I manage to print analog values on webpage but I got the same results on all three of them, and I want to measure separate lines. I have L1, L2, L3, Null, and earth.

I will try your way but if you have some time please explain better.

Thank you!

I,m not sure about what your application is...

I took a look back to the example,

double Irms = emon1.calcIrms(2000); calculate the current, but it have to do it in void loop(); to constently do it. if You what to print it but but at each loop, put it inside a condition, a counter or timer.

To have many sensor, start many instance as much you need.

EnergyMonitor emon1; 
EnergyMonitor emon2; 
EnergyMonitor emon3;

you will need setup function for each instance:

emon1.current(1, 111.1);
emon2.current(1, 111.1);
emon3.current(1, 111.1);

and acces current reading the same way:

double Irms = emon1.calcIrms(2000);
double Irms = emon2.calcIrms(2000);
double Irms = emon3.calcIrms(2000);

For the reason to not read 3 line a the same device, if it is a 3 phase motor for example, current on the 3 line will be the same. ( I1=I2=I3 ), so no need to monitor all 3 line.

if you plan to monitor a distribution panel with many device, working at different time and/or are not balanced, this is another story. It is a good idea to monitor each line.

BTW, emonLib use timer to measure the current and it take some time for each reading, so with many probe, It could be difficult to run a web server on top of it.

regards

Nitrof

Thanks again Nitrof,

I follow your sugestions and if I put

   double Irms = emon1.calcIrms(2000);  // Calculate Irms only
   double Irms = emon2.calcIrms(2000);  // Calculate Irms only
   double Irms = emon3.calcIrms(2000);  // Calculate Irms only

I receive the following error:

exit status 1
redeclaration of 'double Irms'

If I use only one like:
double Irms = emon1.calcIrms(2000); // Calculate Irms only

its working. but monitor only one phase.

The code:

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1; 
EnergyMonitor emon2; 
EnergyMonitor emon3; 

// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ   50

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80
File webFile;               // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0;              // index into HTTP_req buffer

void setup()
{
    // disable Ethernet chip
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);

    Serial.begin(9600);       // for debugging

    emon1.current(3, 51.5);             // Current: input pin, calibration.
    emon2.current(4, 51.5);             // Current: input pin, calibration.
    emon3.current(5, 51.5);             // Current: input pin, calibration.

    
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm file.");
    
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    
}

void loop()
{
  
double Irms = emon1.calcIrms(1440);  // Calculate Irms only
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // buffer first part of HTTP request in HTTP_req array (string)
                // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
                if (req_index < (REQ_BUF_SZ - 1)) {
                    HTTP_req[req_index] = c;          // save HTTP request character
                    req_index++;
                }
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    // remainder of header follows below, depending on if
                    // web page or XML page is requested
                    // Ajax request - send XML file
                    if (StrContains(HTTP_req, "ajax_inputs")) {
                        // send rest of HTTP header
                        client.println("Content-Type: text/xml");
                        client.println("Connection: keep-alive");
                        client.println();
                        // send XML file containing input states
                        XML_response(client);
                    }
                    else {  // web page request
                        // send rest of HTTP header
                        client.println("Content-Type: text/html");
                        client.println("Connection: keep-alive");
                        client.println();
                        // send web page
                        webFile = SD.open("index.htm");        // open web page file
                        if (webFile) {
                            while(webFile.available()) {
                                client.write(webFile.read()); // send web page to client
                            }
                            webFile.close();
                        }
                    }
                    // display received HTTP request on serial port
                    Serial.print(HTTP_req);
                    // reset buffer index and all buffer elements to 0
                    req_index = 0;
                    StrClear(HTTP_req, REQ_BUF_SZ);
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}
// send the XML file containing analog value
void XML_response(EthernetClient cl)
{
    int analog_val;            // stores value read from analog inputs
    int count;                 // used by 'for' loops
    
    cl.print("<?xml version = \"1.0\" ?>");
    cl.print("<inputs>");




    
   double Irms = emon1.calcIrms(2000);  // Calculate Irms only
  
cl.print("<analog>");
  cl.print(" ");
  cl.print(Irms*232.0/100);         // Apparent power
  cl.println("</analog>");
  cl.print("<analog>");
  cl.print(" ");
  cl.println(Irms);          // Irms
cl.println("</analog>");


cl.print("<analog>");
  cl.print(" ");
  cl.print(Irms*232.0/100);         // Apparent power
  cl.println("</analog>");
  cl.print("<analog>");
  cl.print(" ");
  cl.println(Irms);          // Irms
cl.println("</analog>");


cl.print("<analog>");
  cl.print(" ");
  cl.print(Irms*232.0/100);         // Apparent power
  cl.println("</analog>");
  cl.print("<analog>");
  cl.print(" ");
  cl.println(Irms);          // Irms
cl.println("</analog>");
}







// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;

    len = strlen(str);
    
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }

    return 0;
}

Can you assist me further please?

oups sorry... declaration of double variable have to have single name to...

double Irms1 = emon1.calcIrms(2000);  // Calculate Irms only
   double Irms2 = emon2.calcIrms(2000);  // Calculate Irms only
   double Irms3 = emon3.calcIrms(2000);  // Calculate Irms only

Thank you very much for your help
it's working :))))))))))))
you are a hero. Thx