Skip to content

Commit

Permalink
added automatic web upload to emonCMS
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartpittaway committed Feb 7, 2012
1 parent 0331a36 commit ce1638b
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 109 deletions.
27 changes: 3 additions & 24 deletions Bluetooth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,6 @@ void quickblink() {
digitalWrite( RED_LED, HIGH);
}


/*
void testreceiveloop()
{
//Loop whilst there is data available from serial
while(blueToothSerial.available()) {
unsigned char a=blueToothSerial.read();
if (a==0x7e) Serial.println("");
if (a<=0xf) Serial.print("0");
Serial.print(a,HEX);
}
digitalWrite( RED_LED, HIGH);
delay(1000);
digitalWrite( RED_LED, LOW);
delay(1000);
}
*/


void BTInitStartup(bool ForcePair) {
BTSwitchOff();

Expand Down Expand Up @@ -126,11 +105,11 @@ void BTScanForSMAInverterToPairWith()
//Send AT wake up
BTSendStringAndWait(retstring,F("AT"));
//Firmware Version = +VERSION=2.0-20100601
BTSendStringAndWait(retstring,F("AT+VERSION?"));
//BTSendStringAndWait(retstring,F("AT+VERSION?"));
//Factory reset to defaults
BTSendStringAndWait(retstring,F("AT+ORGL"));
//Name of chip
BTSendStringAndWait(retstring,F("AT+NAME=NANODE_SMA_SOLAR"));
//BTSendStringAndWait(retstring,F("AT+NAME=NANODE_SMA_SOLAR"));
//Set password to zeros
BTSendStringAndWait(retstring,F("AT+PSWD=0000"));
//MASTER mode
Expand Down Expand Up @@ -223,7 +202,7 @@ void BTScanForSMAInverterToPairWith()
delay(100);
digitalWrite(BT_KEY, LOW); // Turn off command mode
delay(500);
debugMsgln("Bluetooth configure. Finished.");
//debugMsgln("Bluetooth configure. Finished.");
}


Expand Down
25 changes: 14 additions & 11 deletions NanodeSMASolar.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@


#undef debugMsgln
#define debugMsgln(s) (__extension__( {Serial.println(F(s));} ))
//#define debugMsgln(s) (__extension__( {Serial.println(F(s));} ))
#define debugMsgln(s) (__extension__( {delay(0);} ))
#undef debugMsg
#define debugMsg(s) (__extension__( {Serial.print(F(s));} ))
//#define debugMsg(s) (__extension__( {Serial.print(F(s));} ))
#define debugMsg(s) (__extension__( { delay(0); } ))


byte Ethernet::buffer[650]; // tcp/ip send and receive buffer
Expand Down Expand Up @@ -93,7 +95,7 @@ unsigned char SMAInverterPasscode[]={
void setup()
{
Serial.begin(115200); //Serial port for debugging output
debugMsgln("PWR UP");
//debugMsgln("PWR UP");

pinMode(RED_LED, OUTPUT);
digitalWrite( RED_LED, HIGH);
Expand All @@ -111,7 +113,7 @@ void setup()
debugMsgln("DHCP");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
{
debugMsgln("Fail Ethernet chip");
debugMsgln("Fail Ether chip");
error();
}

Expand All @@ -132,10 +134,9 @@ void setup()

void loop()
{
debugMsgln("LOOP..");
//debugMsgln("LOOP..");


HowMuchMemory();
//HowMuchMemory();

initialiseSMAConnection();

Expand All @@ -156,6 +157,9 @@ void loop()

webservicePVoutputOrg pvoutputorg;
pvoutputorg.begin();

webserviceemonCMS emoncms;
emoncms.begin();

//We dont actually use the value this returns, but we can set the clock from its reply
getDailyYield();
Expand Down Expand Up @@ -203,6 +207,7 @@ void loop()
getTotalPowerGeneration();

//Upload value to various websites/services, depending upon frequency
emoncms.CountDownAndUpload(currentvalue,spotpowerac,datetime);

solarstats.CountDownAndUpload(currentvalue,spotpowerac,datetime);

Expand All @@ -221,7 +226,7 @@ void loop()

if ( (now() > (datetime+3600)) && (spotpowerac==0)) {
//Inverter output hasnt changed for an hour, so put Nanode to sleep till the morning
debugMsgln("Time for bed");
debugMsgln("Bed time");

//Workout what time sunrise is and sleep till then...
//longitude, latitude (london uk=-0.126236,51.500152)
Expand All @@ -234,9 +239,7 @@ void loop()
datetime=checktime+80000;
}

debugMsg("Waiting for... ");
digitalClockDisplay( checktime );
debugMsgln("");
//debugMsg("Wait for ");digitalClockDisplay( checktime );debugMsgln("");
}
}//end while
}
Expand Down
2 changes: 1 addition & 1 deletion PVoutputOrg.ino
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void webservicePVoutputOrg::preparePacket(unsigned long totalkWhGenerated,unsign
"X-Pvoutput-Apikey: $F" "\r\n"
"X-Pvoutput-SystemId: $F" "\r\n"
"Accept: */*\r\n"
"User-Agent: NanodeSMAPVMonitor" "\r\n"
//"User-Agent: NanodeSMAPVMonitor" "\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
Expand Down
6 changes: 3 additions & 3 deletions PachubeCom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ void webservicePachube::preparePacket(unsigned long totalkWhGenerated,unsigned l
stash.print(F("{\"version\":\"1.0.0\",\"datastreams\":["));
//PV Generation
stash.print(F("{\"id\":\"1\",\"current_value\":\""));
stash.print((double)currentvalue/(double)1000);
stash.print((double)totalkWhGenerated/(double)1000);
stash.print(F("\"}"));
//Spot power
stash.print(F(",{\"id\":\"4\",\"current_value\":\""));
stash.print(spotpowerac);
stash.print(spotTotalPowerAC);
stash.print(F("\"}"));
//Uptime in whole seconds
stash.print(F(",{\"id\":\"2\",\"current_value\":\""));
Expand All @@ -58,7 +58,7 @@ void webservicePachube::preparePacket(unsigned long totalkWhGenerated,unsigned l
Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.json HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"X-PachubeApiKey: $F" "\r\n"
"User-Agent: NanodeSMAPVMonitor" "\r\n"
//"User-Agent: NanodeSMAPVMonitor" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
Expand Down
6 changes: 3 additions & 3 deletions SMANetArduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ void readLevel1PacketFromBluetoothStream(int index) {
packetlength=index+level1headerlength;
packetposition=index;
}
else Serial.println(F("Pkt wrng snder"));
else Serial.println(F("P wrng snder"));
}
else Serial.println(F("Pkt wrng dest"));
else Serial.println(F("P wrng dest"));
}
else Serial.println(F("Invalid hdr"));
else Serial.println(F("Inv hdr"));
}

