Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. WR Diehl Platinum RS485 auslesen.

    NEWS

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    WR Diehl Platinum RS485 auslesen.

    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Marcolotti last edited by

      Hallo,

      hat jemand von C Ahnung, der nur die Abfragen und die Checksummenbildung in Nodered oder als Script umbauen kann?

      `/**********************************************************/
      /* Verschil tussen omvormer en groene stroommeter = 130kw.*/
      /* in het nadeel van de groene stroommeter.		  */
      /**********************************************************/
      
      #define _GNU_SOURCE
      
      #include <stdio.h>#include <stdlib.h>#include <string.h>#include <strings.h>#include <sys types.h="">#include <sys socket.h="">#include <netinet in.h="">#include <netdb.h>#include <mysql mysql.h="">#include <regex.h>#include <time.h>#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <pthread.h>#include <termios.h>#define TERM_DEVICE "/dev/ttyUSB0"   
      #define TERM_SPEED B19200    
      #define DETAIL 1
      #define DAY 0
      #define SUMMARY 0
      #define DETAIL 1
      #define HIGHDETAIL 2
      #define METERCORR 0.008994579
      
      int i, fd, old_flags, counter, Ret_n, value;
      int DEBUG = 0, n = 0, Detail = 0, vtime=0, vmin=0, RSdays=1, dataok = 1;
      
      ssize_t length;
      
      struct termios term_attr;
      FILE* file;
      char* file_name;
      char logmsg[35];
      char* mode = "a";
      MYSQL* connection = NULL;
      char query[512];
      MYSQL_ROW  row;
      
      unsigned char rValue[2];
      
      time_t t;
      struct tm *ts;
      
      // unsigned char tx[38] ;
      unsigned char message_power[38] ;
      
      FILE* error_file = NULL;
      char* error_file_name = "/var/log/Diehl-error.log";
      char* error_mode = "w";
      FILE* debug_file = NULL;
      char* debug_file_name = "/var/log/Diehl-debug.log";
      char* debug_mode = "w";
      
      int skip = 1;
      int Day_index = 0;
      unsigned char* checkSums;
      char buffer[512];
      char* buffer1;
      //unsigned char RS_TX[512];
      unsigned char* RS_TX;
      
      int f1_i;
      char f2_d[10], f3_d[10];
      int f4_i;
      int f5_i;
      int f6_i;
      float f7_f;
      float f8_f;
      int f9_i;
      float f10_f;
      float f11_f;
      int f12_i;
      int f13_i;
      int f14_i;
      int f15_i;
      int f16_i;
      
      char day[3], month[3], year[5];
      
      void error_exit(const char* msg) {
      	perror(msg);
      	if (error_file != NULL)
      		fclose(error_file);
      	if (debug_file != NULL)
      		fclose(debug_file);
      	exit(0);
      }
      
      void error_retry(char* msg) {
      	time_t timestamp = time(NULL);
      	char error_msg[512];
      	char *time_now = ctime(×tamp);
      	time_now[strlen(time_now)-1]=0;
      	if (error_file == NULL)
      		error_exit("ERROR writing to error.log file");
      	sprintf(error_msg, "%s %s", time_now, msg);
      	fprintf(error_file, "%s\n", error_msg);
      
      }
      
      void debug_entry(char* msg) {
      	time_t timestamp = time(NULL);
      	char debug_msg[512];
      	char *time_now = ctime(×tamp);
      	time_now[strlen(time_now)-1]=0;
      	if (debug_file == NULL)
      		error_exit("ERROR writing to debug.log file");
      	sprintf(debug_msg, "%s %s", time_now, msg);
      	fprintf(debug_file, "%s\n", debug_msg);
      }
      
      /* START - RSC Checksum routine */
      int crc16tab[] = { 0, 4129, 8258, 12387, 16516, 
      20645, 24774, 28903, 33032, 37161, 41290, 45419, 49548, 
      53677, 57806, 61935, 4657, 528, 12915, 8786, 21173, 
      17044, 29431, 25302, 37689, 33560, 45947, 41818, 54205, 
      50076, 62463, 58334, 9314, 13379, 1056, 5121, 25830, 
      29895, 17572, 21637, 42346, 46411, 34088, 38153, 58862, 
      62927, 50604, 54669, 13907, 9842, 5649, 1584, 30423, 
      26358, 22165, 18100, 46939, 42874, 38681, 34616, 63455, 
      59390, 55197, 51132, 18628, 22757, 26758, 30887, 2112, 
      6241, 10242, 14371, 51660, 55789, 59790, 63919, 35144, 
      39273, 43274, 47403, 23285, 19156, 31415, 27286, 6769, 
      2640, 14899, 10770, 56317, 52188, 64447, 60318, 39801, 
      35672, 47931, 43802, 27814, 31879, 19684, 23749, 11298, 
      15363, 3168, 7233, 60846, 64911, 52716, 56781, 44330, 
      48395, 36200, 40265, 32407, 28342, 24277, 20212, 15891, 
      11826, 7761, 3696, 65439, 61374, 57309, 53244, 48923, 
      44858, 40793, 36728, 37256, 33193, 45514, 41451, 53516, 
      49453, 61774, 57711, 4224, 161, 12482, 8419, 20484, 
      16421, 28742, 24679, 33721, 37784, 41979, 46042, 49981, 
      54044, 58239, 62302, 689, 4752, 8947, 13010, 16949, 
      21012, 25207, 29270, 46570, 42443, 38312, 34185, 62830, 
      58703, 54572, 50445, 13538, 9411, 5280, 1153, 29798, 
      25671, 21540, 17413, 42971, 47098, 34713, 38840, 59231, 
      63358, 50973, 55100, 9939, 14066, 1681, 5808, 26199, 
      30326, 17941, 22068, 55628, 51565, 63758, 59695, 39368, 
      35305, 47498, 43435, 22596, 18533, 30726, 26663, 6336, 
      2273, 14466, 10403, 52093, 56156, 60223, 64286, 35833, 
      39896, 43963, 48026, 19061, 23124, 27191, 31254, 2801, 
      6864, 10931, 14994, 64814, 60687, 56684, 52557, 48554, 
      44427, 40424, 36297, 31782, 27655, 23652, 19525, 15522, 
      11395, 7392, 3265, 61215, 65342, 53085, 57212, 44955, 
      49082, 36825, 40952, 28183, 32310, 20053, 24180, 11923, 
      16050, 3793, 7920 };
      
      unsigned char* calcChecksum(unsigned char* msg, int RSC_length) {
      	int crc = 65535; 
      	//unsigned char rValue[2];
      
      	for (int RS_index = 0; RS_index < RSC_length; RS_index++) {
      		int dataInt = msg[RS_index] & 0xFF;
      		int rIndex = dataInt ^ (crc & 0xFFFF) >> 8;
      		rIndex &= 65535;
      		crc = crc16tab[rIndex] ^ (crc & 0xFFFF) << 8;
      		crc &= 65535;
      	}
        	crc &= 65535;
      	rValue[1] = (crc & 0xFF);
      	rValue[0] = (crc & 0xFF00) >> 8;
      	return rValue;
      }
      /* END - RSC Checksum routine */
      
      char*  getValue( int  valueType)
      {
      unsigned char vtx[7];
      
      vtx[0] = 53;
      vtx[1] = 19;
      vtx[2] = 1;
      vtx[3] = valueType;
      
      checkSums = calcChecksum(vtx, 4);
      vtx[4] = checkSums[0];
      vtx[5] = checkSums[1];
      
      // printf("WR Send value --- ");
      // for ( i=0; i < 6; i++) {
      // 	printf(" %02d", vtx[i]);
      // 	}
      // printf("\n");
      
      n = write(fd,vtx,6);
      if (n < 0) {
      	close(fd);
         	error_retry("ERROR sending ");
         	exit(1);
      	}
      
      // Read answer
      	bzero(buffer, 256);
      	n = read(fd, buffer, 255);
      
      // printf("WR receive value --- ");
      // for ( i=0; i < n; i++) {
      	// printf(" %02X", buffer[i]);
      	// }
      // printf("\n");
      
      return buffer; 
      }
      
      unsigned char* getDataDataFmtIntv(unsigned char* tx, int objIndex)
      {
      tx[0] = 45;
      tx[1] = 19;
      tx[2] = 2;
      tx[3] = 16;
      tx[4] = objIndex & 0xFF;
      
      checkSums = calcChecksum(tx, 5);
      tx[5] = checkSums[0];
      tx[6] = checkSums[1];
      
      return tx; 
      }
      
      unsigned char* getDataDataFmtDay(unsigned char* tx, int objIndex)
      {
      tx[0] = 45;
      tx[1] = 19;
      tx[2] = 2;
      tx[3] = 17;
      tx[4] = objIndex & 0xFF;
      
      checkSums = calcChecksum(tx, 5);
      tx[5] = checkSums[0];
      tx[6] = checkSums[1];
      
      return tx; 
      }
      
      unsigned char* getDataIntvData(unsigned char* tx, struct tm *RS_date, int dayIndex)
      {
      unsigned char RS_year = (RS_date->tm_year + 1900 - 2000) & 0xFF;
      unsigned char RS_month = RS_date->tm_mon + 1;
      unsigned char RS_day = RS_date->tm_mday;
      unsigned char diH = (unsigned char)((dayIndex & 0xFF00) >> 8);
      unsigned char diL = (unsigned char)(dayIndex & 0xFF);
      
      tx[0] = 45;
      tx[1] = 19;
      tx[2] = 6;
      tx[3] = 18;
      tx[4] = RS_year;
      tx[5] = RS_month;
      tx[6] = RS_day;
      tx[7] = diH;
      tx[8] = diL;
      
      checkSums = calcChecksum(tx, 9);
      tx[9] = checkSums[0];
      tx[10] = checkSums[1];
      
      return tx; 
      }
      
      unsigned char* getDataDayData(unsigned char* tx, struct tm *RS_date)
      {
      unsigned char RS_year = (RS_date->tm_year + 1900 - 2000) & 0xFF;
      unsigned char RS_month = RS_date->tm_mon + 1;
      unsigned char RS_day = RS_date->tm_mday;
      
      printf("Day in function: %d-%d-%d\n", RS_day,RS_month, RS_year);
      
      tx[0] = 45;
      tx[1] = 19;
      tx[2] = 4;
      tx[3] = 19;
      tx[4] = RS_year;
      tx[5] = RS_month;
      tx[6] = RS_day;
      
      checkSums = calcChecksum(tx, 7);
      tx[7] = checkSums[0];
      tx[8] = checkSums[1];
      
      return tx; 
      }
      
      int main(int argc, char *argv[]) {
      	// Check commandline arguments
      	if (argc < 2)
      		error_exit("ERROR program needs extra parameter");
      
      	if ( argv[1][0] == 'S' ) {
      		Detail=SUMMARY; //summary
      		vmin = 47;
      		vtime = 5;	
      		if ( argc == 3 ){
      			RSdays= atoi( argv[2] );
      			// t = make_time(argv[2]);
      		}
      		else {
      			RSdays = 0;
      		}
      	} 
      	if ( argv[1][0] == 's' ) {
      		Detail=SUMMARY; //summary
      		vmin = 47;
      		vtime = 5;	
      		if ( argc == 3 ){
      			RSdays= atoi( argv[2] );
      			// t = make_time(argv[2]);
      		}
      		else {
      			RSdays = 1;
      		}
      	} 
      	if ( argv[1][0] == 'D' ) {
      		Detail=DETAIL; //detail
      		vmin = 30;
      		vtime = 5;	
      		printf ("Reading detail data\n");		
      		if ( argc == 3 ){
      			RSdays= atoi( argv[2] );
      			// t = make_time(argv[2]);
      		}
      		else {
      			RSdays = 0;
      		}
      	} 
      	if ( argv[1][0] == 'd' ) {
      		Detail=HIGHDETAIL; //detail
      		vmin = 30;
      		vtime = 5;	
      		RSdays = 1;
      		if ( argc == 3 ){
      			Day_index = atoi( argv[2] );
      		}
      		printf("idx;\tDate;\t\tTime;\t\tStatus;\tEvent;\tDC[V];\tDC[A];\tDC[W];\tAC[V];\tAC[A];\tAC[W];\tD[Wh];\tT1[∞C];\tT2[∞C];\tT3[∞C];\tUnknown\n");
      	}
      	if ( argv[1][0] == 'v' ) {
      		Detail=4; //detail
      		vmin = 30;
      		vtime = 5;	
      		if ( argc == 3 ){
      			value = atoi( argv[2] );
      		}
      	}
      
      	printf ("Argv: %s, Detail: %d \n", argv[1], Detail);		
      
      	// Connect to database
      	// DBhost=192.168.*.*
      	// DBname=Diehl
      	// DBtabprefix=log
      	// DBuser=stendec
      	// DBpass=********
      	// Hostname=192.168.*.*
      	// Hostport=3306
      
      	connection = mysql_init(NULL);
      	// if (!mysql_real_connect(connection, dbhost, dbuser, dbpass, dbname, 0, NULL, 0))
      	if (!mysql_real_connect(connection, "192.168.*.*","stendec", "********", "Diehl", 0, NULL, 0))
      		error_exit(mysql_error(connection));
      
      	// Try to open error log file
      	if ((error_file = fopen(error_file_name, error_mode)) == NULL)
      		error_exit("ERROR opening error.log file");
      
      	// Make file unbuffered
      	setbuf(error_file, NULL);
      
         	if ((fd = open(TERM_DEVICE, O_RDWR)) == -1) {
              perror("terminal: Can't open device " TERM_DEVICE);
              return(1);
         	}
         	// Configure RS232 
      
         	if (tcgetattr(fd, &term_attr) != 0) {
      	    perror("terminal: tcgetattr() failed");
              return(1);
         	}
      
         	term_attr.c_cflag = TERM_SPEED | CS8 | CLOCAL | CREAD;
         	term_attr.c_iflag = 0;
         	term_attr.c_oflag = 0;
         	term_attr.c_lflag = 0;
         	term_attr.c_cc[VMIN] = vmin; // 30
         	term_attr.c_cc[VTIME] = vtime; // 10
         	if (tcsetattr(fd, TCSAFLUSH, &term_attr) != 0)
              perror("terminal: tcsetattr() failed");
      
         	tcgetattr(STDIN_FILENO, &term_attr) ;
      
         	old_flags = term_attr.c_lflag;
         	term_attr.c_lflag &= ~(ICANON | ECHO);
      	// tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attr) ;
      
      	// Get current date
      	//int day;
          	time_t now, db_time;
      	time(&now);
      	struct tm *myTm;
      	myTm = localtime(&now);
      
      	if (Detail == SUMMARY ){
      		printf("Day now: %d\n", myTm->tm_mday);
      		now -= ((60*60*24)*(RSdays));
      		myTm = localtime(&now);
      		printf("Day set: %d\n", myTm->tm_mday);
      		}
      
      	if( (Detail == SUMMARY) && (RSdays == 0) ) {
      		mysql_query(connection, "SELECT MAX(`Date`) FROM `Summary`");
      		MYSQL_RES *confres = mysql_store_result(connection);
      		while((row = mysql_fetch_row(confres))) {
      			printf("Row returned: %s\n", row[0]);
         			// sscanf( row[0], "%4[^-],%2[^-],%2[^-]", year, month, day );
      			strncpy(year, &row[0][0], 4);
      			strncpy(month, &row[0][5], 2);
      			strncpy(day, &row[0][8], 2);
         			myTm->tm_year = atoi(year) - 1900;
      			myTm->tm_mon = atoi(month) - 1;
      			myTm->tm_mday = atoi(day); 
      			db_time = mktime( myTm );
      		}
      		printf("Starting from: date: %d-%02d-%02d \n", (myTm->tm_year)+1900, (myTm->tm_mon)+1, myTm->tm_mday );
      		time_t d_diff = (now - db_time)*60*60*24;
      		printf("Difference indays = %d\n",(int) d_diff);
      		RSdays = (int) d_diff;
      
      	}
      
      	// GETVALUE
      	if( Detail == 4 ) {
      		buffer1 = getValue( value );
      		printf("getValue message --- ");
      		for ( i=0; i < n; i++) {
      			printf(" %02d", buffer1[i]);
      		}
      		printf("\n");
      		exit(0);
      	}
      
      	if( (Detail == DETAIL) && (RSdays == 0) ) {
      			// mysql_query(connection, query);
      		mysql_query(connection, "SELECT `Date`, MAX(`Day_index`) FROM `Detail` WHERE `Date` = (SELECT MAX(`Date`) FROM `Detail`)");
      		MYSQL_RES *confres = mysql_store_result(connection);
      		while((row = mysql_fetch_row(confres))) {
      			printf("row[0]: %s\n", row[0]);
         			//sscanf( row[0], "%4d-%2d-%2d", &myTm->tm_year, &myTm->tm_mon, &myTm->tm_mday );
      			Day_index = atoi(row[1]);	
      			strncpy(year, &row[0][0], 4);
      			strncpy(month, &row[0][5], 2);
      			strncpy(day, &row[0][8], 2);
         			myTm->tm_year = atoi(year) - 1900;
      			myTm->tm_mon = atoi(month) - 1;
      			myTm->tm_mday = atoi(day); 
      			db_time = mktime( myTm );
      
      		}
      		printf("Starting from: Day_index: %d, date: %d-%02d-%02d \n", Day_index,  myTm->tm_year, myTm->tm_mon, myTm->tm_mday );
      		printf("Now = %d\n", (int) now);
      		printf("DB  = %d\n", (int) db_time);
      		time_t d_diff = (now - db_time)/60/60/24;
      		printf("Difference in days = %d\n",(int) d_diff);
      		RSdays = (int) d_diff;
      	}
      
      	// Start sending the data requests and logging the answers
      	// RSC Start of main loop
      	while (1) {
      
      		// Send message
      		// printf("\n\n\n------------------------------------------\n ");
      		dataok = 1;
      
      		if ( Detail==SUMMARY ) {
      			if ( RSdays > 0 ) {
      				Ret_n=9;
      				// printf ("Reading yesterdayday data\n");
      				printf("Day before: %d\n", myTm->tm_mday);
      				now += ((60*60*24));
      				myTm = localtime(&now);
      				printf("Day after: %d\n", myTm->tm_mday);
      			}
              		RS_TX = getDataDayData(message_power, myTm);
      		}
      		else // DETAIL
      		{
      			Ret_n=11;
      			// printf ("Reading detail data\n");		
      			RS_TX = getDataIntvData(message_power, myTm, Day_index);
      		}
      
      		if (DEBUG) {
      			printf("WR Send message --- ");
      			for ( i=0; i < Ret_n; i++) {
      				printf(" %02d", RS_TX[i]);
      			}
      			printf("\n");
      		}
      
      		n = write(fd,RS_TX,Ret_n);
      		if (n < 0) {
         			close(fd);
         			error_retry("ERROR sending ");
         			break;
      		}
      
      		if (DEBUG) {
              	printf("\nWR= message 1 gesendet: %s\n", message_power);
              	printf("\nWR= Num char: %d\n", n);
              	printf("\nWR= itteration: %d\n", Day_index);
      		}
      
      		// Read answer
      		bzero(buffer, 256);
      		n = read(fd, buffer, 255);
      /*
         		if (DEBUG)
      			printf("\nWR= read: %d\n\n", n);
      		for (counter = 0; counter < 100 && n < 0; counter++) {
         			if (DEBUG)
            			debug_entry("Socket contains no data, trying to read again later");
         			n = read(fd, buffer, 255);
      		 }
      */
      
      		 if (n < 0) {
         			close(fd);
      			printf("Received error : %s\n", buffer);
         			error_retry("ERROR receiving ");
         			continue;
      		}
      
      		if (1) {
      			printf("Received answer : %s\n", buffer);
      		}
      
      		checkSums = calcChecksum((unsigned char*) buffer, n-2);
      		int chksum_calc=((checkSums[0]*256)+checkSums[1]);
      		int chksum_ret =((buffer[n-2]*256)+buffer[n-1]);
      
      		if ( chksum_calc != chksum_ret ){
      			printf("Checksum Error\n");	
      			printf("Calculated Checksum: %02d %02d\n", checkSums[0], checkSums[1] );
      			printf("Buffer     Checksum: %d %d\n", buffer[n-2], buffer[n-1] );
      			dataok = 0;
      			continue;
      		}
      		if (DEBUG){ 
      			printf("Calculated Checksum: %02d %02d\n", checkSums[0], checkSums[1] );
      			printf("Buffer     Checksum: %d %d\n", buffer[n-2], buffer[n-1] );
      		}
      
      		if (DEBUG) 
      		for (counter = 0 ; counter < n; counter++) { 
         			printf("DEBUG: Received answer b%02d: %02d \n",counter,  buffer[counter]);
      		}
      
      		//if ((Detail == 1) || (Detail == 0)) { 
      		if (DEBUG == 1) { 
      			printf("WR Receive message --- ");
      			for ( i=0; i < n; i++) {
      				printf(" %02d", buffer[i]);
      			}
      		printf("\n");
      		}	
      
      		// End of index reached now skip 1
      		if ( buffer[0] == 132) {
      			error_retry("End list ");
      			dataok = 0;
      			if ( RSdays == 0 ){
      				sleep(2);
      				skip=1;
      				//Day_index += skip;
      				if( Detail == SUMMARY ){
      					exit(0);
      				}
      			}
      			else {	
      				Day_index = 0;
      				if ( --RSdays > 0 ) {
      					now += ((60*60*24));
      					myTm = localtime(&now);
      				}
      			}
      			// printf("---Index = %d, RSdays = %d\n", Day_index, RSdays);
      			continue;
      		}
      
      		if ( Detail == SUMMARY ) { // Summary
      			if ( RSdays > 0 ){
      				RSdays--;
      			} else {
      			break;
      			}
      		}
      		else {
      			Day_index += skip;
      			// if ( buffer[7] == 13 ){
      		//		Day_index = 0;
      		//		RSdays--;
      		//	}
      		}
      
      		if( (Detail == DETAIL) || (Detail == HIGHDETAIL)) {
      			f1_i = Day_index;
                      	sprintf(f2_d,"'%d-%d-%d'", myTm->tm_year - 100 + 2000, myTm->tm_mon+1, myTm->tm_mday );
                      	sprintf(f3_d,"'%02d:%02d:%02d'", buffer[4],buffer[5],buffer[6]);
      			f4_i = buffer[7];
      			f5_i = buffer[8];
      			f6_i = buffer[9]*256+buffer[10];
      			f7_f = (buffer[11])/10;
      			f8_f = (buffer[12]*256+buffer[13])/10; 
      			f9_i = buffer[14]*256+buffer[15];
      			f10_f = (buffer[16])/10;
      			f11_f = (buffer[17]*256+buffer[18])/10;
      			f12_i = buffer[19]*256*256*256+buffer[20]*256*256+buffer[21]*256+buffer[22];
      			f13_i = buffer[23];
      			f14_i = buffer[24];
      			f15_i = buffer[25];
      			f16_i = buffer[26]*256+buffer[27];
      			}
      
      		if ( Detail == DETAIL ) { // detail
      			printf("Date: %d-%d-%d\n", myTm->tm_mday, myTm->tm_mon+1, myTm->tm_year - 100 + 2000);
      			printf("Time: %02d:%02d:%02d\n",buffer[4],buffer[5],buffer[6]); 
      			printf("Status: %02d\n",buffer[7]); 
      			printf("Event: %02d\n",buffer[8]); 
      			printf("DC Voltage: %d\n",buffer[9]*256+buffer[10]); 
      			printf("DC Current: %d\n",buffer[11]); 
      			printf("DC Power  : %d\n",buffer[12]*256+buffer[13]); 
      			printf("AC Voltage: %d\n",buffer[14]*256+buffer[15]); 
      			printf("AC Current: %d\n",buffer[16]); 
      			printf("AC Power  : %d\n",buffer[17]*256+buffer[18]); 
      			printf("Energy Day: %d\n",buffer[19]*256*256*256+buffer[20]*256*256+buffer[21]*256+buffer[22]); 
      			printf("Temp 1    : %d\n",buffer[23]); 
      			printf("Temp 2    : %d\n",buffer[24]); 
      			printf("Temp 3    : %d\n",buffer[25]); 
      			printf("Unknown   : %d\n",buffer[26]*256+buffer[27]); 
      
      			if (dataok == 1){
      				sprintf(query, "INSERT INTO Detail (Day_index , Date, Time, Status, Event, DC_V, DC_A, DC_W, AC_V, AC_A, AC_W, Energy_day, Temp_1, Temp_2, Temp_3, Unknown) VALUES (%d, %s, %s, %d, %d, %d,%0.2f,%0.2f,%d,%0.2f,%0.2f,%d,%d,%d,%d, %d);", f1_i, f2_d, f3_d, f4_i, f5_i, f6_i, f7_f, f8_f, f9_i, f10_f, f11_f, f12_i, f13_i,f14_i, f15_i, f16_i );
      				// Execute the query to write the data into db
      				printf ("Q - %s -\n", query);
      				mysql_query(connection, query);
      
      				if (mysql_errno(connection)){
      					printf("mysql_error  : %s\n",mysql_error(connection)); 
      					//error_exit(mysql_error(connection));
      				}
      			}
      		}
      
      		if ( Detail==SUMMARY ) { //summary
      		char Sf1_d[11];
      		sprintf(Sf1_d,"20%02d-%02d-%02d", buffer[4],buffer[5], buffer[6]);
      		int Sf2_i=buffer[7]*256+buffer[8]; 
      		int Sf3_i=buffer[9]*256+buffer[10]; 
      		float Sf4_f=(float) buffer[11]/10; 
      		float Sf5_f=(float) buffer[12]/10; 
      		float Sf6_f=(float) (buffer[13]*256+buffer[14])/10; 
      		float Sf7_f=(float) (buffer[15]*256+buffer[16])/10; 
      		int Sf8_i=buffer[17]*256+buffer[18]; 
      		int Sf9_i=buffer[19]*256+buffer[20]; 
      		float Sf10_f=(float) buffer[21]/10; 
      		float Sf11_f=(float) buffer[22]/10; 
      		float Sf12_f=(float) (buffer[23]*256+buffer[24])/10; 
      		float Sf13_f=(float) (buffer[25]*256+buffer[26])/10; 
      		int Sf14_i=buffer[29]*256+buffer[30]; 
      		int Sf15_i=buffer[31]; 
      		int Sf16_i=buffer[32]; 
      		int Sf17_i=buffer[33]; 
      		int Sf18_i=buffer[34]; 
      		int Sf19_i=buffer[35]; 
      		int Sf20_i=buffer[36]; 
      		int Sf21_i=buffer[37]*256*256*256+buffer[38]*256*256+buffer[39]*256+buffer[40]; 
      		int Sf22_i=buffer[41]*256*256*256+buffer[42]*256*256+buffer[43]*256+buffer[44]; 
      		float Sf23_f=(float)((Sf21_i-(Sf21_i*METERCORR))/1000); // correctie voor de groene meter stand 
      
      		printf("mysql_dat = %s \n",Sf1_d );
      		printf("Writing to db? data_ok: %d \n", dataok); 
      
      		if (dataok == 1){
      			sprintf(query, "INSERT INTO Summary (Date, DC_V_peak, DC_V_mean, DC_A_peak, DC_A_mean, DC_W_peak, DC_W_mean, AC_V_peak, AC_V_mean, AC_A_peak, AC_A_mean, AC_W_peak, AC_W_mean, Energy_day_Wh, Unknown_1, Unknown_2, Unknown_3, Unknown_4, Unknown_5, Unknown_6, Energy_Sum_Wh, Temp_peak, MeterStand) VALUES ('%s', %d, %d, %0.2f, %0.2f, %0.2f, %0.2f, %d, %d, %0.2f, %0.2f, %0.2f, %0.2f, %d, %d,%d,%d, %d, %d, %d,%d, %d, %0.2f);",Sf1_d, Sf2_i, Sf3_i, Sf4_f, Sf5_f, Sf6_f, Sf7_f, Sf8_i, Sf9_i, Sf10_f, Sf11_f, Sf12_f, Sf13_f, Sf14_i, Sf15_i, Sf16_i, Sf17_i, Sf18_i, Sf19_i, Sf20_i, Sf21_i, Sf22_i, Sf23_f );
      			if (DEBUG) {
      				sprintf(buffer, "Executing query: %s", query);
      				debug_entry(buffer);
      			}
      
      			// Execute the query to write the data into db
      			mysql_query(connection, query);
      
      			if (mysql_errno(connection)){
      				printf("mysql_error  : %s\n",mysql_error(connection)); 
      				//error_exit(mysql_error(connection));
      				}
      			}
      		}
      		if ( Detail == HIGHDETAIL ) { //detail
      			if ( (Day_index % 30) == 0 ) {
      				printf("Idx \tDate \t\tTime \t\tStatus \tEvent \tDC[V] \tDC[A] \tDC[W] \t\tAC[V] \tAC[A] \tAC[W] \t\tD[Wh] \tT1[∞C] \tT2[∞C] \tT3[∞C] \tUnknown\n");
      				}
      			printf("%d\t%s\t%s\t%d\t%d\t%d\t%0.2f\t%0.2f\t\t%d\t%0.2f\t%0.2f\t\t%d\t%d\t%d\t%d\t%d\n", f1_i, f2_d, f3_d, f4_i, f5_i, f6_i, f7_f, f8_f, f9_i, f10_f, f11_f, f12_i, f13_i,f14_i, f15_i, f16_i );
      		//	printf("%d;\t%d-%d-%d;\t%02d:%02d:%02d;\t%d;\t%d;\t%d;\t%d;\t%d;\t%d;\t%d;\t%d;\t%d;\t%d;\t%d;\t%d;\t%d\n", Day_index, myTm->tm_mday, myTm->tm_mon+1, myTm->tm_year - 100 + 2000,buffer[4],buffer[5],buffer[6],buffer[7],buffer[8],buffer[9]*256+buffer[10],buffer[11],buffer[12]*256+buffer[13],buffer[14]*256+buffer[15],buffer[16],buffer[17]*256+buffer[18],buffer[19]*256*256*256+buffer[20]*256*256+buffer[21]*256+buffer[22],buffer[23],buffer[24],buffer[25],buffer[26]*256+buffer[27]);
      		}	
      	} // while 
      
      }</termios.h></pthread.h></errno.h></fcntl.h></unistd.h></time.h></regex.h></mysql></netdb.h></netinet></sys></sys></strings.h></string.h></stdlib.h></stdio.h>` 
      
      Gruß Marco[/i][/i][/i][/i][/i]
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post

      Support us

      ioBroker
      Community Adapters
      Donate

      800
      Online

      31.7k
      Users

      79.8k
      Topics

      1.3m
      Posts

      1
      1
      740
      Loading More Posts
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes
      Reply
      • Reply as topic
      Log in to reply
      Community
      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
      The ioBroker Community 2014-2023
      logo