Last active
August 29, 2015 14:04
-
-
Save tomsoft1/cdc856c91da6a1dfd86d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
#include "EmonLib.h" // Include Emon Library | |
#include <LiquidCrystal.h> | |
//#include <SD.h> | |
#define ETHERNET_LOGIN | |
#define APIKEY "<API_KEY>" // See www.opensensorcloud.com | |
#define DEVICE_ID "<DEVICE ID>" // See www.opensensorcloud.com | |
#define USERAGENT "Arduino" // user agent is the project name | |
#ifdef LCD | |
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); | |
int lcd_key = 0; | |
int adc_key_in = 0; | |
#define btnRIGHT 0 | |
#define btnUP 1 | |
#define btnDOWN 2 | |
#define btnLEFT 3 | |
#define btnSELECT 4 | |
#define btnNONE 5 | |
#endif | |
String one_reading(); | |
void sendData(String &thisData) ; | |
// assign a MAC address for the ethernet controller. | |
// fill in your address here: | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; | |
// fill in an available IP address on your network here, | |
// for manual configuration: | |
const IPAddress ip(10,0,1,20); | |
// initialize the library instance: | |
EthernetClient client; | |
// if you don't want to use DNS (and reduce your sketch size) | |
// use the numeric IP instead of the name for the server: | |
IPAddress server(88,190,31,53); // n | |
//const int port=80; | |
const int port=4000; | |
//const char server[] = "api.opensensorcloud.com"; // name address for opensensorcloud | |
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds | |
boolean lastConnected = false; // state of the connection last time through the main loop | |
const unsigned long postingInterval = 10*1000; //delay between updates to xively.com | |
#define NB_SENSORS 3 | |
EnergyMonitor emon[NB_SENSORS]; // Create an instance | |
double watts[NB_SENSORS]; | |
double sum[NB_SENSORS]; | |
double total; | |
int nb_samples; | |
int max=0; | |
int min=30000; | |
double total_watts=0.0; | |
double time_total=0.0; | |
void getData(char *server,String &theUrl); | |
#define JNPin 13 | |
#define chipSelect 4 | |
#define VOLT_FACTOR 220.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 | |
} | |
#ifdef LCD | |
lcd.begin(16, 2); | |
// Print a message to the LCD. | |
lcd.print("Energy Monitor"); | |
#endif | |
for(int i=0;i<NB_SENSORS;i++){ | |
// EnergyMonitor *tmpEmon=new EnergyMonitor(); | |
// emon[i]=tmpEmon; | |
emon[i].current(1+i, 60.6); // Current: input pin, calibration. | |
sum[i]=0.0; | |
} | |
one_reading(); | |
for(int i=0;i<NB_SENSORS;i++){ | |
sum[i]=0; | |
watts[i]=0; | |
} | |
total=0; | |
nb_samples=0; | |
// give the ethernet module time to boot up: | |
delay(1000); | |
#ifdef SD | |
// make sure that the default chip select pin is set to | |
// output, even if you don't use it: | |
pinMode(10, OUTPUT); | |
pinMode(JNPin, INPUT); | |
// see if the card is present and can be initialized: | |
if (!SD.begin(chipSelect)){ | |
Serial.println("Card failed, or not present"); | |
}else{ | |
Serial.println("card initialized."); | |
} | |
#endif | |
#ifdef ETHERNET_LOGIN | |
Serial.println("\nStarting ethernet"); | |
// start the Ethernet connection: | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
// DHCP failed, so use a fixed IP address: | |
Ethernet.begin(mac, ip); | |
} | |
Serial.println("Ethernet done..."); | |
#endif | |
#ifdef LCD | |
lcd.setCursor(0,1); | |
lcd.print("Init done"); | |
#endif | |
} | |
String res; | |
void loop() { | |
// float val = analogRead(A1); | |
one_reading(); // Use res | |
int current_watt=int((VOLT_FACTOR)*total/nb_samples); | |
if(current_watt>max){ | |
max=current_watt; | |
}; | |
if(current_watt<min){ | |
min=current_watt; | |
}; | |
Serial.println(" "); | |
#ifdef LCD | |
lcd.setCursor(0,0); | |
if(lcd_key==btnRIGHT){ | |
lcd.print("M:"+String(max)+" m:"+String(min)); | |
}else if(lcd_key==btnLEFT){ | |
lcd.print("Total:"+String(total_watts)+" kW "); | |
lcd.setCursor(0,1); | |
lcd.print("Times:"+String(time_total,0)+" sec "); | |
}else{ | |
lcd.print(res +" "); | |
lcd.setCursor(0,1); | |
lcd.print("Total: "+String(current_watt)+" W "); | |
} | |
#endif | |
// if there's incoming data from the net connection. | |
// send it out the serial port. This is for debugging | |
// purposes only: | |
while (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
} | |
// if there's no net connection, but there was one last time | |
// through the loop, then stop the client | |
if ( !client.connected() && lastConnected ) { | |
Serial.println(); | |
Serial.println("disconnecting."); | |
client.stop(); | |
lastConnected=NULL; | |
} | |
// if you're not connected, and ten seconds have passed since | |
// your last connection, then connect again and send data: | |
Serial.println("Time:"+String(millis()-lastConnectionTime )); | |
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { | |
Serial.println("Will post..."); | |
String data="["; | |
for(int i=0;i<NB_SENSORS;i++){ | |
data=data+"{\"name\":\"watt_"+String(i)+"\",\"value\":"+String((sum[i]/nb_samples)*VOLT_FACTOR)+"},"; | |
sum[i]=0; | |
} | |
double watts=(total/nb_samples)*VOLT_FACTOR; | |
data+="{\"name\":\"watt\",\"value\":"+String(watts)+"}"; | |
data+="]"; | |
// Compute kilowatt/h | |
long delta=(millis() - lastConnectionTime)/1000 ; // sec | |
time_total+=delta; | |
total_watts+=watts/1000.0; // Conso total en kW/h | |
Serial.println("total:"+String(watts)+" millis:"+String(delta)+" kW sincestart:"+String(total_watts,5)+" Time total:"+String(time_total)+" kW/h:"+String(total_watts*time_total/3600,5)); | |
Serial.println("Sending:"+data); | |
total=0.0; | |
nb_samples=0; | |
#ifdef ETHERNET_LOGIN | |
sendData(data); | |
// getData("emoncms.org",emonData); | |
// sendSensorVal("watt",val); | |
#else | |
Serial.println("No ethernet, logging"+data); | |
#endif | |
/* | |
data="/input/post.json?apikey=9f9a6c10ddeec10e1c37811f216bd16f&json={"; | |
for(int i=0;i<NB_SENSORS;i++){ | |
data+="watt_"+String(i)+":"+String((sum[i]/nb_samples)*VOLT_FACTOR)+","; | |
sum[i]=0; | |
} | |
data+="{watt:"+String(watts)+"}"; | |
*/ | |
// note the time that the connection was made or attempted: | |
lastConnectionTime = millis(); | |
} | |
} | |
void getData(char *server,String &theUrl) | |
{ | |
// if you get a connection, report back via serial: | |
if (client.connect(server, port)) { | |
Serial.println("connected"); | |
// Make a HTTP request: | |
client.println("GET "+theUrl+" HTTP/1.1"); | |
client.println("Host: "+String(server)); | |
client.println("Connection: close"); | |
client.println(); | |
} | |
else { | |
// kf you didn't get a connection to the server: | |
Serial.println("connection failed"); | |
} | |
} | |
// this method makes a HTTP connection to the server: | |
void sendData(String &thisData) { | |
// if there's a successful connection: | |
Serial.println(thisData); | |
if (client.connect(server, 80)) { | |
Serial.println("connecting..."); | |
// send the HTTP PUT request: | |
client.println("POST /device/"+String(DEVICE_ID)+" HTTP/1.1"); | |
Serial.println("Sending:"); | |
client.println("Host:api.opensensorcloud.com"); | |
client.print("X-ApiKey: "); | |
client.println(APIKEY); | |
client.print("User-Agent: "); | |
client.println(USERAGENT); | |
client.print("Content-Length: "); | |
client.println(thisData.length()); | |
// last pieces of the HTTP PUT request: | |
client.println("Content-Type: application/json"); | |
client.println("Connection: close"); | |
client.println(); | |
// here's the actual content of the PUT request: | |
client.println(thisData); | |
client.println(); | |
Serial.println("Request done:"+thisData); | |
} | |
else { | |
// if you couldn't make a connection: | |
Serial.println("connection failed"); | |
Serial.println(); | |
Serial.println("disconnecting."); | |
client.stop(); | |
} | |
// store the state of the connection for next time through | |
// the loop: | |
lastConnected = client.connected(); | |
} | |
String one_reading() | |
{ | |
res=""; | |
for(int i=0;i<NB_SENSORS;i++){ | |
// lcd_key = read_LCD_buttons(); // read the buttons | |
double Irms = emon[i].calcIrms(2000); // Calculate Irms only | |
double watt=Irms*VOLT_FACTOR; | |
//Serial.print("Read:"+String(val)+" "); | |
Serial.print(watt); // Apparent power | |
Serial.print(" "); | |
Serial.print(Irms); // Irms | |
Serial.print("| "); | |
watts[i] = watt; | |
sum[i] += Irms; | |
total += Irms; | |
// emon1.serialprint(); | |
res+=(String(int(watt)) +" "); | |
} | |
nb_samples++; | |
return res; | |
} | |
#ifdef LCD | |
// read the buttons | |
int read_LCD_buttons() | |
{ | |
adc_key_in = analogRead(0); // read the value from the sensor | |
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741 | |
// we add approx 50 to those values and check to see if we are close | |
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result | |
if (adc_key_in < 50) return btnRIGHT; | |
if (adc_key_in < 195) return btnUP; | |
if (adc_key_in < 380) return btnDOWN; | |
if (adc_key_in < 555) return btnLEFT; | |
if (adc_key_in < 790) return btnSELECT; | |
return btnNONE; // when all others fail, return this... | |
} | |
#endif | |
#ifdef SD | |
void logger() | |
{ | |
// make a string for assembling the data to log: | |
//String dataString = ""; | |
long time=millis(); | |
// open the file. note that only one file can be open at a time, | |
// so you have to close this one before opening another. | |
File dataFile = SD.open("datalog.csv", FILE_WRITE); | |
// if the file is available, write to it: | |
if (dataFile) { | |
dataFile.print(time); | |
dataFile.print(";"); | |
for(int i=0;i<NB_SENSORS;i++){ | |
dataFile.print(watts[i]); | |
dataFile.print(";"); | |
} | |
dataFile.println(""); | |
dataFile.close(); | |
// print to the serial port too: | |
Serial.println("stored"); | |
} | |
// if the file isn't open, pop up an error: | |
else { | |
Serial.println("error opening datalog.csv"); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment