/************************************************************************************
** myinter.c      cgi interface für den raspberry
**
** 25.01.2014 li Erweiterung: 18 Monate Regenmenge
** 16.02.2014 li Korrektur Regenmenge 18 Monate 
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>     // string function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <time.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/shm.h>    //Used for shared memory
#include <sys/ipc.h>
#include "rxdcb.h"

typedef struct
{
   unsigned char ss;            // Sekunden
   unsigned char mm;            // Minute
   unsigned char hh;            // Stunde
   unsigned char day;           // Tag
   unsigned char wday;          // Wochentag
   unsigned char mon;           // Monat
   unsigned char year;          // Jahr
   unsigned char dmz;           // MEZ oder MESZ
   unsigned char message_flag;  // time message überwachung
} S_CLOCK;

S_CLOCK s_clock;                // Zeit gmess

// ------------------------------- globale variablen ----
extern int errno;

// *************************************** shared memory Basis ***************
int shared_memory1_id;
S_BASIS *a_basis;                  // reiner Datenteil

int shared_memory2_id;
char *tdata;                       // Temperatur Werte für 2 Tage

int shared_memory3_id;
unsigned char *regenwerte;         // Regen Werte

int shared_memory4_id;
unsigned char *counter;            // Heizung Strom und Gas

char cnt_text[20];
char monattext[20];                // hier weil als return used
char query[100];
char *qx[10];
int qanz = 0;
                       // 1   2   3   4   5   6   7   8   9   10  11  12
unsigned char motab[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };


int ff_read;
int ff_readcount;

// ------------------------------- Prototypen -----------
void print_header( int jssw, char *title );
void print_footer(void);
void seite1( void );
void seite2( void );
void seite3( void );
void seite4( void );
void seite5( void );
void seite6( void );
void seite7( void );
void errorseite( void );
void testseite( void );

unsigned int a_to_i( char *quelle, int anzahl);
unsigned char get_sonnen( char *pziel, unsigned char typ, int hh );
unsigned int compute_addr( int varnum , unsigned char mon,  unsigned char year );
char* read_counter( int varnum );
long read_longFsram( unsigned int addr );
void set_stime( void );
char* datums_text(int minus);
char* regen_datum(int b);
char* regen_monat( int b );
int read_intregen( int addr );
char * regenmenge( int varnum , int out );
int get_day_regen_addr( int jj, int mm, int day );
void print_fx( char *fx, int index, unsigned char aus2, unsigned char d);

int shared_varxx(void);
int shared_temperatur(void);
int shared_regen(void);
int shared_counter(void);

void  textfifo_startread(S_FIFO_TEXT *ff);
char* textfifo_get(S_FIFO_TEXT *ff);


/************************************************************************************
** Main
*/
int main(int argc, char *argv[])
{
   int pos,len;
   char *p_query ;             // query string
   char *p_req_method;         // request Method

   p_query = getenv( "QUERY_STRING" );
   p_req_method = getenv( "REQUEST_METHOD" );
   strcpy( query, p_query );
   len = strlen(query);
   if ( len > 0 )
   {
      qanz = 1;
      qx[0] = query;
   }

   for( pos = 0; pos < len; pos++)
   {
      if ( query[pos] == '&' )
      {
         query[pos] = '\0';
         qx[qanz++] = query+pos+1;
      }
   }

   if ( strncmp( p_req_method, "GET", 3) == 0 )
   {
      if ( qanz == 0 )
         seite1();
      else if ( strlen( qx[0]) == 1 && strncmp( qx[0], "S" , 1 ) == 0 )    // Status
         seite2();
      else if (strlen( qx[0]) == 1 && strncmp( qx[0], "H" , 1 ) == 0 )     // Heizung
         seite3();
      else if ( strlen( qx[0]) == 4 && strncmp( qx[0], "H=18" , 4 ) == 0 )  // Heizung 18
         seite4();
      else if (strlen( qx[0]) >= 1 && strncmp( qx[0], "R" ,1 ) == 0 )      // Regen
         seite5();
      else if (strlen( qx[0]) >= 1 && strncmp( qx[0], "N" ,1 ) == 0 )      // Regen 18 Monate
         seite7();
      else if ( strlen( qx[0]) >= 1 && strncmp( qx[0], "T" ,1 ) == 0 )     // Temperatur
         seite6();
      else
         errorseite();
   }
   else
      errorseite();
   return 0;
}



/************************************************************************************
** shared memory varxx suchen
*/
int shared_varxx(void)
{
   char *s;

   shared_memory1_id = shmget((key_t)1433571165, 0, 0 );
   if (shared_memory1_id == -1)
   {
      printf("Shared memory1 shmget() failed %d<br>\n",errno);
      return 1;
   }
   s = (char*)shmat( shared_memory1_id, 0, SHM_RDONLY ); //attach
   if (s == (void *)-1)
   {
      printf("Shared memory1 shmat() failed<br>\n");
      return 1;
   }
   a_basis = (S_BASIS*)s;                  //Assign the shared_memory segment
   return 0;
}



/************************************************************************************
** shared memory temperatur suchen
*/
int shared_temperatur(void)
{
   char *s;

   shared_memory2_id = shmget((key_t)1433571166, 0, 0 );
   if (shared_memory2_id == -1)
   {
      printf("Shared memory2 shmget() failed %d<br>\n",errno);
      return 1;
   }
   s = (char*)shmat( shared_memory2_id, 0, SHM_RDONLY ); //attach
   if (s == (void *)-1)
   {
      printf("Shared memory2 shmat() failed<br>\n");
      return 1;
   }
   tdata = s;                                //Assign the shared_memory segment
   return 0;
}



/************************************************************************************
** shared memory regenwerte suchen
*/
int shared_regen(void)
{
   char *s;

   shared_memory3_id = shmget((key_t)1433571167, 0, 0 );
   if (shared_memory3_id == -1)
   {
      printf("Shared memory3 shmget() failed %d<br>\n",errno);
      return 1;
   }
   s = (char*)shmat( shared_memory3_id, 0, SHM_RDONLY ); //attach
   if (s == (void *)-1)
   {
      printf("Shared memory3 shmat() failed<br>\n");
      return 1;
   }
   regenwerte = (unsigned char*)s;           //Assign the shared_memory segment
   return 0;
}



/************************************************************************************
** shared memory counter suchen
*/
int shared_counter(void)
{
   char *s;

   shared_memory4_id = shmget((key_t)1433571168, 0, 0 );
   if (shared_memory2_id == -1)
   {
      printf("Shared memory4 shmget() failed %d<br>\n",errno);
      return 1;
   }
   s = (char*)shmat( shared_memory4_id, 0, SHM_RDONLY ); //attach
   if (s == (void *)-1)
   {
      printf("Shared memory4 shmat() failed<br>\n");
      return 1;
   }
   counter = (unsigned char*)s;                 //Assign the shared_memory segment
   return 0;
}



/************************************************************************************
** Seite 1 = home Seite
** 27.12.2013 li ok
*/
void seite1( void )
{
   time_t zeit;
   char *pt;
   char p_on[] = "/ledon.gif";
   char p_off[] = "/ledoff.gif";
   char *px;
   char txt1[20];
   char txt2[10];
   int n;

   zeit = time(NULL);
   pt = ctime(&zeit);

   print_header(1,"home");             // mit scri1.js

   if ( shared_varxx() )               // shared memory var xx
      printf("Sorry, shared Memory1 not found<br>\n");
   else
   {
      printf("Zeit: %s<br><br>\n",pt);
      n = a_to_i( pt+11,2);            // Stunde
      get_sonnen( txt1, 0, n );        // Sonnen auf u. Untergang
      get_sonnen( txt2, 1, n );        // Tageslänge in hh:mm

      printf("Sonne: %s Tageslänge: %s<br>\n", txt1, txt2 );
      printf("<hr>\n");
      printf("<table class=\"tempTbl\" align=\"center\" border=\"1\">\n");
      printf("<tr>\n");
      printf("<th width=\"160\">Alarm Meldungen</th>\n");
      printf("<th width=\"50\">ein/aus</th>\n");
      printf("</tr>\n");

      if ( a_basis->alarm & A_TEG ) px = p_on; else px = p_off;
      printf("<tr><td>Terrassentür EG auf</td><td><img src=\"%s\" alt=\"\"></td></tr>\n",px);
      if ( a_basis->alarm & A_TWA ) px = p_on; else px = p_off;
      printf("<tr><td>Tür Waschküche auf</td><td><img src=\"%s\" alt=\"\"></td></tr>\n",px);
      if ( a_basis->alarm & A_TKE ) px = p_on; else px = p_off;
      printf("<tr><td>Keller Tür zu</td><td><img src=\"%s\" alt=\"\"></td></tr>\n",px);
      if ( a_basis->alarm & A_BWM ) px = p_on; else px = p_off;
      printf("<tr><td>Bewegungsmelder</td><td><img src=\"%s\" alt=\"\"></td></tr>\n",px);
      if ( a_basis->alarm & A_FEG ) px = p_on; else px = p_off;
      printf("<tr><td>Fenster EG auf</td><td><img src=\"%s\" alt=\"\"></td></tr>\n",px);
      if ( a_basis->alarm & A_REG ) px = p_on; else px = p_off;
      printf("<tr><td>Regen Messer tot</td><td><img src=\"%s\" alt=\"\"></td></tr>\n",px);
      if ( a_basis->alarm & A_BTOT ) px = p_on; else px = p_off;
      printf("<tr><td>Basis tot</td><td><img src=\"%s\" alt=\"\"></td></tr>\n",px);

      printf("</table>\n");
      printf("<hr>\n");
      printf("<h3>Temperatur Messwerte</h3>\n");
      printf("<canvas id=\"mC1\" width=\"500\" height=\"60\"></canvas>\n");
      printf("<script>\n");
      printf("init2d(\"mC1\");\n");
                                                    // temperatur 4stellig
      printf("hgrad(%04d,\"Aussen\");\n", a_basis->temperatur_C);
      printf("hgrad(%04d,\"GWH\");\n", a_basis->temperatur_G);
      printf("close_t();\n");
      printf("</script>\n");
      printf("<br><hr>\n");
      printf("<h3>Regenmesswerte</h3>\n");
      printf("<canvas id=\"mC2\" width=\"550\" height=\"100\"></canvas>\n");
      printf("<script>\n");
      printf("init2d(\"mC2\");\n");
                                                     // regen 5stellig
      printf("hregen(%05d,\"Akt.h\");\n", a_basis->regen_stunde + 10000);
      printf("hregen(%05d,\"Heute\");\n", a_basis->regen_heute + 10000);
      printf("hregen(%05d,\"Gestern\");\n", a_basis->regen_gestern + 10000);
      printf("hregen(%05d,\"Vorgestern\");\n", a_basis->regen_vorgestern + 10000);
      printf("close_r();\n");
      printf("</script>\n");
   }
   print_footer();
}



/**************************************************************
** Sonnen auf oder Untergang
** oder Tageslänge
** 012345678901234567
** tpy=0:  SA hh:mm SU hh:mm
** tpy=1:  hh:mm
** hh = aktuelle Stunde, wenn > 12, dann Form SU ... SA ....
*/
unsigned char get_sonnen( char *pziel, unsigned char typ, int hh )
{
   int sz1, sz2, duration;

   sz1 = a_basis->sonnen_auf;       // Zeit in minuten
   sz2 = a_basis->sonnen_unt;       // zeit in Minuten

   if ( typ == 0)    // Sonnen Aufgangs- und Untergangszeit
   {
      if ( sz1 > sz2 )
	      sprintf( pziel,"SU %2d:%02d SA %2d:%02d", sz1/60, sz1%60, sz2/60, sz2%60);
      else
         sprintf( pziel,"SA %2d:%02d SU %2d:%02d", sz1/60, sz1%60, sz2/60, sz2%60);
   }
   else
   {
      if ( sz1 > sz2 )
         duration = sz1-sz2;
      else
         duration = sz2-sz1;
      sprintf( pziel,"%2d:%02d", duration/60, duration%60 );
   }
   return strlen( pziel );
}



/************************************************************************************
** Status Seite
*/
void seite2(void)
{
   time_t ts;
   char *pt;
   int tu, tc;
   S_FIFO_TEXT*sft;


   print_header(0,"Status");
   if ( shared_varxx() )               // shared memory var xx
      printf("Sorry, shared Memory1 not found<br>\n");
   else
   {
      printf("<div id=\"main\">\n");
      printf("27.12.2013: Umstellung auf den raspberry PI B<br><br>\n");
      printf("Compiliert am "__DATE__" um " __TIME__"<br><br>\n");
      printf("Compiliert mit GCC Version "__VERSION__"<br><br>\n");

      printf("Nachrichten RS232 OK...: %.0f<br><br>\n", a_basis->uart_ok );
      printf("Nachrichten RS232 Error: %.0f<br><br>\n", a_basis->uart_error );
      printf("rxdgmess Version %d<br><br>\n", a_basis->version );

      ts = (time_t)a_basis->stime;
      pt = ctime(&ts);
      printf("rxdgmess Startzeit %s<br><br>\n", pt );

      ts = (time_t)a_basis->last_time;
      pt = ctime(&ts);
      printf("rxdgmess last timestamp %s<br><br>\n", pt );

      printf("rxdgmess Regenadresse: %d<br><br>\n", a_basis->regen_adresse);
      printf("rxdgmess Temperaturswitch: %d<br><br>\n", a_basis->temperatur_switch);
      printf("<br>\n");
      printf("</div>\n");
   }
   print_footer();
}
/*****************************************************************************
** zeichen aus dem fifo buffer holen
** 2.1.2014 locale var ff_read und ff_readcount verwenden
*/
void textfifo_startread(S_FIFO_TEXT *ff)
{
   int i;

   ff_read = ff->write;
   ff_readcount = ff->count;
/*
   for ( i=0; i < ff_readcount; i++)
   {
      ff_read -= 1;
      ff_read &= 31;
   }
*/
}
/*****************************************************************************
** zeichen aus dem fifo buffer holen
** 2.1.2014 locale var ff_read und ff_readcount verwenden
*/
char* textfifo_get(S_FIFO_TEXT *ff)
{
   char *s;

   if ( ff_readcount == 0 )
      return NULL ;

   ff_read &= 31;   // wrap around
   s = ff->adrbuf[ ff_read ];
   ff_read++ ;
   ff_readcount--;

   return s;
}



/************************************************************************************
** Heizungs Werte
*/
void seite3(void)
{
   time_t ts;
   char *pt;


   print_header(1,"Heizung");

   if ( shared_varxx() )               // shared memory var xx
      printf("Sorry, shared Memory1 not found<br>\n");
   else if ( shared_counter() )     // shared memory counter
      printf("Sorry, shared Memory2 not found<br>\n");
   else
   {
      set_stime();                  // struct s_clock beleben

      printf("<div id=\"main\">\n");
      printf("<h3>Temperatur Messwerte</h3>\n");
      printf("<canvas id=\"mC1\" width=\"500\" height=\"100\"></canvas>\n");
      printf("<script>\n");
      printf( "init2d(\"mC1\");\n");
      printf( "hgrad(%d,\"Aussen\");\n",  a_basis->temperatur_C);
      printf( "hgrad(%d,\"W.Wasser\");\n",a_basis->temperatur_W);
      printf( "hgrad(%d,\"Vorlauf\");\n", a_basis->temperatur_V);
      printf( "hgrad(%d,\"Rücklauf\");\n",a_basis->temperatur_B);
      printf( "close_t();\n");
      printf("</script>\n");

      printf("<br><hr><br>\n");
      printf("<h3>Verbrauch</h3>\n");
      printf("<table class=\"tempTbl\" align=\"center\" border=\"1\">\n");
      printf("<tbody>\n");
      printf("<tr>\n");
      printf("<th width=\"150\" >Zähler</th>\n");
      printf("<th width=\"114\" >Zählerstand</th>\n");

      printf("<th width=\"114\">%s</th>\n",datums_text(0));
      printf("<th width=\"114\">%s</th>\n",datums_text(1));
      printf("<th width=\"114\">%s</th>\n",datums_text(2));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>Gas gesamt</td>\n");
      printf("<td>%s m³</td>\n", read_counter(0));      //             Zählerstand
      printf("<td>%s m³</td>\n", read_counter(128));    // 128 + 0*4    Verbrauch aktuell
      printf("<td>%s m³</td>\n", read_counter(132));    // 128 + 1*4    Verbrauch Monat-1
      printf("<td>%s m³</td>\n", read_counter(136));    // 128 + 2*4    Verbrauch Monat-2
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>Warmwasser Anteil</td>\n");
      printf("<td>%s m³</td>\n", read_counter(1));      //             Zählerstand
      printf("<td>%s m³</td>\n", read_counter(129));
      printf("<td>%s m³</td>\n", read_counter(133));
      printf("<td>%s m³</td>\n", read_counter(137));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>Strom (Heizung)</td>\n");
      printf("<td>%s kWh</td>\n", read_counter(2));     //             Zählerstand
      printf("<td>%s kWh</td>\n", read_counter(130));
      printf("<td>%s kWh</td>\n", read_counter(134));
      printf("<td>%s kWh</td>\n", read_counter(138));
      printf("</tr>\n");
      printf("</tbody>\n");
      printf("</table>\n");
      printf("&nbsp;<br>\n");

      printf("</div>\n");
   }
   print_footer();
}
/************************************************************************************
**  monat aufbereiten  => Jan. 2014
*/
char* datums_text(int minus)
{
   int i;
   unsigned char monat;
   int jahr;
   char monate[] = "JanFebMarAprMaiJunJulAugSepOktNovDez";
   char mtext[4] = "123";

   monat = s_clock.mon;
   jahr  = s_clock.year;

   for (i=0; i < minus; i++)
   {
      monat -= 1;
      if ( monat == 0 )
      {
         monat = 12;
         jahr -= 1;
      }
   }
   jahr += 2000;
   monat -= 1;
   monat *= 3;
   strncpy( mtext, monate+monat, 3);
   sprintf(monattext,"%s. %d",mtext,jahr);
   return ( monattext );
}
/************************************************************************************
** 0 = Gas 1=GasB 2=Strom 3=not used
** .... .... .dmm mmcc
**            ||     |- Counter 0-3
**            ||------- Monate minus
**            |-------- Differenz Bilden
*/
char* read_counter( int varnum )
{
   unsigned int r,rr;
   int flg,addr;
   float x,y,z;
   unsigned char mon,jahr,p;
   char *s;

   r = compute_addr( varnum, s_clock.mon, s_clock.year );

   rr = r & 0xFFF0 ;        // compute zeilen Adresse
   p = (unsigned char)(varnum & 0x0003);
   flg = counter[ rr+12+p ];

   if ((flg & 0x0F) == 0 )    // Monat vorhanden im flag ??
   {
      x = 0.0;
   }
   else
   {
      x =  read_longFsram( r ); // read counter Monat

      if ( varnum & 0x0080 )   // 23.9.2011 Verbrauch ??
      {
         z = 0.0;
         if ( (flg & 0xF0 ) )  // patch vorhanden ??
         {
            addr = flg & 0xF0;               // patch adresse ermitteln
            addr >>= 1;                      //
            addr += 0x208;                   // beginn der area
            z = read_longFsram( addr+4 );    // read Start counter Wechsel

            x -= z;                          // Differenz = Verbrauch neuer Zähler
            z = x;                           // merken
            x = read_longFsram( addr );      // read Ende alter Zähler

         }
         mon = s_clock.mon -1;
         jahr = s_clock.year;
         if ( mon == 0 )
         {
            mon = 12;
            jahr -= 1;
         }
         r = compute_addr( varnum, mon, jahr );  // Vormonat Adresse
         rr = r & 0xFFF0 ;        // compute zeilen Adresse
         p = (unsigned char)(varnum & 0x0003);
         flg = counter[ rr+12+p];
         if ((flg & 0x0F) == 0 )    // Monat vorhanden im flag ??
         {
            x = 0.0;
         }
         else
         {
            y = read_longFsram( r );  // read last counter Vormonat
            x -= y;                   // Differenz = Verbrauch
            x += z;
         }
      }
   }
   sprintf(cnt_text,"%6.3f", x/1000.0);
   s = strchr(cnt_text, '.' );        // punkt suchen
   if ( s != NULL )
      *s = ',';                       // in komma verwandeln
   return(cnt_text);
}

/**************************************************************
** SRAM Adresse für den counter berechnen
** .... .... dmmm mmcc
**           ||     |- Counter 0-3
**           ||------- Monate minus ( maximal 23, deswegen 5 Bits )
**           |-------- Differenz Bilden
*/
unsigned int compute_addr( int varnum , unsigned char mon,  unsigned char year )
{
   unsigned int adr, r;

   r = varnum & 0x007C;              // Monat Filter
   r >>= 2;                          // anzahl monate zurück
   adr = mon;                        // aktueller Monat
   if ( r >= adr )
   {
      adr +=
      12;
      year -= 1;
   }
   if ( r >= adr )
   {
      adr += 12;
      year -= 1;
   }
   adr -= r;
   adr -= 1;
   adr *= 16;
   if ( year & 0x01 )
      adr += 0x100;
   r = (varnum & 0x0003);
   r *= 4;                            // counter liegen je 4 Byte auseinander
   adr += r;
   return adr;
}
/**************************************************************
** read long int from sram
*/
long read_longFsram( unsigned int addr )
{
   long l;
   l = counter[addr++];
   l <<= 8;
   l += counter[addr++];
   l <<= 8;
   l += counter[ addr++ ];
   l <<= 8;
   l += counter[ addr ];
   return l;
}


/*****************************************************************************
** s_clock setzen
*/
void set_stime( void )
{
   int i;
   time_t xt;
   unsigned char x,c;
   char *ct;
   char tt[30];
   char mtext[50];

   xt = a_basis->last_time;

   ct = ctime( &xt );                       // Zeit aufbereiten
   strcpy( tt, ct );

   i = a_to_i( tt+17, 2 );                  // Sekunde
   s_clock.ss  = (unsigned char)i;

   i = a_to_i( tt+14, 2 );                  // Minute
   s_clock.mm  = (unsigned char)i;

   i = a_to_i( tt+11, 2 );                  // Stunde
   s_clock.hh  = (unsigned char)i;

   i = a_to_i( tt+8, 2 );                   // Tag
   s_clock.day = (unsigned char)i;

   // Monate: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
   c = tt[4];
   switch ( c )
   {
     case 'F': s_clock.mon = 2; break;

     case 'M':
      if ( tt[6] == 'r' )
         s_clock.mon = 3;
      else
         s_clock.mon = 5;
      break;

     case 'A':
      if ( tt[5] == 'p' )
         s_clock.mon = 4;
      else
         s_clock.mon = 8;
      break;

     case 'J':
      if ( tt[5] == 'a' )
         s_clock.mon = 1;
      else if ( tt[6] == 'n' )
         s_clock.mon = 6;
      else
         s_clock.mon = 7;
      break;

     case 'S':  s_clock.mon = 9; break;
     case 'O':  s_clock.mon = 10; break;
     case 'N':  s_clock.mon = 11; break;
     case 'D':  s_clock.mon = 12; break;
   }
   i = a_to_i( tt+22, 2 );                  // Jahr
   s_clock.year = (unsigned char)i;
}
/************************************************************************************
** 18 Monate Counter werte
*/
void seite4(void)
{
   char dd[20];

   print_header(0,"Heizung 18 Monate");

   if ( shared_varxx() )               // shared memory var xx
      printf("Sorry, shared Memory varxx not found<br>\n");
   else if ( shared_counter() )     // shared memory counter
      printf("Sorry, shared Memory counter not found<br>\n");
   else
   {
      set_stime();                  // struct s_clock beleben

      sprintf(dd,"%02d.%02d.%d", s_clock.day, s_clock.mon, s_clock.year + 2000);

      printf("<div id=\"main\">\n");
      printf("<h3>Heizungs-Verbrauchswerte, Rückblick auf 18 Monate vom %s</h3>\n",dd);
      printf("<table class=\"tempTbl\" align=\"center\" border=\"1\">\n");
      printf("<tbody>\n");
      printf("<tr>\n");
      printf("<th width=\"80\">Monat</th>\n");
      printf("<th width=\"114\">Gas gesamt</th>\n");
      printf("<th width=\"114\">W.Wasser Anteil</th>\n");
      printf("<th width=\"114\">Strom (Heizung)</th>\n");
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(1));
      printf("<td>%s m³</td>\n",read_counter(132));      // 128+ 1*4
      printf("<td>%s m³</td>\n",read_counter(133));
      printf("<td>%s kWh</td>\n",read_counter(134));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(2));
      printf("<td>%s m³</td>\n",read_counter(136));      // 128+ 2*4
      printf("<td>%s m³</td>\n",read_counter(137));
      printf("<td>%s kWh</td>\n",read_counter(138));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(3));
      printf("<td>%s m³</td>\n",read_counter(140));      // 128+ 3*4
      printf("<td>%s m³</td>\n",read_counter(141));
      printf("<td>%s kWh</td>\n",read_counter(142));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(4));
      printf("<td>%s m³</td>\n",read_counter(144));      // 128+ 4*4
      printf("<td>%s m³</td>\n",read_counter(145));
      printf("<td>%s kWh</td>\n",read_counter(146));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(5));
      printf("<td>%s m³</td>\n",read_counter(148));      // 128+ 5*4
      printf("<td>%s m³</td>\n",read_counter(149));
      printf("<td>%s kWh</td>\n",read_counter(150));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(6));
      printf("<td>%s m³</td>\n",read_counter(152));      // 128+ 6*4
      printf("<td>%s m³</td>\n",read_counter(153));
      printf("<td>%s kWh</td>\n",read_counter(154));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(7));
      printf("<td>%s m³</td>\n",read_counter(156));      // 128+ 7*4
      printf("<td>%s m³</td>\n",read_counter(157));
      printf("<td>%s kWh</td>\n",read_counter(158));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(8));
      printf("<td>%s m³</td>\n",read_counter(160));      // 128+ 8*4
      printf("<td>%s m³</td>\n",read_counter(161));
      printf("<td>%s kWh</td>\n",read_counter(162));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(9));
      printf("<td>%s m³</td>\n",read_counter(164));      // 128+ 9*4
      printf("<td>%s m³</td>\n",read_counter(165));
      printf("<td>%s kWh</td>\n",read_counter(166));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(10));
      printf("<td>%s m³</td>\n",read_counter(168));      // 128+ 10*4
      printf("<td>%s m³</td>\n",read_counter(169));
      printf("<td>%s kWh</td>\n",read_counter(170));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(11));
      printf("<td>%s m³</td>\n",read_counter(172));      // 128+ 11*4
      printf("<td>%s m³</td>\n",read_counter(173));
      printf("<td>%s kWh</td>\n",read_counter(174));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(12));
      printf("<td>%s m³</td>\n",read_counter(176));      // 128+ 12*4
      printf("<td>%s m³</td>\n",read_counter(177));
      printf("<td>%s kWh</td>\n",read_counter(178));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(13));
      printf("<td>%s m³</td>\n",read_counter(180));      // 128+ 13*4
      printf("<td>%s m³</td>\n",read_counter(181));
      printf("<td>%s kWh</td>\n",read_counter(182));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(14));
      printf("<td>%s m³</td>\n",read_counter(184));      // 128+ 14*4
      printf("<td>%s m³</td>\n",read_counter(185));
      printf("<td>%s kWh</td>\n",read_counter(186));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(15));
      printf("<td>%s m³</td>\n",read_counter(188));      // 128+ 15*4
      printf("<td>%s m³</td>\n",read_counter(189));
      printf("<td>%s kWh</td>\n",read_counter(190));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(16));
      printf("<td>%s m³</td>\n",read_counter(192));      // 128+ 16*4
      printf("<td>%s m³</td>\n",read_counter(193));
      printf("<td>%s kWh</td>\n",read_counter(194));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(17));
      printf("<td>%s m³</td>\n",read_counter(196));      // 128+ 17*4
      printf("<td>%s m³</td>\n",read_counter(197));
      printf("<td>%s kWh</td>\n",read_counter(198));
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td>%s</td>\n",datums_text(18));
      printf("<td>%s m³</td>\n",read_counter(200));      // 128+ 18*4
      printf("<td>%s m³</td>\n",read_counter(201));
      printf("<td>%s kWh</td>\n",read_counter(202));
      printf("</tr>\n");
      printf("</tbody>\n");
      printf("</table>\n");
      printf("<br>\n");
      printf("</div>\n");
      print_footer();
   }
}



