Show
Ignore:
Timestamp:
10/17/10 18:13:03 (14 years ago)
Author:
phil
Message:

we now query and display the solar yields of the previous days (day - 1) for all the channels

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • webserver/example/EnergyMeters/Source/EnergyMeters/MetersIncludes.h

    r69 r70  
    22void initMeterItems(void); 
    33 
    4 portCHAR checkRxOneByteCheckSum(unsigned char* dataStart, unsigned char firstByte, unsigned char lastByte, unsigned charCheckSumStartByte); 
    5 portCHAR checkRxTwoByteCheckSum(unsigned char* dataStart, unsigned char firstByte, unsigned char lastByte, unsigned charCheckSumStartByte ); 
     4portCHAR checkRxOneByteCheckSum(unsigned char* dataStart, unsigned char firstByte, unsigned char lastByte, unsigned char CheckSumStartByte); 
     5portCHAR checkRxTwoByteCheckSum(unsigned char* dataStart, unsigned char firstByte, unsigned char lastByte, unsigned char CheckSumStartByte ); 
     6 
     7 
     8static unsigned char calculate_crc8(unsigned char * d_serdata_tuc, unsigned char nbytes); 
    69 
    710int extractBinaryHexDigitString(unsigned char* srcDataStart,  
     
    1215 
    1316unsigned long long getEpochTimeWithMs(void); 
     17 
     18 
     19void send_yield_query_day(unsigned char channel /* 0 to 5 */, unsigned char day /* 1 to 31 */); 
     20void send_yield_query_hour(unsigned char channel /* 0 to 5 */, unsigned char hour /* 0 to 23 */); 
     21void send_yield_query_minute(unsigned char channel /* 0 to 5 */, unsigned char minute /* 0 to 59 */); 
    1422 
    1523#define NUMBER_OF_METERS 6 /* the number of meters we use */ 
     
    2432 
    2533#define METERS_INT_CHECK_INTERVAL_MS 100 /* how often (in ms) do we check if one of the I/O lines has triggered */ 
     34 
     35 
     36#define NUMBER_OF_SOLAR_CHANNELS 6 /* number of solar channels */ 
     37#define DAILY_RX_YIELD_LENGTH 9 /* length of the rx data */ 
     38 
    2639 
    2740 
     
    5063extern int historyRemainTime; /* how many ticks have we left until the next history entry must be written? */ 
    5164 
     65/* bit values for calculate_crc8() */ 
     66 
     67#define BIT0 (1U << 0) 
     68#define BIT1 (1U << 1) 
     69#define BIT2 (1U << 2) 
     70#define BIT3 (1U << 3) 
     71#define BIT4 (1U << 4) 
     72#define BIT5 (1U << 5) 
     73#define BIT6 (1U << 6) 
     74#define BIT7 (1U << 7) 
     75 
     76 
  • webserver/example/EnergyMeters/Source/EnergyMeters/SolarCountUART.c

    r69 r70  
    103103 
    104104 
     105int dayRxYield[NUMBER_OF_SOLAR_CHANNELS]; /* daily yield data */ 
     106 
     107 
    105108const unsigned char query_sc_time[] = "\x02\xB0\xF0\x03\x00\x04\xAB\xB9\x03"; // Zeit abfragen 
    106109const unsigned char query_sc_time_rsp[] = "\x02\xF0\xB0\x08\x00\x84\x00\x00\x00\x00\x00\x00\x00\x03"; // Zeit abfragen 
     
    120123//specification: unsigned char req_month_yield[] = "\x02\x31\x3c\x4d\x30\x31\x30\x30\x03"; // Frame for querying the yield. Will be filled in by solQueryYield() 
    121124 
    122 //                                   02  31  3c  4d  30  30  30  30  30  31  fd  03 
    123 unsigned char req_month_yield[] = "\x02\x31\x3c\x4d\x30\x30\x30\x30\x30\x31\xfd\x03"; // Frame for querying the yield. Will be filled in by solQueryYield() 
     125//                                   
     126unsigned char req_month_yield[] = "\x02\x31\x3C\x6d\x30\x32\x30\x30\x30\x32\xff\x03"; // Frame for querying the monthly yield. Will be filled in by solQueryYield() 
     127 
     128 
     129unsigned char req_minute_yield[] = "\x02\x31\x3C\x6D\x30\x32\x30\x30\x30\x32\x00\x03"; // Frame for querying the minutely yield. Will be filled in by solQueryYield() 
     130const unsigned char yield_minute_rsp[] = "\x02\x31\x6D\x30\x31\x30\x30\x30\x32\x30\x30\x30\x30\x30\x32\x41\x44\xC8\x03\x03"; 
     131const unsigned char yield_minute_r_m[] = "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"; 
     132 
     133 
     134unsigned char req_day_yield[]    = "\x02\x31\x3c\x44\x30\x32\x30\x30\x30\x32\x00\x03"; // Frame for querying the minutely yield. Will be filled in by solQueryYield() 
     135const unsigned char yield_day_rsp[]    = "\x02\x31\x44\x30\x31\x30\x30\x30\x32\x30\x30\x30\x30\x30\x32\x41\x44\xC8\x03\x03"; 
     136const unsigned char yield_day_r_m[]    = "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"; 
     137 
     138 
     139unsigned char req_hour_yield[]    = "\x02\x31\x3c\x68\x30\x32\x30\x30\x30\x32\x00\x03"; // Frame for querying the minutely yield. Will be filled in by solQueryYield() 
     140const unsigned char yield_hour_rsp[]    = "\x02\x31\x68\x30\x31\x30\x30\x30\x32\x30\x30\x30\x30\x30\x30\x30\x30\x30\x03\x03"; 
     141const unsigned char yield_hour_r_m[]    = "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"; 
     142 
     143 
     144 
     145 
     146 
    124147 
    125148// tt (time) values: m=minute , h = hour, D = day, M = month (\x31), Y = year 
     
    129152// ad: unit address 
    130153// r1 to r7: result binary coded hex 
    131 //                                                    ad  tt  cc  m1  m2  r1  r2  r3  r4  r5  r6  r7  ck  
    132 //specification: unsigned char yield_month_rsp[] = "\x02\x31\x4d\x30\x00\x00\x09\x0c\x04\x0a\x26\x86\xcd\x00\x03"; /* exp. query yield response. Will be filled in by solQueryYield() */ 
    133 //specification: unsigned char yield_month_r_m[] = "\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff"; /* exp. query yield seq response mask. Will be filled in by solQueryYield() */ 
    134  
    135 //                                   02  31  4d  30  30  30  30  30  31  30  30  30  30  31  35  44  43  9a  03  03 
    136 //                                   00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19 
    137 unsigned char yield_month_rsp[] = "\x02\x31\x4d\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03"; /* exp. query yield response. Will be filled in by solQueryYield() */ 
    138 unsigned char yield_month_r_m[] = "\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff"; /* exp. query yield seq response mask. Will be filled in by solQueryYield() */ 
     154 
     155 
     156//                                   02  31  4d  30  30  30  30  30  31  30  30  30  30  31  35  44  43 
     157//                                   00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16 
     158unsigned char yield_month_rsp[] = "\x02\x31\x4d\x33\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03"; /* exp. query yield response. Will be filled in by solQueryYield() */ 
     159unsigned char yield_month_r_m[] = "\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff"; /* exp. query yield seq response mask. Will be filled in by solQueryYield() */ 
    139160 
    140161 
     
    229250{ 
    230251  int i; 
     252  int k; 
     253 
    231254  xLastSolarStateChangeTime = xTaskGetTickCount(); 
    232255  xLastTimeCheckTime = xTaskGetTickCount(); 
     
    243266 
    244267  /* do not invalidate history here so we do not lose all data */ 
     268 
     269  /* init daily rx yield array with 0 */ 
     270  for (i = 0; i < NUMBER_OF_SOLAR_CHANNELS; i++) 
     271  { 
     272      dayRxYield[i] = 0; 
     273  } /* for */ 
    245274 
    246275 
     
    417446void solarFSM(void) 
    418447{ 
     448  unsigned char i; 
    419449  int cmpResult; 
     450  unsigned char power; /* variable for conversions */ 
     451  unsigned char checkDate; /* temp variable for day of month */ 
    420452 
    421453 
     
    537569 
    538570        currentChannel = 0; /* start yield scan on channel 0 */ 
    539         solarState = SOL_QUERY_YIELD; /* change state: time response received, now init and query the channels */ 
     571        solarState = SOL_QUERY_YIELD; //xxxxxxx SOL_NO_INIT; //SOL_QUERY_YIELD; /* change state: time response received, now init and query the channels */ 
    540572      } 
    541573      else 
     
    552584        /* first, fill in data for tx */ 
    553585        // TODO 
    554    
    555  
    556 //        req_month_yield[4] = 0x30 + 1; // channel 
    557 //        req_month_yield[8] = 0x30 + 0; // month MSB (binary coded hex)  
    558 //        req_month_yield[9] = 0x30 + 4; // month LSB (binary coded hex) 
    559  
    560         /* then, calculate the CRC for tx */ 
    561        (void)checkRxOneByteCheckSum(&(req_month_yield[0]), 1, 7, 8); 
    562        /* checksum now in calculatedCrc1Byte */ 
    563  
    564  
    565         // crc 
    566         //req_month_yield[7] = calculatedCrc1Byte; /* fill in checksum */ 
    567  
    568         resetUart3RxBuf(); 
    569         send_uart3((unsigned char *)req_month_yield, sizeof(req_month_yield)); 
    570  
    571         solarState = SOL_QUERY_YIELD_WAIT_REPLY; /* change state: now wait for response */ 
     586 
     587 
     588        if (currentChannel >= 6) 
     589        { 
     590          currentChannel = 0; 
     591          solarState = SOL_NO_INIT; /* we are done with all the channels. go ahead */ 
     592        } 
     593        else 
     594        { 
     595          /* query the next channel */ 
     596 
     597          checkDate = scDate[2]-1; /* we query the previous day */ 
     598 
     599          if (checkDate == 0) 
     600          { 
     601            /* first day of month */ 
     602 
     603            /* check new month */ 
     604            if (scDate[1] == 3) 
     605            { 
     606              /* looking for end of february? handle leap years */ 
     607              if ((scDate[0] % 4) == 0) 
     608              { 
     609                checkDate = 29; /* leap year */ 
     610              } 
     611              else 
     612              { 
     613                checkDate = 28; /* not a leap year */ 
     614              } 
     615            } 
     616            else if  
     617                 ((scDate[1] == 1) || 
     618                 (scDate[1] == 5) || 
     619                 (scDate[1] == 7) || 
     620                 (scDate[1] == 8) || 
     621                 (scDate[1] == 10) || 
     622                 (scDate[1] == 12)) 
     623                 {  
     624                    checkDate = 31; 
     625                 } 
     626                 else 
     627                 { 
     628                    checkDate = 30; 
     629                 } 
     630          } 
     631 
     632          send_yield_query_day(/* channel: */ currentChannel, /* day: (yesterday) */ checkDate ); 
     633          xLastSolarStateChangeTime = xTaskGetTickCount(); 
     634          solarState = SOL_QUERY_YIELD_WAIT_REPLY; /* query next channel. change state: now wait for response */ 
     635 
     636        } 
     637 
     638        //send_yield_query_minute(/* channel: */ 1, /* minute: */ 40 ); 
     639         
     640        // hour is broken... ?! 
     641        //send_yield_query_hour(/* channel: */ 1, /* hour: */ 11 ); 
     642 
     643 
    572644 
    573645    break; /* end of case SOL_QUERY_YIELD */ 
     
    576648 
    577649 
    578  
    579        cmpResult = checkUart3Received(yield_month_rsp, yield_month_r_m,  sizeof(yield_month_rsp)-1); 
     650       // cmpResult = checkUart3Received(yield_minute_rsp, yield_minute_r_m,  sizeof(yield_minute_rsp)-1); 
     651 
     652        // the query for hour is broken... ?! 
     653        //cmpResult = checkUart3Received(yield_hour_rsp, yield_hour_r_m,  sizeof(yield_hour_rsp)-1); 
     654 
     655       cmpResult = checkUart3Received(yield_day_rsp, yield_day_r_m,  sizeof(yield_day_rsp)-1); 
     656 
    580657 
    581658 
     
    584661      { 
    585662        /* we have got a match! */ 
    586          
    587  
    588         resultValueRaw = extractBinaryHexDigitString( &(rxUART3[0]),  
    589                                     9, /* first byte of binary hex string */ 
    590                                     16 /* last byte of binary hex string */ 
    591                                     ); 
    592  
    593  
    594  
    595         solarState = SOL_NO_INIT; /* go ahead */ 
    596   
     663        
     664          if (currentChannel <= 5) 
     665          { 
     666 
     667 
     668              dayRxYield[currentChannel] = 
     669                extractBinaryHexDigitString( &(rxUART3[0]), 13, 16 ); 
     670 
     671          } /* if */ 
     672          else 
     673          { 
     674            /* all channels done. nothing to do, we leave via SOL_QUERY_YIELD */ 
     675          } 
     676 
     677 
     678          currentChannel++; /* increment channel counter so we query the next channel in the next round */ 
     679          xLastSolarStateChangeTime = xTaskGetTickCount(); 
     680          solarState = SOL_QUERY_YIELD; /* go ahead */ 
    597681     } 
    598 #if 0 
    599         currentChannel++; /* next channel */ 
    600  
    601         if (currentChannel >= 6) 
    602         { 
    603           solarState = SOL_NO_INIT; /* change state: last channel done */ 
    604         } /* if */ 
    605         else 
    606         { 
    607           solarState = SOL_QUERY_YIELD; /* scan next channel */ 
    608         } /* else */ 
    609 #endif 
    610682 
    611683    break; /* end of case SOL_QUERY_YIELD_WAIT_REPLY */ 
     
    10121084 
    10131085 
    1014 portCHAR checkRxOneByteCheckSum(unsigned char* dataStart, unsigned char firstByte, unsigned char lastByte, unsigned charCheckSumStartByte) 
     1086portCHAR checkRxOneByteCheckSum(unsigned char* dataStart, unsigned char firstByte, unsigned char lastByte, unsigned char CheckSumStartByte) 
    10151087{ 
    10161088  portCHAR retVal; /* return value */ 
     
    10201092  int x; 
    10211093 
    1022   unsigned long crcLONG = 0
     1094  unsigned long crcLONG
    10231095  unsigned char crcCHAR; 
    10241096  unsigned char crcCHARinv; 
    1025   unsigned char carry = 0; 
     1097  unsigned char carry; 
     1098 
     1099  crcLONG = 0; 
     1100  crcCHAR = 0; 
     1101  crcCHARinv = 0; 
     1102  carry = 0; 
     1103 
    10261104 
    10271105  /* use bytes 1 to 13 for the checksum */ 
     
    10291107  { 
    10301108 
    1031     /* overflow? */ 
    1032  //   if (((portSHORT)crc + (portSHORT)(dataStart[x])) > 0xFF) 
    1033  //   { 
    1034  //     carry++; 
    1035  //   } 
    1036  
    10371109    crcLONG = (crcLONG + dataStart[x]); /* add and truncate to 8 bit */ 
    10381110  } 
     
    10401112  carry = (unsigned char)(((crcLONG & 0xFF00) >> 8)); 
    10411113 
    1042   crcCHAR = (unsigned char)(crcLONG & 0xFF); 
    1043  
    1044   crcCHAR = (unsigned char)(crcCHAR + carry); 
     1114  crcCHAR = ((unsigned char)crcLONG) + carry; 
    10451115  crcCHARinv = (unsigned char)(~crcCHAR); 
    10461116 
     
    10481118  calculatedCrc1Byte = crcCHARinv; /* save calculated CRC (for use in case of checksum generation) */ 
    10491119 
    1050   if (crcCHARinv == dataStart[charCheckSumStartByte]) 
     1120  if (crcCHARinv == dataStart[CheckSumStartByte]) 
    10511121  { 
    10521122    retVal = 1; /* checksum OK, data is valid */ 
     
    10601130 
    10611131 
    1062 portCHAR checkRxTwoByteCheckSum(unsigned char* dataStart, unsigned char firstByte, unsigned char lastByte, unsigned charCheckSumStartByte ) 
     1132portCHAR checkRxTwoByteCheckSum(unsigned char* dataStart, unsigned char firstByte, unsigned char lastByte, unsigned char CheckSumStartByte ) 
    10631133{ 
    10641134  unsigned short cmpValue; 
     
    10771147 
    10781148 
    1079   cmpValue = ((((unsigned short)dataStart[charCheckSumStartByte+1])<<8) | dataStart[charCheckSumStartByte]); 
     1149  cmpValue = ((((unsigned short)dataStart[CheckSumStartByte+1])<<8) | dataStart[CheckSumStartByte]); 
    10801150 
    10811151  calculatedCrc2Byte = staticCRC; /* save calculated CRC (for use in case of checksum generation) */ 
     
    11321202 
    11331203 } /* extractBinaryHexDigitString() */ 
     1204 
     1205 
     1206static unsigned char calculate_crc8(unsigned char * d_serdata_tuc, unsigned char nbytes) 
     1207{ 
     1208   unsigned char i; 
     1209   unsigned char j; 
     1210   unsigned char d_currbyte_tuc; 
     1211   unsigned char d_crc_tuc; 
     1212    
     1213   /* start with BISISTX and repeat the checksum calculation for all bytes until CHKS8 (excl.) */ 
     1214    d_crc_tuc = 0; //   init the checksum 
     1215 
     1216    for( j = 1; j < (nbytes-2); j ++  ) 
     1217    { 
     1218 
     1219      d_currbyte_tuc = d_serdata_tuc[ j ]; 
     1220 
     1221      for( i = 8; i > 0; i --  ) 
     1222      { 
     1223         /*  if  current bit != 0 */ 
     1224         if (( (d_crc_tuc ^ d_currbyte_tuc) & BIT0  ) != 0) 
     1225         { 
     1226            d_crc_tuc ^= 0x18; 
     1227            d_crc_tuc >>= 1; 
     1228            d_crc_tuc |= BIT7; 
     1229         } 
     1230         else 
     1231         { 
     1232          d_crc_tuc >>= 1; 
     1233         } 
     1234         d_currbyte_tuc >>= 1;   //       prepare for next databit 
     1235      } /* for */ 
     1236    } // this byte is finished 
     1237 
     1238  /* checksum is complete now - result always ORed with 0x80  and then compare */ 
     1239  /* calculated and received checksum */ 
     1240  d_crc_tuc |= BIT7; 
     1241  return d_crc_tuc; 
     1242} 
     1243 
     1244 
     1245void send_yield_query_day(unsigned char channel /* 0 to 5 */, unsigned char day /* 1 to 31 */) 
     1246{ 
     1247        unsigned char newChkSum; 
     1248 
     1249        req_day_yield[5] = 0x30 + channel; // channel 
     1250 
     1251 
     1252        /* convert day to binary coded hex */ 
     1253        if (day > 15) 
     1254        { 
     1255            req_day_yield[8] = 0x31; /* hex MSB day is 1 */ 
     1256            day = day - 16;          /* decrease day (is now remainder */ 
     1257        } 
     1258        else 
     1259        { 
     1260            req_day_yield[8] = 0x30; /* hex MSB day is 0 */ 
     1261        } 
     1262        req_day_yield[9] = 0x30 + day; /* hex LSB day */ 
     1263 
     1264        newChkSum = calculate_crc8(  (&(req_day_yield[0])), (sizeof(req_day_yield)-1)); 
     1265        req_day_yield[10] = newChkSum; /* fill in checksum */ 
     1266 
     1267        resetUart3RxBuf(); 
     1268        send_uart3((unsigned char *)req_day_yield, sizeof(req_day_yield)); 
     1269 
     1270} /* prepare_yield_query_day() */ 
     1271 
     1272 
     1273void send_yield_query_hour(unsigned char channel /* 0 to 5 */, unsigned char hour /* 0 to 23 */) 
     1274{ 
     1275 
     1276        unsigned char newChkSum; 
     1277        unsigned char hourMSB; 
     1278        unsigned char hourLSB; 
     1279 
     1280        req_hour_yield[5] = 0x30 + channel; // channel 
     1281 
     1282      
     1283        hourMSB = hour % 16; 
     1284        hourLSB = hour - hourMSB; 
     1285         
     1286        req_hour_yield[8] = 0x30 + hourMSB; // minute MSB (binary coded hex)  
     1287        req_hour_yield[9] = 0x30 + hourLSB; // minute LSB (binary coded hex) 
     1288 
     1289        newChkSum = calculate_crc8(  (&(req_hour_yield[0])), (sizeof(req_hour_yield)-1)); 
     1290 
     1291        // crc 
     1292        req_hour_yield[10] = newChkSum; /* fill in checksum */ 
     1293 
     1294        resetUart3RxBuf(); 
     1295        send_uart3((unsigned char *)req_hour_yield, sizeof(req_hour_yield)); 
     1296 
     1297 
     1298 
     1299} /* prepare_yield_query_hour() */ 
     1300 
     1301 
     1302void send_yield_query_minute(unsigned char channel /* 0 to 5 */, unsigned char minute /* 0 to 59 */) 
     1303{ 
     1304        unsigned char newChkSum; 
     1305        unsigned char minuteMSB; 
     1306        unsigned char minuteLSB; 
     1307 
     1308        req_minute_yield[5] = 0x30 + channel; // channel 
     1309 
     1310      
     1311        minuteMSB = minute % 16; 
     1312        minuteLSB = minute - minuteMSB; 
     1313         
     1314        req_minute_yield[6] = 0x30 + minuteMSB; // minute MSB (binary coded hex)  
     1315        req_minute_yield[7] = 0x30 + minuteLSB; // minute LSB (binary coded hex) 
     1316 
     1317        newChkSum = calculate_crc8(  (&(req_minute_yield[0])), (sizeof(req_minute_yield)-1)); 
     1318 
     1319        // crc 
     1320        req_minute_yield[10] = newChkSum; /* fill in checksum */ 
     1321 
     1322        resetUart3RxBuf(); 
     1323        send_uart3((unsigned char *)req_minute_yield, sizeof(req_minute_yield)); 
     1324 
     1325 
     1326} /* prepare_yield_query_minute() */