void prepareToReceive(){
Expand Down
9 changes: 5 additions & 4 deletions SolarStatsCoUk.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void webserviceSolarStats::preparePacket(unsigned long totalkWhGenerated,unsigne
d[4]='-';

char watthour[12];
ultoa(currentvalue,watthour,10);
ultoa(totalkWhGenerated,watthour,10);
byte sd = stash.create();

//Perhaps use instead...
Expand All @@ -69,10 +69,10 @@ void webserviceSolarStats::preparePacket(unsigned long totalkWhGenerated,unsigne
stash.print(F("GET http://www.solarstats.co.uk/api/Update.ashx?apikey="));
//Would like to do... stash.print(solarstatswebsite);

stash.print(F("XXXXXXXXXX"));
stash.print(F("6AAEA7C448"));

stash.print(F("&apipassword="));
stash.print(F("YYYYYYYYYY"));
stash.print(F("8E4279FE0A"));

stash.print(F("&date="));
stash.print(d);
Expand All @@ -82,7 +82,8 @@ void webserviceSolarStats::preparePacket(unsigned long totalkWhGenerated,unsigne
stash.print(watthour);

stash.print(F(" HTTP/1.0\r\nHost: www.solarstats.co.uk\r\n"));
stash.print(F("User-Agent: NanodeSMAPVMonitor\r\nContent-Length: 0 \r\n"));
//stash.print(F("User-Agent: NanodeSMAPVMonitor\r\n"));
stash.print(F("Content-Length: 0 \r\n"));
stash.print(F("\r\n"));
stash.save();

Expand Down
59 changes: 59 additions & 0 deletions Sunrise.ino
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
static time_t ComputeSun(float longitude,float latitude,time_t when, bool rs) {
//Borrowed from TimeLord library http://swfltek.com/arduino/timelord.html
//rs=true for sunrise, false=sunset

//digitalClockDisplay(when); Serial.println("");

uint8_t a;

float lon=-longitude/57.295779513082322;
float lat=latitude/57.295779513082322;

//approximate hour;
a=6; //6am
if(rs) a=18; //6pm

// approximate day of year
float y= (month(when)-1) * 30.4375 + (day(when)-1) + a/24.0; // 0... 365

// compute fractional year
y *= 1.718771839885e-02; // 0... 1

// compute equation of time... .43068174
float eqt=229.18 * (0.000075+0.001868*cos(y) -0.032077*sin(y) -0.014615*cos(y*2) -0.040849*sin(y* 2) );

// compute solar declination... -0.398272
float decl=0.006918-0.399912*cos(y)+0.070257*sin(y)-0.006758*cos(y*2)+0.000907*sin(y*2)-0.002697*cos(y*3)+0.00148*sin(y*3);

//compute hour angle
float ha=( cos(1.585340737228125) / (cos(lat)*cos(decl)) -tan(lat) * tan(decl) );

if(fabs(ha)>1.0){// we're in the (ant)arctic and there is no rise(or set) today!
return when;
}

ha=acos(ha);
if(rs==false) ha=-ha;

// compute minutes from midnight
unsigned int seconds=60*(720+4*(lon-ha)*57.295779513082322-eqt);

// convert from UTC back to our timezone
//minutes+= timezone;

tmElements_t tm;

tm.Year = year(when)-1970;
tm.Month = month(when);
tm.Day = day(when);
tm.Hour = 0;
tm.Minute = 0;
tm.Second = 0;

time_t midnight=makeTime(tm);

//Move to tomorrow and add on the minutes till sunrise (as seconds)
midnight+=86400 +seconds;

return midnight;
}

63 changes: 2 additions & 61 deletions Utility.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ static int freeMemory() {


static void HowMuchMemory() {
Serial.print(F("free mem="));
Serial.print(F("mem="));
Serial.println(freeMemory());
}



static void error() {
Serial.println(F("\r\n*Error*"));
Serial.println(F("\r\n*Err*"));
int loop=150;
while (loop>0) {
digitalWrite( RED_LED, HIGH);
Expand Down Expand Up @@ -94,63 +94,4 @@ static void printMacAddress(byte mymac[]) {
}


static time_t ComputeSun(float longitude,float latitude,time_t when, bool rs) {
//Borrowed from TimeLord library http://swfltek.com/arduino/timelord.html
//rs=true for sunrise, false=sunset

//digitalClockDisplay(when); Serial.println("");

uint8_t a;

float lon=-longitude/57.295779513082322;
float lat=latitude/57.295779513082322;

//approximate hour;
a=6; //6am
if(rs) a=18; //6pm

// approximate day of year
float y= (month(when)-1) * 30.4375 + (day(when)-1) + a/24.0; // 0... 365

// compute fractional year
y *= 1.718771839885e-02; // 0... 1

// compute equation of time... .43068174
float eqt=229.18 * (0.000075+0.001868*cos(y) -0.032077*sin(y) -0.014615*cos(y*2) -0.040849*sin(y* 2) );

// compute solar declination... -0.398272
float decl=0.006918-0.399912*cos(y)+0.070257*sin(y)-0.006758*cos(y*2)+0.000907*sin(y*2)-0.002697*cos(y*3)+0.00148*sin(y*3);

//compute hour angle
float ha=( cos(1.585340737228125) / (cos(lat)*cos(decl)) -tan(lat) * tan(decl) );

if(fabs(ha)>1.0){// we're in the (ant)arctic and there is no rise(or set) today!
return when;
}

ha=acos(ha);
if(rs==false) ha=-ha;

// compute minutes from midnight
unsigned int seconds=60*(720+4*(lon-ha)*57.295779513082322-eqt);

// convert from UTC back to our timezone
//minutes+= timezone;

tmElements_t tm;

tm.Year = year(when)-1970;
tm.Month = month(when);
tm.Day = day(when);
tm.Hour = 0;
tm.Minute = 0;
tm.Second = 0;

time_t midnight=makeTime(tm);

//Move to tomorrow and add on the minutes till sunrise (as seconds)
midnight+=86400 +seconds;

return midnight;
}

9 changes: 8 additions & 1 deletion WebSiteService.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@ public WebSiteService
int getTimerResetValue();
};


class webserviceemonCMS :
public WebSiteService
{
public:
bool dnsLookup();
void preparePacket(unsigned long totalkWhGenerated,unsigned long spotTotalPowerAC, time_t dt) ;
int getTimerResetValue();
};

1 change: 0 additions & 1 deletion WebSiteService.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ void WebSiteService::CountDown () {
if (this->minutecountdownvalue > 0) {
this->minutecountdownvalue-=1;
}
//Serial.print(F("CountDown="));Serial.println(minutecountdownvalue);
}//end function

void WebSiteService::begin() {
Expand Down
Loading

0 comments on commit ce1638b

Please sign in to comment.