/************************************************************************************
** Regen Seite
*/
void seite5(void)
{
   int parnum;
   char sel[] = "selected=\"true\"";
   char snl[] = "";
   char *sx;

   if ( strlen( qx[0]) == 3 && strncmp( qx[0], "R=0", 3 ) == 0 )           // Regen
      parnum = 0;
   else if ( strlen( qx[0]) == 4 && strncmp( qx[0], "R=32", 4 ) == 0 )     // Regen
      parnum = 32;
   else if ( strlen( qx[0]) == 4 && strncmp( qx[0], "R=64", 4 ) == 0 )     // Regen
      parnum = 64;
   else if ( strlen( qx[0]) == 4 && strncmp( qx[0], "R=96", 4 ) == 0 )     // Regen
      parnum = 96;
   else if ( strlen( qx[0]) == 5 && strncmp( qx[0], "R=128", 5 ) == 0 )    // Regen
      parnum = 128;
   else
      parnum = 0;

   print_header(1,"Regen");

   if ( shared_varxx() )               // shared memory var xx
      printf("Sorry, shared Memory varxx not found<br>\n");
   else if ( shared_regen() )     // shared memory regen
      printf("Sorry, shared Memory regenwerte not found<br>\n");
   else
   {
      set_stime();                  // struct s_clock beleben

      printf("<div id=\"main\">\n");
      printf("<table align=\"center\" border=\"0\">\n");
      printf("<tr>\n");
      printf("<td rowspan=\"2\">\n");
      printf("<h4>Regenmengen %d bis %d Tage zurück</script></h4>\n", parnum+1, parnum+33 );
      printf("<canvas id=\"mC2\" width=\"510\" height=\"620\"></canvas>\n");
      printf("<script>\n");
      printf("init2d(\"mC2\");\n");
      printf("hregen(%s,\"%s\");\n",regenmenge(257,parnum), regen_datum(1+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(258,parnum), regen_datum(2+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(259,parnum), regen_datum(3+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(260,parnum), regen_datum(4+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(261,parnum), regen_datum(5+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(262,parnum), regen_datum(6+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(263,parnum), regen_datum(7+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(264,parnum), regen_datum(8+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(265,parnum), regen_datum(9+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(266,parnum), regen_datum(10+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(267,parnum), regen_datum(11+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(268,parnum), regen_datum(12+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(269,parnum), regen_datum(13+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(270,parnum), regen_datum(14+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(271,parnum), regen_datum(15+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(272,parnum), regen_datum(16+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(273,parnum), regen_datum(17+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(274,parnum), regen_datum(18+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(275,parnum), regen_datum(19+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(276,parnum), regen_datum(20+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(277,parnum), regen_datum(21+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(278,parnum), regen_datum(22+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(279,parnum), regen_datum(23+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(280,parnum), regen_datum(24+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(281,parnum), regen_datum(25+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(282,parnum), regen_datum(26+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(283,parnum), regen_datum(27+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(284,parnum), regen_datum(28+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(285,parnum), regen_datum(29+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(286,parnum), regen_datum(30+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(287,parnum), regen_datum(31+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(288,parnum), regen_datum(32+parnum));
      printf("hregen(%s,\"%s\");\n",regenmenge(289,parnum), regen_datum(33+parnum));
      printf("close_r();\n");
      printf("</script>\n");
      printf("</td>\n");
      printf("<td valign=\"bottom\">\n");
      printf("<h3>Zeitauswahl</h3>\n");
      printf("<form name=\"RT\" method=\"GET\" action=\"/cgi-bin/myinter.cgi\">\n");
      printf("<select name=\"R\" size=\"5\">\n");
      if ( parnum == 0 ) sx = sel; else sx = snl;
      printf("<option value=\"0\" %s>  1-33 Tage</option>\n",sx);
      if ( parnum == 32 ) sx = sel; else sx = snl;
      printf("<option value=\"32\" %s> 33-65 Tage</option>\n",sx);
      if ( parnum == 64 ) sx = sel; else sx = snl;
      printf("<option value=\"64\" %s> 65-97 Tage</option>\n",sx);
      if ( parnum == 96 ) sx = sel; else sx = snl;
      printf("<option value=\"96\" %s> 97-129 Tage</option>\n",sx);
      if ( parnum == 128 ) sx = sel; else sx = snl;
      printf("<option value=\"128\" %s>129-161 Tage</option>\n",sx);
      printf("</select>\n");
      printf("</td>\n");
      printf("</tr>\n");
      printf("<tr>\n");
      printf("<td valign=\"top\">\n");
      printf("<input name=\"SUB\" value=\"Senden\" type=\"submit\">\n");
      printf("</form>\n");
      printf("</td>\n");
      printf("</tr>\n");
      printf("</table>\n");
      printf("</div>\n");
   }
   print_footer();
}

/**************************************************************
** Regenmenge aus dem Speicher
*/
char * regenmenge( int varnum, int out )
{
   int x;
   char *s;
   int tag,mon,jahr,r,addr;
   int regen_korr = 10000;

   x = 0;
   varnum += out;                    // 25.2.2013 output parameter
   if ( varnum & 0x0100 )            // Regen werte je Tag ??  256 bis 511
   {
      r = varnum & 0x00FF;           // 0-255 zulassen

      s = regen_datum(r);            // r = anzahl der Tage minus current datum

      tag =  a_to_i( s, 2);       // Tag x
      mon =  a_to_i( s+3, 2);     // monat
      jahr = a_to_i( s+8, 2);     // jahr

      if ( jahr == 12 )              // ab dem 16.12.2012 Regen Korr 10000
      {                              // wegen Regenmesser mit anderer Kalibrierung
         if ( mon < 12 )
            regen_korr = 0;
         else
         {
            if ( tag < 16 )
               regen_korr = 0;
         }
      }
      addr = get_day_regen_addr( jahr, mon, tag );  // gleiche Routine wie Speichern
      x = read_intregen( addr );         // 2 Byte Regen menge

   }
   else                                   // nuer für Stunde und heute gültig
   {
      if ( varnum == 0 || varnum == 1 )   // current Regen Mengen ??
         x = read_intregen( (varnum*2)+0x0020 );    // 0x0020   oder 0x0022
   }
   x += regen_korr;        // ab dem 16.12.2012 10000 dazu wegen applet
   sprintf( cnt_text,"%d",x);
   return( cnt_text );
}

/**************************************************************
** read int from shared memory regen
*/
int read_intregen( int addr )
{
   int y;

   y = regenwerte[ addr ];
   y <<= 8;
   y += regenwerte[ addr+1 ];
   return y;
}

/**************************************************************
** Aufbereitung Datum für die Regenmenge
** Ergebnis = 01.10.2011
** returnwert = string länge = 10
*/
char* regen_datum( int b )
{
   unsigned char i=0,j;
   int tage=0;

   j = s_clock.year ;
   if ( j )               // Zeit da ??
   {
      // 1. Schritt = berechnen der Tage des aktuellen Tages

      if ( j % 4 == 0 )        // Schaltjahr ??
         motab[1] = 29;
      else
         motab[1] = 28;

      for ( i=0; i < (s_clock.mon -1) ; i++)
         tage += motab[i];
      tage += s_clock.day;

      if ( b >= tage )
      {
         j--;                  // jahr -1
         if ( j % 4 == 0 )     // Schaltjahr ??
         {
            motab[1] = 29;
            tage += 366;
         }
         else
         {
            motab[1] = 28;
            tage += 365;
         }
      }
      tage -= b;
      for ( i = 0; tage > motab[i]; i++ )
         tage -= motab[i];
   }
   sprintf( monattext, "%02d.%02d.%04d",tage,i+1,j+2000);

   return(monattext);
}


/**************************************************************
** adresse regen tag
*/
int get_day_regen_addr( int jj, int mm, int day )
{
   int i;
   int addr = 0, jahr;

   if ( jj % 4 == 0 )       // Schaltjahr ??
      motab[1] = 29;
   else
      motab[1] = 28;

   for ( i=0; i < (mm -1); i++)
      addr += motab[i];

   addr += day;
   addr *= 2;
   jahr = jj & 0x03 ;       // speicher Adresse Start ermitteln
   jahr *= 0x0400;
   addr += jahr;

   return addr;
}


/************************************************************************************
** Regen Seite, 18 Monate
*/
void seite7(void)
{
   print_header( 1, "Regen 18 Monate");

   if ( shared_varxx() )            // shared memory var xx
      printf("Sorry, shared Memory varxx not found<br>\n");
   else if ( shared_regen() )       // shared memory regen
      printf("Sorry, shared Memory regenwerte not found<br>\n");
   else
   {
      set_stime();                  // struct s_clock beleben

      printf("<div id=\"main\">\n");
      printf("<table align=\"center\" border=\"0\">\n");
      printf("<tr><td>\n");
      printf("<h4>Regenmengen der letzen 18 Monate</script></h4>\n" );
      printf("<canvas id=\"mC2\" width=\"510\" height=\"400\"></canvas>\n");
      printf("<script>\n");
      printf("init2d(\"mC2\");\n");
      printf("hregen(%s);\n",regen_monat(0));
      printf("hregen(%s);\n",regen_monat(1));
      printf("hregen(%s);\n",regen_monat(2));
      printf("hregen(%s);\n",regen_monat(3));
      printf("hregen(%s);\n",regen_monat(4));
      printf("hregen(%s);\n",regen_monat(5));
      printf("hregen(%s);\n",regen_monat(6));
      printf("hregen(%s);\n",regen_monat(7));
      printf("hregen(%s);\n",regen_monat(8));
      printf("hregen(%s);\n",regen_monat(9));
      printf("hregen(%s);\n",regen_monat(10));
      printf("hregen(%s);\n",regen_monat(11));
      printf("hregen(%s);\n",regen_monat(12));
      printf("hregen(%s);\n",regen_monat(13));
      printf("hregen(%s);\n",regen_monat(14));
      printf("hregen(%s);\n",regen_monat(15));
      printf("hregen(%s);\n",regen_monat(16));
      printf("hregen(%s);\n",regen_monat(17));
      printf("hregen(%s);\n",regen_monat(18));
      printf("close_r();\n");
      printf("</script>\n");
      printf("</td></tr></table>\n");  //16.2.14 table ende fehlte 
      printf("</div>\n");
   }
   print_footer();
}


/************************************************************************************
** Aufbereitung Datum für die Regenmenge Monat
** Ergebnis = xxxxx,"Jan. 2014"
** returnwert = string länge = 18
** ausw = 0, aktueller Monat, ausw=1 -> monat-1, usw.
*/
char* regen_monat( int ausw )
{
   unsigned char i=0,j;
   int monat;
   int addr;
   int anzahl=0;
   int menge = 0;
   char mtxt[10] ;

   j = s_clock.year;
   monat = s_clock.mon;

   if ( j % 4 == 0 )        // Schaltjahr ??
      motab[1] = 29;
   else
      motab[1] = 28;

   if ( ausw == 0 )
   {
      anzahl = s_clock.day - 1;
   }
   else
   {
      for ( i = 0; i < ausw; i++ )
      {
         monat--;
         if ( monat == 0 )
         {
            monat = 12;
            j--;
            if ( j % 4 == 0 )        // Schaltjahr ??
               motab[1] = 29;
            else
               motab[1] = 28;
         }
      }
      anzahl = motab[ monat -1 ];
   }

   if ( anzahl )
   {
      addr = get_day_regen_addr( j, monat, 1 );       // 1. Tag des Monats

      for ( i=0; i < anzahl; i++ )
      {
         menge += read_intregen( addr );
         addr += 2;
      }
   }
   if ( j > 12 )                   // jahr > 2012 ??
      menge += 10000;

   sprintf( mtxt,"%s", datums_text(ausw));

   sprintf( monattext, "%05d, \"%s\"", menge, mtxt);

   return( monattext );
}


/************************************************************************************
** Temperratur Seite
*/
void seite6(void)
{
   int addr,i,p;
   int offset = 0;
   char sel[] = "selected=\"true\"";
   char snl[] = "";
   char *sx;
   unsigned char cm, auswahl2=0 ;
   int auswahl,auswahl1=0,auswahl3=0;
   char text[28];

   print_header( 2, "Temperatur Grafik" );

   if ( shared_varxx() )               // shared memory var xx
      printf("Sorry, shared Memory1 not found<br>\n");
   else if ( shared_temperatur() )     // shared memory temperatur
      printf("Sorry, shared Memory2 not found<br>\n");
   else
   {
      for ( i=0; i < qanz; i++)             // loop durch die query
      {
         sx = qx[i];
         switch ( sx[0] )
         {
           // ..........................................................
           case 'D':                        // D=n
            auswahl3 = sx[2] & 0x03;        // D=0 0-6 ....  3 18-24
            break;
            // ..........................................................
           case 'T':                        // T=1   Heute, T=0 gestern
            auswahl1 = sx[2] & 0x01;
            break;
           // ..........................................................
           case 'Z':                        // Z=n
            auswahl2 = sx[2] & 0x01;        // Z=0 24h  Z=1 6H
            break;
           // ..........................................................
           case 'M':                        // M=0 oder 1  mean
            cm = sx[2] & 0x01;
            break;

           default:
            break;
         }
      }

      auswahl = auswahl1;
      if ( auswahl2 )               // 6h auswahl
      {
         if ( auswahl1 )            // heute ?
            auswahl += auswahl3+5;
         else
            auswahl += auswahl3+2;
      }

      p = a_basis->temperatur_switch & 0x01 ;  // T-Flag lesen

      switch( auswahl )
      {
         case 0: p ^= 1; strcpy(text,"von Gestern, 24h");break;
         case 1: strcpy(text,"von Heute, 24h"); break;
         case 2: p ^= 1; strcpy(text,"von Gestern, 0:00-6:00");break;
         case 3: offset =  720; p ^= 1; strcpy(text,"von Gestern, 6:00-12:00"); break;
         case 4: offset = 1440; p ^= 1; strcpy(text,"von Gestern, 12:00-18:00"); break;
         case 5: offset = 2160; p ^= 1; strcpy(text,"von Gestern, 18:00-24:00"); break;
         case 6: strcpy(text,"von heute, 0:00-6:00"); break;
         case 7: offset =  720; strcpy(text,"von Heute, 6:00-12:00"); break;
         case 8: offset = 1440; strcpy(text,"von Heute, 12:00-18:00"); break;
         case 9: offset = 2160; strcpy(text,"von Heute, 18:00-24:00"); break;
      }
      addr = 0;
      if ( p )
         addr = 0x4000;

      printf("<div id=\"main\">\n");
      printf("<table align=\"center\" border=\"0\">\n");
      printf("<tr>\n");
      printf("<td>\n");
      printf("<h3>Temperatur Verlauf&nbsp;&nbsp;</h3>\n");
      printf("</td>\n");
      printf("<td>\n");
      printf("<form name=\"TT\" method=\"GET\" action=\"/cgi-bin/myinter.cgi\">\n");
      printf("<select name=\"T\" size=\"2\">\n");
      if ( auswahl1 == 0 ) sx = sel; else sx = snl;
      printf("<option value=\"0\" %s>Gestern</option>\n", sx );
      if ( auswahl1 == 1 ) sx = sel; else sx = snl;
      printf("<option value=\"1\" %s>Heute</option>\n", sx );
      printf("</select>\n");
      printf("</td>\n");
      printf("<td>\n");
      printf("<select name=\"Z\" size=\"2\">\n");
      if ( auswahl2 == 0 ) sx = sel; else sx = snl;
      printf("<option value=\"0\" %s>24 Stunden</option>\n", sx );
      if ( auswahl2 == 1 ) sx = sel; else sx = snl;
      printf("<option value=\"1\" %s> 6 Stunden</option>\n", sx );
      printf("</select>\n");
      printf("</td>\n");
      printf("<td>\n");
      printf("<select name=\"D\" size=\"4\">\n");
      if ( auswahl3 == 0 ) sx = sel; else sx = snl;
      printf("<option value=\"0\" %s>00:00-06:00</option>\n", sx );
      if ( auswahl3 == 1 ) sx = sel; else sx = snl;
      printf("<option value=\"1\" %s>06:00-12:00</option>\n", sx );
      if ( auswahl3 == 2 ) sx = sel; else sx = snl;
      printf("<option value=\"2\" %s>12:00-18:00</option>\n", sx );
      if ( auswahl3 == 3 ) sx = sel; else sx = snl;
      printf("<option value=\"3\" %s>18:00-24:00</option>\n", sx );
      printf("</select>\n");
      printf("</td>\n");
      printf("<td>\n");
      printf("<input name=\"SUB\" value=\"Senden\" type=\"submit\">\n");
      printf("</form>\n");
      printf("</td>\n");
      printf("<td>\n");
      printf("<h3>&nbsp;&nbsp;&nbsp;&nbsp;Zeitauswahl</h3>\n");
      printf("</td>\n");
      printf("</tr>\n");
      printf("</table>\n");
      printf("<canvas id=\"C3\" width=\"1200\" height=\"555\"></canvas>\n");
      printf("<script>\n");

      print_fx( "f1", addr + offset, auswahl2, cm );              // ********** F1
      print_fx( "f2", addr + offset + 3072,     auswahl2, cm );   // ********** F2
      print_fx( "f3", addr + offset + (3072*2), auswahl2, cm );   // ********** F3
      print_fx( "f4", addr + offset + (3072*3), auswahl2, cm );   // ********** F4
      print_fx( "f5", addr + offset + (3072*4), auswahl2, cm );   // ********** F5

      printf("close_tk(\"C3\",%d);\n", auswahl );   // out parameter
   }
   printf("</script>\n");
   printf("<h3>Temperatur Grafik %s</h3>\n",text);
   printf("</div>\n");
   print_footer();
}



/************************************************************************************
** temperaturwerte ausgeben
** wenn aus2=0 =24h und d = 1 dann werden aus je 5 Werten der Durchschnittswert gebildet
*/
void print_fx( char *fx, int index, unsigned char aus2, unsigned char d)
{
   char s[25];
   char *sx;
   int n,x;
   int mean=0;
   int w=0;
   int anz=0;
   char c;
   char *pa;
                     // eigener base64 key
                     //           1         2         3         4         5         6
                     // 0123456789012345678901234567890123456789012345678901234567890123
   static char key[] ="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*=";


   s[24] = '\0';

   if ( aus2 )                  // 1 == 6 Stunden Werte
   {
      sx = s;
      for ( n =0 ; n < 720; n++)
      {
         if ( n%24 == 0 && n > 0)      // 12 Werte da ?
         {
            printf("%s(\"%s\");\n",fx,s);
            sx = s;
         }
         *sx  = tdata[ index++ ];
         sx++;
      }
      printf("%s(\"%s\");\n",fx,s);
   }
   else                          // 24h Werte
   {
      if ( d )                   // Aus je 5 Werten den Mittelwert bilden
      {
         pa = key;
         sx = s;
         for ( n =0 ; n < 2880; n++)
         {
            if ( n%120 == 0 && n > 0)      // 12 Werte da ?
            {
               printf("%s(\"%s\");\n",fx,s);
               sx = s;
            }
            w *=64;
            c = tdata[ index++ ];
            for ( x = 0; x < 64; x++)
            {
               if ( key[x] == c )
                  break;
            }
            w += x;
            if ( n%2 == 1)
            {
               //printf(" w=%d n=%d",w,n);
               if ( w < 4032 )
               {
                  anz++;
                  mean += w;
                  w = 0;
               }
               else
                  w=0;
            }
            if ( (n+1) % 10 == 0 & n > 0 )
            {
               if ( mean == 0 )
                  mean = 4032 ;
               else
               {
                  if ( anz )
                     mean /= anz;
                  //printf(" mean=%d a=%d\n",mean,anz);
               }
               *sx = key[ mean / 64 ];
               sx++;
               *sx = key[ mean % 64 ];
               sx++;
               mean = 0;
               anz = 0;
               w = 0;
            }
         }
         printf("%s(\"%s\");\n",fx,s);
      }
      else
      {
         sx = s;
         for ( n =0 ; n < 576; n++)
         {
            if ( n%24 == 0 && n > 0)      // 12 Werte da ?
            {
               printf("%s(\"%s\");\n",fx,s);
               sx = s;
            }
            if ( n%2 == 0 & n > 0)
               index +=8;
            *sx = tdata[ index++ ];
            sx++;
         }
         printf("%s(\"%s\");\n",fx,s);
      }
   }
}



/************************************************************************************
** Status Seite
*/
void errorseite(void)
{
   print_header(0,"Error");
   printf("<h3>Seite nicht definiert, query falsch</h3><br>");
   print_footer();
}



/*****************************************************************************
**  ascii to int
*/
unsigned int a_to_i( char *quelle, int anzahl)
{
   unsigned int x=0;
   unsigned char n;

   for ( n=0; n < anzahl; n++)
   {
      x *= 10;
      x += *quelle & 0x0F;
      quelle++;
   }
   return x;
}



/************************************************************************************
** header und navigation senden
*/
void print_header( int jssw, char *title )
{
   printf("Content-Type: text/html\n\n");
   printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
   printf("<html>\n");
   printf("<head>\n");
   printf("<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\">\n");
   //printf("<meta http-equiv=\"refresh\" content=\"60; URL=index.htm\">\n");
   printf("<meta http-equiv=\"expires\" content=\"0\">\n");
   printf("<title>Raspberry %s</title>\n",title);
   printf("<link rel=\"stylesheet\" type=\"text/css\" href=\"/style.css\">\n");
   if ( jssw == 1 )
      printf("<script language=\"JavaScript\" type=\"text/javascript\" src=\"/scri1.js\"></script>\n");
   else if ( jssw == 2 )
      printf("<script language=\"JavaScript\" type=\"text/javascript\" src=\"/scri2.js\"></script>\n");
   printf("</head>\n");
   printf("<body>\n");
   printf("<h1>Raspberry PI web server</h1>\n");
   printf("<div id=\"nav\">\n");
   printf("<a href=\"/cgi-bin/myinter.cgi\">PI Home</a>\n");
   printf("<a href=\"/cgi-bin/myinter.cgi?S\">Status</a>\n");
   printf("<a href=\"/cgi-bin/myinter.cgi?H\">Heizung</a>\n");
   printf("<a href=\"/cgi-bin/myinter.cgi?H=18\">18 Monate</a>\n");
   printf("<a href=\"/cgi-bin/myinter.cgi?R\">Regen 33 Tage</a>\n");
   printf("<a href=\"/cgi-bin/myinter.cgi?N\">Regen 18 Monate</a>\n");
   printf("<a href=\"/cgi-bin/myinter.cgi?T=1&Z=0\">°C Grafik</a>\n");
   printf("<a href=\"/cgi-bin/myinterw.cgi?C=0&A=0&R=256\">Wartung</a>\n");
   printf("</div>\n");
   printf("<div id=\"main\">\n");
}



/************************************************************************************
** ende der html Seite
*/
void print_footer(void)
{
   printf("<br>\n");
   printf("</div>\n");
   printf("<div id=\"tail\"><a href=\"http://www.ralidzba.de\">www.ralidzba.de</a>Reinhard Alfons Lidzba 2014</div>\n");
   printf("</body>\n");
   printf("</html>\n");
}
