Skip to content

Commit

Permalink
Improved DC power reading code and reduced total code size by merging…
Browse files Browse the repository at this point in the history
… arrays
  • Loading branch information
stuartpittaway committed Apr 24, 2012
1 parent 5172ce9 commit 4d27fca
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 170 deletions.
10 changes: 6 additions & 4 deletions Bluetooth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ NANODE SMA PV MONITOR
SoftwareSerial blueToothSerial(RxD,TxD);

void BTStart() {

if (!readArrayFromEEPROM(myBTAddress,6,ADDRESS_MY_BTADDRESS) || !readArrayFromEEPROM(smaBTInverterAddressArray,6,ADDRESS_SMAINVERTER_BTADDRESS))
{
debugMsgln("Checksum failed - pairing");
debugMsgln("Chksum fail-pair");
BTInitStartup(true);
}
else BTInitStartup(false);

//Start normal connection to BT chip, it will auto-connect as master to the SMA inverter (slave)
blueToothSerial.begin(9600);

debugMsgln("BT Rdy"); //Bluetooth ready
}

void sendPacket(unsigned char *btbuffer) {
Expand All @@ -46,7 +48,7 @@ void sendPacket(unsigned char *btbuffer) {

void quickblink() {
digitalWrite( RED_LED, LOW);
delay(100);
delay(30);
digitalWrite( RED_LED, HIGH);
}

Expand All @@ -66,7 +68,7 @@ void BTInitStartup(bool ForcePair) {

void BTSwitchOn() {
analogWrite(BLUETOOTH_POWER_PIN, 255); //Ensure BT chip is on
delay(1500); //Give chip time to warm up and enter command mode
delay(1800); //Give chip time to warm up and enter command mode
}

void BTSwitchOff() {
Expand All @@ -93,7 +95,7 @@ void BTScanForSMAInverterToPairWith()
char smabtinverteraddress[14];
unsigned char tempaddress[6]; //BT address

debugMsgln("Config Bluetooth");
debugMsgln("Config BT");

digitalWrite(BT_KEY, HIGH); // Turn BT chip into command mode (HC05)
delay(200);
Expand Down
2 changes: 1 addition & 1 deletion PVoutputOrg.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool webservicePVoutputOrg::dnsLookup() {

void webservicePVoutputOrg::preparePacket(unsigned long totalkWhGenerated,unsigned long spotTotalPowerAC,unsigned long spotTotalPowerDC, time_t dt)
{
debugMsg("Uploading to "); Serial.println(pvoutputwebsite);
//debugMsg("Upload to "); Serial.println(pvoutputwebsite);

byte sd = stash.create();
stash.print(F("d="));
Expand Down
13 changes: 9 additions & 4 deletions SMANetArduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,27 @@ NANODE SMA PV MONITOR
All code is copyright Stuart Pittaway, (c)2012.
*/


const byte maxlevel1packetsize = 120;
const byte level1headerlength = 18;
#define maxlevel1packetsize 120
#define level1headerlength 18
//const byte maxlevel1packetsize = 120;
//const byte level1headerlength = 18;

unsigned int packetlength=0;
unsigned int cmdcode=0;

//Should be moved into PROGMEM
unsigned char sixzeros[6]={ 0x00,0x00,0x00,0x00,0x00,0x00};
//Should be moved into PROGMEM
unsigned char sixff[6]={ 0xff,0xff,0xff,0xff,0xff,0xff};

unsigned int packetposition=0;

unsigned int FCSChecksum=0xffff;

unsigned char Level1SrcAdd[6];
unsigned char Level1DestAdd[6];

char invertername[15]={0};
//char invertername[15]={0};
unsigned char packet_send_counter=0;
unsigned char lastpacketindex=0;

Expand Down
32 changes: 23 additions & 9 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("P wrng snder"));
else debugMsgln("P wrng snder");
}
else Serial.println(F("P wrng dest"));
else debugMsgln("P wrng dest");
}
else Serial.println(F("Inv hdr"));
else debugMsgln("Inv hdr");
}

void prepareToReceive(){
Expand All @@ -106,14 +106,18 @@ bool containsLevel2Packet() {


void waitForPacket(unsigned int cmdcodetowait) {
//debugMsg("waitForPacket=0x"); Serial.print(cmdcodetowait,HEX);

prepareToReceive();

while (cmdcode!=cmdcodetowait) {
readLevel1PacketFromBluetoothStream(0);
}
//debugMsgln(" done");
}

void waitForMultiPacket(unsigned int cmdcodetowait) {
//debugMsg("waitForMultiPacket=0x"); Serial.print(cmdcodetowait,HEX);
prepareToReceive();

while (cmdcode!=cmdcodetowait) {
Expand All @@ -129,6 +133,7 @@ void writeSMANET2Long(unsigned char *btbuffer,unsigned long v) {
writeSMANET2SingleByte(btbuffer,(unsigned char)((v >> 24) & 0xFF)) ;
}

/*
void displaySpotValues(int gap) {
unsigned long value = 0;
unsigned int valuetype=0;
Expand All @@ -151,11 +156,10 @@ void displaySpotValues(int gap) {
memcpy(&datetime,&level1packet[i+4],4);
digitalClockDisplay(datetime);
Serial.print("=");
Serial.println(value);
Serial.print("=");Serial.println(value);
}

}
*/

void writeSMANET2SingleByte(unsigned char *btbuffer, unsigned char v) {
//Keep a rolling checksum over the payload
Expand All @@ -170,14 +174,16 @@ void writeSMANET2SingleByte(unsigned char *btbuffer, unsigned char v) {
}
}

void writeSMANET2Array(unsigned char *btbuffer, unsigned char bytes[],int loopcount) {
void writeSMANET2Array(unsigned char *btbuffer, unsigned char bytes[],byte loopcount) {
//Escape the packet if needed....
for(int i=0;i<loopcount;i++) {
writeSMANET2SingleByte(btbuffer,bytes[i]);
}//end for
}

void writeSMANET2ArrayFromProgmem(unsigned char *btbuffer,prog_uchar ProgMemArray[],int loopcount) {
void writeSMANET2ArrayFromProgmem(unsigned char *btbuffer,prog_uchar ProgMemArray[],byte loopcount) {
//debugMsg("writeSMANET2ArrayFromProgmem=");
//Serial.println(loopcount);
//Escape the packet if needed....
for(int i=0;i<loopcount;i++) {
writeSMANET2SingleByte(btbuffer,pgm_read_byte_near(ProgMemArray + i));
Expand Down Expand Up @@ -231,6 +237,13 @@ void writeSMANET2PlusPacketTrailer(unsigned char *btbuffer){
//Serial.print("Send Checksum=");Serial.println(FCSChecksum,HEX);
}

void writePacketHeader(unsigned char *btbuffer) {
writePacketHeader(btbuffer,0x01,0x00,smaBTInverterAddressArray);
}
void writePacketHeader(unsigned char *btbuffer,unsigned char *destaddress) {
writePacketHeader(btbuffer,0x01,0x00,destaddress);
}

void writePacketHeader(unsigned char *btbuffer,unsigned char cmd1,unsigned char cmd2, unsigned char *destaddress) {

if (packet_send_counter>75) packet_send_counter=1;
Expand Down Expand Up @@ -376,6 +389,7 @@ bool validateChecksum(){
}
}

/*
void InquireBlueToothSignalStrength() {
writePacketHeader(level1packet,0x03,0x00,smaBTInverterAddressArray);
//unsigned char a[2]= {0x05,0x00};
Expand All @@ -392,7 +406,7 @@ void InquireBlueToothSignalStrength() {
Serial.print(bluetoothSignalStrength);
Serial.println("%");
}

*/


bool ValidateSenderAddress() {
Expand Down
4 changes: 2 additions & 2 deletions SolarStatsCoUk.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ bool webserviceSolarStats::dnsLookup() {

void webserviceSolarStats::preparePacket(unsigned long totalkWhGenerated,unsigned long spotTotalPowerAC,unsigned long spotTotalPowerDC, time_t dt)
{
Serial.print(F("Uploading to "));
Serial.println(solarstatswebsite);
//Serial.print(F("Uploading to "));
//Serial.println(solarstatswebsite);

// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Expand Down
2 changes: 1 addition & 1 deletion Utility.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static int freeMemory() {


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

Expand Down
14 changes: 3 additions & 11 deletions WebSiteService.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void WebSiteService::begin() {
}

void WebSiteService::lookupDNSHostIP() {
debugMsgln("DNS lookup");
debugMsgln("DNS lkup");
if (this->dnsLookup())
{
ether.copyIp(this->hostip,ether.hisip);
Expand All @@ -60,14 +60,6 @@ void WebSiteService::stashPrintTwoDigits(byte num)
stash.write('0' + (num % 10));
}

/*
void WebSiteService::formatTwoDigits(char* strOut, int num)
{
strOut[0] = '0' + (num / 10);
strOut[1] = '0' + (num % 10);
}
*/

void WebSiteService::CountDownAndUpload(unsigned long totalkWhGenerated,unsigned long spotTotalPowerAC,unsigned long spotTotalPowerDC, time_t dt)
{
CountDown();
Expand All @@ -85,9 +77,9 @@ void WebSiteService::CountDownAndUpload(unsigned long totalkWhGenerated,unsigned
//Wait for a bit to process any possible replies, ideally we
//would like to wait here for a successful HTTP 200 message
//or timeout/error condition
for(int i=0; i<250; i++)
for(int i=0; i<150; i++)
{
delay(5);
delay(10);
ether.packetLoop(ether.packetReceive());
}
}
Expand Down
Loading

0 comments on commit 4d27fca

Please sign in to comment.